Skip to content

Commit cc33afd

Browse files
committed
add basic ui elements
1 parent d3276b7 commit cc33afd

2 files changed

Lines changed: 159 additions & 1 deletion

File tree

PyPortal_Wakeup_Light/wake_up_light.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,72 @@
22
This example uses a PyPortal and rgbw leds for a simple "wake up" light.
33
The strip starts to brighten 30 minutes before set wake up time.
44
This program assumes a neopixel strip is attached to D4 on the Adafruit PyPortal.
5-
"""
5+
"""
6+
7+
import time
8+
import board
9+
import neopixel
10+
import busio
11+
from digitalio import DigitalInOut
12+
from analogio import AnalogIn
13+
import adafruit_adt7410
14+
15+
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
16+
from adafruit_io.adafruit_io import RESTClient, AdafruitIO_RequestError
17+
18+
# thermometer graphics helper
19+
import wakeup_helper
20+
21+
# rate at which to refresh the pyportal screen, in seconds
22+
PYPORTAL_REFRESH = 15
23+
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
30+
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)
39+
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.')
49+
50+
# Create an instance of the Adafruit IO REST client
51+
io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)
52+
53+
# Get the temperature feed from Adafruit IO
54+
temperature_feed = io.get_feed('temperature')
55+
56+
# init. graphics helper
57+
gfx = wakeup_helper.Thermometer_GFX(celsius=False)
58+
59+
# init. adt7410
60+
i2c_bus = busio.I2C(board.SCL, board.SDA)
61+
62+
while True:
63+
try: # WiFi Connection
64+
# Get and display date and time form Adafruit IO
65+
print('Getting time from Adafruit IO...')
66+
datetime = io.receive_time()
67+
print('displaying time...')
68+
gfx.display_date_time(datetime)
69+
except (ValueError, RuntimeError) as e: # WiFi Connection Failure
70+
print("Failed to get data, retrying\n", e)
71+
wifi.reset()
72+
continue
73+
time.sleep(PYPORTAL_REFRESH)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
GFX helper file for
3+
wake_up_light.py.py
4+
"""
5+
import board
6+
import displayio
7+
from adafruit_display_text.label import Label
8+
from adafruit_bitmap_font import bitmap_font
9+
10+
cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
11+
12+
# Fonts within /fonts folder
13+
info_font = cwd+"/fonts/Nunito-Black-17.bdf"
14+
time_font = cwd+"/fonts/Nunito-Light-75.bdf"
15+
16+
get_up_time = "6:30"
17+
get_up_time_text = "Wake up at: " + get_up_time + "am"
18+
light_on_time_text = "Light starting at: " # - 30 minutes? how?
19+
20+
class Thermometer_GFX(displayio.Group):
21+
def __init__(self, celsius=True, usa_date=True):
22+
"""Creates a Thermometer_GFX object.
23+
:param bool usa_date: Use mon/day/year date-time formatting.
24+
"""
25+
# root displayio group
26+
root_group = displayio.Group(max_size=20)
27+
board.DISPLAY.show(root_group)
28+
super().__init__(max_size=20)
29+
30+
self._usa_date = usa_date
31+
32+
# create text object group
33+
self._text_group = displayio.Group(max_size=6)
34+
self.append(self._text_group)
35+
36+
self._cwd = cwd
37+
38+
print('loading fonts...')
39+
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:/ '
40+
self.info_font = bitmap_font.load_font(info_font)
41+
self.info_font.load_glyphs(glyphs)
42+
43+
self.time_font = bitmap_font.load_font(time_font)
44+
self.time_font.load_glyphs(glyphs)
45+
46+
print('setting up labels...')
47+
self.title_text = Label(self.info_font, text="PyPortal Wake-Up Light")
48+
self.title_text.x = 15
49+
self.title_text.y = 15
50+
self._text_group.append(self.title_text)
51+
52+
# Wake up time
53+
self.subtitle_text = Label(self.info_font, text= get_up_time_text)
54+
self.subtitle_text.x = 15
55+
self.subtitle_text.y = 200
56+
self._text_group.append(self.subtitle_text)
57+
58+
#Light on time - how to subtract 30 min from time string
59+
self.subtitle2_text = Label(self.info_font, text= light_on_time_text)
60+
self.subtitle2_text.x = 15
61+
self.subtitle2_text.y = 240
62+
# self._text_group.append(self.subtitle2_text)
63+
64+
# Time
65+
self.time_text = Label(self.time_font, max_glyphs=40)
66+
self.time_text.x = 65
67+
self.time_text.y = 120
68+
self._text_group.append(self.time_text)
69+
70+
# Date
71+
self.date_text = Label(self.info_font, max_glyphs=40)
72+
self.date_text.x = 30
73+
self.date_text.y = 160
74+
self._text_group.append(self.date_text)
75+
76+
board.DISPLAY.show(self._text_group)
77+
78+
79+
def display_date_time(self, io_time):
80+
"""Parses and displays the time obtained from Adafruit IO, based on IP
81+
:param struct_time io_time: Structure used for date/time, returned from Adafruit IO.
82+
"""
83+
self.time_text.text = '%02d:%02d'%(io_time[3],io_time[4])
84+
'''
85+
# not siplaying date just yet
86+
if not self._usa_date:
87+
self.date_text.text = '{0}/{1}/{2}'.format(io_time[2], io_time[1], io_time[0])
88+
else:
89+
self.date_text.text = '{0}/{1}/{2}'.format(io_time[1], io_time[2], io_time[0])
90+
'''

0 commit comments

Comments
 (0)