Skip to content

Commit 09d5e7c

Browse files
authored
Merge pull request #9 from brentru/add-cursorcontrol
Add CursorControl for non-touchscreen hardware and fixes for smaller rez. displays
2 parents cf60390 + 2ef73d4 commit 09d5e7c

File tree

3 files changed

+102
-64
lines changed

3 files changed

+102
-64
lines changed

adafruit_pyoa.py

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@
5151
from digitalio import DigitalInOut
5252
import displayio
5353
import adafruit_touchscreen
54+
from adafruit_cursorcontrol.cursorcontrol import Cursor
55+
from adafruit_cursorcontrol.cursorcontrol_cursormanager import CursorManager
5456
import audioio
5557
from adafruit_display_text.label import Label
56-
from adafruit_bitmap_font import bitmap_font
5758
from adafruit_button import Button
59+
import terminalio
5860

5961
__version__ = "0.0.0-auto.0"
6062
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PYOA.git"
@@ -74,20 +76,31 @@ def __init__(self):
7476

7577
self._speaker_enable = DigitalInOut(board.SPEAKER_ENABLE)
7678
self._speaker_enable.switch_to_output(False)
77-
self.audio = audioio.AudioOut(board.AUDIO_OUT)
79+
if hasattr(board, 'AUDIO_OUT'):
80+
self.audio = audioio.AudioOut(board.AUDIO_OUT)
81+
elif hasattr(board, 'SPEAKER'):
82+
self.audio = audioio.AudioOut(board.SPEAKER)
83+
else:
84+
raise AttributeError('Board does not have an audio output!')
7885

7986
self._background_file = None
8087
self._wavfile = None
8188

8289
board.DISPLAY.auto_brightness = False
8390
self.backlight_fade(0)
8491
board.DISPLAY.show(self.root_group)
85-
86-
self.touchscreen = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
87-
board.TOUCH_YD, board.TOUCH_YU,
88-
calibration=((5200, 59000),
89-
(5800, 57000)),
90-
size=(320, 240))
92+
self.touchscreen = None
93+
if hasattr(board, 'TOUCH_XL'):
94+
self.touchscreen = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
95+
board.TOUCH_YD, board.TOUCH_YU,
96+
calibration=((5200, 59000),
97+
(5800, 57000)),
98+
size=(320, 240))
99+
elif hasattr(board, 'BUTTON_CLOCK'):
100+
self.mouse_cursor = Cursor(board.DISPLAY, display_group=self.root_group, cursor_speed=8)
101+
self.cursor = CursorManager(self.mouse_cursor)
102+
else:
103+
raise AttributeError('PYOA requires a touchscreen or cursor.')
91104
self._gamedirectory = None
92105
self._gamefilename = None
93106
self._game = None
@@ -103,30 +116,32 @@ def load_game(self, game_directory):
103116
"""Load a game.
104117
105118
:param game_directory: where the game files are stored
106-
107119
"""
108120
self._gamedirectory = game_directory
109-
110-
self._text_font = bitmap_font.load_font(game_directory+"/fonts/Arial-Bold-12.bdf")
111-
#self._text_font = fontio.BuiltinFont
112-
try:
113-
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. "\'?!'
114-
print("Preloading font glyphs:", glyphs)
115-
self._text_font.load_glyphs(glyphs)
116-
except AttributeError:
117-
pass # normal for built in font
118-
119-
self._left_button = Button(x=10, y=195, width=120, height=40,
121+
self._text_font = terminalio.FONT
122+
# Button Attributes
123+
btn_left = 10
124+
btn_right = btn_left+180
125+
btn_mid = btn_left+90
126+
button_y = 195
127+
button_width = 120
128+
button_height = 40
129+
if board.DISPLAY.height < 200:
130+
button_y /= 2
131+
button_y += 10
132+
button_width /= 2
133+
button_height /= 2
134+
btn_right /= 2
135+
btn_mid /= 2
136+
self._left_button = Button(x=int(btn_left), y=int(button_y), width=int(button_width), height=int(button_height),
120137
label="Left", label_font=self._text_font,
121138
style=Button.SHADOWROUNDRECT)
122-
self._right_button = Button(x=190, y=195, width=120, height=40,
139+
self._right_button = Button(x=int(btn_right), y=int(button_y), width=int(button_width), height=int(button_height),
123140
label="Right", label_font=self._text_font,
124141
style=Button.SHADOWROUNDRECT)
125-
self._middle_button = Button(x=100, y=195, width=120, height=40,
142+
self._middle_button = Button(x=int(btn_mid), y=int(button_y), width=int(button_width), height=int(button_height),
126143
label="Middle", label_font=self._text_font,
127144
style=Button.SHADOWROUNDRECT)
128-
129-
130145
self._gamefilename = game_directory+"/cyoa.json"
131146
try:
132147
game_file = open(self._gamefilename, "r")
@@ -137,18 +152,19 @@ def load_game(self, game_directory):
137152

138153
def _fade_to_black(self):
139154
"""Turn down the lights."""
155+
self.mouse_cursor.is_hidden = True
140156
self.backlight_fade(0)
141157
# turn off background so we can render the text
142158
self.set_background(None, with_fade=False)
143159
self.set_text(None, None)
144160
for _ in range(len(self._button_group)):
145161
self._button_group.pop()
162+
self.mouse_cursor.is_hidden = False
146163

147164
def _display_buttons(self, card):
148165
"""Display the buttons of a card.
149166
150167
:param card: The active card
151-
152168
"""
153169
button01_text = card.get('button01_text', None)
154170
button02_text = card.get('button02_text', None)
@@ -166,15 +182,13 @@ def _display_background_for(self, card):
166182
"""If there's a background on card, display it.
167183
168184
:param card: The active card
169-
170185
"""
171186
self.set_background(card.get('background_image', None), with_fade=False)
172187

173188
def _display_text_for(self, card):
174189
"""Display the main text of a card.
175190
176191
:param card: The active card
177-
178192
"""
179193
text = card.get('text', None)
180194
text_color = card.get('text_color', 0x0) # default to black
@@ -189,7 +203,6 @@ def _play_sound_for(self, card):
189203
"""If there's a sound, start playing it.
190204
191205
:param card: The active card
192-
193206
"""
194207
sound = card.get('sound', None)
195208
loop = card.get('sound_repeat', False)
@@ -207,9 +220,15 @@ def _wait_for_press(self, card):
207220
"""
208221
button01_text = card.get('button01_text', None)
209222
button02_text = card.get('button02_text', None)
223+
point_touched = None
210224
while True:
211-
point_touched = self.touchscreen.touch_point
212-
if point_touched:
225+
if self.touchscreen is not None:
226+
point_touched = self.touchscreen.touch_point
227+
else:
228+
self.cursor.update()
229+
if self.cursor.is_clicked is True:
230+
point_touched = self.mouse_cursor.x, self.mouse_cursor.y
231+
if point_touched is not None:
213232
print("touch: ", point_touched)
214233
if button01_text and not button02_text:
215234
# showing only middle button
@@ -223,12 +242,12 @@ def _wait_for_press(self, card):
223242
if self._right_button.contains(point_touched):
224243
print("Right button")
225244
return card.get('button02_goto_card_id', None)
245+
time.sleep(0.1)
226246

227247
def display_card(self, card_num):
228248
"""Display and handle input on a card.
229249
230250
:param card_num: the index of the card to process
231-
232251
"""
233252
card = self._game[card_num]
234253
print(card)
@@ -269,7 +288,6 @@ def play_sound(self, filename, *, wait_to_finish=True, loop=False):
269288
:param filename: The filename of the sound to play
270289
:param wait_to_finish: Whether playing the sound should block
271290
:param loop: Whether the sound should loop
272-
273291
"""
274292
self._speaker_enable.value = False
275293
self.audio.stop()
@@ -309,21 +327,28 @@ def set_text(self, text, color):
309327
self._text_group.pop()
310328
if not text or not color:
311329
return # nothing to do!
312-
text = self.wrap_nicely(text, 37)
330+
text_wrap = 37
331+
if board.DISPLAY.height < 130:
332+
text_wrap = 25
333+
text = self.wrap_nicely(text, text_wrap)
313334
text = '\n'.join(text)
314335
print("Set text to", text, "with color", hex(color))
336+
text_x = 8
337+
text_y = 95
338+
if board.DISPLAY.height < 130:
339+
text_y = 38
340+
text_x = 3
315341
if text:
316342
self._text = Label(self._text_font, text=str(text))
317-
self._text.x = 10
318-
self._text.y = 100
343+
self._text.x = text_x
344+
self._text.y = text_y
319345
self._text.color = color
320346
self._text_group.append(self._text)
321347

322348
def set_background(self, filename, *, with_fade=True):
323349
"""The background image to a bitmap file.
324350
325351
:param filename: The filename of the chosen background
326-
327352
"""
328353
print("Set background to", filename)
329354
if with_fade:

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
autodoc_mock_imports = ["rtc", "supervisor", "pulseio", "audioio", "displayio", "neopixel",
2424
"microcontroller", "adafruit_touchscreen", "adafruit_bitmap_font",
2525
"adafruit_display_text", "adafruit_esp32spi", "secrets",
26-
"adafruit_sdcard", "storage", "adafruit_io", "adafruit_button"]
26+
"adafruit_sdcard", "storage", "adafruit_io", "adafruit_button", "adafruit_cursorcontrol",
27+
"terminalio"]
2728

2829

2930
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}

0 commit comments

Comments
 (0)