-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.py
137 lines (110 loc) · 3.84 KB
/
clock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import time
import machine
import badger2040
# We're going to keep the badger on, so slow down the system clock if on battery
badger2040.system_speed(badger2040.SYSTEM_SLOW)
TEXT_SCALING = 3
LINE_WIDTH = 10
HALF_HEIGHT = int(badger2040.HEIGHT / 2)
RTC = machine.RTC()
BADGER = badger2040.Badger2040()
BADGER.update_speed(badger2040.UPDATE_NORMAL)
BADGER.font('sans')
# Set up the buttons
BUTTON_DOWN = machine.Pin(badger2040.BUTTON_DOWN, machine.Pin.IN, machine.Pin.PULL_DOWN)
BUTTON_UP = machine.Pin(badger2040.BUTTON_UP, machine.Pin.IN, machine.Pin.PULL_DOWN)
BUTTON_A = machine.Pin(badger2040.BUTTON_A, machine.Pin.IN, machine.Pin.PULL_DOWN)
BUTTON_B = machine.Pin(badger2040.BUTTON_B, machine.Pin.IN, machine.Pin.PULL_DOWN)
BUTTON_C = machine.Pin(badger2040.BUTTON_C, machine.Pin.IN, machine.Pin.PULL_DOWN)
CURSORS = ["hour", "minute"]
set_clock = False
cursor = 0
last = 0
# Button handling function
def button(pin):
global last, set_clock, cursor, hour, minute, last_minute
time.sleep(0.01)
if not pin.value():
return
if BUTTON_A.value() and BUTTON_C.value():
machine.reset()
adjust = 0
changed = False
if pin == BUTTON_B:
set_clock = not set_clock
changed = True
if set_clock:
BADGER.update_speed(badger2040.UPDATE_TURBO)
if not set_clock:
RTC.datetime((1970, 1, 1, 0, hour, minute, 0, 0))
BADGER.update_speed(badger2040.UPDATE_NORMAL)
if set_clock:
if pin == BUTTON_C:
cursor += 1
cursor %= len(CURSORS)
if pin == BUTTON_A:
cursor -= 1
cursor %= len(CURSORS)
if pin == BUTTON_UP:
adjust = 1
if pin == BUTTON_DOWN:
adjust = -1
if CURSORS[cursor] == "hour":
hour += adjust
hour %= 12
if hour == 0:
hour = 12
if CURSORS[cursor] == "minute":
minute += adjust
minute %= 60
if set_clock or changed:
draw_clock()
# Register the button handling function with the buttons
BUTTON_DOWN.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
BUTTON_UP.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
BUTTON_A.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
BUTTON_B.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
BUTTON_C.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
def x_with_offset(text, x, width, scaling):
text_width = BADGER.measure_text(text, scaling)
text_offset = int(x + (width / 2) - (text_width / 2))
return text_offset
def draw_background():
background = bytearray(int(296*128/8))
open("background.bin", "r").readinto(background)
BADGER.image(background)
def draw_centered_text(str, x_pos, y_pos, width, scaling):
str_pos = x_with_offset(str, x_pos, width, scaling)
BADGER.text(str, str_pos, y_pos, scaling)
def draw_clock():
hour_str = str(hour)
minute_str = "{:02}".format(minute)
draw_background()
BADGER.pen(15)
BADGER.thickness(LINE_WIDTH)
draw_centered_text(hour_str, 9, HALF_HEIGHT, 134, TEXT_SCALING)
draw_centered_text(minute_str, 155, HALF_HEIGHT, 134, TEXT_SCALING)
BADGER.pen(0)
BADGER.thickness(3)
BADGER.line(10, HALF_HEIGHT, 142, HALF_HEIGHT)
BADGER.line(155, HALF_HEIGHT, 286, HALF_HEIGHT)
if set_clock:
BADGER.pen(8)
if CURSORS[cursor] == "hour":
BADGER.line(10, 96, 142, 96)
if CURSORS[cursor] == "minute":
BADGER.line(155, 96, 286, 96)
BADGER.update()
_, _, _, _, hour, minute, _, _ = RTC.datetime()
last_minute = 61
while True:
if not set_clock:
_, _, _, _, hour, minute, _, _ = RTC.datetime()
if hour > 12:
hour %= 12
if hour == 0:
hour = 12
if minute != last_minute:
draw_clock()
last_minute = minute
time.sleep(1)