import smbus import time after = '' 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, 0x00) # GPAを全て出力に設定 #i2c.write_byte_data(ICADDR, REG_IODIRB, 0xFF) # GPBを全て入力に設定 i2c.write_byte_data(ICADDR, REG_IODIRA, 0xFF) # GPAを全て入力に設定 i2c.write_byte_data(ICADDR, REG_IODIRB, 0x00) # GPBを全て出力に設定 try: while True: # sw = i2c.read_byte_data(ICADDR, REG_GPIOB) #GPBの読み込み # i2c.write_byte_data(ICADDR, REG_OLATA, sw) #GPAへGPBの値を出力 sw = i2c.read_byte_data(ICADDR, REG_GPIOA) #GPAの読み込み # print(bin(sw)) if (sw == 0b11111110): print('N') after = 'N' elif(sw == 0b11111101): print('NE') after = 'NE' elif(sw == 0b11111011): print('E') after = 'E' elif(sw == 0b11110111): print('SE') after = 'SE' elif(sw == 0b11101111): print('S') after = 'S' elif(sw == 0b11011111): print('SW') after = 'SW' elif(sw == 0b10111111): print('W') after = 'W' elif(sw == 0b01111111): print('NW') after = 'NW' else: print(after) time.sleep(5) except KeyboardInterrupt: pass finally: print('END')