forked from adafruit/Adafruit_Learning_System_Guides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
134 lines (115 loc) · 3.37 KB
/
code.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
# SPDX-FileCopyrightText: 2022 Eva Herrada for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
import board
from adafruit_lc709203f import LC709203F
from adafruit_pcf8523.pcf8523 import PCF8523
from simpleio import tone
import neopixel
from adafruit_led_animation.animation.rainbow import Rainbow
i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
rtc = PCF8523(i2c)
battery = LC709203F(i2c)
indicator = neopixel.NeoPixel(board.A1, 1)
LEDs = neopixel.NeoPixel(board.A2, 20)
LEDs.fill((0, 0, 0))
rainbow = Rainbow(LEDs, speed=0.1, period=2)
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
sleep = {
0: (22, 50),
1: (23, 8),
2: (22, 50),
3: (22, 50),
4: (22, 50),
5: None,
6: None,
}
wake = {
0: (11, 30),
1: (9, 50),
2: (9, 50),
3: (9, 50),
4: (9, 50),
5: (9, 50),
6: (11, 30),
}
BPM = 160
ringtone = [
(330, 0.5),
(294, 0.5),
(185, 1),
(208, 1),
(277, 0.5),
(247, 0.5),
(147, 1),
(165, 1),
(247, 0.5),
(220, 0.5),
(139, 1),
(165, 1),
(220, 2),
]
SET_DATE = False # Set this to True on first run and False for subsequent runs
if SET_DATE:
# Make sure to set this to the current date and time before using
YEAR = 2022
MONTH = 3
DAY = 21
HOUR = 16
MINUTE = 54
SEC = 0
WDAY = 1 # 0 Sunday, 1 Monday, etc.
t = time.struct_time((YEAR, MONTH, DAY, HOUR, MINUTE, SEC, WDAY, -1, -1))
print("Setting time to:", t) # uncomment for debugging
rtc.datetime = t
print()
on_always = True # Set to False for animation only to be on when alarm rings
indicate = True
start = time.monotonic()
wait = 1
while True:
if on_always:
rainbow.animate()
if time.monotonic() - start > wait:
start = time.monotonic()
wait = 1
t = rtc.datetime
bat = min(max(int(battery.cell_percent), 0), 100)
g = int((bat / 100) * 255)
r = int((1 - (bat / 100)) * 255)
if bat >= 15:
indicator.fill([r, g, 0])
if bat < 15:
if indicate:
indicator.fill([0, 0, 0])
indicate = False
else:
indicator.fill([r, g, 0])
indicate = True
print(f"Date: {days[t.tm_wday]} {t.tm_mday}/{t.tm_mon}/{t.tm_year}")
print(f"Time: {t.tm_hour}:{t.tm_min}:{t.tm_sec}")
print(f"Batt: {bat}%\n")
night = sleep[t.tm_wday]
morning = wake[t.tm_wday]
if night:
if night[0] == t.tm_hour and night[1] == t.tm_min:
for i in ringtone:
print(i[0])
tone(board.A0, i[0], i[1] * (60 / BPM))
wait = 60
if not on_always:
while time.monotonic() - start < 5:
rainbow.animate()
rainbow.fill([0, 0, 0])
if morning:
if morning[0] == t.tm_hour and morning[1] == t.tm_min:
for i in ringtone:
print(i[0])
tone(board.A0, i[0], i[1] * (60 / BPM))
wait = 60
if not on_always:
while time.monotonic() - start < 5:
rainbow.animate()
rainbow.fill([0, 0, 0])