-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
356 lines (250 loc) · 9.57 KB
/
main.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from machine import Pin, I2C, RTC, PWM
import time
import RGB1602
from random import randint
# Global variables
SETTINGS = [
"Set alarm",
"Show message"
]
MESSAGES = [
"Test message!!"
]
# Buzzer variables - PWM
BUZZ_FREQ = 4000
BUZZ_DUTY = 65536 / 2
BUZZ_LENGTH = 10
# Initiate main clock, pico rtc, lcd, buzzer and buttons
rtc = machine.RTC()
rtc.datetime((2023, 2, 26, 3, 22, 15, 30, 50))
lcd = RGB1602.RGB1602(16, 2)
buzzer = PWM(Pin(26))
# Changes background color ---> green wire
buttonRGB = Pin(12, Pin.IN)
# Select menu entry ---> yellow wire
buttonMulti = Pin(15, Pin.IN)
# Enters and navigates menu entries ---> white wire
buttonSettings = Pin(7, Pin.IN)
""" Initiate rtc module (STILL TESTING)
i2c = machine.I2C(1, sda=machine.Pin(2),scl=machine.Pin(3),freq=400000)
rtc = DS3231(i2c) """
def setColorRed(lcd):
""" Change lcd background color to red """
lcd.setRGB(255, 0, 0)
def setColorBlue(lcd):
""" Change lcd background color to blue """
lcd.setRGB(0, 0, 255)
def setColorGreen(lcd):
""" Change lcd background color to green """
lcd.setRGB(0, 255, 0)
def changeRGB(lcd):
""" Change lcd background color to one of the following 3:
Blue, Red, Green """
rnd = randint(1, 3)
if rnd == 1:
setColorBlue(lcd)
elif rnd == 2:
setColorGreen(lcd)
elif rnd == 3:
setColorRed(lcd)
def rgbHandler(pin):
""" Routine for buttonRGB irq """
global buttonRGB_state
# Prevent multiple routines simultaneously
buttonRGB.irq(handler=None)
print(" ---------- RGB TRIGGER ROUTINE INIT ---------- ")
if buttonRGB.value() == 0 and buttonRGB_state == 1:
changeRGB(lcd)
time.sleep(0.2)
print(" *** Color changed *** ")
buttonRGB.irq(handler=rgbHandler)
print(" ---------- RGB TRIGGER ROUTINE END ---------- \n")
def settingsHandler(pin):
""" Routine for buttonSettings irq
- The 0.2 sleeps are to prevent multiple
reads on the same button press.
- There are 2 timers in this routine which will
end this routine if no buttons are pressed:
1) mainTimer will be activated in the main menu
2) menuTimer will be activated after entering a menu entry """
global buttonSettings_state
global alarmHour
global alarmMinute
buttonSettings.irq(handler=None)
time.sleep(0.2)
print(" ---------- SETTINGS TRIGGER ROUTINE INIT ---------- ")
lcd.clear()
mainTimer = 100
menuTimer = 100
setting = 0
while mainTimer > 0:
# Stops the interrupt once the last setting is reached
# and the main program is resumed
if setting == len(SETTINGS):
break
lcd.clear()
lcd.setCursor(0, 0)
lcd.printout(SETTINGS[setting])
# Changes for the next menu entry
if buttonSettings.value() == 0:
lcd.clear()
time.sleep(0.2)
lcd.setCursor(0, 0)
lcd.printout(SETTINGS[setting])
setting += 1
mainTimer = 100
continue
# Enters the current entry and reset the 10s timer
if buttonMulti.value() == 0:
time.sleep(0.2)
# setting = 0 --> set alarm function
# set hour first, then minutes - one loop for each
# After the minute loop, display the alarm timer set
if setting == 0:
while menuTimer > 0:
lcd.clear()
lcd.setCursor(0, 0)
lcd.printout(f"Hour: {formatNumber(alarmHour)}")
if buttonSettings.value() == 0:
alarmHour += 1
if alarmHour > 23:
alarmHour = 0
time.sleep(0.2)
menuTimer = 100
if buttonMulti.value() == 0:
menuTimer = 100
break
menuTimer -= 1
time.sleep(0.1)
# Prevents a second read after hour assignment
time.sleep(0.2)
while menuTimer > 0:
lcd.clear()
lcd.setCursor(0, 0)
lcd.printout(f"Minute: {formatNumber(alarmMinute)}")
if buttonSettings.value() == 0:
alarmMinute += 1
if alarmMinute > 59:
alarmMinute = 0
time.sleep(0.2)
menuTimer = 100
if buttonMulti.value() == 0:
menuTimer = 100
lcd.setCursor(0, 0)
lcd.printout("Alarm set to:")
lcd.setCursor(0, 1)
lcd.printout(
f"{formatNumber(alarmHour)}:{formatNumber(alarmMinute)}")
time.sleep(2)
break
menuTimer -= 1
# Reset the alarm variables in case no buttons are pressed
# just before exiting the menuTimer loop
if menuTimer == 1:
alarmHour = 0
alarmMinute = 0
time.sleep(0.1)
break
# Prints a random message from the MESSAGES variable
elif setting == 1:
index = randint(0, len(MESSAGES0))
lcd.clear()
lcd.setCursor(0, 0)
lcd.printout(MESSAGES0[index])
time.sleep(5)
break
mainTimer -= 1
time.sleep(0.1)
buttonSettings.irq(handler=settingsHandler,
trigger=machine.Pin.IRQ_FALLING)
print(" ---------- SETTINGS TRIGGER ROUTINE END ---------- \n")
def formatDate():
""" Function to return a date string with format dd/MM/yyyy """
if rtc.datetime()[2] < 10 and rtc.datetime()[1] < 10:
date = (
f"0{rtc.datetime()[2]}/0{rtc.datetime()[1]}/{rtc.datetime()[0]}")
elif rtc.datetime()[2] < 10 and rtc.datetime()[1] >= 10:
date = (
f"0{rtc.datetime()[2]}/{rtc.datetime()[1]}/{rtc.datetime()[0]}")
elif rtc.datetime()[2] >= 10 and rtc.datetime()[1] < 10:
date = (
f"{rtc.datetime()[2]}/0{rtc.datetime()[1]}/{rtc.datetime()[0]}")
elif rtc.datetime()[2] >= 10 and rtc.datetime()[1] >= 10:
date = (f"{rtc.datetime()[2]}/{rtc.datetime()[1]}/{rtc.datetime()[0]}")
return date
def formatTime():
""" Function to return a time string with format hh:mm:ss """
timeString = ""
if rtc.datetime()[4] < 10:
timeString += "0" + str(rtc.datetime()[4]) + ":"
else:
timeString += str(rtc.datetime()[4]) + ":"
if rtc.datetime()[5] < 10:
timeString += "0" + str(rtc.datetime()[5]) + ":"
else:
timeString += str(rtc.datetime()[5]) + ":"
if rtc.datetime()[6] < 10:
timeString += "0" + str(rtc.datetime()[6])
else:
timeString += str(rtc.datetime()[6])
return timeString
def formatNumber(number):
""" Function to return a formatted string for hour/minute/second """
value = 0
if number < 10:
value = f"0{number}"
else:
value = str(number)
return value
# Assign irq's
buttonRGB.irq(handler=rgbHandler, trigger=machine.Pin.IRQ_FALLING)
buttonSettings.irq(handler=settingsHandler, trigger=machine.Pin.IRQ_FALLING)
# Initiate the LCD background color and buttons' current state
setColorGreen(lcd)
buttonRGB_state = buttonRGB.value()
buttonSettings_state = buttonSettings.value()
buttonMulti_state = buttonMulti.value()
# Alarm variables and initial buzzer config
alarmHour = 0
alarmMinute = 0
buzzer.freq(BUZZ_FREQ)
buzzer.duty_u16(0)
def main():
global alarmHour
global alarmMinute
global BUZZ_LENGTH
while True:
# Update the button values at every clock pulse
# which will be used in the event of an irq
buttonRGB_state = buttonRGB.value()
buttonSettings_state = buttonSettings.value()
buttonMulti_state = buttonMulti.value()
# Update the clock display with the RTC time
lcd.setCursor(0, 0)
lcd.clear()
lcd.printout(formatTime())
lcd.setCursor(0, 1)
lcd.printout(formatDate())
# Activate buzzer if alarm is triggered
if rtc.datetime()[4] == alarmHour and rtc.datetime()[5] == alarmMinute:
# Checks for button press for an early alarm stop
if buttonMulti.value() == 0:
buzzer.duty_u16(0)
alarmHour = 0
alarmMinute = 0
BUZZ_LENGTH = 10
continue
# Alternates between low and high values for alarm sound until timer finishes
if BUZZ_LENGTH > 0:
if BUZZ_LENGTH % 2 == 0:
buzzer.duty_u16(int(BUZZ_DUTY))
else:
buzzer.duty_u16(0)
BUZZ_LENGTH -= 1
else:
buzzer.duty_u16(0)
alarmHour = 0
alarmMinute = 0
BUZZ_LENGTH = 10
time.sleep(1)
main()