multibutton and aswitch.py
Posted: Thu Jul 30, 2020 1:05 pm
Hello,I had used the aswitch.py to scan the switch press and release function. Follow this py file is scan multi switch function.Are there aswitch.py can scan two or more switch press at same time and read the switch value?Thanks!
Code: Select all
class switch_ext:
"""
pb12 = Y5 Turn Right low effective byte1.bit0-bit1
Pb13 = Y6 Turn Left low effective byte1.bit2-bit3
Pb14 = Y7 Open door low effective byte1.bit4-bit5
pb15 = Y8 Turn Back low effective byte1.bit6-bit7
pc7 = Y2 Light byte2.bit4-bit5
pc8 = SD_D0 Acc low effecive byte2.bit0-bit1
"""
def __init__(self, verbose = False):
self.verbose = verbose
self.timer =delay_ms.Delay_ms(duration = 2000)
self.key_buf = 0x00 # Init key value
for item in (('Y2', 0x01),('Y5', 0x02),('Y6',0x04), ('Y7',0x08), ('Y8',0x10), ('SD_D0',0x20)) :
sw, idn = item
sw = switch.Switch(Pin(sw, Pin.IN, Pin.PULL_UP))
sw.close_func(self.toggle_key, (1, idn)) # press , return 0
sw.open_func(self.toggle_key, (0, idn))
def toggle_key(self, key, idn):
if key == 0:
self.key_buf = 0x00
else:
if idn = 0x01:
self.key_buf = 0x01 & 0x30
else if idn = 0x02:
self.key_buf = 0x01 & 0x03
else if idn = 0x04:
self.key_buf = 0x01 & 0xC0
else if idn = 0x08:
self.key_buf = 0x01 & 0x30
else if idn = 0x10:
self.key_buf = 0x01 & 0xc0
else idn = 0x20:
self.key_buf = 0x01 & 0x03
def read(self):
self.verbose and print(self.key_buf)
return (self.key_buf)