|
import board |
|
from digitalio import DigitalInOut, Direction, Pull |
|
from oso_lcd.lcdwing_lite import LCDWingLite, Indicator |
|
import time |
|
import alarm |
|
import rtc |
|
|
|
import lights |
|
import network |
|
import button |
|
|
|
display = LCDWingLite(board.I2C()) |
|
clock = rtc.RTC() |
|
|
|
if clock.datetime.tm_year < 2020: |
|
display.print("StClk") |
|
display.set_indicator(Indicator.WIFI) |
|
network.set_time_if_needed() |
|
display.clear_indicator(Indicator.WIFI) |
|
alarm.sleep_memory[0] = 14 # The default hour for the lights to turn on, 10 AM |
|
alarm.sleep_memory[1] = 0 # The default minute for the lights to turn on (10:00 AM) |
|
alarm.sleep_memory[2] = 1 # The default duration for the lights to remain on (1 hour) |
|
|
|
def display_time(hour, minute): |
|
hour_12 = hour % 12 |
|
display.print("{:2d}:{:02d}".format(hour_12 if hour_12 else 12, minute)) |
|
if hour < 12: |
|
display.clear_indicator(Indicator.PM) |
|
display.set_indicator(Indicator.AM) |
|
else: |
|
display.clear_indicator(Indicator.AM) |
|
display.set_indicator(Indicator.PM) |
|
|
|
MODE_SHOW_TIME = 0 |
|
MODE_SET_ON_TIME = 1 |
|
MODE_SET_DURATION = 2 |
|
NUM_MODES = 3 |
|
|
|
mode = 0 |
|
time_needs_display = True |
|
on_time_hours = alarm.sleep_memory[0] |
|
on_time_minutes = alarm.sleep_memory[1] |
|
on_duration_hours = alarm.sleep_memory[2] |
|
deep_sleep_at = time.monotonic() + 100 |
|
lights_on = False |
|
|
|
while True: |
|
dt = clock.datetime |
|
(press, long_press) = button.update() |
|
if press or long_press: |
|
deep_sleep_at = max(deep_sleep_at, time.monotonic() + 5) |
|
|
|
if long_press: |
|
mode = (mode + 1) % NUM_MODES |
|
display.clear_indicator(Indicator.ALL) |
|
time_needs_display = True |
|
|
|
if mode == MODE_SHOW_TIME: |
|
if time_needs_display or dt.tm_sec == 0: |
|
display_time(dt.tm_hour, dt.tm_min) |
|
time_needs_display = False |
|
elif mode == MODE_SET_ON_TIME: |
|
display.set_indicator(Indicator.BELL) |
|
if press: |
|
on_time_minutes += 15 |
|
if on_time_minutes == 60: |
|
on_time_minutes = 0 |
|
on_time_hours = (on_time_hours + 1) % 24 |
|
display_time(on_time_hours, on_time_minutes) |
|
elif mode == MODE_SET_DURATION: |
|
if press: |
|
if on_duration_hours == 12: |
|
on_duration_hours = 1 |
|
else: |
|
on_duration_hours += 1 |
|
display.print("{:2d} hr".format(on_duration_hours)) |
|
|
|
if lights_on: |
|
display.set_indicator(Indicator.DATA) |
|
lights.animate() |
|
else: |
|
if dt.tm_hour == on_time_hours and dt.tm_min >= on_time_minutes and mode == 0: |
|
lights.turn_on() |
|
lights_on = True |
|
deep_sleep_at = 1 + time.monotonic() + on_duration_hours * 60 * 60 |
|
elif isinstance(alarm.wake_alarm, alarm.time.TimeAlarm): |
|
deep_sleep_at = time.monotonic() |
|
|
|
if time.monotonic() >= deep_sleep_at: |
|
lights.prepare_for_sleep() |
|
display.clear_indicator(Indicator.DATA) |
|
alarm.sleep_memory[0] = on_time_hours |
|
alarm.sleep_memory[1] = on_time_minutes |
|
alarm.sleep_memory[2] = on_duration_hours |
|
button.prepare_for_sleep() |
|
pin_alarm = alarm.pin.PinAlarm(pin=board.BOOT0, value=False, pull=True) |
|
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 60) |
|
display.set_indicator(Indicator.MOON) |
|
alarm.exit_and_deep_sleep_until_alarms(time_alarm, pin_alarm) |
|
|
|
|
|
time.sleep(0.01667) |
This should be fixed now. Please reach out if you are still having issues. Thank you for the detailed report!