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.

Re: multibutton and aswitch.py

Posted: Wed Aug 12, 2020 12:22 pm
by skylin008
Hi, Follow this my code,the function is ok.

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
    
    pc8    =  SD_D0   Acc      low effecive      byte2.bit0-bit1
    pc7    =  Y2   Light       high effective    byte2.bit4-bit5
    """
    def __init__(self,  verbose = False):
    	""" BYTE 2 """
    	ACC   = 1
    	LIGHT = 16
    	""" BYTE 1 """
    	LEFT  =  4
    	RIGHT =  1
    	BACK  =  64
    	DOOR  =  16
        self.verbose = verbose        
        self.buffer1 = 0x00
        self.buffer2 = 0x00

        sw_right= switch.Switch(Pin('Y5', Pin.IN, Pin.PULL_UP))
        sw_right.close_func(self.toggle_right, (1, RIGHT))
        sw_right.open_func(self.toggle_right, (0, RIGHT))

        sw_left= switch.Switch(Pin('Y6', Pin.IN, Pin.PULL_UP))
        sw_left.close_func(self.toggle_left, (1, LEFT))
        sw_left.open_func(self.toggle_left, (0, LEFT))

        sw_door= switch.Switch(Pin('Y7', Pin.IN, Pin.PULL_UP))
        sw_door.close_func(self.toggle_door, (1, DOOR))
        sw_door.open_func(self.toggle_door, (0, DOOR))

        sw_back= switch.Switch(Pin('Y8', Pin.IN, Pin.PULL_UP))
        sw_back.close_func(self.toggle_back, (1, BACK))
        sw_back.open_func(self.toggle_back, (0, BACK))

        sw_acc= switch.Switch(Pin('SD_D0', Pin.IN, Pin.PULL_UP))
        sw_acc.close_func(self.toggle_acc, (1, ACC))
        sw_acc.open_func(self.toggle_acc, (0, ACC))

        sw_light= switch.Switch(Pin('Y2', Pin.IN, Pin.PULL_UP))
        sw_light.close_func(self.toggle_light, (1, LIGHT))
        sw_light.open_func(self.toggle_light, (0, LIGHT))


    def toggle_right(self, key, idn):
    	if key:
        	self.buffer1 = self.buffer1 | idn
        else:
        	self.buffer1 = self.buffer1 & 0xFE

    def toggle_left(self, key, idn):
        if key:
        	self.buffer1 = self.buffer1 | idn
        else:
        	self.buffer1 = self.buffer1 & 0xFB
        	

    def toggle_door(self, key, idn):
       	if key:
        	self.buffer1 = self.buffer1 | idn
        else:
        	self.buffer1 = self.buffer1 & 0xEF
        

    def toggle_back(self, key, idn):
        if key:
        	self.buffer1 = self.buffer1 | idn
        else:
        	self.buffer1 = self.buffer1 & 0xBF
        	

    def toggle_acc(self, key, idn):
        if key:
        	self.buffer2 = self.buffer2 | idn
        else:
        	self.buffer2 = self.buffer2 & 0xFE
        	

    def toggle_light(self, key, idn):
        if key:
        	self.buffer2 = self.buffer2 & 0xEF
        else:
        	self.buffer2 = self.buffer2 | idn