Skip to content

Commit aab20a5

Browse files
authored
Merge pull request #15 from makermelissa/main
Apply rotation to display touch
2 parents 24d6432 + 9208674 commit aab20a5

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

adafruit_qualia/displays/__init__.py

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,54 @@
3131
TOUCH_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+
3482
class 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

adafruit_qualia/graphics.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def __init__(
9090
scale=scale,
9191
debug=debug,
9292
)
93-
self.display.rotation = rotation
93+
self._dotclock_display.rotation = rotation
9494

9595
def init_display(self, display_type: str, *, auto_refresh: bool = True):
9696
"""Load the Display Class, then initialize the display and touch driver"""
@@ -134,3 +134,13 @@ def touch(self):
134134
def dotclockdisplay(self):
135135
"""Return the dotclock display object"""
136136
return self._dotclock_display
137+
138+
@property
139+
def rotation(self):
140+
"""Return the rotation"""
141+
return self._dotclock_display.rotation
142+
143+
@rotation.setter
144+
def rotation(self, value):
145+
"""Set the rotation"""
146+
self._dotclock_display.rotation = value

0 commit comments

Comments
 (0)