Skip to content

Commit efc25b4

Browse files
authored
Create code.py
1 parent adaec21 commit efc25b4

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

  • CircuitPython_Simple_Wordclock
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Vertical Word Clock using the Adafruit Feather M4 and
2+
# the Adafruit DS3231 real-time clock FeatherWing
3+
4+
import time
5+
import board
6+
import busio as io
7+
import digitalio
8+
import adafruit_ds3231
9+
import neopixel
10+
11+
i2c = io.I2C(board.SCL, board.SDA)
12+
13+
# Create the RTC instance:
14+
rtc = adafruit_ds3231.DS3231(i2c)
15+
16+
LED13 = digitalio.DigitalInOut(board.D13)
17+
LED13.direction = digitalio.Direction.OUTPUT
18+
19+
pixel_pin = board.D5
20+
num_pixels = 21
21+
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.8)
22+
pixels.fill((0, 0, 0))
23+
COLOR = (0, 200, 0) # Green
24+
25+
# Bitmap values for each value. These can be OR'ed together
26+
THREE = 0b00000000000000000001
27+
EIGHT = 0b00000000000000000010
28+
ELEVEN = 0b00000000000000000100
29+
TWO = 0b00000000000000001000
30+
SIX = 0b00000000000000010000
31+
FOUR = 0b00000000000000100000
32+
SEVEN = 0b00000000000001000000
33+
NOON = 0b00000000000010000000
34+
TEN = 0b00000000000100000000
35+
ONE = 0b00000000001000000000
36+
FIVE = 0b00000000010000000000
37+
MIDNIGHT = 0b00000000100000000000
38+
NINE = 0b00000001000000000000
39+
PAST = 0b00000010000000000000
40+
TO = 0b00000100000000000000
41+
FIVEMIN = 0b00001000000000000000
42+
QUARTER = 0b00010000000000000000
43+
TENMIN = 0b00100000000000000000
44+
HALF = 0b01000000000000000000
45+
TWENTY = 0b10000000000000000000
46+
47+
def writetime(the_hr, the_min): # Pass in hour and minute, return LED bitmask
48+
value = 0b00000000000000000000
49+
if (the_hr == 24) and (the_min == 0): # Special cases: Midnight and Noon
50+
return MIDNIGHT
51+
if (the_hr == 12) and (the_min == 0):
52+
return NOON
53+
# set minute
54+
if (the_min > 4) and (the_min < 10):
55+
value = value | FIVEMIN
56+
if (the_min > 9) and (the_min < 15):
57+
value = value | TENMIN
58+
if (the_min > 14) and (the_min < 20):
59+
value = value | QUARTER
60+
if (the_min > 19) and (the_min < 25):
61+
value = value | TWENTY
62+
if (the_min > 25) and (the_min < 30):
63+
value = value | TWENTY | FIVEMIN
64+
if (the_min > 29) and (the_min < 35):
65+
value = value | HALF
66+
if (the_min > 34) and (the_min < 40):
67+
value = value | TWENTY | FIVEMIN
68+
if (the_min > 39) and (the_min < 45):
69+
value = value | TWENTY
70+
if (the_min > 44) and (the_min < 50):
71+
value = value | QUARTER
72+
if (the_min > 49) and (the_min < 55):
73+
value = value | TENMIN
74+
if the_min > 54:
75+
value = value | FIVEMIN
76+
# before or after
77+
if the_min <= 30:
78+
value = value | PAST
79+
else:
80+
the_hr = the_hr + 1 # for the TO case
81+
value = value | TO
82+
# set hour
83+
if the_hr > 12:
84+
the_hr = the_hr - 12 # Convert 24 hour format to 12 hour
85+
if the_hr == 1:
86+
value = value | ONE
87+
if the_hr == 2:
88+
value = value | TWO
89+
if the_hr == 3:
90+
value = value | THREE
91+
if the_hr == 4:
92+
value = value | FOUR
93+
if the_hr == 5:
94+
value = value | FIVE
95+
if the_hr == 6:
96+
value = value | SIX
97+
if the_hr == 7:
98+
value = value | SEVEN
99+
if the_hr == 8:
100+
value = value | EIGHT
101+
if the_hr == 9:
102+
value = value | NINE
103+
if the_hr == 10:
104+
value = value | TEN
105+
if the_hr == 11:
106+
value = value | ELEVEN
107+
if the_hr == 0:
108+
value = value | MIDNIGHT
109+
if the_hr == 12:
110+
value = value | NOON
111+
return value
112+
# end def
113+
114+
# Main loop
115+
LEDstate = 0
116+
117+
while True:
118+
t = rtc.datetime
119+
# print("The date is {} {}/{}/{}".format(days[int(t.tm_wday)],
120+
# t.tm_mday, t.tm_mon, t.tm_year))
121+
# print("The time is {}:{:02}:{:02}".format(t.tm_hour, t.tm_min, t.tm_sec))
122+
hour = t.tm_hour
123+
minute = t.tm_min
124+
second = t.tm_sec
125+
if second == 59:
126+
print("The time is {}:{:02}".format(t.tm_hour, t.tm_min))
127+
pixels.fill((0, 0, 0)) # blank all pixels for change
128+
the_time = writetime(hour, minute)
129+
for i in range(1, 21): # Check all 30 bits
130+
if the_time & 1 << i: # If the bit is true
131+
pixels[i+1] = COLOR # set pixel on (shift up 2 for buried one)
132+
pixels.show()
133+
if LEDstate == 0: # Flash the D13 LED every other second for activity
134+
LED13.value = True
135+
LEDstate = 1
136+
else:
137+
LED13.value = False
138+
LEDstate = 0
139+
time.sleep(1) # wait a second

0 commit comments

Comments
 (0)