Skip to content

Commit 2ff4c77

Browse files
authored
Merge pull request #781 from isaacwellish/pyportal-wakeup-light
Pyportal wakeup light updates
2 parents 75dafd1 + dd5d687 commit 2ff4c77

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

PyPortal_Wakeup_Light/wake_up_light.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
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.
4-
This program assumes a neopixel strip is attached to D4 on the Adafruit PyPortal.
4+
This program assumes a neopixel strip is attached to D3 on the Adafruit PyPortal.
55
"""
66
import time
77
import board
@@ -27,6 +27,7 @@
2727
up_time_saturday,
2828
up_time_sunday,
2929
default_wake_up)
30+
days_str = ("Mon.", "Tues.", "Wed.", "Thurs.", "Fri.", "Sat.", "Sun.")
3031

3132
# set neopixel min and max brightness
3233
BRIGHTNESS = 0
@@ -83,6 +84,15 @@
8384
pyportal.splash.append(wakeup_time_textarea)
8485
pyportal.splash.append(light_on_time_textarea)
8586

87+
while True:
88+
try:
89+
print("Getting time from internet!")
90+
pyportal.get_local_time()
91+
except RuntimeError as e:
92+
print("Some error occured, retrying! -", e)
93+
continue
94+
break
95+
8696
# parse given time string into hour minute and AM_PM elements
8797
def parseTime(time_before):
8898
hours_before, minutes_before = time_before.split(":")
@@ -122,16 +132,19 @@ def whichDay():
122132
if now_mk < val_times[day]:
123133
if current_day == day:
124134
input_wake_up_time = wake_up_times[day]
125-
# set wake up time to the next day's wake up time the night before
135+
use_day = day
136+
# set wake up time to the next day's wake up time after current day's wake up time
126137
else:
127138
if current_day == 6:
128139
input_wake_up_time = wake_up_times[0]
140+
use_day = 0
129141
else:
130142
if current_day == day:
131143
input_wake_up_time = wake_up_times[day+1]
132-
input_wake_up_time_text = "Wake up at " + input_wake_up_time
144+
use_day = day + 1
145+
input_wake_up_time_text = "Wake up " + days_str[use_day] + " at " + input_wake_up_time
133146
wakeup_time_textarea.text = input_wake_up_time_text
134-
return current_day
147+
return use_day
135148

136149
def displayTime():
137150
now = time.localtime()
@@ -155,14 +168,14 @@ def formatTime(raw_hours, raw_minutes):
155168
time_str = format_str % (raw_hours, raw_minutes)
156169
return time_str
157170

158-
# backlight function - if screen tapped, turn on back light for 30 seconds?
159-
def backLight(day):
171+
def backLight():
160172
now = time.localtime()
161173
now_val = time.mktime((now[0], now[1], now[2], now[3], now[4], now[5], now[6], now[7], now[8]))
162-
wake_up_day_val = val_times[day]
174+
wake_up_day_val = val_times[now[6]]
175+
# if time is more than 9 hours after current day's wake up time,
176+
# or time is before light start time, backlight off, tap to turn on
163177
if (now_val - wake_up_day_val) > 32400 or (now_val - wake_up_day_val) < -1800:
164-
# if time is more than 9 hours after wake up time, or time is before light start time:
165-
# backlight off, tap to turn on
178+
pyportal.set_backlight(backlight_off)
166179
if pyportal.touchscreen.touch_point:
167180
pyportal.set_backlight(backlight_on)
168181
time.sleep(5)
@@ -186,6 +199,7 @@ def subtract30min(day): # subtract 30 min
186199
refresh_time = None
187200

188201
while True:
202+
time_now = time.localtime()
189203
# only query the online time once per hour (and on first run)
190204
if (not refresh_time) or (time.monotonic() - refresh_time) > 3600:
191205
try:
@@ -197,15 +211,16 @@ def subtract30min(day): # subtract 30 min
197211
continue
198212
time_str_text = displayTime()
199213
print(time_str_text)
200-
currentHour = time.localtime()
201214
# determine which wake up time to choose based on the day
202215
wake_up_day = whichDay()
203-
# if time is more than 9 hours after wake up time, backlight off and can tap to turn on
204-
backLight(wake_up_day)
216+
# if time is more than 9 hours after previous day's wake up time,
217+
# backlight off and can tap to turn on
218+
backLight()
205219
# start the light 30 min before wake up time
206220
start_light_time = subtract30min(wake_up_day)
207-
# If wake up time - 30 minutes equals current time, start the light
208-
if time_str_text == start_light_time:
221+
# If current day is same as wake up day and
222+
# wake up time - 30 minutes equals current time, start the light
223+
if wake_up_day == time_now[6] and time_str_text == start_light_time:
209224
print("Starting wake up light")
210225
# turn on backlight
211226
pyportal.set_backlight(backlight_on)

0 commit comments

Comments
 (0)