2
2
#
3
3
# SPDX-License-Identifier: MIT
4
4
"""
5
- Notes by @PaulskPt: tested on an Adafruit PyPortal Titano
5
+ Notes by @PaulskPt
6
+ Script tested on an Adafruit PyPortal Titano
6
7
(Product ID 4444. See: https://www.adafruit.com/product/4444)
7
8
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
9
10
instead the NTP class from adafruit_ntp.py will be used.
10
11
"""
11
-
12
12
import time
13
-
14
- # import gc
15
13
import board
16
14
import busio
17
15
import displayio
31
29
from adafruit_bitmap_font import bitmap_font
32
30
from adafruit_displayio_layout .layouts .tab_layout import TabLayout
33
31
32
+
34
33
# +-------------------------------------------------------+
35
34
# | Definition for variables in the past defined as global|
36
35
# +-------------------------------------------------------+
@@ -233,18 +232,6 @@ def list(self):
233
232
spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
234
233
esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
235
234
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
-
248
235
# ------------- Screen Setup ------------- #
249
236
pyportal = None
250
237
timeout_cnt = 0
@@ -367,8 +354,10 @@ def refresh_from_NTP():
367
354
if timeout_cnt2 > 10 :
368
355
print ("Timeout while trying to get ntp datetime to set the internal rtc" )
369
356
break
357
+
370
358
if myVars .read ("my_debug" ):
371
359
print ("Value ntp.valid_time = " , ntp .valid_time )
360
+
372
361
if ntp .valid_time :
373
362
myVars .write ("online_time_present" , True )
374
363
myVars .write ("ntp_refresh" , False )
@@ -377,6 +366,7 @@ def refresh_from_NTP():
377
366
ntp_current_time = time .time ()
378
367
if myVars .read ("my_debug" ):
379
368
print ("Seconds since Jan 1, 1970: {} seconds" .format (ntp_current_time ))
369
+
380
370
# Convert the current time in seconds since Jan 1, 1970 to a struct_time
381
371
myVars .write ("default_dt" , time .localtime (ntp_current_time ))
382
372
if not myVars .read ("my_debug" ):
@@ -463,16 +453,16 @@ def refresh_from_NTP():
463
453
display .show (main_group )
464
454
465
455
# 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
468
458
469
459
# create the page layout
470
460
test_page_layout = TabLayout (
471
461
x = 0 ,
472
462
y = 0 ,
473
463
display = board .DISPLAY ,
474
464
tab_text_scale = 2 ,
475
- custom_font = font ,
465
+ custom_font = font_term ,
476
466
inactive_tab_spritesheet = "lib/adafruit_displayio_layout/examples/bmps/inactive_tab_sprite.bmp" ,
477
467
showing_tab_spritesheet = "lib/adafruit_displayio_layout/examples/bmps/active_tab_sprite.bmp" ,
478
468
showing_tab_text_color = 0x00AA59 ,
@@ -501,34 +491,29 @@ def set_image(group, filename):
501
491
:param filename: The filename of the chosen image
502
492
"""
503
493
print ("Set image to " , filename )
494
+ image = None
495
+ image_sprite = None
504
496
if group :
505
497
group .pop ()
506
498
if not filename :
507
499
return # we're done, no icon desired
508
500
# CircuitPython 6 & 7 compatible
509
- image_file = None
510
501
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 )
514
503
except OSError as exc :
515
504
if exc .args [0 ] == 2 : # No such file/directory
516
505
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 )
529
513
530
514
531
515
# ------------- Setup for Images ------------- #
516
+
532
517
bg_group = displayio .Group ()
533
518
set_image (bg_group , "/images/BGimage4.bmp" )
534
519
print (
@@ -544,71 +529,70 @@ def set_image(group, filename):
544
529
545
530
# labels
546
531
pge1_lbl = Label (
547
- font = terminalio . FONT ,
532
+ font = font_term ,
548
533
scale = 2 ,
549
534
text = "This is the first page!" ,
550
535
anchor_point = (0 , 0 ),
551
536
anchored_position = (10 , 10 ),
552
537
)
553
538
pge1_lbl2 = Label (
554
- font = terminalio . FONT ,
539
+ font = font_term ,
555
540
scale = 2 ,
556
541
text = "Please wait..." ,
557
542
anchor_point = (0 , 0 ),
558
543
anchored_position = (10 , 150 ),
559
544
)
560
545
pge2_lbl = Label (
561
- font = terminalio . FONT ,
546
+ font = font_term ,
562
547
scale = 2 ,
563
548
text = "This page is the second page!" ,
564
549
anchor_point = (0 , 0 ),
565
550
anchored_position = (10 , 10 ),
566
551
)
567
552
pge3_lbl = Label (
568
- font = terminalio . FONT ,
553
+ font = font_term ,
569
554
scale = 2 ,
570
555
text = myVars .read ("pge3_lbl_dflt" ), # Will be "Date/time:"
571
556
anchor_point = (0 , 0 ),
572
557
anchored_position = (10 , 10 ),
573
558
)
574
559
pge3_lbl2 = Label (
575
- font = terminalio . FONT ,
560
+ font = font_term ,
576
561
scale = 2 ,
577
562
text = "" , # pge3_lbl2_dflt, # Will be DD-MO-YYYY or Month-DD-YYYY
578
563
anchor_point = (0 , 0 ),
579
564
anchored_position = (10 , 40 ),
580
565
)
581
566
pge3_lbl3 = Label (
582
- font = terminalio . FONT ,
567
+ font = font_term ,
583
568
scale = 2 ,
584
569
text = "" , # pge3_lbl3_dflt, # Will be HH:MM:SS
585
570
anchor_point = (0 , 0 ),
586
571
anchored_position = (10 , 70 ),
587
572
)
588
573
pge3_lbl4 = Label (
589
- font = terminalio . FONT ,
574
+ font = font_term ,
590
575
scale = 2 ,
591
576
text = "" , # pge3_lbl3_dflt, # Will be time until next NTP sync in MM:SS
592
577
anchor_point = (0 , 0 ),
593
578
anchored_position = (10 , 200 ),
594
579
)
595
580
pge4_lbl = Label (
596
- font = terminalio . FONT ,
581
+ font = font_term ,
597
582
scale = 2 ,
598
583
text = myVars .read ("pge4_lbl_dflt" ),
599
584
anchor_point = (0 , 0 ),
600
585
anchored_position = (10 , 10 ),
601
586
)
602
587
pge4_lbl2 = Label (
603
- font = terminalio . FONT ,
588
+ font = font_term ,
604
589
scale = 2 ,
605
590
text = "" , # Will be "Temperature"
606
591
anchor_point = (0 , 0 ),
607
592
anchored_position = (10 , 130 ),
608
593
)
609
-
610
594
pge4_lbl3 = Label (
611
- font = font_arial ,
595
+ font = font_arial , # bitmap_font.load_font("/fonts/Arial-16.bdf"),
612
596
scale = 2 ,
613
597
text = "" , # Will be "xx.yy ºC"
614
598
anchor_point = (0 , 0 ),
@@ -620,6 +604,7 @@ def set_image(group, filename):
620
604
circle = Circle (50 , 100 , r = 30 , fill = 0xDD00DD )
621
605
triangle = Triangle (50 , 0 , 100 , 50 , 0 , 50 , fill = 0xDDDD00 )
622
606
rectangle = Rect (x = 80 , y = 60 , width = 100 , height = 50 , fill = 0x0000DD )
607
+
623
608
triangle .x = 80
624
609
triangle .y = 70
625
610
@@ -653,12 +638,24 @@ def set_image(group, filename):
653
638
# add it to the group that is showing on the display
654
639
main_group .append (test_page_layout )
655
640
# 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
+
656
651
pge2_group = 1
657
652
658
653
659
- """If the temperature sensor has been disconnected,
654
+ """
655
+ If the temperature sensor has been disconnected,
660
656
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
+ """
662
659
663
660
664
661
def connect_temp_sensor ():
@@ -689,8 +686,10 @@ def connect_temp_sensor():
689
686
myVars .write ("t2" , None )
690
687
691
688
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
+ """
694
693
695
694
696
695
def connect_rtc ():
@@ -716,12 +715,14 @@ def connect_rtc():
716
715
print ("Failed to connect RTC" )
717
716
718
717
719
- """Function gets a value from the external temperature sensor
718
+ """
719
+ Function gets a value from the external temperature sensor
720
720
It only updates if the value has changed compared to the previous value
721
721
A fixed text is set in pge4_lbl2.text. The variable temperature value is set in pge4_lbl3.text
722
722
If no value obtained (for instance if the sensor is disconnected),
723
723
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
+ """
725
726
726
727
727
728
def get_temp ():
@@ -761,7 +762,6 @@ def get_temp():
761
762
) # clean the line (eventually: t2)
762
763
pge4_lbl2 .text = "Sensor disconnected."
763
764
pge4_lbl3 .text = "Check wiring."
764
-
765
765
return RetVal
766
766
767
767
@@ -843,11 +843,14 @@ def handle_dt(dt):
843
843
return RetVal
844
844
845
845
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;
847
849
b) if using online NTP pool server then get the date and time from the function time.localtime
848
850
This time.localtime has before been set with data from the NTP server.
849
851
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
+ """
851
854
852
855
853
856
def get_dt ():
@@ -905,16 +908,13 @@ def ck_next_NTP_sync():
905
908
c_elapsed = c_cnt - s_cnt
906
909
if c_elapsed < 10 : # continue only when c_elapsed >= 10
907
910
return
908
-
909
911
TAG = "ck_next_NTP_sync(): "
910
912
my_debug = myVars .read ("my_debug" )
911
913
t1 = myVars .read ("next_NTP_sync_t1" )
912
914
t3 = myVars .read ("next_NTP_sync_t3" )
913
915
five_min = myVars .read ("five_min_cnt" )
914
916
myVars .write ("s_cnt" , hms_to_cnt ())
915
-
916
917
# --- five minutes count down calculations #1 ---
917
-
918
918
if my_debug :
919
919
print (
920
920
TAG + "five_min = {}, s_cnt = {}, c_cnt = {}" .format (five_min , s_cnt , c_cnt )
@@ -958,7 +958,6 @@ def main():
958
958
while True :
959
959
touch = ts .touch_point
960
960
try :
961
-
962
961
if use_ntp :
963
962
ck_next_NTP_sync ()
964
963
ntp_refresh = myVars .read ("ntp_refresh" )
@@ -986,7 +985,6 @@ def main():
986
985
# the touch data has lost
987
986
if myVars .read ("temp_in_REPL" ):
988
987
myVars .write ("temp_in_REPL" , False )
989
-
990
988
cnt = inc_cnt (cnt )
991
989
except KeyboardInterrupt as exc :
992
990
print ("Keyboard interrupt...exiting..." )
0 commit comments