Skip to content

Commit ca68e8c

Browse files
committed
Initial CST816 touch screen driver
Resolves #23
1 parent 307ea7c commit ca68e8c

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

cv2_drivers/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from . import displays
2-
from . import cameras
2+
from . import cameras
3+
from . import touch_screens

cv2_drivers/touch_screens/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import cst816

cv2_drivers/touch_screens/cst816.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
from .cv2_touch_screen import CV2_Touch_Screen
2+
3+
# Derived from:
4+
# https://github.com/fbiego/CST816S
5+
class CST816(CV2_Touch_Screen):
6+
_I2C_ADDRESS = 0x15
7+
_CHIP_ID = 0xB6
8+
9+
# Registers
10+
_REG_GESTURE_ID = 0x01
11+
_REG_FINGER_NUM = 0x02
12+
_REG_X_POS_H = 0x03
13+
_REG_X_POS_L = 0x04
14+
_REG_Y_POS_H = 0x05
15+
_REG_Y_POS_L = 0x06
16+
_REG_BPC0H = 0xB0
17+
_REG_BPC0L = 0xB1
18+
_REG_BPC1H = 0xB2
19+
_REG_BPC1L = 0xB3
20+
_REG_CHIP_ID = 0xA7
21+
_REG_PROJ_ID = 0xA8
22+
_REG_FW_VERSION = 0xA9
23+
_REG_MOTION_MASK = 0xEC
24+
_REG_IRQ_PULSE_WIDTH = 0xED
25+
_REG_NOR_SCAN_PER = 0xEE
26+
_REG_MOTION_SL_ANGLE = 0xEF
27+
_REG_LP_SCAN_RAW_1H = 0xF0
28+
_REG_LP_SCAN_RAW_1L = 0xF1
29+
_REG_LP_SCAN_RAW_2H = 0xF2
30+
_REG_LP_SCAN_RAW_2L = 0xF3
31+
_REG_LP_AUTO_WAKE_TIME = 0xF4
32+
_REG_LP_SCAN_TH = 0xF5
33+
_REG_LP_SCAN_WIN = 0xF6
34+
_REG_LP_SCAN_FREQ = 0xF7
35+
_REG_LP_SCAN_IDAC = 0xF8
36+
_REG_AUTO_SLEEP_TIME = 0xF9
37+
_REG_IRQ_CTL = 0xFA
38+
_REG_AUTO_RESET = 0xFB
39+
_REG_LONG_PRESS_TIME = 0xFC
40+
_REG_IO_CTL = 0xFD
41+
_REG_DIS_AUTO_SLEEP = 0xFE
42+
43+
def __init__(self, i2c, address=_I2C_ADDRESS, width=240, height=320, rotation=1):
44+
self.i2c = i2c
45+
self.address = address
46+
self.width = width
47+
self.height = height
48+
self.rotation = rotation
49+
50+
def is_connected(self):
51+
"""
52+
Check if the CST816 touch screen is connected by reading the chip ID.
53+
54+
Returns:
55+
bool: True if connected, False otherwise
56+
"""
57+
try:
58+
# Try to read the chip ID
59+
# If it throws an I/O error - the device isn't connected
60+
chip_id = self.read_register_value(self._REG_CHIP_ID)
61+
62+
# Confirm the chip ID is correct
63+
if chip_id == self._CHIP_ID:
64+
return True
65+
else:
66+
return False
67+
except:
68+
return False
69+
70+
def getChipID(self):
71+
return self.read_register_value(self._REG_CHIP_ID)
72+
73+
def get_touch(self):
74+
x = self.read_register_value(self._REG_X_POS_H, 2) & 0x0FFF
75+
y = self.read_register_value(self._REG_Y_POS_H, 2) & 0x0FFF
76+
touch_num = self.read_register_value(self._REG_FINGER_NUM)
77+
78+
# Adjust for the rotation
79+
if self.rotation == 0:
80+
x,y = x, y
81+
elif self.rotation == 1:
82+
x,y = y, self.width - x
83+
elif self.rotation == 2:
84+
x,y = self.height - x, self.width - y
85+
elif self.rotation == 3:
86+
x,y = self.height - y, x
87+
88+
return (x, y, touch_num)
89+
90+
def read_register_value(self, reg, num_bytes=1):
91+
"""
92+
Read a single byte from the specified register.
93+
94+
Args:
95+
reg (int): Register address to read from
96+
97+
Returns:
98+
int: Value read from the register
99+
"""
100+
data = self.i2c.readfrom_mem(self.address, reg, num_bytes)
101+
value = 0
102+
for i in range(num_bytes):
103+
value = (value << 8) | data[i]
104+
return value
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class CV2_Touch_Screen():
2+
def __init__(self):
3+
pass
4+
5+
# TODO: Implement common methods for all touch screens

0 commit comments

Comments
 (0)