Skip to content

Commit 89d9bba

Browse files
committed
Fixed touchscreen issue, moved stuff around
1 parent 3d20ba9 commit 89d9bba

File tree

2 files changed

+78
-45
lines changed

2 files changed

+78
-45
lines changed

adafruit_pyportal/__init__.py

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import gc
2828
import time
2929
import board
30-
import busio
31-
import pulseio
3230
import terminalio
3331
import supervisor
3432
from adafruit_portalbase import PortalBase
@@ -143,7 +141,7 @@ def __init__(
143141
if external_spi: # If SPI Object Passed
144142
spi = external_spi
145143
else: # Else: Make ESP32 connection
146-
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
144+
spi = board.SPI()
147145

148146
if image_json_path or image_url_path:
149147
if debug:
@@ -184,20 +182,17 @@ def __init__(
184182
debug=debug,
185183
)
186184

187-
self.peripherals = Peripherals(spi, debug=debug)
185+
self.peripherals = Peripherals(spi, display=self.display, debug=debug)
186+
self.set_backlight = self.peripherals.set_backlight
187+
self.sd_check = self.peripherals.sd_check
188+
self.play_file = self.peripherals.play_file
188189

189-
try:
190-
if hasattr(board, "TFT_BACKLIGHT"):
191-
self._backlight = pulseio.PWMOut(
192-
board.TFT_BACKLIGHT
193-
) # pylint: disable=no-member
194-
elif hasattr(board, "TFT_LITE"):
195-
self._backlight = pulseio.PWMOut(
196-
board.TFT_LITE
197-
) # pylint: disable=no-member
198-
except ValueError:
199-
self._backlight = None
200-
self.set_backlight(1.0) # turn on backlight
190+
if hasattr(self.peripherals, "touchscreen"):
191+
self.touchscreen = self.peripherals.touchscreen
192+
if hasattr(self.peripherals, "mouse_cursor"):
193+
self.mouse_cursor = self.peripherals.mouse_cursor
194+
if hasattr(self.peripherals, "cursor"):
195+
self.cursor = self.peripherals.cursor
201196

202197
# show thank you and bootup file if available
203198
for bootscreen in ("/thankyou.bmp", "/pyportal_startup.bmp"):
@@ -293,34 +288,6 @@ def set_caption(self, caption_text, caption_position, caption_color):
293288
)
294289
self.set_text(caption_text, index)
295290

296-
def play_file(self, file_name, wait_to_finish=True):
297-
"""Play a wav file.
298-
299-
:param str file_name: The name of the wav file to play on the speaker.
300-
301-
"""
302-
self.peripherals.play_file(file_name, wait_to_finish)
303-
304-
def set_backlight(self, val):
305-
"""Adjust the TFT backlight.
306-
307-
:param val: The backlight brightness. Use a value between ``0`` and ``1``, where ``0`` is
308-
off, and ``1`` is 100% brightness.
309-
310-
"""
311-
val = max(0, min(1.0, val))
312-
if self._backlight:
313-
self._backlight.duty_cycle = int(val * 65535)
314-
else:
315-
self.display.auto_brightness = False
316-
self.display.brightness = val
317-
318-
def sd_check(self):
319-
"""Returns True if there is an SD card preset and False
320-
if there is no SD card.
321-
"""
322-
return self.peripherals.sd_check()
323-
324291
def image_converter_url(self, image_url, width, height, color_depth=16):
325292
"""Generate a converted image url from the url passed in,
326293
with the given width and height. aio_username and aio_key must be

adafruit_pyportal/peripherals.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
2525
"""
2626

27+
import gc
2728
import board
2829
from digitalio import DigitalInOut
30+
import pulseio
2931
import audioio
3032
import audiocore
3133
import storage
@@ -47,11 +49,13 @@ class Peripherals:
4749
"""Peripherals Helper Class for the PyPortal Library"""
4850

4951
# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
50-
def __init__(self, spi, debug=False):
52+
def __init__(self, spi, display, debug=False):
5153
# Speaker Enable
5254
self._speaker_enable = DigitalInOut(board.SPEAKER_ENABLE)
5355
self._speaker_enable.switch_to_output(False)
5456

57+
self._display = display
58+
5559
if hasattr(board, "AUDIO_OUT"):
5660
self.audio = audioio.AudioOut(board.AUDIO_OUT)
5761
elif hasattr(board, "SPEAKER"):
@@ -73,6 +77,68 @@ def __init__(self, spi, debug=False):
7377
except OSError as error:
7478
print("No SD card found:", error)
7579

80+
try:
81+
if hasattr(board, "TFT_BACKLIGHT"):
82+
self._backlight = pulseio.PWMOut(
83+
board.TFT_BACKLIGHT
84+
) # pylint: disable=no-member
85+
elif hasattr(board, "TFT_LITE"):
86+
self._backlight = pulseio.PWMOut(
87+
board.TFT_LITE
88+
) # pylint: disable=no-member
89+
except ValueError:
90+
self._backlight = None
91+
self.set_backlight(1.0) # turn on backlight
92+
93+
if hasattr(board, "TOUCH_XL"):
94+
import adafruit_touchscreen
95+
96+
if debug:
97+
print("Init touchscreen")
98+
# pylint: disable=no-member
99+
self.touchscreen = adafruit_touchscreen.Touchscreen(
100+
board.TOUCH_XL,
101+
board.TOUCH_XR,
102+
board.TOUCH_YD,
103+
board.TOUCH_YU,
104+
calibration=((5200, 59000), (5800, 57000)),
105+
size=(board.DISPLAY.width, board.DISPLAY.height),
106+
)
107+
# pylint: enable=no-member
108+
109+
self.set_backlight(1.0) # turn on backlight
110+
elif hasattr(board, "BUTTON_CLOCK"):
111+
from adafruit_cursorcontrol.cursorcontrol import Cursor
112+
from adafruit_cursorcontrol.cursorcontrol_cursormanager import CursorManager
113+
114+
if debug:
115+
print("Init cursor")
116+
self.mouse_cursor = Cursor(
117+
board.DISPLAY, display_group=self.splash, cursor_speed=8
118+
)
119+
self.mouse_cursor.hide()
120+
self.cursor = CursorManager(self.mouse_cursor)
121+
else:
122+
raise AttributeError(
123+
"PyPortal module requires either a touchscreen or gamepad."
124+
)
125+
126+
gc.collect()
127+
128+
def set_backlight(self, val):
129+
"""Adjust the TFT backlight.
130+
131+
:param val: The backlight brightness. Use a value between ``0`` and ``1``, where ``0`` is
132+
off, and ``1`` is 100% brightness.
133+
134+
"""
135+
val = max(0, min(1.0, val))
136+
if self._backlight:
137+
self._backlight.duty_cycle = int(val * 65535)
138+
else:
139+
self._display.auto_brightness = False
140+
self._display.brightness = val
141+
76142
def play_file(self, file_name, wait_to_finish=True):
77143
"""Play a wav file.
78144

0 commit comments

Comments
 (0)