Skip to content

Commit 79c29f8

Browse files
authored
Merge pull request #754 from adafruit/TheKitty-patch-93
Create code.py for Vertical Wordclock guide
2 parents b884fab + de0d1c2 commit 79c29f8

1 file changed

Lines changed: 146 additions & 0 deletions

File tree

  • CircuitPython_Simple_Wordclock
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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 = 22
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 = 1
27+
EIGHT = 1<<1
28+
ELEVEN = 1<<2
29+
TWO = 1<<3
30+
SIX = 1<<4
31+
FOUR = 1<<5
32+
SEVEN = 1<<6
33+
NOON = 1<<7
34+
TEN = 1<<8
35+
ONE = 1<<9
36+
FIVE = 1<<10
37+
MIDNIGHT = 1<<11
38+
NINE = 1<<12
39+
PAST = 1<<13
40+
TO = 1<<14
41+
FIVEMIN = 1<<15
42+
QUARTER = 1<<16
43+
TENMIN = 1<<17
44+
HALF = 1<<18
45+
TWENTY = 1<<19
46+
47+
# Pass in hour and minute, return LED bitmask
48+
# pylint: disable=too-many-branches
49+
# pylint: disable=too-many-statements
50+
def writetime(the_hr, the_min):
51+
value = 0 # Start with zero, which is no words
52+
if (the_hr == 24) and (the_min == 0): # Special cases: Midnight and Noon
53+
return MIDNIGHT
54+
if (the_hr == 12) and (the_min == 0):
55+
return NOON
56+
# set minute
57+
if (the_min > 4) and (the_min < 8):
58+
value = value | FIVEMIN
59+
if (the_min > 7) and (the_min < 13):
60+
value = value | TENMIN
61+
if (the_min > 14) and (the_min < 18):
62+
value = value | QUARTER
63+
if (the_min > 17) and (the_min < 23):
64+
value = value | TWENTY
65+
if (the_min > 22) and (the_min < 28):
66+
value = value | TWENTY | FIVEMIN
67+
if (the_min > 27) and (the_min < 33):
68+
value = value | HALF
69+
if (the_min > 32) and (the_min < 38):
70+
value = value | TWENTY | FIVEMIN
71+
if (the_min > 37) and (the_min < 43):
72+
value = value | TWENTY
73+
if (the_min > 42) and (the_min < 48):
74+
value = value | QUARTER
75+
if (the_min > 47) and (the_min <= 53):
76+
value = value | TENMIN
77+
if the_min >= 54:
78+
value = value | FIVEMIN
79+
# before or after
80+
if the_min <= 30:
81+
value = value | PAST
82+
else:
83+
the_hr = the_hr + 1 # for the TO case
84+
value = value | TO
85+
# set hour
86+
if the_hr > 12:
87+
the_hr = the_hr - 12 # Convert 24 hour format to 12 hour
88+
if the_hr == 1:
89+
value = value | ONE
90+
if the_hr == 2:
91+
value = value | TWO
92+
if the_hr == 3:
93+
value = value | THREE
94+
if the_hr == 4:
95+
value = value | FOUR
96+
if the_hr == 5:
97+
value = value | FIVE
98+
if the_hr == 6:
99+
value = value | SIX
100+
if the_hr == 7:
101+
value = value | SEVEN
102+
if the_hr == 8:
103+
value = value | EIGHT
104+
if the_hr == 9:
105+
value = value | NINE
106+
if the_hr == 10:
107+
value = value | TEN
108+
if the_hr == 11:
109+
value = value | ELEVEN
110+
if the_hr == 0:
111+
value = value | MIDNIGHT
112+
if the_hr == 12:
113+
value = value | NOON
114+
return value
115+
# end def
116+
# pylint: enable=too-many-branches
117+
# pylint: enable=too-many-statements
118+
119+
# Main loop
120+
LEDstate = 0
121+
FirstLoop = True
122+
123+
while True:
124+
t = rtc.datetime
125+
# print("The date is {} {}/{}/{}".format(days[int(t.tm_wday)],
126+
# t.tm_mday, t.tm_mon, t.tm_year))
127+
# print("The time is {}:{:02}:{:02}".format(t.tm_hour, t.tm_min, t.tm_sec))
128+
hour = t.tm_hour
129+
minute = t.tm_min
130+
second = t.tm_sec
131+
if second == 59 or FirstLoop:
132+
# print("The time is {}:{:02}".format(t.tm_hour, t.tm_min))
133+
pixels.fill((0, 0, 0)) # blank all pixels for change
134+
the_time = writetime(hour, minute)
135+
for i in range(1, 21): # Check all 30 bits
136+
if the_time & 1 << i: # If the bit is true
137+
pixels[i+1] = COLOR # set pixel on (shift up 2 for buried one)
138+
pixels.show()
139+
if LEDstate == 0: # Flash the D13 LED every other second for activity
140+
LED13.value = True
141+
LEDstate = 1
142+
else:
143+
LED13.value = False
144+
LEDstate = 0
145+
Firstloop = False
146+
time.sleep(1) # wait a second

0 commit comments

Comments
 (0)