import time import RPi.GPIO as GPIO import tkinter as tk import smbus cnt = 0 sensor_pin = 5 after = '' GPIO.setmode(GPIO.BCM) GPIO.setup(sensor_pin, GPIO.IN) CHANNEL = 1 ICADDR = 0x20 REG_IODIRA = 0x00 # GPAの方向レジスタ REG_IODIRB = 0x01 # GPBの方向レジスタ REG_GPIOA = 0x12 # GPAの入力データレジスタ REG_OLATA = 0x14 # GPAの出力データレジスタ REG_GPIOB = 0x13 # GPBの入力データレジスタ REG_OLATB = 0x15 # GPBの出力データレジスタ i2c = smbus.SMBus(CHANNEL) i2c.write_byte_data(ICADDR, REG_IODIRA, 0xFF) # GPAを全て入力に設定 i2c.write_byte_data(ICADDR, REG_IODIRB, 0x00) # GPBを全て出力に設定 def pulse_count(channel): global cnt cnt += 1 def get_counter(): global cnt, after sw = i2c.read_byte_data(ICADDR, REG_GPIOA) #GPAの読み込み if (sw == 0b11111110): direction = 'N' after = 'N' elif(sw == 0b11111101): direction = 'NE' after = 'NE' elif(sw == 0b11111011): direction = 'E' after = 'E' elif(sw == 0b11110111): direction = 'SE' after = 'SE' elif(sw == 0b11101111): direction = 'S' after = 'S' elif(sw == 0b11011111): direction = 'SW' after = 'SW' elif(sw == 0b10111111): direction = 'W' after = 'W' elif(sw == 0b01111111): direction = 'NW' after = 'NW' else: direction = after label2['text'] = ' ' label2['text'] = direction label4['text'] = ' ' label4['text'] = str(cnt / 100) cnt = 0 root.after(5000, get_counter) root = tk.Tk() root.geometry('300x200+20+100') root.title('風向・風速計テスト') label1 = tk.Label(root, text = '風向:') label1.place(x=80, y=60) label2 = tk.Label(root, text = str('')) label2.place(x=200, y=60) label3 = tk.Label(root, text = '風速(m/s):') label3.place(x=80, y=100) label4 = tk.Label(root, text = str(cnt)) label4.place(x=200, y=100) GPIO.add_event_detect(sensor_pin, GPIO.RISING, callback=pulse_count, bouncetime=10) get_counter() root.mainloop()