Skip to content

Commit d52bbdf

Browse files
committed
various mods in 2nd test script
idem
1 parent f3d0ac1 commit d52bbdf

File tree

1 file changed

+59
-61
lines changed

1 file changed

+59
-61
lines changed

examples/hotplug_sensor_examples/displayio_layout_tablayout_hotplug_temp_sensor_datetime_fm_NTP_touch_and_gVars_test.py

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
#
33
# SPDX-License-Identifier: MIT
44
"""
5-
Notes by @PaulskPt: tested on an Adafruit PyPortal Titano
5+
Notes by @PaulskPt
6+
Script tested on an Adafruit PyPortal Titano
67
(Product ID 4444. See: https://www.adafruit.com/product/4444)
78
This script can make use of an I2C Realtime Clock type DS3231
8-
When the flag 'use_ntp' is set, the DS3231 will not be used,
9+
However, when the flag 'use_ntp' is set, the DS3231 will not be used
910
instead the NTP class from adafruit_ntp.py will be used.
1011
"""
11-
1212
import time
13-
14-
# import gc
1513
import board
1614
import busio
1715
import displayio
@@ -31,6 +29,7 @@
3129
from adafruit_bitmap_font import bitmap_font
3230
from adafruit_displayio_layout.layouts.tab_layout import TabLayout
3331

32+
3433
# +-------------------------------------------------------+
3534
# | Definition for variables in the past defined as global|
3635
# +-------------------------------------------------------+
@@ -233,18 +232,6 @@ def list(self):
233232
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
234233
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
235234

236-
# ---------- Text Boxes ------------- #
237-
# Set the font and preload letters
238-
font_arial = bitmap_font.load_font(
239-
"/fonts/Arial-16.bdf"
240-
) # was: Helvetica-Bold-16.bdf")
241-
# font.load_glyphs(b"abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ()")
242-
glyphs = b' "(),-.0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
243-
font_arial.load_glyphs(glyphs)
244-
font_arial.load_glyphs(("°",)) # a non-ascii character we need
245-
# gc.collect() # ADDED by @PaulskPt -- to prevent MemoryError - memory allocation failed,
246-
# allocating 6444 bytes
247-
248235
# ------------- Screen Setup ------------- #
249236
pyportal = None
250237
timeout_cnt = 0
@@ -367,8 +354,10 @@ def refresh_from_NTP():
367354
if timeout_cnt2 > 10:
368355
print("Timeout while trying to get ntp datetime to set the internal rtc")
369356
break
357+
370358
if myVars.read("my_debug"):
371359
print("Value ntp.valid_time = ", ntp.valid_time)
360+
372361
if ntp.valid_time:
373362
myVars.write("online_time_present", True)
374363
myVars.write("ntp_refresh", False)
@@ -377,6 +366,7 @@ def refresh_from_NTP():
377366
ntp_current_time = time.time()
378367
if myVars.read("my_debug"):
379368
print("Seconds since Jan 1, 1970: {} seconds".format(ntp_current_time))
369+
380370
# Convert the current time in seconds since Jan 1, 1970 to a struct_time
381371
myVars.write("default_dt", time.localtime(ntp_current_time))
382372
if not myVars.read("my_debug"):
@@ -463,16 +453,16 @@ def refresh_from_NTP():
463453
display.show(main_group)
464454

465455
# font = bitmap_font.load_font("fonts/Helvetica-Bold-16.bdf")
466-
# font = bitmap_font.load_font("/fonts/Arial-16.bdf")
467-
font = terminalio.FONT
456+
font_arial = bitmap_font.load_font("/fonts/Arial-16.bdf")
457+
font_term = terminalio.FONT
468458

469459
# create the page layout
470460
test_page_layout = TabLayout(
471461
x=0,
472462
y=0,
473463
display=board.DISPLAY,
474464
tab_text_scale=2,
475-
custom_font=font,
465+
custom_font=font_term,
476466
inactive_tab_spritesheet="lib/adafruit_displayio_layout/examples/bmps/inactive_tab_sprite.bmp",
477467
showing_tab_spritesheet="lib/adafruit_displayio_layout/examples/bmps/active_tab_sprite.bmp",
478468
showing_tab_text_color=0x00AA59,
@@ -501,34 +491,29 @@ def set_image(group, filename):
501491
:param filename: The filename of the chosen image
502492
"""
503493
print("Set image to ", filename)
494+
image = None
495+
image_sprite = None
504496
if group:
505497
group.pop()
506498
if not filename:
507499
return # we're done, no icon desired
508500
# CircuitPython 6 & 7 compatible
509-
image_file = None
510501
try:
511-
# image_file = open(filename, "rb")
512-
with open(filename, "rb") as image_file:
513-
image = displayio.OnDiskBitmap(image_file)
502+
image = displayio.OnDiskBitmap(filename)
514503
except OSError as exc:
515504
if exc.args[0] == 2: # No such file/directory
516505
return
517-
finally:
518-
if image_file is not None:
519-
image_file.close()
520-
521-
image_sprite = displayio.TileGrid(
522-
image, pixel_shader=getattr(image, "pixel_shader", displayio.ColorConverter())
523-
)
524-
525-
# # CircuitPython 7+ compatible
526-
# image = displayio.OnDiskBitmap(filename)
527-
# image_sprite = displayio.TileGrid(image, pixel_shader=image.pixel_shader)
528-
main_group.append(image_sprite)
506+
if image is not None:
507+
image_sprite = displayio.TileGrid(
508+
image,
509+
pixel_shader=getattr(image, "pixel_shader", displayio.ColorConverter()),
510+
)
511+
if image_sprite is not None:
512+
main_group.append(image_sprite)
529513

530514

531515
# ------------- Setup for Images ------------- #
516+
532517
bg_group = displayio.Group()
533518
set_image(bg_group, "/images/BGimage4.bmp")
534519
print(
@@ -544,71 +529,70 @@ def set_image(group, filename):
544529

545530
# labels
546531
pge1_lbl = Label(
547-
font=terminalio.FONT,
532+
font=font_term,
548533
scale=2,
549534
text="This is the first page!",
550535
anchor_point=(0, 0),
551536
anchored_position=(10, 10),
552537
)
553538
pge1_lbl2 = Label(
554-
font=terminalio.FONT,
539+
font=font_term,
555540
scale=2,
556541
text="Please wait...",
557542
anchor_point=(0, 0),
558543
anchored_position=(10, 150),
559544
)
560545
pge2_lbl = Label(
561-
font=terminalio.FONT,
546+
font=font_term,
562547
scale=2,
563548
text="This page is the second page!",
564549
anchor_point=(0, 0),
565550
anchored_position=(10, 10),
566551
)
567552
pge3_lbl = Label(
568-
font=terminalio.FONT,
553+
font=font_term,
569554
scale=2,
570555
text=myVars.read("pge3_lbl_dflt"), # Will be "Date/time:"
571556
anchor_point=(0, 0),
572557
anchored_position=(10, 10),
573558
)
574559
pge3_lbl2 = Label(
575-
font=terminalio.FONT,
560+
font=font_term,
576561
scale=2,
577562
text="", # pge3_lbl2_dflt, # Will be DD-MO-YYYY or Month-DD-YYYY
578563
anchor_point=(0, 0),
579564
anchored_position=(10, 40),
580565
)
581566
pge3_lbl3 = Label(
582-
font=terminalio.FONT,
567+
font=font_term,
583568
scale=2,
584569
text="", # pge3_lbl3_dflt, # Will be HH:MM:SS
585570
anchor_point=(0, 0),
586571
anchored_position=(10, 70),
587572
)
588573
pge3_lbl4 = Label(
589-
font=terminalio.FONT,
574+
font=font_term,
590575
scale=2,
591576
text="", # pge3_lbl3_dflt, # Will be time until next NTP sync in MM:SS
592577
anchor_point=(0, 0),
593578
anchored_position=(10, 200),
594579
)
595580
pge4_lbl = Label(
596-
font=terminalio.FONT,
581+
font=font_term,
597582
scale=2,
598583
text=myVars.read("pge4_lbl_dflt"),
599584
anchor_point=(0, 0),
600585
anchored_position=(10, 10),
601586
)
602587
pge4_lbl2 = Label(
603-
font=terminalio.FONT,
588+
font=font_term,
604589
scale=2,
605590
text="", # Will be "Temperature"
606591
anchor_point=(0, 0),
607592
anchored_position=(10, 130),
608593
)
609-
610594
pge4_lbl3 = Label(
611-
font=font_arial,
595+
font=font_arial, # bitmap_font.load_font("/fonts/Arial-16.bdf"),
612596
scale=2,
613597
text="", # Will be "xx.yy ºC"
614598
anchor_point=(0, 0),
@@ -620,6 +604,7 @@ def set_image(group, filename):
620604
circle = Circle(50, 100, r=30, fill=0xDD00DD)
621605
triangle = Triangle(50, 0, 100, 50, 0, 50, fill=0xDDDD00)
622606
rectangle = Rect(x=80, y=60, width=100, height=50, fill=0x0000DD)
607+
623608
triangle.x = 80
624609
triangle.y = 70
625610

@@ -653,12 +638,24 @@ def set_image(group, filename):
653638
# add it to the group that is showing on the display
654639
main_group.append(test_page_layout)
655640
# test_page_layout.tab_tilegrids_group[3].x += 50
641+
# ---------- Text Boxes ------------- #
642+
# Set the font and preload letters
643+
# font = bitmap_font.load_font("/fonts/Arial-16.bdf") # was: Helvetica-Bold-16.bdf")
644+
# font.load_glyphs(b"abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ()")
645+
glyphs = b' "(),-.0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
646+
font_arial.load_glyphs(glyphs)
647+
font_arial.load_glyphs(("°",)) # a non-ascii character we need
648+
# gc.collect() # ADDED by @PaulskPt -- to prevent MemoryError - memory allocation failed,
649+
# allocating 6444 bytes
650+
656651
pge2_group = 1
657652

658653

659-
"""If the temperature sensor has been disconnected,
654+
"""
655+
If the temperature sensor has been disconnected,
660656
this function will try to reconnect (test if the sensor is present by now)
661-
If reconnected this function creates the temp_sensor object"""
657+
If reconnected this function creates the temp_sensor object
658+
"""
662659

663660

664661
def connect_temp_sensor():
@@ -689,8 +686,10 @@ def connect_temp_sensor():
689686
myVars.write("t2", None)
690687

691688

692-
"""If the external rtc has been disconnected,
693-
this function will try to reconnect (test if the external rtc is present by now)"""
689+
"""
690+
If the external rtc has been disconnected,
691+
this function will try to reconnect (test if the external rtc is present by now)
692+
"""
694693

695694

696695
def connect_rtc():
@@ -716,12 +715,14 @@ def connect_rtc():
716715
print("Failed to connect RTC")
717716

718717

719-
"""Function gets a value from the external temperature sensor
718+
"""
719+
Function gets a value from the external temperature sensor
720720
It only updates if the value has changed compared to the previous value
721721
A fixed text is set in pge4_lbl2.text. The variable temperature value is set in pge4_lbl3.text
722722
If no value obtained (for instance if the sensor is disconnected),
723723
the function sets the pge4_lbl to a default text and makes empty
724-
pge4_lbl2.text and pge4_lbl3.text"""
724+
pge4_lbl2.text and pge4_lbl3.text
725+
"""
725726

726727

727728
def get_temp():
@@ -761,7 +762,6 @@ def get_temp():
761762
) # clean the line (eventually: t2)
762763
pge4_lbl2.text = "Sensor disconnected."
763764
pge4_lbl3.text = "Check wiring."
764-
765765
return RetVal
766766

767767

@@ -843,11 +843,14 @@ def handle_dt(dt):
843843
return RetVal
844844

845845

846-
""" Function gets the date and time: a) if an rtc is present from the rtc;
846+
"""
847+
Function gets the date and time:
848+
a) if an rtc is present from the rtc;
847849
b) if using online NTP pool server then get the date and time from the function time.localtime
848850
This time.localtime has before been set with data from the NTP server.
849851
In both cases the date and time will be set to the pge3_lbl, pge3_lbl12 and pge3_lbl3
850-
If no (valid) datetime a default text will be shown on the pge3_lbl"""
852+
If no (valid) date and time has been received then a default text will be shown on the pge3_lbl
853+
"""
851854

852855

853856
def get_dt():
@@ -905,16 +908,13 @@ def ck_next_NTP_sync():
905908
c_elapsed = c_cnt - s_cnt
906909
if c_elapsed < 10: # continue only when c_elapsed >= 10
907910
return
908-
909911
TAG = "ck_next_NTP_sync(): "
910912
my_debug = myVars.read("my_debug")
911913
t1 = myVars.read("next_NTP_sync_t1")
912914
t3 = myVars.read("next_NTP_sync_t3")
913915
five_min = myVars.read("five_min_cnt")
914916
myVars.write("s_cnt", hms_to_cnt())
915-
916917
# --- five minutes count down calculations #1 ---
917-
918918
if my_debug:
919919
print(
920920
TAG + "five_min = {}, s_cnt = {}, c_cnt = {}".format(five_min, s_cnt, c_cnt)
@@ -958,7 +958,6 @@ def main():
958958
while True:
959959
touch = ts.touch_point
960960
try:
961-
962961
if use_ntp:
963962
ck_next_NTP_sync()
964963
ntp_refresh = myVars.read("ntp_refresh")
@@ -986,7 +985,6 @@ def main():
986985
# the touch data has lost
987986
if myVars.read("temp_in_REPL"):
988987
myVars.write("temp_in_REPL", False)
989-
990988
cnt = inc_cnt(cnt)
991989
except KeyboardInterrupt as exc:
992990
print("Keyboard interrupt...exiting...")

0 commit comments

Comments
 (0)