-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_clock.py
234 lines (201 loc) · 5.92 KB
/
binary_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
rtc = machine.RTC()
import time
from machine import Pin
import bluetooth
from ble_simple_peripheral import BLESimplePeripheral
from machine import Pin
# Create a Pin object for the onboard LED, configure it as an output
led = Pin("LED", Pin.OUT)
# Initialize the LED state to 0 (off)
led_state = 0
HOLD = machine.Pin(19, machine.Pin.IN, machine.Pin.PULL_UP)
SLOW = machine.Pin(20, machine.Pin.IN, machine.Pin.PULL_UP)
FAST = machine.Pin(21, machine.Pin.IN, machine.Pin.PULL_UP)
gnow=0
delay = 100
DEBUG = False
NOON = {
"year": 2023,
"month": 1,
"day": 1,
"weekday": 1,
"hours": 12,
"mins": 0,
"secs": 0,
"subsecs": 0,
}
def init_ble():
ble = bluetooth.BLE()
sp = BLESimplePeripheral(ble)
return [ble, sp]
def init_gpio(pins):
arr = []
for i in range(0,len(pins)):
arr.append(Pin(pins[i], Pin.OUT))
return arr
# Define a callback function to handle received data
def on_rx(data):
global gnow
global delay
global DEBUG
try:
print("Data received: ", data) # Print the received data
words = data.decode('ascii').strip().split(" ", 1)
print(words)
if words[0] == "time":
new_time = words[1].strip()
gnow = dup_datetime(NOON)
gnow["hours"] = int(new_time[0:2])
gnow["mins"] = int(new_time[2:4])
gnow["secs"] = int(new_time[4:6])
print("new time", gnow)
set_datetime(gnow)
gnow = get_datetime()
if words[0] == "delay":
delay = int(words[1].strip())
if words[0] == "debug":
DEBUG = words[1] == "on"
except:
print("ERROR... resetting clock")
set_datetime(NOON)
def dup_datetime(now):
return dict(now)
def set_datetime(now):
prev_now = prev_datetime(now) ### fixes a quirk in RTC
tup = to_tuple(prev_now)
rtc.datetime(tup)
def to_tuple(now):
return (now["year"], now["month"], now["day"], now["weekday"], now["hours"], now["mins"], now["secs"], now["subsecs"])
def get_datetime():
now=rtc.datetime()
#print(now)
dict = {
"year": now[0],
"month": now[1],
"day": now[2],
"weekday": now[3],
"hours": now[4],
"mins": now[5],
"secs": now[6],
"subsecs": now[7],
}
return dict;
def next_datetime(_now, dh=0, dm=0, ds=1):
now = dup_datetime(_now)
now["secs"] += ds
now["mins"] += dm
now["hours"] += dh
if now["secs"] > 59:
now["mins"] += now["secs"] // 60
now["secs"] = now["secs"] % 60
if now["mins"] > 59:
now["hours"] += now["mins"] // 60
now["mins"] = now["mins"] % 60
if now["hours"] > 23:
now["hours"] = now["hours"] % 24
return now;
def prev_datetime(_now):
now = dup_datetime(_now)
now["secs"] -= 1
if now["secs"] < 0:
now["secs"] = 59
now["mins"] -= 1
if now["mins"] < 0:
now["mins"] = 59
now["hours"] -= 1
if now["hours"] < 0:
now["hours"] = 23
return now;
def split_int(n, d):
a = int(n/d)
b = n % d
return [a,b]
def set_leds(bits, leds):
for i in range(0, len(leds)):
leds[i].value(bits[i])
def main():
global gnow
global delay
global DEBUG
global led_state
[ble, sp] = init_ble();
set_datetime(NOON)
print(rtc.datetime())
print("starting...")
secs_lo_leds = init_gpio([0,1,2,3])
secs_hi_leds = init_gpio([4,5,6])
mins_lo_leds = init_gpio([7,8,9,10])
mins_hi_leds = init_gpio([11,12,13])
hours_leds = init_gpio([14,15,16,17])
gnow = get_datetime()
DEFAULT_DELAY = 100
delay = DEFAULT_DELAY
reset_secs = False
while True:
if sp.is_connected(): # Check if a BLE connection is established
sp.on_write(on_rx) # Set the callback function for data reception
gnow = get_datetime()
if not DEBUG:
delay = DEFAULT_DELAY
else:
gnow = next_datetime(gnow);
if HOLD.value():
hold_time = get_datetime()
set_datetime(hold_time)
if FAST.value() == 0:
print(gnow)
gnow = next_datetime(gnow, 0, 0, 59)
set_datetime(gnow)
delay = 20
reset_secs = "fast"
elif reset_secs == "fast":
# gnow["secs"] = 0
# gnow["mins"] = 0
set_datetime(gnow)
print(reset_secs, gnow)
reset_secs = False
if SLOW.value() == 0:
gnow = next_datetime(gnow, 0, 0, 1)
set_datetime(gnow)
delay = 1
reset_secs = "slow"
elif reset_secs == "slow":
# gnow["secs"] = 0
set_datetime(gnow)
print(reset_secs, gnow)
reset_secs = False
if delay: time.sleep_ms(delay)
hours = gnow["hours"] % 12
mins = gnow["mins"]
secs = gnow["secs"]
if hours == 0: hours = 12
[mins_hi, mins_lo] = split_int(mins, 10)
[secs_hi, secs_lo] = split_int(secs, 10)
bin_hours = to_binary(hours)
bin_mins_hi = to_binary(mins_hi)
bin_mins_lo = to_binary(mins_lo)
bin_secs_hi = to_binary(secs_hi)
bin_secs_lo = to_binary(secs_lo)
#print(bin_hours)
set_leds(bin_hours, hours_leds)
set_leds(bin_mins_hi, mins_hi_leds)
set_leds(bin_mins_lo, mins_lo_leds)
set_leds(bin_secs_hi, secs_hi_leds)
set_leds(bin_secs_lo, secs_lo_leds)
led_state = not led_state
led.value(led_state)
#print("...")
#rtc.datetime(newtime)
#print("?",now)
def to_binary(n):
a = n % 2
n = n >> 1
b = n % 2
n = n >> 1
c = n % 2
n = n >> 1
d = n % 2
arr = [d, c, b, a]
arr.reverse()
return arr
main()