Page 1 of 1

multibutton and aswitch.py

Posted: Thu Jul 30, 2020 1:05 pm
by skylin008
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)          

Re: multibutton and aswitch.py

Posted: Thu Jul 30, 2020 4:04 pm
by pythoncoder
I'm puzzling over what you're trying to do here. Some of your bitwise and expressions will always return 0. For example:

Code: Select all

self.key_buf = 0x01 & 0xC0
.key_buf isn't really a buffer as you overwrite its contents with each keypress, so you'll only ever read the last key pressed.

If you want to detect cases of multiple buttons pressed I can think of two ways. One would be to store the key ID, press or release status and timestamp in a queue. The queue contents could then be analysed to determine press and release sequences.

Alternatively, and probably easier, the press callback for one key could read the instantaneous state of the other keys and take appropriate action if any are already pressed. You'll never get absolutely simultaneous presses in practice.

An asynchronous driver for a matrix of keys with N key rollover is something I've considered writing. But I've never actually got round to doing it and I don't think that's quite what you're after.

Re: multibutton and aswitch.py

Posted: Fri Jul 31, 2020 9:28 am
by skylin008
Thanks pythoncoder. I will be try and feedback.