Exemplo n.º 1
0
    def __init__(self, ipcon, key_queue):
        if not config.UID_DUAL_BUTTON_BRICKLET[0]:
            print("Not Configured: Dual Button 1")

        if not config.UID_DUAL_BUTTON_BRICKLET[1]:
            print("Not Configured: Dual Button 2")

        self.key_queue = key_queue
        self.ipcon = ipcon

        if config.UID_DUAL_BUTTON_BRICKLET[0]:
            self.db1 = DualButton(config.UID_DUAL_BUTTON_BRICKLET[0], self.ipcon)
        else:
            self.db1 = None

        if config.UID_DUAL_BUTTON_BRICKLET[1]:
            self.db2 = DualButton(config.UID_DUAL_BUTTON_BRICKLET[1], self.ipcon)
        else:
            self.db2 = None

        if self.db1:
            try:
                self.db1.get_button_state()
                print("Found: Dual Button 1 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[0])
            except:
                self.db1 = None
                print("Not Found: Dual Button 1 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[0])

        if self.db2:
            try:
                self.db2.get_button_state()
                print("Found: Dual Button 2 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[1])
            except:
                self.db2 = None
                print("Not Found: Dual Button 2 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[1])

        if self.db1:
            self.db1.register_callback(self.db1.CALLBACK_STATE_CHANGED, self.cb_state_changed1)
        if self.db2:
            self.db2.register_callback(self.db2.CALLBACK_STATE_CHANGED, self.cb_state_changed2)

        self.press_timer = RepeatedTimer(0.1, self.press_tick)
Exemplo n.º 2
0
class DualButtonInput:
    current_state = 0
    current_state_counter = [0]*4
    press_timer = None

    def __init__(self, ipcon, key_queue):
        if not config.UID_DUAL_BUTTON_BRICKLET[0]:
            print("Not Configured: Dual Button 1")

        if not config.UID_DUAL_BUTTON_BRICKLET[1]:
            print("Not Configured: Dual Button 2")

        self.key_queue = key_queue
        self.ipcon = ipcon

        if config.UID_DUAL_BUTTON_BRICKLET[0]:
            self.db1 = DualButton(config.UID_DUAL_BUTTON_BRICKLET[0], self.ipcon)
        else:
            self.db1 = None

        if config.UID_DUAL_BUTTON_BRICKLET[1]:
            self.db2 = DualButton(config.UID_DUAL_BUTTON_BRICKLET[1], self.ipcon)
        else:
            self.db2 = None

        if self.db1:
            try:
                self.db1.get_button_state()
                print("Found: Dual Button 1 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[0])
            except:
                self.db1 = None
                print("Not Found: Dual Button 1 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[0])

        if self.db2:
            try:
                self.db2.get_button_state()
                print("Found: Dual Button 2 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[1])
            except:
                self.db2 = None
                print("Not Found: Dual Button 2 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[1])

        if self.db1:
            self.db1.register_callback(self.db1.CALLBACK_STATE_CHANGED, self.cb_state_changed1)
        if self.db2:
            self.db2.register_callback(self.db2.CALLBACK_STATE_CHANGED, self.cb_state_changed2)

        self.press_timer = RepeatedTimer(0.1, self.press_tick)

    def stop(self):
        if self.press_timer is not None:
            self.press_timer.stop()

    def cb_state_changed1(self, button_l, button_r, led_l, led_r):
        l = button_l == DualButton.BUTTON_STATE_PRESSED
        r = button_r == DualButton.BUTTON_STATE_PRESSED
        state = (l << 0) | (r << 1)

        changed_state = (self.current_state ^ state) & 0b0011
        self.current_state = state

        self.state_to_queue(changed_state & self.current_state)

    def cb_state_changed2(self, button_l, button_r, led_l, led_r):
        l = button_l == DualButton.BUTTON_STATE_PRESSED
        r = button_r == DualButton.BUTTON_STATE_PRESSED
        state = (l << 2) | (r << 3)

        changed_state = (self.current_state ^ state) & 0b1100
        self.current_state = state

        self.state_to_queue(changed_state & self.current_state)

    def state_to_queue(self, state):
        for item in config.KEYMAP_DUAL_BUTTON.items():
            if state & (1 << item[0]):
                self.key_queue.put(item[1])

    def press_tick(self):
        state = 0
        for i in range(4):
            if self.current_state & (1 << i):
                self.current_state_counter[i] += 1
            else:
                self.current_state_counter[i] = 0

            if self.current_state_counter[i] > 1:
                state |= (1 << i)

        self.state_to_queue(state)