33The strip starts to brighten 30 minutes before set wake up time.
44This program assumes a neopixel strip is attached to D4 on the Adafruit PyPortal.
55"""
6-
76import time
87import board
9- import neopixel
10- import busio
11- from digitalio import DigitalInOut
12- from analogio import AnalogIn
13- import displayio
14- from adafruit_display_text .label import Label
8+ from adafruit_pyportal import PyPortal
159from adafruit_bitmap_font import bitmap_font
16- from adafruit_esp32spi import adafruit_esp32spi , adafruit_esp32spi_wifimanager
17- from adafruit_io .adafruit_io import RESTClient , AdafruitIO_RequestError
18-
19- cwd = ("/" + __file__ ).rsplit ('/' , 1 )[0 ] # the current working directory (where this file is)
20-
21- # rate at which to refresh the pyportal screen, in seconds
22- PYPORTAL_REFRESH = 15
10+ from adafruit_display_text .Label import Label
2311
24- # Get wifi details and more from a secrets.py file
25- try :
26- from secrets import secrets
27- except ImportError :
28- print ("WiFi secrets are kept in secrets.py, please add them there!" )
29- raise
12+ # Set to True for '12 hour + AM/PM' time, set to false for 24 hour time
13+ AM_PM = True
3014
31- # PyPortal ESP32 Setup
32- esp32_cs = DigitalInOut (board .ESP_CS )
33- esp32_ready = DigitalInOut (board .ESP_BUSY )
34- esp32_reset = DigitalInOut (board .ESP_RESET )
35- spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
36- esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
37- status_light = neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.2 )
38- wifi = adafruit_esp32spi_wifimanager .ESPSPI_WiFiManager (esp , secrets , status_light )
15+ # determine the current working directory
16+ # needed so we know where to find files
17+ cwd = ("/" + __file__ ).rsplit ('/' , 1 )[0 ]
18+ # Initialize the pyportal object and let us know what data to fetch and where
19+ # to display it
3920
40- # Set your Adafruit IO Username and Key in secrets.py
41- # (visit io.adafruit.com if you need to create an account,
42- # or if you need your Adafruit IO key.)
43- try :
44- ADAFRUIT_IO_USER = secrets ['aio_username' ]
45- ADAFRUIT_IO_KEY = secrets ['aio_key' ]
46- except KeyError :
47- raise KeyError ('To use this code, you need to include your Adafruit IO username \
48- and password in a secrets.py file on the CIRCUITPY drive.' )
21+ pyportal = PyPortal (status_neopixel = board .NEOPIXEL ,
22+ default_bg = 0x000000 )
4923
50- # Fonts within /fonts folder
51- info_font = cwd + "/fonts/Nunito-Black-17.bdf"
52- time_font = cwd + "/fonts/Nunito-Light-75.bdf"
24+ big_font = bitmap_font .load_font (cwd + "/fonts/Nunito-Light-75.bdf" )
25+ big_font .load_glyphs (b'0123456789:AP' ) # pre-load glyphs for fast printing
26+ print ('loading fonts...' )
27+ info_font = bitmap_font .load_font (cwd + "/fonts/Nunito-Black-17.bdf" )
28+ info_font .load_glyphs (b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:/ ' )
5329
5430# Type in time to get up
5531input_wake_up_time = "6:30"
5632input_wake_up_time_text = "Wake up at: " + input_wake_up_time + "am"
5733#light_on_time_text = "Light starting at: " # - 30 minutes? how?
5834
59- # create text object group
60- text_group = displayio .Group (max_size = 6 )
61- text_group .append (text_group )
62-
63- print ('loading fonts...' )
64- glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:/ '
65-
66- # Font for time
67- time_font = bitmap_font .load_font (time_font )
68- time_font .load_glyphs (glyphs )
69-
70- # Font for other info
71- info_font = bitmap_font .load_font (info_font )
72- info_font .load_glyphs (glyphs )
73-
74- # Time
75- time_text = Label (time_font , max_glyphs = 40 )
76- time_text .x = 65
77- time_text .y = 120
78- text_group .append (time_text )
35+ time_color = 0xFFFFFF
36+ time_position = (0 ,130 )
37+ time_textarea = Label (big_font , max_glyphs = 15 , color = time_color ,
38+ x = time_position [0 ], y = time_position [1 ])
7939
80- # Wake up time
81- wakeup_time_text = Label (info_font , text = input_wake_up_time_text )
82- wakeup_time_text .x = 15
83- wakeup_time_text .y = 200
84- text_group .append (wakeup_time_text )
40+ wakeup_time_color = 0xFFFFFF
41+ wakeup_time_position = (15 ,200 )
42+ wakeup_time_textarea = Label (info_font , max_glyphs = 30 , color = wakeup_time_color ,
43+ x = wakeup_time_position [0 ], y = wakeup_time_position [1 ])
8544
86- board .DISPLAY .show (time_text )
87- # board.DISPLAY.show(text_group) # when running this line, the pyportal crashes
45+ pyportal .splash .append (time_textarea )
46+ wakeup_time_textarea .text = input_wake_up_time_text
47+ pyportal .splash .append (wakeup_time_textarea )
8848
89- # Create an instance of the Adafruit IO REST client
90- io = RESTClient (ADAFRUIT_IO_USER , ADAFRUIT_IO_KEY , wifi )
49+ refresh_time = None
9150
9251while True :
93- try : # WiFi Connection
94- # Get and display date and time from Adafruit IO
95- print ('Getting time from Adafruit IO...' )
96- datetime = io .receive_time ()
97- print ('displaying time...' )
98- time_text .text = '%02d:%02d' % (datetime [3 ],datetime [4 ])
99- except (ValueError , RuntimeError ) as e : # WiFi Connection Failure
100- print ("Failed to get data, retrying\n " , e )
101- wifi .reset ()
102- continue
103- time .sleep (PYPORTAL_REFRESH )
52+ # only query the online time once per hour (and on first run)
53+ if (not refresh_time ) or (time .monotonic () - refresh_time ) > 3600 :
54+ try :
55+ print ("Getting time from internet!" )
56+ pyportal .get_local_time ()
57+ refresh_time = time .monotonic ()
58+ except RuntimeError as e :
59+ print ("Some error occured, retrying! -" , e )
60+ continue
61+ print (time .monotonic ())
62+ now = time .localtime ()
63+ hour , minute = now [3 :5 ]
64+ print (now )
65+ print ("Current time: %02d:%02d" % (hour , minute ))
66+
67+ # display the time in a nice big font
68+ format_str = "%d:%02d"
69+ if AM_PM :
70+ if hour >= 12 :
71+ hour -= 12
72+ format_str = format_str + "P"
73+ else :
74+ format_str = format_str + "A"
75+ if hour == 0 :
76+ hour = 12
77+ if hour < 10 :
78+ format_str = " " + format_str
79+ time_str = format_str % (hour , minute )
80+ time_textarea .text = time_str
81+
82+ # update every 15 seconds
83+ time .sleep (15 )
0 commit comments