diff --git a/gex/__init__.py b/gex/__init__.py index 276571f..ea39353 100644 --- a/gex/__init__.py +++ b/gex/__init__.py @@ -19,6 +19,7 @@ from gex.units.OneWire import OneWire from gex.units.ADC import ADC from gex.units.SIPO import SIPO from gex.units.FCAP import FCAP +from gex.units.TOUCH import TOUCH # General, low level diff --git a/gex/units/TOUCH.py b/gex/units/TOUCH.py new file mode 100644 index 0000000..2667324 --- /dev/null +++ b/gex/units/TOUCH.py @@ -0,0 +1,64 @@ +import gex +from gex.Client import EventReport + +CMD_READ = 0 +CMD_SET_BIN_THR = 1 +CMD_DISABLE_ALL_REPORTS = 2 +CMD_GET_CH_COUNT = 10 + +class TOUCH(gex.Unit): + """ + Touch sensing + """ + + def _init(self): + self._handlers = {} + + def _type(self): + return 'TOUCH' + + def read(self): + """ Read raw values """ + + msg = self._query(CMD_READ) + pp = gex.PayloadParser(msg) + + items = [] + while pp.length() > 0: + items.append(pp.u16()) + + return items + + def set_button_thresholds(self, thresholds, confirm=True): + """ Set binary report thresholds """ + pb = gex.PayloadBuilder() + for t in thresholds: + pb.u16(t) + + self._send(CMD_SET_BIN_THR, pb.close(), confirm=confirm) + + def disable_button_mode(self, confirm=True): + """ Disable all button reports by clearing the thresholds """ + self._send(CMD_DISABLE_ALL_REPORTS, confirm=confirm) + + def get_channel_count(self, confirm=True): + """ Read nbr of channels """ + resp = self._query(CMD_GET_CH_COUNT) + pp = gex.PayloadParser(resp) + return pp.u8() + + def listen(self, nb, handler): + self._handlers[nb] = handler + + def _on_event(self, evt:EventReport): + l = [] + pp = gex.PayloadParser(evt.payload) + snap = pp.u32() + changed = pp.u32() + + for i in range(0, 32): + if changed & (1 << i): + if i in self._handlers: + self._handlers[i]((snap & (1 << i)) != 0, evt.timestamp) + + diff --git a/ttouch.py b/ttouch.py new file mode 100644 index 0000000..1d82b32 --- /dev/null +++ b/ttouch.py @@ -0,0 +1,18 @@ +import time + +import gex + +with gex.Client(gex.TrxRawUSB()) as client: + tsc = gex.TOUCH(client, 'tsc') + + print("There are %d touch channels." % tsc.get_channel_count()) + + tsc.set_button_thresholds([1225, 1440, 1440]) + + tsc.listen(0, lambda state, ts: print("Pad 1: %d" % state)) + tsc.listen(1, lambda state, ts: print("Pad 2: %d" % state)) + tsc.listen(2, lambda state, ts: print("Pad 3: %d" % state)) + + while True: + print(tsc.read()) + time.sleep(0.5)