2
2
import board
3
3
import displayio
4
4
import busio
5
- from digitalio import DigitalInOut
6
5
from analogio import AnalogIn
7
6
import neopixel
8
7
import adafruit_adt7410
37
36
38
37
# ------------- Other Helper Functions------------- #
39
38
# 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
44
43
else :
45
44
return 1
46
45
@@ -61,10 +60,13 @@ def set_backlight(val):
61
60
62
61
# Touchscreen setup
63
62
# ------Rotate 270:
63
+ screen_width = 240
64
+ screen_height = 320
64
65
ts = adafruit_touchscreen .Touchscreen (board .TOUCH_YD , board .TOUCH_YU ,
65
66
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 ))
68
70
69
71
70
72
# ------------- Display Groups ------------- #
@@ -73,17 +75,16 @@ def set_backlight(val):
73
75
view2 = displayio .Group (max_size = 15 ) # Group for View 2 objects
74
76
view3 = displayio .Group (max_size = 15 ) # Group for View 3 objects
75
77
76
- def hideLayer (i ):
78
+ def hideLayer (hide_target ):
77
79
try :
78
- splash .remove (i )
80
+ splash .remove (hide_target )
79
81
except ValueError :
80
82
pass
81
83
82
- def showLayer (i ):
83
- global rect
84
+ def showLayer (show_target ):
84
85
try :
85
86
time .sleep (0.1 )
86
- splash .append (i )
87
+ splash .append (show_target )
87
88
except ValueError :
88
89
pass
89
90
@@ -116,17 +117,14 @@ def set_image(group, filename):
116
117
117
118
if not filename :
118
119
return # we're done, no icon desired
119
- try :
120
- if image_file :
121
- image_file .close
122
- except NameError :
123
- pass
120
+
124
121
image_file = open (filename , "rb" )
125
122
image = displayio .OnDiskBitmap (image_file )
126
123
try :
127
124
image_sprite = displayio .TileGrid (image , pixel_shader = displayio .ColorConverter ())
128
125
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 ))
130
128
group .append (image_sprite )
131
129
132
130
set_image (bg_group , "/images/BGimage.bmp" )
@@ -161,6 +159,7 @@ def set_image(group, filename):
161
159
sensor_data .y = 170
162
160
view3 .append (sensor_data )
163
161
162
+
164
163
text_hight = Label (font , text = "M" , color = 0x03AD31 , max_glyphs = 10 )
165
164
# return a reformatted string with word wrapping using PyPortal.wrap_nicely
166
165
def text_box (target , top , string , max_chars ):
@@ -183,48 +182,48 @@ def text_box(target, top, string, max_chars):
183
182
184
183
# We want three buttons across the top of the screen
185
184
TAPS_HEIGHT = 40
186
- TAPS_WIDTH = int (ts . _size [ 0 ] / 3 )
185
+ TAPS_WIDTH = int (screen_width / 3 )
187
186
TAPS_Y = 0
188
187
189
188
# 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 )
193
192
194
193
# This group will make it easy for us to read a button press later.
195
194
buttons = []
196
195
197
196
# Main User Interface Buttons
198
197
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 )
204
203
buttons .append (button_view1 ) # adding this button to the buttons group
205
204
206
205
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 )
212
211
buttons .append (button_view2 ) # adding this button to the buttons group
213
212
214
213
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 )
220
219
buttons .append (button_view3 ) # adding this button to the buttons group
221
220
222
221
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 )
228
227
buttons .append (button_switch ) # adding this button to the buttons group
229
228
230
229
button_2 = Button (x = BIG_BUTTON_WIDTH , y = BIG_BUTTON_Y ,
@@ -242,31 +241,31 @@ def text_box(target, top, string, max_chars):
242
241
243
242
# Make a button to change the icon image on view2
244
243
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 )
250
249
buttons .append (button_icon ) # adding this button to the buttons group
251
250
252
251
# Add this button to view2 Group
253
252
view2 .append (button_icon .group )
254
253
255
254
# Make a button to play a sound on view2
256
255
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 )
262
261
buttons .append (button_sound ) # adding this button to the buttons group
263
262
264
263
# Add this button to view2 Group
265
264
view3 .append (button_sound .group )
266
265
267
- def switch_view (i ):
266
+ def switch_view (what_view ):
268
267
global view_live
269
- if i == 1 :
268
+ if what_view == 1 :
270
269
hideLayer (view2 )
271
270
hideLayer (view3 )
272
271
button_view1 .selected = False
@@ -275,7 +274,7 @@ def switch_view(i):
275
274
showLayer (view1 )
276
275
view_live = 1
277
276
print ("View1 On" )
278
- elif i == 2 :
277
+ elif what_view == 2 :
279
278
# global icon
280
279
hideLayer (view1 )
281
280
hideLayer (view3 )
@@ -313,8 +312,10 @@ def switch_view(i):
313
312
button_switch .selected = True
314
313
315
314
# 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 )
318
319
319
320
text_box (feed2_label , TABS_Y , 'Tap on the Icon button to meet a new friend.' , 18 )
320
321
@@ -329,25 +330,25 @@ def switch_view(i):
329
330
tempC = round (adt .temperature )
330
331
tempF = tempC * 1.8 + 32
331
332
332
- sensor_data .text = 'Touch: {}\n Light: {}\n Temp : {}°F' .format (touch , light , tempF )
333
+ sensor_data .text = 'Touch: {}\n Light: {}\n Temp : {}°F' .format (touch , light , tempF )
333
334
334
335
# ------------- Handle Button Press Detection ------------- #
335
336
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
337
338
for i , b in enumerate (buttons ):
338
339
if b .contains (touch ): # Test each button to see if it was pressed
339
340
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
341
342
pyportal .play_file (soundTab )
342
343
switch_view (1 )
343
344
while ts .touch_point :
344
345
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
346
347
pyportal .play_file (soundTab )
347
348
switch_view (2 )
348
349
while ts .touch_point :
349
350
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
351
352
pyportal .play_file (soundTab )
352
353
switch_view (3 )
353
354
while ts .touch_point :
@@ -376,7 +377,6 @@ def switch_view(i):
376
377
# Momentary button type
377
378
b .selected = True
378
379
print ('Button Pressed' )
379
- button_mode
380
380
button_mode = numberUP (button_mode , 5 )
381
381
if button_mode == 1 :
382
382
pixel .fill (RED )
@@ -396,7 +396,7 @@ def switch_view(i):
396
396
pass
397
397
print ("Button released" )
398
398
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
400
400
pyportal .play_file (soundBeep )
401
401
b .selected = True
402
402
while ts .touch_point :
@@ -412,11 +412,10 @@ def switch_view(i):
412
412
b .selected = False
413
413
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 )
414
414
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
416
416
b .selected = True
417
417
while ts .touch_point :
418
418
pass
419
419
print ("Sound Button Pressed" )
420
420
pyportal .play_file (soundDemo )
421
421
b .selected = False
422
-
0 commit comments