3131TOUCH_INIT_DELAY = 0.1 # 100ms
3232
3333
34+ class Touch :
35+ """Simple passthrough touch class that allows rotation transformation"""
36+
37+ def __init__ (self , touch_driver , display , rotation = 0 ):
38+ self ._driver = touch_driver
39+ self ._width = display .width
40+ self ._height = display .height
41+ self ._rotation = rotation
42+
43+ def transform (self , x , y ):
44+ """Transform touch coordinates based on rotation"""
45+ if self ._rotation == 0 :
46+ return x , y
47+ elif self ._rotation == 90 :
48+ return y , self ._height - x
49+ elif self ._rotation == 180 :
50+ return self ._width - x , self ._height - y
51+ elif self ._rotation == 270 :
52+ return self ._width - y , x
53+ else :
54+ raise ValueError ("Rotation must be 0, 90, 180, or 270" )
55+
56+ @property
57+ def rotation (self ):
58+ """Return the rotation"""
59+ return self ._rotation
60+
61+ @rotation .setter
62+ def rotation (self , value ):
63+ """Set the rotation"""
64+ if value not in {0 , 90 , 180 , 270 }:
65+ raise ValueError ("Rotation must be 0, 90, 180, or 270" )
66+ self ._rotation = value
67+
68+ @property
69+ def touches (self ):
70+ """Return the number of touches"""
71+ touches = self ._driver .touches
72+ for touch in touches :
73+ touch ["x" ], touch ["y" ] = self .transform (touch ["x" ], touch ["y" ])
74+ return touches
75+
76+ @property
77+ def touched (self ):
78+ """Return whether the screen is touched"""
79+ return self ._driver .touched
80+
81+
3482class DotClockDisplay :
3583 """DotClock Display Base Class for the Adafruit Qualia ESP32-S3 Library"""
3684
@@ -43,7 +91,7 @@ def __init__(self):
4391 self ._touch_driver = TOUCH_FOCALTOUCH
4492 self ._touch_address = 0x38
4593 self .display = None
46- self .touch = None
94+ self ._touch = None
4795 self ._i2c = None
4896 self ._round = False
4997
@@ -73,9 +121,14 @@ def init_touch(self):
73121 self ._i2c = busio .I2C (board .SCL , board .SDA )
74122 time .sleep (TOUCH_INIT_DELAY ) # Wait for Focaltouch Chip to finish resetting
75123 try :
76- self .touch = self ._touch_driver (self ._i2c , self ._touch_address )
124+ self ._touch = Touch (self ._touch_driver (self ._i2c , self ._touch_address ), self .display )
125+
77126 except (RuntimeError , ValueError ):
78- self .touch = None
127+ self ._touch = None
128+
129+ def _transform_touch (self , x , y ):
130+ """Transform touch coordinates if needed"""
131+ return x , y
79132
80133 @property
81134 def width (self ):
@@ -102,3 +155,27 @@ def i2c_bus(self):
102155 def round (self ):
103156 """Return whether the display is circular"""
104157 return self ._round
158+
159+ @property
160+ def rotation (self ):
161+ """Return the display touch rotation"""
162+ display_rotation = self .display .rotation if self .display is not None else 0
163+ touch_rotation = self ._touch .rotation if self ._touch is not None else 0
164+ if display_rotation != touch_rotation :
165+ raise ValueError ("Display and touch rotation do not match" )
166+ return self ._touch .rotation
167+
168+ @rotation .setter
169+ def rotation (self , value : int ):
170+ """Set the display rotation"""
171+ if value not in {0 , 90 , 180 , 270 }:
172+ raise ValueError ("Rotation must be 0, 90, 180, or 270" )
173+ self ._touch .rotation = value
174+ # Update the display rotation if already initialized
175+ if self .display is not None :
176+ self .display .rotation = value
177+
178+ @property
179+ def touch (self ):
180+ """Return the touch driver"""
181+ return self ._touch
0 commit comments