Skip to content

Commit 1392c38

Browse files
committed
add light starting time function
1 parent 15b28eb commit 1392c38

1 file changed

Lines changed: 48 additions & 33 deletions

File tree

PyPortal_Wakeup_Light/wake_up_light.py

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313

1414
# Type in time to get up
15-
input_wake_up_time = "7:45P"
15+
input_wake_up_time = "12:29A"
1616

1717
BRIGHTNESS = 0
1818
MIN_BRIGHTNESS = 0
19-
MAX_BRIGHTNESS = 0.5 # brightness above 0.2 crashes pyportal
19+
MAX_BRIGHTNESS = 0.85
2020

2121
strip = neopixel.NeoPixel(board.D3, 40, brightness=BRIGHTNESS)
2222

@@ -47,7 +47,7 @@
4747
info_font.load_glyphs(b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:/ ')
4848

4949

50-
input_wake_up_time_text = "Wake up 30 min after " + input_wake_up_time
50+
input_wake_up_time_text = "Wake up at " + input_wake_up_time
5151
#light_on_time_text = "Light starting at: " # - 30 minutes? how?
5252

5353
time_color = 0xFFFFFF
@@ -70,51 +70,63 @@
7070
pyportal.splash.append(wakeup_time_textarea)
7171
pyportal.splash.append(light_on_time_textarea)
7272

73-
74-
'''
75-
76-
def subtract30min(time_raw):
77-
now = time.localtime()
78-
hour, minute = now[3:5]
79-
if time_raw[4] >= 30:
80-
minute = time_raw[4] - 30
73+
def subtract30min(time_before):
74+
hours_before, minutes_before = time_before.split(":")
75+
AM_PM_str = minutes_before[-1:]
76+
minutes_before = int(minutes_before[:-1])
77+
if AM_PM_str == 'P':
78+
hours_before = int(hours_before) + 12
79+
elif ((hours_before == '12') and (AM_PM_str == 'A')):
80+
hours_before = 0
81+
print("Its midnight")
82+
else:
83+
hours_before = int(hours_before)
84+
print("else")
85+
print(AM_PM_str)
86+
print(hours_before)
87+
print(minutes_before)
88+
if minutes_before >= 30:
89+
minutes_after = minutes_before - 30
8190
# display the time in a nice big font
8291
format_str = "%d:%02d"
8392
if AM_PM:
84-
if hour >= 12:
85-
hour -= 12
93+
if hours_before >= 12:
94+
hours_after = hours_before - 12
8695
format_str = format_str+"P"
8796
else:
97+
hours_after = hours_before
8898
format_str = format_str+"A"
89-
if hour == 0:
90-
hour = 12
91-
if hour < 10:
99+
if hours_before == 0:
100+
hours_after = 12
101+
if hours_before < 10:
92102
format_str = ""+format_str
93-
sub30_str = format_str % (hour, minute)
103+
print(hours_after)
104+
print(minutes_after)
105+
sub30_str = format_str % (hours_after, minutes_after)
94106
light_on_time_textarea.text = "Light starting at: " + sub30_str
95107
return sub30_str
96-
97-
98-
elif time_raw[4] < 30:
99-
minute = time_raw[4] + 30
100-
hour = hour - 1
108+
elif minutes_before < 30:
109+
minutes_after = minutes_before + 30
110+
hours_after = hours_before - 1
101111
format_str = "%d:%02d"
102112
if AM_PM:
103-
if hour >= 12:
104-
hour -= 12
113+
if hours_before >= 12:
114+
hours_after = hours_before - 12
105115
format_str = format_str+"P"
106116
else:
117+
hours_after = hours_before
107118
format_str = format_str+"A"
108-
if hour == 0:
109-
hour = 12
110-
if hour < 10:
119+
if hours_before == 0:
120+
hours_after = 11
121+
format_str = format_str[:-1]+"P"
122+
if hours_before < 10:
111123
format_str = ""+format_str
112-
sub30_str = format_str % (hour, minute)
124+
print(hours_after)
125+
print(minutes_after)
126+
sub30_str = format_str % (hours_after, minutes_after)
113127
light_on_time_textarea.text = "Light starting at: " + sub30_str
114128
return sub30_str
115129

116-
'''
117-
118130
def displayTime():
119131
now = time.localtime()
120132
hour, minute = now[3:5]
@@ -142,6 +154,9 @@ def displayTime():
142154

143155
pyportal.set_backlight(0.1)
144156

157+
158+
159+
145160
while True:
146161

147162
# only query the online time once per hour (and on first run)
@@ -171,7 +186,7 @@ def displayTime():
171186

172187

173188
# sub30 = subtract30min(currentHour) # input needs to be input_wake_up_time_text
174-
189+
subtract30min(input_wake_up_time)
175190

176191
# If wake up time - 30 minutes equals current time, start the light
177192
if time_str_text is input_wake_up_time:
@@ -182,10 +197,10 @@ def displayTime():
182197
strip.fill((255, 255, 255))
183198
strip.brightness = BRIGHTNESS
184199
displayTime()
185-
time.sleep(1)
200+
time.sleep(60) # 60 for once per min
186201
while not pyportal.touchscreen.touch_point:
187202
displayTime()
188-
time.sleep(60) # 60 for once per min
203+
time.sleep(1)
189204
pass
190205
strip.brightness = MIN_BRIGHTNESS
191206

0 commit comments

Comments
 (0)