1616from .cv2_touch_screen import CV2_Touch_Screen
1717
1818class CST816 (CV2_Touch_Screen ):
19+ """
20+ OpenCV CST816 touch screen driver using an I2C interface.
21+ """
1922 _I2C_ADDRESS = 0x15
2023 _CHIP_ID = 0xB6
2124
@@ -53,24 +56,42 @@ class CST816(CV2_Touch_Screen):
5356 _REG_IO_CTL = 0xFD
5457 _REG_DIS_AUTO_SLEEP = 0xFE
5558
56- def __init__ (self , i2c , address = _I2C_ADDRESS , width = 240 , height = 320 , rotation = 1 ):
59+ def __init__ (self , i2c , width = 240 , height = 320 , rotation = 1 , address = _I2C_ADDRESS ):
60+ """
61+ Initializes the CST816 driver.
62+
63+ Args:
64+ i2c (I2C): I2C object for communication
65+ width (int, optional): Touch screen width in pixels.
66+ Default is 240
67+ height (int, optional): Touch screen height in pixels.
68+ Default is 320
69+ rotation (int, optional): Orientation of touch screen
70+ - 0: Portrait (default)
71+ - 1: Landscape
72+ - 2: Inverted portrait
73+ - 3: Inverted landscape
74+ address (int, optional): I2C address of the camera.
75+ Default is 0x15
76+ """
5777 self .i2c = i2c
5878 self .address = address
5979 self .width = width
6080 self .height = height
6181 self .rotation = rotation
6282
63- def is_connected (self ):
83+ def _is_connected (self ):
6484 """
65- Check if the CST816 touch screen is connected by reading the chip ID.
85+ Checks if the touch screen is connected by reading the chip ID.
6686
6787 Returns:
68- bool: True if connected, False otherwise
88+ bool: True if the touch screen is connected and the chip ID is
89+ correct, otherwise False.
6990 """
7091 try :
7192 # Try to read the chip ID
7293 # If it throws an I/O error - the device isn't connected
73- chip_id = self .read_register_value ( self . _REG_CHIP_ID )
94+ chip_id = self ._get_chip_id ( )
7495
7596 # Confirm the chip ID is correct
7697 if chip_id == self ._CHIP_ID :
@@ -80,7 +101,13 @@ def is_connected(self):
80101 except :
81102 return False
82103
83- def getChipID (self ):
104+ def _get_chip_id (self ):
105+ """
106+ Reads the chip ID.
107+
108+ Returns:
109+ int: The chip ID of the HM01B0 (should be 0xB6).
110+ """
84111 return self .read_register_value (self ._REG_CHIP_ID )
85112
86113 def is_touched (self ):
@@ -97,6 +124,13 @@ def is_touched(self):
97124 return touch_num > 0
98125
99126 def get_touch_xy (self ):
127+ """
128+ Get the X and Y coordinates of the touch point. Will return the last
129+ touch point if no touch is currently detected.
130+
131+ Returns:
132+ tuple: (x, y) coordinates of the touch point
133+ """
100134 x = self .read_register_value (self ._REG_X_POS_H , 2 ) & 0x0FFF
101135 y = self .read_register_value (self ._REG_Y_POS_H , 2 ) & 0x0FFF
102136
@@ -118,6 +152,8 @@ def read_register_value(self, reg, num_bytes=1):
118152
119153 Args:
120154 reg (int): Register address to read from
155+ num_bytes (int, optional): Number of bytes to read from the register.
156+ Default is 1
121157
122158 Returns:
123159 int: Value read from the register
0 commit comments