Skip to content

Commit 8c76668

Browse files
committed
refactor subtract30min fucntion
1 parent 5bd0bb7 commit 8c76668

1 file changed

Lines changed: 33 additions & 47 deletions

File tree

PyPortal_Wakeup_Light/wake_up_light.py

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from adafruit_bitmap_font import bitmap_font
1111
from adafruit_display_text.Label import Label
1212

13-
# Type in time to get up each day of the week
13+
# type in time to get up each day of the week
1414
default_wake_up = "6:30A"
1515
up_time_monday = default_wake_up
1616
up_time_tuesday = default_wake_up
@@ -31,7 +31,7 @@
3131
BRIGHTNESS = 0
3232
MIN_BRIGHTNESS = 0
3333
MAX_BRIGHTNESS = 0.85
34-
#initialize neopixel strip
34+
# initialize neopixel strip
3535
num_pixels = 30
3636
ORDER = neopixel.RGBW
3737
strip = neopixel.NeoPixel(board.D3, num_pixels, brightness=BRIGHTNESS,
@@ -46,7 +46,7 @@
4646
# needed so we know where to find files
4747
cwd = ("/"+__file__).rsplit('/', 1)[0]
4848

49-
# Initialize the pyportal object and let us know what data to fetch and where
49+
# initialize the pyportal object and let us know what data to fetch and where
5050
# to display it
5151
pyportal = PyPortal(status_neopixel=board.NEOPIXEL,
5252
default_bg=0x000000)
@@ -100,62 +100,48 @@ def whichDay():
100100
wakeup_time_textarea.text = input_wake_up_time_text
101101
return input_wake_up_time
102102

103-
def subtract30min(time_before):
104-
hours_before, minutes_before = time_before.split(":")
105-
AM_PM_str = minutes_before[-1:]
106-
minutes_before = int(minutes_before[:-1])
107-
if AM_PM_str == 'P':
108-
hours_before = int(hours_before) + 12
109-
elif ((hours_before == '12') and (AM_PM_str == 'A')):
110-
hours_before = 0
111-
else:
112-
hours_before = int(hours_before)
113-
if minutes_before >= 30:
114-
minutes_after = minutes_before - 30
115-
format_str = "%d:%02d"
116-
if hours_before >= 12:
117-
hours_after = hours_before - 12
118-
format_str = format_str+"P"
119-
else:
120-
hours_after = hours_before
121-
format_str = format_str+"A"
122-
if hours_before == 0:
123-
hours_after = 12
124-
sub30_str = format_str % (hours_after, minutes_after)
125-
elif minutes_before < 30:
126-
minutes_after = minutes_before + 30
127-
hours_after = hours_before - 1
128-
format_str = "%d:%02d"
129-
if hours_before >= 12:
130-
hours_after = hours_after - 12
131-
format_str = format_str+"P"
132-
else:
133-
format_str = format_str+"A"
134-
if hours_before == 0:
135-
hours_after = 11
136-
format_str = format_str[:-1]+"P"
137-
sub30_str = format_str % (hours_after, minutes_after)
138-
light_on_time_textarea.text = "Light starting at: " + sub30_str
139-
return sub30_str
140-
141103
def displayTime():
142104
now = time.localtime()
143105
hour, minute = now[3:5]
144106
print(now)
145107
print("Current time: %02d:%02d" % (hour, minute))
108+
formatTime(hour, minute)
109+
time_textarea.text = formatTime(hour, minute)
110+
return formatTime(hour, minute)
111+
112+
def formatTime(raw_hours, raw_minutes):
146113
# display the time in a nice big font
147114
format_str = "%d:%02d"
148-
if hour >= 12:
149-
hour -= 12
115+
if raw_hours >= 12:
116+
raw_hours -= 12
150117
format_str = format_str+"P"
151118
else:
152119
format_str = format_str+"A"
153-
if hour == 0:
154-
hour = 12
155-
time_str = format_str % (hour, minute)
156-
time_textarea.text = time_str
120+
if raw_hours == 0:
121+
raw_hours = 12
122+
time_str = format_str % (raw_hours, raw_minutes)
157123
return time_str
158124

125+
def subtract30min(time_before):
126+
# parse given time string into hour minute and AM_PM elements
127+
hours_before, minutes_before = time_before.split(":")
128+
AM_PM_str = minutes_before[-1:]
129+
minutes_before = int(minutes_before[:-1])
130+
if AM_PM_str == 'P':
131+
hours_before = int(hours_before) + 12
132+
elif ((hours_before == '12') and (AM_PM_str == 'A')):
133+
hours_before = 0
134+
else:
135+
hours_before = int(hours_before)
136+
# subtract 30 min
137+
now = time.localtime()
138+
future = time.mktime((now[0], now[1], now[2], hours_before, minutes_before - 30, now[5], now[6], now[7], now[8]))
139+
futureTime = time.localtime(future)
140+
future_hour = futureTime[3]
141+
future_minutes = futureTime[4]
142+
light_on_time_textarea.text = "Light starting at: " + formatTime(future_hour, future_minutes)
143+
return formatTime(future_hour, future_minutes)
144+
159145
refresh_time = None
160146

161147
while True:

0 commit comments

Comments
 (0)