Skip to content

Commit 8d1d64b

Browse files
committed
change backlight functionality
1 parent 8c76668 commit 8d1d64b

1 file changed

Lines changed: 35 additions & 8 deletions

File tree

PyPortal_Wakeup_Light/wake_up_light.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
pyportal = PyPortal(status_neopixel=board.NEOPIXEL,
5252
default_bg=0x000000)
5353

54+
# set backlight default to off
55+
backlight_off = 0
56+
backlight_on = 0.8
57+
pyportal.set_backlight(backlight_off)
58+
5459
big_font = bitmap_font.load_font(cwd+"/fonts/Nunito-Light-75.bdf")
5560
big_font.load_glyphs(b'0123456789:AP') # pre-load glyphs for fast printing
5661
print('loading fonts...')
@@ -122,7 +127,7 @@ def formatTime(raw_hours, raw_minutes):
122127
time_str = format_str % (raw_hours, raw_minutes)
123128
return time_str
124129

125-
def subtract30min(time_before):
130+
def parseTime(time_before):
126131
# parse given time string into hour minute and AM_PM elements
127132
hours_before, minutes_before = time_before.split(":")
128133
AM_PM_str = minutes_before[-1:]
@@ -133,7 +138,12 @@ def subtract30min(time_before):
133138
hours_before = 0
134139
else:
135140
hours_before = int(hours_before)
136-
# subtract 30 min
141+
parsed_time = [hours_before, minutes_before]
142+
return parsed_time
143+
144+
def subtract30min(time_before): # subtract 30 min
145+
parsed_time = parseTime(time_before)
146+
hours_before, minutes_before = parsed_time[0:2]
137147
now = time.localtime()
138148
future = time.mktime((now[0], now[1], now[2], hours_before, minutes_before - 30, now[5], now[6], now[7], now[8]))
139149
futureTime = time.localtime(future)
@@ -142,6 +152,23 @@ def subtract30min(time_before):
142152
light_on_time_textarea.text = "Light starting at: " + formatTime(future_hour, future_minutes)
143153
return formatTime(future_hour, future_minutes)
144154

155+
# backlight function - if screen tapped, turn on back light for 30 seconds?
156+
def backLight(time_before):
157+
parsed_time = parseTime(time_before)
158+
hours_before, minutes_before = parsed_time[0:2]
159+
now = time.localtime()
160+
wakeUpNow = time.mktime((now[0], now[1], now[2], hours_before, minutes_before, now[5], now[6], now[7], now[8]))
161+
nowVal = time.mktime((now[0], now[1], now[2], now[3], now[4], now[5], now[6], now[7], now[8]))
162+
print(nowVal - wakeUpNow)
163+
if (nowVal - wakeUpNow) > 32400:
164+
# if time is more than 9 hours after wake up time, backlight off, tap to turn on
165+
if pyportal.touchscreen.touch_point:
166+
pyportal.set_backlight(backlight_on)
167+
time.sleep(5)
168+
pyportal.set_backlight(backlight_off)
169+
else:
170+
pyportal.set_backlight(backlight_on)
171+
145172
refresh_time = None
146173

147174
while True:
@@ -157,18 +184,18 @@ def subtract30min(time_before):
157184
time_str_text = displayTime()
158185
print(time_str_text)
159186
currentHour = time.localtime()
160-
# if after 9am and before 9pm light is = 0.8
161-
if currentHour[3] > 9 and currentHour[3] < 21:
162-
pyportal.set_backlight(0.8)
163-
# if after 9pm and before 9am light = 0.1
164-
else:
165-
pyportal.set_backlight(0.1)
187+
# determine which wake up time to choose based on the day
166188
wake_up_time = whichDay()
189+
# if time is more than 9 hours after wake up time, backlight off and can tap to turn on
190+
backLight(wake_up_time)
191+
# start the light 30 min before wake up time
167192
start_light_time = subtract30min(wake_up_time)
168193
print(start_light_time)
169194
# If wake up time - 30 minutes equals current time, start the light
170195
if time_str_text == start_light_time:
171196
print("Starting wake up light")
197+
# turn on backlight
198+
pyportal.set_backlight(backlight_on)
172199
for i in range(light_minutes - 1):
173200
BRIGHTNESS = BRIGHTNESS + (MAX_BRIGHTNESS/light_minutes) # max 0.25, min 0.0
174201
strip.fill(WHITE)

0 commit comments

Comments
 (0)