diff options
Diffstat (limited to 'wasp/drivers')
| -rw-r--r-- | wasp/drivers/cst816s.py | 51 |
1 files changed, 43 insertions, 8 deletions
diff --git a/wasp/drivers/cst816s.py b/wasp/drivers/cst816s.py index 4a372c0..7461a5f 100644 --- a/wasp/drivers/cst816s.py +++ b/wasp/drivers/cst816s.py @@ -1,4 +1,14 @@ -"""Hynitron CST816S touch contoller driver for MicroPython.""" +"""Hynitron CST816S touch contoller driver for MicroPython. + +After modifying this file we can replace the frozen driver with the +test driver with the following command:: + + ./tools/wasptool \ + --exec wasp/drivers/cst816s.py \ + --eval "watch.touch = CST816S(watch.i2c)"` +""" + +import array class CST816S: """Hynitron CST816S I2C touch controller driver.""" @@ -6,23 +16,48 @@ class CST816S: def __init__(self, bus): self.i2c = bus self.dbuf = bytearray(6) + self.event = array.array('H', (0, 0, 0)) - def get_event(self, queue): + def get_event(self): """Receive a touch event. Check for a pending touch event and, if an event is pending, prepare it ready to go in the event queue. - :return: True if an event is received, False otherwise. + :return: An event record if an event is received, None otherwise. """ dbuf = self.dbuf + event = self.event + + # TODO: check the interrupt pin try: self.i2c.readfrom_mem_into(21, 1, dbuf) except OSError: - return False + return None + + # Skip junk events + if dbuf[0] == 0: + return None + + x = ((dbuf[2] & 0xf) << 8) + dbuf[3] + y = ((dbuf[4] & 0xf) << 8) + dbuf[5] + swipe_start = dbuf[2] & 0x80 + + # Skip identical events... when the I2C interface comes alive + # we can still get back stale events + if dbuf[0] == event[0] and x == event[1] and y == event[2] \ + and not swipe_start: + return None + + # This is a good event, lets save it + event[0] = dbuf[0] + event[1] = x + event[2] = y + + # Do not forward swipe start events + if dbuf[2] & 0x80: + event[0] = 0 + return None - queue[0] = dbuf[0] - queue[1] = ((dbuf[2] & 0xf) << 8) + dbuf[3] - queue[2] = ((dbuf[4] & 0xf) << 8) + dbuf[5] - return True + return event |
