示例#1
0
    def __init__(self, ipcon, key_queue):
        if not config.UID_MULTI_TOUCH_BRICKLET:
            print("Not Configured: Multi Touch")
            return

        self.key_queue = key_queue
        self.ipcon = ipcon
        self.mt = MultiTouch(config.UID_MULTI_TOUCH_BRICKLET, self.ipcon)

        try:
            self.mt.get_electrode_sensitivity()
            print("Found: Multi Touch ({0})").format(
                config.UID_MULTI_TOUCH_BRICKLET)
        except:
            print("Not Found: Multi Touch ({0})").format(
                config.UID_MULTI_TOUCH_BRICKLET)
            return

        self.mt.set_electrode_sensitivity(100)
        self.mt.register_callback(self.mt.CALLBACK_TOUCH_STATE,
                                  self.cb_touch_state)

        self.touch_timer = RepeatedTimer(0.1, self.touch_tick)
# Callback function for touch_state
def cb_touch_state(touch_state):
    s = ''
    if touch_state & (1 << 12):
        s += 'In proximity, '

    if (touch_state & 0xFFF) == 0:
        s += 'No electrodes touched\n'
    else:
        s += 'Electrodes '
        for i in range(12):
            if touch_state & (1 << i):
                s += str(i) + ' '
        s += 'touched\n'

    print(s)


if __name__ == "__main__":
    ipcon = IPConnection()  # Create IP connection
    mt = MultiTouch(UID, ipcon)  # Create device object

    ipcon.connect(HOST, PORT)  # Connect to brickd
    # Don't use device before ipcon is connected

    # Register touch state callback to function cb_touch_state
    mt.register_callback(mt.CALLBACK_TOUCH_STATE, cb_touch_state)

    raw_input('Press key to exit\n')  # Use input() in Python 3
    ipcon.disconnect()