Skip to content

Commit 67f03f9

Browse files
authored
Merge pull request #16 from makermelissa/master
Adjusted sizing to add support for PyPortal Titano
2 parents e4fe9ef + ae406f9 commit 67f03f9

18 files changed

+32774
-22
lines changed

adafruit_pyoa.py

100644100755
Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@
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
54+
try: # No need for Cursor Control on the PyPortal
55+
from adafruit_cursorcontrol.cursorcontrol import Cursor
56+
from adafruit_cursorcontrol.cursorcontrol_cursormanager import CursorManager
57+
except ImportError:
58+
pass
5659
import audioio
5760
from adafruit_display_text.label import Label
5861
from adafruit_button import Button
@@ -66,14 +69,18 @@ class PYOA_Graphics():
6669

6770
def __init__(self):
6871
self.root_group = displayio.Group(max_size=15)
69-
72+
self._display = board.DISPLAY
7073
self._background_group = displayio.Group(max_size=1)
7174
self.root_group.append(self._background_group)
7275
self._text_group = displayio.Group(max_size=1)
7376
self.root_group.append(self._text_group)
7477
self._button_group = displayio.Group(max_size=2)
7578
self.root_group.append(self._button_group)
7679

80+
if self._display.height > 250:
81+
self._text_group.scale = 2
82+
self._button_group.scale = 2
83+
7784
self._speaker_enable = DigitalInOut(board.SPEAKER_ENABLE)
7885
self._speaker_enable.switch_to_output(False)
7986
if hasattr(board, 'AUDIO_OUT'):
@@ -86,19 +93,20 @@ def __init__(self):
8693
self._background_file = None
8794
self._wavfile = None
8895

89-
board.DISPLAY.auto_brightness = False
96+
self._display.auto_brightness = False
9097
self.backlight_fade(0)
91-
board.DISPLAY.show(self.root_group)
98+
self._display.show(self.root_group)
9299
self.touchscreen = None
93100
self.mouse_cursor = None
94101
if hasattr(board, 'TOUCH_XL'):
95102
self.touchscreen = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
96103
board.TOUCH_YD, board.TOUCH_YU,
97104
calibration=((5200, 59000),
98105
(5800, 57000)),
99-
size=(320, 240))
106+
size=(self._display.width,
107+
self._display.height))
100108
elif hasattr(board, 'BUTTON_CLOCK'):
101-
self.mouse_cursor = Cursor(board.DISPLAY, display_group=self.root_group, cursor_speed=8)
109+
self.mouse_cursor = Cursor(self._display, display_group=self.root_group, cursor_speed=8)
102110
self.cursor = CursorManager(self.mouse_cursor)
103111
else:
104112
raise AttributeError('PYOA requires a touchscreen or cursor.')
@@ -120,20 +128,33 @@ def load_game(self, game_directory):
120128
"""
121129
self._gamedirectory = game_directory
122130
self._text_font = terminalio.FONT
131+
# Possible Screen Sizes are:
132+
# 320x240 PyPortal and PyPortal Pynt
133+
# 160x128 PyBadge and PyGamer
134+
# 480x320 PyPortal Titano
135+
# 240x240 if we wanted to use HalloWing M4
136+
123137
# Button Attributes
124138
btn_left = 10
125139
btn_right = btn_left+180
126140
btn_mid = btn_left+90
127141
button_y = 195
128142
button_width = 120
129143
button_height = 40
130-
if board.DISPLAY.height < 200:
144+
if self._display.height < 200:
131145
button_y /= 2
132146
button_y += 10
133147
button_width /= 2
134148
button_height /= 2
135149
btn_right /= 2
136150
btn_mid /= 2
151+
elif self._display.height > 250:
152+
button_y *= .75
153+
button_y -= 20
154+
button_width *= .75
155+
button_height *= .75
156+
btn_right *= .75
157+
btn_mid *= .75
137158
self._left_button = Button(x=int(btn_left), y=int(button_y), width=int(button_width), height=int(button_height),
138159
label="Left", label_font=self._text_font,
139160
style=Button.SHADOWROUNDRECT)
@@ -232,6 +253,8 @@ def _wait_for_press(self, card):
232253
if self.cursor.is_clicked is True:
233254
point_touched = self.mouse_cursor.x, self.mouse_cursor.y
234255
if point_touched is not None:
256+
point_touched = (point_touched[0] // self._button_group.scale,
257+
point_touched[1] // self._button_group.scale)
235258
print("touch: ", point_touched)
236259
if button01_text and not button02_text:
237260
# showing only middle button
@@ -264,10 +287,10 @@ def display_card(self, card_num):
264287
self.backlight_fade(1.0)
265288
self._display_text_for(card)
266289
try:
267-
board.DISPLAY.refresh(target_frames_per_second=60)
290+
self._display.refresh(target_frames_per_second=60)
268291
except AttributeError:
269-
board.DISPLAY.refresh_soon()
270-
board.DISPLAY.wait_for_frame()
292+
self._display.refresh_soon()
293+
self._display.wait_for_frame()
271294

272295
self._play_sound_for(card)
273296

@@ -305,9 +328,9 @@ def play_sound(self, filename, *, wait_to_finish=True, loop=False):
305328
filename = self._gamedirectory+"/"+filename
306329
print("Playing sound", filename)
307330
try:
308-
board.DISPLAY.refresh(target_frames_per_second=60)
331+
self._display.refresh(target_frames_per_second=60)
309332
except AttributeError:
310-
board.DISPLAY.wait_for_frame()
333+
self._display.wait_for_frame()
311334
try:
312335
self._wavfile = open(filename, "rb")
313336
except OSError:
@@ -336,16 +359,18 @@ def set_text(self, text, color):
336359
if not text or not color:
337360
return # nothing to do!
338361
text_wrap = 37
339-
if board.DISPLAY.height < 130:
362+
if self._display.height < 130:
340363
text_wrap = 25
341364
text = self.wrap_nicely(text, text_wrap)
342365
text = '\n'.join(text)
343366
print("Set text to", text, "with color", hex(color))
344367
text_x = 8
345368
text_y = 95
346-
if board.DISPLAY.height < 130:
347-
text_y = 38
369+
if self._display.height < 130:
348370
text_x = 3
371+
text_y = 38
372+
elif self._display.height > 250:
373+
text_y = 50
349374
if text:
350375
self._text = Label(self._text_font, text=str(text))
351376
self._text.x = text_x
@@ -375,26 +400,26 @@ def set_background(self, filename, *, with_fade=True):
375400
self._background_group.append(self._background_sprite)
376401
if with_fade:
377402
try:
378-
board.DISPLAY.refresh(target_frames_per_second=60)
403+
self._display.refresh(target_frames_per_second=60)
379404
except AttributeError:
380-
board.DISPLAY.refresh_soon()
381-
board.DISPLAY.wait_for_frame()
405+
self._display.refresh_soon()
406+
self._display.wait_for_frame()
382407
self.backlight_fade(1.0)
383408

384409
def backlight_fade(self, to_light):
385410
"""Adjust the TFT backlight. Fade from one value to another
386411
"""
387-
from_light = board.DISPLAY.brightness
412+
from_light = self._display.brightness
388413
from_light = int(from_light*100)
389414
to_light = max(0, min(1.0, to_light))
390415
to_light = int(to_light*100)
391416
delta = 1
392417
if from_light > to_light:
393418
delta = -1
394419
for val in range(from_light, to_light, delta):
395-
board.DISPLAY.brightness = val/100
420+
self._display.brightness = val/100
396421
time.sleep(0.003)
397-
board.DISPLAY.brightness = to_light/100
422+
self._display.brightness = to_light/100
398423

399424

400425
# return a list of lines with wordwrapping

examples/cyoa_titano/Mystery.wav

1.32 MB
Binary file not shown.

examples/cyoa_titano/cyoa.json

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[
2+
{
3+
"card_id": "startup",
4+
"background_image": "startup.bmp",
5+
"sound": "startup.wav",
6+
"auto_advance": "5"
7+
},
8+
{
9+
"card_id": "home",
10+
"background_image": "home.bmp",
11+
"sound": "home.wav",
12+
"sound_repeat": "True",
13+
"button01_text": "Help",
14+
"button01_goto_card_id": "help",
15+
"button02_text": "Start",
16+
"button02_goto_card_id": "want to build?"
17+
},
18+
19+
{
20+
"card_id": "want to build?",
21+
"background_image": "page01.bmp",
22+
"text": "You do not have any friends so you decide that it might be a good idea to build a robot friend. You're unsure if you want to do this, so now is the time to decide. Do you want to build a robot friend?",
23+
"text_color": "0x000001",
24+
"sound": "sound_01.wav",
25+
"button01_text": "Yes",
26+
"button01_goto_card_id": "continue?",
27+
"button02_text": "No",
28+
"button02_goto_card_id": "lazy"
29+
},
30+
{
31+
"card_id": "continue?",
32+
"background_image": "page02.bmp",
33+
"text": "You spend all day, then all week, then all month building a robot, everyone stops talking to you, however a lot of progress has been made. Do you want to keep making the robots?",
34+
"text_color": "0xFFFFFF",
35+
"button01_text": "Yes",
36+
"button01_goto_card_id": "robot friend",
37+
"button02_text": "No",
38+
"button02_goto_card_id": "lazy"
39+
},
40+
{
41+
"card_id": "robot friend",
42+
"background_image": "page03.bmp",
43+
"text": "The robot is now you're friend, everyone else wishes they had a robot, this is the best thing ever. Good work!",
44+
"text_color": "0xFFFFFF",
45+
"sound": "Mystery.wav",
46+
"button01_text": "Next",
47+
"button01_goto_card_id": "happy ending"
48+
},
49+
{
50+
"card_id": "lazy",
51+
"background_image": "page04.bmp",
52+
"sound": "sound_04.wav",
53+
"text": "Welp, not only will you not have any friends, you are lazy. What's the point of playing? Try again.",
54+
"text_color": "0xFFFFFF",
55+
"button01_text": "Start Over",
56+
"button01_goto_card_id": "home"
57+
},
58+
{
59+
"card_id": "help",
60+
"background_image": "help.bmp",
61+
"text": "All you need to do is click the buttons, that's it.\nThis is a new line.",
62+
"text_color": "0xFFFFFF",
63+
"button01_text": "Home",
64+
"button01_goto_card_id": "home"
65+
},
66+
{
67+
"card_id": "happy ending",
68+
"background_image": "happyending.bmp",
69+
"sound": "happy_ending.wav",
70+
"sound_repeat": "True",
71+
"button01_text": "Home",
72+
"button01_goto_card_id": "home"
73+
}
74+
]

0 commit comments

Comments
 (0)