summaryrefslogtreecommitdiff
path: root/wasp/drivers/cst816s.py
blob: 182afeb895659f76b184c9360b1ab014f9dc4082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (C) 2020 Daniel Thompson

"""Hynitron CST816S touch contoller driver
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""

import array

class CST816S:
    """Hynitron CST816S I2C touch controller driver.

    .. automethod:: __init__
    """
    
    def __init__(self, bus):
        """Specify the bus used by the touch controller.

        :param machine.I2C bus: I2C bus for the CST816S.
        """
        self.i2c = bus
        self.dbuf = bytearray(6)
        self.event = array.array('H', (0, 0, 0))

    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: 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 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

        return event