This repository was archived by the owner on Feb 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
195 lines (165 loc) · 4.31 KB
/
Copy pathmain.py
File metadata and controls
195 lines (165 loc) · 4.31 KB
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
# pyright: reportAttributeAccessIssue=false, reportMissingImports=false
import sys
from pye import pye
import machine
from machine import Pin
import time
from time import sleep
import daylightsaving as dls
from neopixel import NeoPixel
# Daylight Saving starts on last Sunday of March at 1AM
# 0 = Northern hemisphere
# 0 = Last week of the month
# 3 = March
# 6 = Sunday
# 1 = 1AM
# Time offset = 60 mins (GMT+2)
CEST = dls.DaylightSavingPolicy(0, 0, 3, 6, 1, 120)
# Daylight Saving ends on last Snday of October at 2AM
# 0 = Northern hemisphere
# 0 = Last week of the month
# 10 = October
# 6 = Sunday
# 2 = 2AM
# Time offset = 0 mins (GMT+1)
CET = dls.StandardTimePolicy(0, 0, 10, 6, 2, 60)
# Create a DaylightSaving object passing in policies
ds = dls.DaylightSaving(CEST, CET)
def localtime():
return time.localtime(ds.localtime(time.time()))
print("Waiting 3s")
sleep(1)
print("DONE")
print("Connecting to wifi", end="")
import network
network.hostname('ws2812clock')
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Nerdberg", "ADD_WIFI_PASSWORD")
print("...", end="")
for i in range(20):
if wlan.isconnected():
break
print(".", end="")
sleep(1)
if wlan.isconnected():
print("DONE")
else:
print("FAILED")
print("Resetting...")
sleep(3)
machine.soft_reset()
ntp_synced = False
print("Initial NTP sync...", end="")
for i in range(5):
try:
import ntptime
ntptime.settime()
print("DONE")
ntp_synced = True
break
except:
print(".", end="")
sleep(1)
if not ntp_synced:
print("FAILED")
print("Resetting...")
sleep(3)
machine.soft_reset()
try:
import webrepl_cfg
print("WebREPL Password: " + webrepl_cfg.PASS)
except:
pass
import webrepl
webrepl.start()
# 25
led = Pin("LED", Pin.OUT)
led.high()
num_leds = 41
ws2812_pin = Pin(28, Pin.OUT)
np = NeoPixel(ws2812_pin, num_leds)
led.low()
def led_address(digit_index, digit):
if isinstance(digit, int) and digit < 0:
return None
elif digit_index == 4:
return 0 if digit == 0 else (10 - digit)
elif digit_index == 3:
return 19 if digit == 0 else ( 9 + digit)
elif digit_index == 2:
return 20 if digit == ':' else None
elif digit_index == 1:
return 30 if digit == 0 else (20 + digit)
elif digit_index == 0:
return 31 if digit == 0 else (41 - digit)
def display(s):
for i in range(num_leds):
np[i] = (0,0,0)
for i in range(5):
digit = s[i]
if digit == ' ':
digit = -1
elif i != 2:
digit = int(digit)
a = led_address(i, digit)
if a is not None:
np[a] = (255,255,255)
np.write()
def test():
for i in range(num_leds):
for j in range(num_leds):
np[j] = (0,0,0)
np[i] = (255,255,255)
led.high()
np.write()
led.low()
sleep(0.05)
DIGIT_SYMBOLS_ZERO = list(' 1234567890')
DIGIT_SYMBOLS = list('1234567890')
def update_with_animation(new, old):
for n, o, i in zip(new, old, range(len(new))):
if n != o:
step = -1
digits = DIGIT_SYMBOLS
if i == 0:
if n == ' ':
step = 1
digits = DIGIT_SYMBOLS_ZERO
elif o == ' ':
digits = DIGIT_SYMBOLS_ZERO
elif i == 2:
continue
# effect
oldl = list(old)
ni = digits.index(n)
oi = digits.index(o)
while ni != oi:
oi += step
oi %= len(digits)
oldl[i] = digits[oi]
old = ''.join(oldl)
display(old)
sleep(0.05)
def mainloop():
last_time = "33:33"
display(last_time)
while True:
if sys.implementation.name == 'micropython' and sys.platform != 'linux':
import ntptime
try:
ntptime.settime()
except:
print("NTP failed")
lt = localtime()
tf = "%2i:%02i" % lt[3:5]
if last_time == tf:
sleep(1)
else:
#display(tf)
update_with_animation(tf, last_time)
# effect
seconds = lt[5]
sleep(60 - seconds)
last_time = tf
mainloop()