Skip to content

Commit 3e5afc9

Browse files
authored
Fixes for Pull Request adafruit#1011
1 parent 43acc24 commit 3e5afc9

File tree

1 file changed

+65
-66
lines changed

1 file changed

+65
-66
lines changed

PyPortal_User_Interface/code.py

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import board
33
import displayio
44
import busio
5-
from digitalio import DigitalInOut
65
from analogio import AnalogIn
76
import neopixel
87
import adafruit_adt7410
@@ -37,10 +36,10 @@
3736

3837
# ------------- Other Helper Functions------------- #
3938
# Helper for cycling through a number set of 1 to x.
40-
def numberUP(i, max_val):
41-
i += 1
42-
if i <= max_val:
43-
return i
39+
def numberUP(num, max_val):
40+
num += 1
41+
if num <= max_val:
42+
return num
4443
else:
4544
return 1
4645

@@ -61,10 +60,13 @@ def set_backlight(val):
6160

6261
# Touchscreen setup
6362
# ------Rotate 270:
63+
screen_width = 240
64+
screen_height = 320
6465
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_YD, board.TOUCH_YU,
6566
board.TOUCH_XR, board.TOUCH_XL,
66-
calibration=((5200, 59000), (5800, 57000)),
67-
size=(240, 320))
67+
calibration=((5200, 59000),
68+
(5800, 57000)),
69+
size=(screen_width, screen_height))
6870

6971

7072
# ------------- Display Groups ------------- #
@@ -73,17 +75,16 @@ def set_backlight(val):
7375
view2 = displayio.Group(max_size=15) # Group for View 2 objects
7476
view3 = displayio.Group(max_size=15) # Group for View 3 objects
7577

76-
def hideLayer(i):
78+
def hideLayer(hide_target):
7779
try:
78-
splash.remove(i)
80+
splash.remove(hide_target)
7981
except ValueError:
8082
pass
8183

82-
def showLayer(i):
83-
global rect
84+
def showLayer(show_target):
8485
try:
8586
time.sleep(0.1)
86-
splash.append(i)
87+
splash.append(show_target)
8788
except ValueError:
8889
pass
8990

@@ -116,17 +117,14 @@ def set_image(group, filename):
116117

117118
if not filename:
118119
return # we're done, no icon desired
119-
try:
120-
if image_file:
121-
image_file.close
122-
except NameError:
123-
pass
120+
124121
image_file = open(filename, "rb")
125122
image = displayio.OnDiskBitmap(image_file)
126123
try:
127124
image_sprite = displayio.TileGrid(image, pixel_shader=displayio.ColorConverter())
128125
except TypeError:
129-
image_sprite = displayio.TileGrid(image, pixel_shader=displayio.ColorConverter(), position=(0, 0))
126+
image_sprite = displayio.TileGrid(image, pixel_shader=displayio.ColorConverter(),
127+
position=(0, 0))
130128
group.append(image_sprite)
131129

132130
set_image(bg_group, "/images/BGimage.bmp")
@@ -161,6 +159,7 @@ def set_image(group, filename):
161159
sensor_data.y = 170
162160
view3.append(sensor_data)
163161

162+
164163
text_hight = Label(font, text="M", color=0x03AD31, max_glyphs=10)
165164
# return a reformatted string with word wrapping using PyPortal.wrap_nicely
166165
def text_box(target, top, string, max_chars):
@@ -183,48 +182,48 @@ def text_box(target, top, string, max_chars):
183182

184183
# We want three buttons across the top of the screen
185184
TAPS_HEIGHT = 40
186-
TAPS_WIDTH = int(ts._size[0]/3)
185+
TAPS_WIDTH = int(screen_width/3)
187186
TAPS_Y = 0
188187

189188
# We want two big buttons at the bottom of the screen
190-
BIG_BUTTON_HEIGHT = int(ts._size[1]/3.2)
191-
BIG_BUTTON_WIDTH = int(ts._size[0]/2)
192-
BIG_BUTTON_Y = int(ts._size[1]-BIG_BUTTON_HEIGHT)
189+
BIG_BUTTON_HEIGHT = int(screen_height/3.2)
190+
BIG_BUTTON_WIDTH = int(screen_width/2)
191+
BIG_BUTTON_Y = int(screen_height-BIG_BUTTON_HEIGHT)
193192

194193
# This group will make it easy for us to read a button press later.
195194
buttons = []
196195

197196
# Main User Interface Buttons
198197
button_view1 = Button(x=0, y=0,
199-
width=TAPS_WIDTH, height=TAPS_HEIGHT,
200-
label="View1", label_font=font, label_color=0xff7e00,
201-
fill_color=0x5c5b5c, outline_color=0x767676,
202-
selected_fill=0x1a1a1a, selected_outline=0x2e2e2e,
203-
selected_label=0x525252)
198+
width=TAPS_WIDTH, height=TAPS_HEIGHT,
199+
label="View1", label_font=font, label_color=0xff7e00,
200+
fill_color=0x5c5b5c, outline_color=0x767676,
201+
selected_fill=0x1a1a1a, selected_outline=0x2e2e2e,
202+
selected_label=0x525252)
204203
buttons.append(button_view1) # adding this button to the buttons group
205204

206205
button_view2 = Button(x=TAPS_WIDTH, y=0,
207-
width=TAPS_WIDTH, height=TAPS_HEIGHT,
208-
label="View2", label_font=font, label_color=0xff7e00,
209-
fill_color=0x5c5b5c, outline_color=0x767676,
210-
selected_fill=0x1a1a1a, selected_outline=0x2e2e2e,
211-
selected_label=0x525252)
206+
width=TAPS_WIDTH, height=TAPS_HEIGHT,
207+
label="View2", label_font=font, label_color=0xff7e00,
208+
fill_color=0x5c5b5c, outline_color=0x767676,
209+
selected_fill=0x1a1a1a, selected_outline=0x2e2e2e,
210+
selected_label=0x525252)
212211
buttons.append(button_view2) # adding this button to the buttons group
213212

214213
button_view3 = Button(x=TAPS_WIDTH*2, y=0,
215-
width=TAPS_WIDTH, height=TAPS_HEIGHT,
216-
label="View3", label_font=font, label_color=0xff7e00,
217-
fill_color=0x5c5b5c, outline_color=0x767676,
218-
selected_fill=0x1a1a1a, selected_outline=0x2e2e2e,
219-
selected_label=0x525252)
214+
width=TAPS_WIDTH, height=TAPS_HEIGHT,
215+
label="View3", label_font=font, label_color=0xff7e00,
216+
fill_color=0x5c5b5c, outline_color=0x767676,
217+
selected_fill=0x1a1a1a, selected_outline=0x2e2e2e,
218+
selected_label=0x525252)
220219
buttons.append(button_view3) # adding this button to the buttons group
221220

222221
button_switch = Button(x=0, y=BIG_BUTTON_Y,
223-
width=BIG_BUTTON_WIDTH, height=BIG_BUTTON_HEIGHT,
224-
label="Switch", label_font=font, label_color=0xff7e00,
225-
fill_color=0x5c5b5c, outline_color=0x767676,
226-
selected_fill=0x1a1a1a, selected_outline=0x2e2e2e,
227-
selected_label=0x525252)
222+
width=BIG_BUTTON_WIDTH, height=BIG_BUTTON_HEIGHT,
223+
label="Switch", label_font=font, label_color=0xff7e00,
224+
fill_color=0x5c5b5c, outline_color=0x767676,
225+
selected_fill=0x1a1a1a, selected_outline=0x2e2e2e,
226+
selected_label=0x525252)
228227
buttons.append(button_switch) # adding this button to the buttons group
229228

230229
button_2 = Button(x=BIG_BUTTON_WIDTH, y=BIG_BUTTON_Y,
@@ -242,31 +241,31 @@ def text_box(target, top, string, max_chars):
242241

243242
# Make a button to change the icon image on view2
244243
button_icon = Button(x=150, y=60,
245-
width=BUTTON_WIDTH, height=BUTTON_HEIGHT,
246-
label="Icon", label_font=font, label_color=0xffffff,
247-
fill_color=0x8900ff, outline_color=0xbc55fd,
248-
selected_fill=0x5a5a5a, selected_outline=0xff6600,
249-
selected_label=0x525252, style=Button.ROUNDRECT)
244+
width=BUTTON_WIDTH, height=BUTTON_HEIGHT,
245+
label="Icon", label_font=font, label_color=0xffffff,
246+
fill_color=0x8900ff, outline_color=0xbc55fd,
247+
selected_fill=0x5a5a5a, selected_outline=0xff6600,
248+
selected_label=0x525252, style=Button.ROUNDRECT)
250249
buttons.append(button_icon) # adding this button to the buttons group
251250

252251
# Add this button to view2 Group
253252
view2.append(button_icon.group)
254253

255254
# Make a button to play a sound on view2
256255
button_sound = Button(x=150, y=170,
257-
width=BUTTON_WIDTH, height=BUTTON_HEIGHT,
258-
label="Sound", label_font=font, label_color=0xffffff,
259-
fill_color=0x8900ff, outline_color=0xbc55fd,
260-
selected_fill=0x5a5a5a, selected_outline=0xff6600,
261-
selected_label=0x525252, style=Button.ROUNDRECT)
256+
width=BUTTON_WIDTH, height=BUTTON_HEIGHT,
257+
label="Sound", label_font=font, label_color=0xffffff,
258+
fill_color=0x8900ff, outline_color=0xbc55fd,
259+
selected_fill=0x5a5a5a, selected_outline=0xff6600,
260+
selected_label=0x525252, style=Button.ROUNDRECT)
262261
buttons.append(button_sound) # adding this button to the buttons group
263262

264263
# Add this button to view2 Group
265264
view3.append(button_sound.group)
266265

267-
def switch_view(i):
266+
def switch_view(what_view):
268267
global view_live
269-
if i == 1:
268+
if what_view == 1:
270269
hideLayer(view2)
271270
hideLayer(view3)
272271
button_view1.selected = False
@@ -275,7 +274,7 @@ def switch_view(i):
275274
showLayer(view1)
276275
view_live = 1
277276
print("View1 On")
278-
elif i == 2:
277+
elif what_view == 2:
279278
# global icon
280279
hideLayer(view1)
281280
hideLayer(view3)
@@ -313,8 +312,10 @@ def switch_view(i):
313312
button_switch.selected = True
314313

315314
# Update out Labels with display text.
316-
text_box(feed1_label, TABS_Y, 'The text on this screen is wrapped so that all of it fits nicely into a text box that is ### x ###.', 30)
317-
text_box(feed1_label, TABS_Y, 'The text on this screen is wrapped so that all of it fits nicely into a text box that is {} x {}.'.format(feed1_label.bounding_box[2], feed1_label.bounding_box[3]*2, feed1_label.line_spacing), 30)
315+
text_box(feed1_label, TABS_Y, "The text on this screen is wrapped so that all of it fits nicely into a text box that is ### x ###.", 30)
316+
text_box(feed1_label, TABS_Y, 'The text on this screen is wrapped so that all of it fits nicely into a text box that is {} x {}.'
317+
.format(feed1_label.bounding_box[2],
318+
feed1_label.bounding_box[3]*2), 30)
318319

319320
text_box(feed2_label, TABS_Y, 'Tap on the Icon button to meet a new friend.', 18)
320321

@@ -329,25 +330,25 @@ def switch_view(i):
329330
tempC = round(adt.temperature)
330331
tempF = tempC * 1.8 + 32
331332

332-
sensor_data.text = 'Touch: {}\nLight: {}\nTemp: {}°F'.format(touch, light, tempF)
333+
sensor_data.text = 'Touch: {}\nLight: {}\n Temp: {}°F'.format(touch, light, tempF)
333334

334335
# ------------- Handle Button Press Detection ------------- #
335336
if touch: # Only do this if the screen is touched
336-
# Do a for loop with buttons using enumerate() to number each button group as i
337+
# loop with buttons using enumerate() to number each button group as i
337338
for i, b in enumerate(buttons):
338339
if b.contains(touch): # Test each button to see if it was pressed
339340
print('button%d pressed' % i)
340-
if i == 0 and view_live != 1: # button_view1 pressed and view1 not visable
341+
if i == 0 and view_live != 1: # only if view1 is visable
341342
pyportal.play_file(soundTab)
342343
switch_view(1)
343344
while ts.touch_point:
344345
pass
345-
if i == 1 and view_live != 2: # button_view2 pressed and view2 not visable
346+
if i == 1 and view_live != 2: # only if view2 is visable
346347
pyportal.play_file(soundTab)
347348
switch_view(2)
348349
while ts.touch_point:
349350
pass
350-
if i == 2 and view_live != 3: # button_view3 pressed and view3 not visable
351+
if i == 2 and view_live != 3: # only if view3 is visable
351352
pyportal.play_file(soundTab)
352353
switch_view(3)
353354
while ts.touch_point:
@@ -376,7 +377,6 @@ def switch_view(i):
376377
# Momentary button type
377378
b.selected = True
378379
print('Button Pressed')
379-
button_mode
380380
button_mode = numberUP(button_mode, 5)
381381
if button_mode == 1:
382382
pixel.fill(RED)
@@ -396,7 +396,7 @@ def switch_view(i):
396396
pass
397397
print("Button released")
398398
b.selected = False
399-
if i == 5 and view_live == 2: # button_icon pressed and view2 is visable
399+
if i == 5 and view_live == 2: # only if view2 is visable
400400
pyportal.play_file(soundBeep)
401401
b.selected = True
402402
while ts.touch_point:
@@ -412,11 +412,10 @@ def switch_view(i):
412412
b.selected = False
413413
text_box(feed2_label, TABS_Y, 'Every time you tap the Icon button the icon image will change. Say hi to {}!'.format(icon_name), 18)
414414
set_image(icon_group, "/images/"+icon_name+".bmp")
415-
if i == 6 and view_live == 3: # button_sound pressed and view3 is visable
415+
if i == 6 and view_live == 3: # only if view3 is visable
416416
b.selected = True
417417
while ts.touch_point:
418418
pass
419419
print("Sound Button Pressed")
420420
pyportal.play_file(soundDemo)
421421
b.selected = False
422-

0 commit comments

Comments
 (0)