|
| 1 | +# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import time |
| 5 | +import board |
| 6 | +import simpleio |
| 7 | +import adafruit_ds3231 |
| 8 | +from adafruit_ticks import ticks_ms, ticks_add, ticks_diff |
| 9 | +from adafruit_ht16k33 import segments |
| 10 | +from adafruit_debouncer import Button |
| 11 | +from adafruit_seesaw import seesaw, rotaryio, digitalio |
| 12 | + |
| 13 | +# min and max display brightness range |
| 14 | +# value must be 0.0 to 1.0 |
| 15 | +max_brightness = 1 |
| 16 | +min_brightness = 0.01 |
| 17 | +# weekday hours to have clock on max brightness |
| 18 | +# (24-hour time) |
| 19 | +weekday_wakeup = 8 |
| 20 | +weekday_sleep = 21 |
| 21 | +# weekend hours to have clock on max brightness |
| 22 | +# (24-hour time) |
| 23 | +weekend_wakeup = 9 |
| 24 | +weekend_sleep = 23 |
| 25 | + |
| 26 | +i2c = board.STEMMA_I2C() |
| 27 | + |
| 28 | +rtc = adafruit_ds3231.DS3231(i2c) |
| 29 | +seesaw = seesaw.Seesaw(i2c, addr=0x36) |
| 30 | +seesaw.pin_mode(24, seesaw.INPUT_PULLUP) |
| 31 | +ss_pin = digitalio.DigitalIO(seesaw, 24) |
| 32 | +button = Button(ss_pin, long_duration_ms=1000) |
| 33 | + |
| 34 | +encoder = rotaryio.IncrementalEncoder(seesaw) |
| 35 | +last_position = 0 |
| 36 | + |
| 37 | +# pylint: disable-msg=using-constant-test |
| 38 | +if False: # change to True if you want to set the time! |
| 39 | + # year, mon, date, hour, min, sec, wday, yday, isdst |
| 40 | + t = time.struct_time((2024, 1, 25, 15, 7, 0, 3, -1, -1)) |
| 41 | + # you must set year, mon, date, hour, min, sec and weekday |
| 42 | + # yearday is not supported, isdst can be set but we don't do anything with it at this time |
| 43 | + print("Setting time to:", t) # uncomment for debugging |
| 44 | + rtc.datetime = t |
| 45 | + print() |
| 46 | +# pylint: enable-msg=using-constant-test |
| 47 | + |
| 48 | +display = segments.BigSeg7x4(i2c) |
| 49 | + |
| 50 | +display.fill(0) |
| 51 | +display.brightness = max_brightness |
| 52 | + |
| 53 | +display.colon = True |
| 54 | + |
| 55 | +def clock_conversion(h, m, set_brightness): |
| 56 | + # pylint: disable-msg=simplifiable-if-expression |
| 57 | + am_pm = False if h < 12 else True |
| 58 | + hour_12 = h if h <= 12 else h - 12 |
| 59 | + if hour_12 == 0: |
| 60 | + hour_12 = 12 |
| 61 | + display.print(f"{(hour_12):02}:{m:02}") |
| 62 | + display.ampm = am_pm |
| 63 | + if set_brightness: |
| 64 | + if awake_hours[0] <= h <= awake_hours[1] - 1: |
| 65 | + display.brightness = max_brightness |
| 66 | + elif h is awake_hours[0] - 1: |
| 67 | + bright = simpleio.map_range(m, 0, 59, min_brightness, max_brightness) |
| 68 | + display.brightness = bright |
| 69 | + elif h is awake_hours[1]: |
| 70 | + bright = simpleio.map_range(m, 0, 59, max_brightness, min_brightness) |
| 71 | + display.brightness = bright |
| 72 | + else: |
| 73 | + display.brightness = min_brightness |
| 74 | + else: |
| 75 | + display.brightness = max_brightness |
| 76 | + |
| 77 | +clock_clock = ticks_ms() |
| 78 | +clock_timer = 1 * 1000 |
| 79 | +clock_mode = True |
| 80 | +set_hour = True |
| 81 | +power_up = True |
| 82 | +hour = 0 |
| 83 | +minute = 0 |
| 84 | + |
| 85 | +while True: |
| 86 | + |
| 87 | + if clock_mode: |
| 88 | + button.update() |
| 89 | + if ticks_diff(ticks_ms(), clock_clock) >= clock_timer: |
| 90 | + t = rtc.datetime |
| 91 | + if t.tm_wday in range(5, 6): |
| 92 | + awake_hours = [weekend_wakeup, weekend_sleep] |
| 93 | + else: |
| 94 | + awake_hours = [weekday_wakeup, weekday_sleep] |
| 95 | + if t.tm_sec < 1 or power_up: |
| 96 | + power_up = False |
| 97 | + clock_conversion(t.tm_hour, t.tm_min, True) |
| 98 | + clock_clock = ticks_add(clock_clock, clock_timer) |
| 99 | + else: |
| 100 | + button.update() |
| 101 | + position = -encoder.position |
| 102 | + if position != last_position: |
| 103 | + if position > last_position: |
| 104 | + if set_hour: |
| 105 | + hour = (hour + 1) % 24 |
| 106 | + else: |
| 107 | + minute = (minute + 1) % 60 |
| 108 | + else: |
| 109 | + if set_hour: |
| 110 | + hour = (hour - 1) % 24 |
| 111 | + else: |
| 112 | + minute = (minute - 1) % 60 |
| 113 | + clock_conversion(hour, minute, False) |
| 114 | + last_position = position |
| 115 | + if button.short_count: |
| 116 | + set_hour = not set_hour |
| 117 | + # toggling dots with not did not seem to work consistantly |
| 118 | + # so setting manually |
| 119 | + if set_hour: |
| 120 | + display.top_left_dot = True |
| 121 | + display.bottom_left_dot = False |
| 122 | + else: |
| 123 | + display.top_left_dot = False |
| 124 | + display.bottom_left_dot = True |
| 125 | + if button.long_press: |
| 126 | + if not clock_mode: |
| 127 | + t = rtc.datetime |
| 128 | + new_t = time.struct_time((t.tm_year, t.tm_mon, t.tm_mday, |
| 129 | + hour, minute, t.tm_sec, t.tm_wday, |
| 130 | + t.tm_yday, t.tm_isdst)) |
| 131 | + print("Setting time to:", new_t) |
| 132 | + rtc.datetime = new_t |
| 133 | + clock_clock = ticks_add(clock_clock, clock_timer) |
| 134 | + power_up = True |
| 135 | + display.top_left_dot = False |
| 136 | + display.bottom_left_dot = False |
| 137 | + else: |
| 138 | + set_hour = True |
| 139 | + t = rtc.datetime |
| 140 | + hour = t.tm_hour |
| 141 | + minute = t.tm_min |
| 142 | + clock_mode = not clock_mode |
| 143 | + display.blink_rate = not display.blink_rate |
0 commit comments