-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
231 lines (198 loc) · 7.39 KB
/
tools.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
#tools.py
from machine import I2C, Pin
import DS3231
import config
import time
import math
i2c = I2C(sda = Pin(config.sda), scl=Pin(config.scl))
try:
ds = DS3231.DS3231(i2c)
print('RTC initiated')
except:
print('Problem with RTC')
def current_time():
try:
return ds.Time()[0], ds.Time()[1], ds.Time()[2]
except:
print('Cannot collect time')
return 0,0,0
def edit_config(old, new):
file = open('config.py')
data = file.read()
data = data.replace(old, new)
file.close()
file = open('config.py', 'w')
file.write(data)
file.close()
def save_color(old, new):
#print('led_color = '+str(old)+' changed in led_color = '+str(new))
edit_config('led_color = '+str(old),'led_color = '+str(new))
def limit(value, min, max):
if int(value) < int(min):
limitvalue = int(min)
elif int(value) > int(max):
limitvalue = int(max)
else:
limitvalue = int(value)
return limitvalue
def set_hour_back(display):
new_hour,minute,second = current_time()
new_hour -= 1
if new_hour < 0:
new_hour = 23
display.show_time(new_hour,minute)
ds.Time([new_hour,minute,0])
time.sleep(2)
def set_hour_forward(display):
new_hour,minute,second = current_time()
new_hour += 1
if new_hour > 23:
new_hour = 0
display.show_time(new_hour,minute)
ds.Time([new_hour,minute,0])
time.sleep(2)
def set_time(display):
wait_time_blinking = 0.2
# First set hours
button_back = Pin(config.button_back, Pin.IN, Pin.PULL_UP)
button_ok = Pin(config.button_ok, Pin.IN, Pin.PULL_UP)
button_next = Pin(config.button_next, Pin.IN, Pin.PULL_UP)
# Blink "HET IS"
for i in range(3):
display.ledstrip.reset()
time.sleep(wait_time_blinking)
display.show_prefix()
time.sleep(wait_time_blinking)
if button_ok.value() == config.invert_button_ok:
# Button is still pushed, wait another second
time.sleep(1)
if button_ok.value() == config.invert_button_ok:
# Button is still pushed, enter set color mode
set_color(display)
# color is set, return to main script (skip setting time)
return
print('Start set time mode')
hour,minute,second = current_time()
new_hour = hour
new_minute = 0
display.show_time(hour,0)
while button_ok.value() != config.invert_button_ok:
# While button OK not pressed
if button_next.value() == config.invert_button_next:
new_hour += 1
if new_hour > 23:
new_hour = 0
display.show_time(new_hour, 0, False)
print('Set time hours: ',new_hour, ':',new_minute)
time.sleep_ms(300)
if button_back.value() == config.invert_button_back:
new_hour -= 1
if new_hour < 0:
new_hour = 23
display.show_time(new_hour, 0, False)
print('Set time hours: ',new_hour, ':',0)
time.sleep_ms(300)
print('Set time in 5 minutes')
# Blink "HET IS [HOUR]"
for i in range(3):
display.ledstrip.reset()
time.sleep(wait_time_blinking)
display.show_time(new_hour, 0, False)
time.sleep(wait_time_blinking)
time.sleep(0.5)
while button_ok.value() != config.invert_button_ok:
# While button OK not pressed
if button_next.value() == config.invert_button_next:
new_minute += 5
if new_minute > 56:
new_minute = 0
new_hour += 1
if new_hour > 23:
new_hour = 0
display.show_time(new_hour,new_minute, False)
print('Set time 5 mins: ',new_hour, ':',new_minute)
time.sleep_ms(300)
if button_back.value() == config.invert_button_back:
new_minute -= 5
if new_minute < 0:
new_minute = 55
new_hour -= 1
if new_hour < 0:
new_hour = 23
display.show_time(new_hour,new_minute, False)
print('Set time 5 mins: ',new_hour, ':',new_minute)
time.sleep_ms(300)
print('Set minute leds')
for i in range(3):
display.minute_leds()
time.sleep(wait_time_blinking)
display.ledstrip.reset()
time.sleep(wait_time_blinking)
# Set minute leds
display.show_time(new_hour,new_minute, False)
minute_leds = 0
while button_ok.value() != config.invert_button_ok:
# While button OK not pressed
if button_next.value() == config.invert_button_next:
minute_leds += 1
if minute_leds >= 5:
minute_leds = 0
display.show_time(new_hour, new_minute+minute_leds, False)
print('Set minute: ',new_minute+minute_leds)
time.sleep_ms(300)
if button_back.value() == config.invert_button_back:
minute_leds -= 1
if minute_leds < 0:
minute_leds = 4
display.show_time(new_hour, new_minute+minute_leds, False)
print('Set minute: ',new_minute+minute_leds)
time.sleep_ms(300)
new_minutes = new_minute + minute_leds
# Write new time to RTC
ds.Time([new_hour,new_minutes,0])
display.ledstrip.reset()
time.sleep(1)
def set_color(display):
# set color values of leds (red, green and blue)
wait_time_blinking = 0.2
button_back = Pin(config.button_back, Pin.IN, Pin.PULL_UP)
button_ok = Pin(config.button_ok, Pin.IN, Pin.PULL_UP)
button_next = Pin(config.button_next, Pin.IN, Pin.PULL_UP)
old_color = config.led_color[:]
# Loop all three colors (r,g,b)
for i in range(3):
# Blink prefix and minute leds
display.ledstrip.reset()
for j in range(3):
display.show_prefix(i) # Blink in color
time.sleep(wait_time_blinking)
display.ledstrip.reset()
time.sleep(wait_time_blinking)
display.show_prefix()
display.show_minute_leds_in_color(i)
print('Set value r/g/b ' + str(i))
new_color = old_color[:]
while button_ok.value() != config.invert_button_ok:
if button_next.value() == config.invert_button_next:
new_color[i] += 5
if new_color[i] > 255:
new_color[i] = 255
save_color(old_color, new_color)
old_color = new_color[:]
display.ledstrip.renew_colors(new_color)
display.show_prefix()
display.show_minute_leds_in_color(i)
time.sleep_ms(50)
if button_back.value() == config.invert_button_back:
new_color[i] -= 5
if new_color[i] < 0:
new_color[i]= 0
save_color(old_color, new_color)
old_color = new_color[:]
display.ledstrip.renew_colors(new_color)
display.show_prefix()
display.show_minute_leds_in_color(i)
time.sleep_ms(50)
# Ready setting up time and color value, reset the clock
display.ledstrip.reset()
time.sleep(1)