Skip to content

Commit c966b40

Browse files
committed
remove parsing code
1 parent 7a9144c commit c966b40

1 file changed

Lines changed: 62 additions & 51 deletions

File tree

PyPortal_Wakeup_Light/wake_up_light.py

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
up_time_thursday,
2626
up_time_friday,
2727
up_time_saturday,
28-
up_time_sunday)
28+
up_time_sunday,
29+
default_wake_up)
2930

3031
# set neopixel min and max brightness
3132
BRIGHTNESS = 0
@@ -36,7 +37,7 @@
3637
ORDER = neopixel.RGBW
3738
strip = neopixel.NeoPixel(board.D3, num_pixels, brightness=BRIGHTNESS,
3839
pixel_order=ORDER)
39-
strip.fill(0)
40+
strip.fill(0) # start it set to off
4041
# color of strip
4142
WHITE = (0, 0, 0, 255)
4243
# number of minutes it takes for strip to fade from min to max
@@ -56,6 +57,7 @@
5657
backlight_on = 0.8
5758
pyportal.set_backlight(backlight_off)
5859

60+
# assign fonts
5961
big_font = bitmap_font.load_font(cwd+"/fonts/Nunito-Light-75.bdf")
6062
big_font.load_glyphs(b'0123456789:AP') # pre-load glyphs for fast printing
6163
print('loading fonts...')
@@ -81,17 +83,43 @@
8183
pyportal.splash.append(wakeup_time_textarea)
8284
pyportal.splash.append(light_on_time_textarea)
8385

86+
# parse given time string into hour minute and AM_PM elements
87+
def parseTime(time_before):
88+
hours_before, minutes_before = time_before.split(":")
89+
AM_PM_str = minutes_before[-1:]
90+
minutes_before = int(minutes_before[:-1])
91+
if (hours_before != '12') and AM_PM_str == 'P':
92+
hours_before = int(hours_before) + 12
93+
elif ((hours_before == '12') and (AM_PM_str == 'P')):
94+
hours_before = int(hours_before)
95+
elif ((hours_before == '12') and (AM_PM_str == 'A')):
96+
hours_before = 0
97+
else:
98+
hours_before = int(hours_before)
99+
parsed_time = [hours_before, minutes_before]
100+
return parsed_time
101+
102+
# get time objects for wake up times
103+
val_times = []
104+
parsed_times = []
105+
for i in range(len(wake_up_times)):
106+
parsed_time_day = parseTime(wake_up_times[i])
107+
hours, minutes = parsed_time_day[0:2]
108+
now_day = time.localtime()
109+
time_obj_mk = time.mktime((now_day[0], now_day[1], now_day[2], hours,
110+
minutes, now_day[5], i, now_day[7], now_day[8]))
111+
time_obj = time.localtime(time_obj_mk)
112+
val_times.append(time_obj_mk)
113+
parsed_times.append(time_obj)
114+
115+
# determine which day it is and print which time waking up on screen
84116
def whichDay():
85117
now = time.localtime()
86-
current_hour, current_minutes = now[3:5]
87118
current_day = now[6]
88-
wake_up_hour, wake_up_minutes = default_wake_up.split(":")
89-
wake_up_hour = int(wake_up_hour)
90-
wake_up_minutes = int(wake_up_minutes[:-1])
91-
print(wake_up_hour, ":", wake_up_minutes)
92-
# if it's after midnight and before the default wakeup time, display the wake up time of today
119+
now_mk = time.mktime((now[0], now[1], now[2], now[3], now[4], now[5], now[6], now[7], now[8]))
120+
# if it's after midnight and before todays wakeup time, display the wake up time of today
93121
for day in range(len(wake_up_times)):
94-
if current_hour < wake_up_hour and current_minutes < wake_up_minutes:
122+
if now_mk < val_times[day]:
95123
if current_day == day:
96124
input_wake_up_time = wake_up_times[day]
97125
# set wake up time to the next day's wake up time the night before
@@ -103,7 +131,7 @@ def whichDay():
103131
input_wake_up_time = wake_up_times[day+1]
104132
input_wake_up_time_text = "Wake up at " + input_wake_up_time
105133
wakeup_time_textarea.text = input_wake_up_time_text
106-
return input_wake_up_time
134+
return current_day
107135

108136
def displayTime():
109137
now = time.localtime()
@@ -127,50 +155,34 @@ def formatTime(raw_hours, raw_minutes):
127155
time_str = format_str % (raw_hours, raw_minutes)
128156
return time_str
129157

130-
def parseTime(time_before):
131-
# parse given time string into hour minute and AM_PM elements
132-
hours_before, minutes_before = time_before.split(":")
133-
AM_PM_str = minutes_before[-1:]
134-
minutes_before = int(minutes_before[:-1])
135-
if (hours_before != '12') and AM_PM_str == 'P':
136-
hours_before = int(hours_before) + 12
137-
elif ((hours_before == '12') and (AM_PM_str == 'P')):
138-
hours_before = int(hours_before)
139-
elif ((hours_before == '12') and (AM_PM_str == 'A')):
140-
hours_before = 0
141-
else:
142-
hours_before = int(hours_before)
143-
parsed_time = [hours_before, minutes_before]
144-
return parsed_time
145-
146-
def subtract30min(time_initial): # subtract 30 min
147-
parsed_time = parseTime(time_initial)
148-
hours_initial, minutes_initial = parsed_time[0:2]
149-
now = time.localtime()
150-
minus30 = time.mktime((now[0], now[1], now[2], hours_initial, minutes_initial - 30, now[5], now[6], now[7], now[8]))
151-
time_minus30 = time.localtime(minus30)
152-
hour_minus30 = time_minus30[3]
153-
minutes_minus30 = time_minus30[4]
154-
light_on_time_textarea.text = "Light starting at: " + formatTime(hour_minus30, minutes_minus30)
155-
return formatTime(hour_minus30, minutes_minus30)
156-
157158
# backlight function - if screen tapped, turn on back light for 30 seconds?
158-
def backLight(time_before):
159-
parsed_time = parseTime(time_before)
160-
hours_before, minutes_before = parsed_time[0:2]
159+
def backLight(day):
161160
now = time.localtime()
162-
wakeUpNow = time.mktime((now[0], now[1], now[2], hours_before, minutes_before, now[5], now[6], now[7], now[8]))
163-
nowVal = time.mktime((now[0], now[1], now[2], now[3], now[4], now[5], now[6], now[7], now[8]))
164-
print(nowVal - wakeUpNow)
165-
if (nowVal - wakeUpNow) > 32400 or (nowVal - wakeUpNow) < -1800:
166-
# if time is more than 9 hours after wake up time, or before wake up time - 30 backlight off, tap to turn on
161+
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]
163+
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
167166
if pyportal.touchscreen.touch_point:
168167
pyportal.set_backlight(backlight_on)
169168
time.sleep(5)
170169
pyportal.set_backlight(backlight_off)
171170
else:
172171
pyportal.set_backlight(backlight_on)
173172

173+
def subtract30min(day): # subtract 30 min
174+
# get the time object from the corresponding day
175+
raw_wake_up_time = parsed_times[day]
176+
now = time.localtime()
177+
# new time subtracting 30 min from wake up time
178+
minus30 = time.mktime((now[0], now[1], now[2], raw_wake_up_time[3],
179+
raw_wake_up_time[4] - 30, now[5], now[6], now[7], now[8]))
180+
time_minus30 = time.localtime(minus30)
181+
hour_minus30 = time_minus30[3]
182+
minutes_minus30 = time_minus30[4]
183+
light_on_time_textarea.text = "Light starting at: " + formatTime(hour_minus30, minutes_minus30)
184+
return formatTime(hour_minus30, minutes_minus30)
185+
174186
refresh_time = None
175187

176188
while True:
@@ -187,12 +199,11 @@ def backLight(time_before):
187199
print(time_str_text)
188200
currentHour = time.localtime()
189201
# determine which wake up time to choose based on the day
190-
wake_up_time = whichDay()
202+
wake_up_day = whichDay()
191203
# if time is more than 9 hours after wake up time, backlight off and can tap to turn on
192-
backLight(wake_up_time)
204+
backLight(wake_up_day)
193205
# start the light 30 min before wake up time
194-
start_light_time = subtract30min(wake_up_time)
195-
print(start_light_time)
206+
start_light_time = subtract30min(wake_up_day)
196207
# If wake up time - 30 minutes equals current time, start the light
197208
if time_str_text == start_light_time:
198209
print("Starting wake up light")
@@ -209,5 +220,5 @@ def backLight(time_before):
209220
time.sleep(1)
210221
continue
211222
strip.brightness = MIN_BRIGHTNESS
212-
# update every 15 seconds
213-
time.sleep(15)
223+
# update every second so that screen can be tapped to view time
224+
time.sleep(1)

0 commit comments

Comments
 (0)