Skip to content

Commit 06ee9c0

Browse files
committed
Merge remote-tracking branch 'adafruit/master'
2 parents 24c86d2 + 0e06cd4 commit 06ee9c0

8 files changed

Lines changed: 217 additions & 54 deletions

File tree

CircuitPython_Simple_Wordclock/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ Make a timekeeping vertical wordclock with Adafruit Feather M4, a RTC FeatherWin
44

55
Follow the Adafruit learn guide here: https://learn.adafruit.com/vertical-wordclock/ !
66

7+
**set_clock.py** is a run once program to set the time. You will need to edit the code as seen in the comments for the exact date and time (without daylight savings time, please). If you move time zones, you will have to reset the time using this code.
8+
9+
**code.py** is the code that will run to display the time. It reads the time previously set. A backup battery in the RTC FeatherWing keeps the time even when power is removed.
10+
11+
Use the slide switch added to the Adafruit Feather M4 to add one hour to the time for daylight savings.
12+
713
Adafruit invests time and resources providing this open source code,
814
please support Adafruit and open-source hardware by purchasing
915
products from [Adafruit](https://www.adafruit.com)!
1016

11-
MIT license, designed and written by Dano Wall, code by Mike Barela
17+
MIT license, designed and guide written by Dano Wall, code by Mike Barela
1218

1319
All text above, and the splash screen below must be included in any redistribution
1420

CircuitPython_Simple_Wordclock/code.py

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,42 @@
1313
# Create the RTC instance:
1414
rtc = adafruit_ds3231.DS3231(i2c)
1515

16+
# Set up Feather M4 onboard LED for output
1617
LED13 = digitalio.DigitalInOut(board.D13)
1718
LED13.direction = digitalio.Direction.OUTPUT
1819

20+
# Set digital 6 as an input for slide switch
21+
Slide_Switch = digitalio.DigitalInOut(board.D6)
22+
Slide_Switch.switch_to_input(pull=digitalio.Pull.UP)
23+
1924
pixel_pin = board.D5
2025
num_pixels = 21
21-
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=1.0)
22-
pixels.fill((0, 0, 0))
23-
COLOR = (0, 200, 0) # Green
26+
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=1.0,
27+
auto_write=False)
28+
pixels.fill((0, 0, 0)) # Blanking Display
29+
COLOR = (0, 200, 0) # Green for time later in code
2430

2531
# Bitmap values for each value. These can be OR'ed together
2632
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
33+
EIGHT = 1 << 1
34+
ELEVEN = 1 << 2
35+
TWO = 1 << 3
36+
SIX = 1 << 4
37+
FOUR = 1 << 5
38+
SEVEN = 1 << 6
39+
NOON = 1 << 7
40+
TEN = 1 << 8
41+
ONE = 1 << 9
42+
FIVE = 1 << 10
43+
MIDNIGHT = 1 << 11
44+
NINE = 1 << 12
45+
PAST = 1 << 13
46+
TO = 1 << 14
47+
FIVEMIN = 1 << 15
48+
QUARTER = 1 << 16
49+
TENMIN = 1 << 17
50+
HALF = 1 << 18
51+
TWENTY = 1 << 19
4652

4753
# Pass in hour and minute, return LED bitmask
4854
# pylint: disable=too-many-branches
@@ -118,17 +124,24 @@ def writetime(the_hr, the_min):
118124

119125
# Main loop
120126
LEDstate = 0
121-
FirstLoop = True
127+
Write_Now = True
122128

123129
while True:
124130
t = rtc.datetime
125131
# print("The date is {} {}/{}/{}".format(days[int(t.tm_wday)],
126132
# t.tm_mday, t.tm_mon, t.tm_year))
127133
# print("The time is {}:{:02}:{:02}".format(t.tm_hour, t.tm_min, t.tm_sec))
128134
hour = t.tm_hour
135+
if not Slide_Switch.value: # Slide switch activate = Daylight savings
136+
# print("Switch detected for daylight savings")
137+
if hour == 24:
138+
hour = 1
139+
else:
140+
hour += 1
141+
Write_Now = True # Trigger a write
129142
minute = t.tm_min
130143
second = t.tm_sec
131-
if second == 59 or FirstLoop:
144+
if second == 59 or Write_Now:
132145
# print("The time is {}:{:02}".format(t.tm_hour, t.tm_min))
133146
pixels.fill((0, 0, 0)) # blank all pixels for change
134147
the_time = writetime(hour, minute)
@@ -142,5 +155,5 @@ def writetime(the_hr, the_min):
142155
else:
143156
LED13.value = False
144157
LEDstate = 0
145-
Firstloop = False
158+
Write_Now = False
146159
time.sleep(1) # wait a second
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Write the time for the Adafruit DS3231 real-time clock.
2+
# Limor Fried/Mike Barela for Adafruit Industries
3+
4+
import time
5+
import board
6+
import busio as io
7+
import digitalio
8+
import adafruit_ds3231
9+
10+
i2c = io.I2C(board.SCL, board.SDA)
11+
12+
# Create the RTC instance:
13+
rtc = adafruit_ds3231.DS3231(i2c)
14+
15+
LED13 = digitalio.DigitalInOut(board.D13)
16+
LED13.direction = digitalio.Direction.OUTPUT
17+
18+
# pylint: disable-msg=bad-whitespace
19+
# pylint: disable-msg=using-constant-test
20+
if True:
21+
# year, mon, date, hour, min, sec, wday, yday, isdst
22+
t = time.struct_time((2019, 7, 10, 17, 00, 0, 0, -1, -1))
23+
# you must set year, mon, date, hour, min, sec and weekday
24+
# yearday is not supported
25+
# isdst can be set but we don't do anything with it at this time
26+
print("Setting time to:", t) # uncomment for debugging
27+
rtc.datetime = t
28+
print("Done!")
29+
LED13.value = True
30+
# pylint: enable-msg=using-constant-test
31+
# pylint: enable-msg=bad-whitespace
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Large-Pi-Based-Thermometer-Clock
2+
3+
Code to accompany this tutorial:
4+
https://learn.adafruit.com/large-pi-based-thermometer-and-clock
87.9 KB
Binary file not shown.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import glob
2+
import time
3+
import datetime
4+
from Adafruit_LED_Backpack import SevenSegment
5+
import board
6+
import digitalio
7+
8+
switch_pin = digitalio.DigitalInOut(board.D18)
9+
switch_pin.direction = digitalio.Direction.INPUT
10+
switch_pin.pull = digitalio.Pull.UP
11+
12+
segment = SevenSegment.SevenSegment(address=0x70)
13+
# Initialize the display. Must be called once before using the display.
14+
segment.begin()
15+
16+
base_dir = '/sys/bus/w1/devices/'
17+
device_folder = glob.glob(base_dir + '28*')[0]
18+
device_file = device_folder + '/w1_slave'
19+
20+
def read_temp_raw():
21+
f = open(device_file, 'r')
22+
lines = f.readlines()
23+
f.close()
24+
return lines
25+
26+
def read_temp():
27+
lines = read_temp_raw()
28+
while lines[0].strip()[-3:] != 'YES':
29+
time.sleep(0.2)
30+
lines = read_temp_raw()
31+
equals_pos = lines[1].find('t=')
32+
if equals_pos != -1:
33+
temp_string = lines[1][equals_pos+2:]
34+
temp_c = float(temp_string) / 1000.0
35+
temp_f = temp_c * 9.0 / 5.0 + 32.0
36+
return temp_c, temp_f
37+
38+
def display_temp():
39+
segment.set_colon(False)
40+
temp = int(read_temp()[1]) # F
41+
# temp = int(read_temp()[0]) # C
42+
sign = (temp < 0)
43+
temp = abs(temp)
44+
digit_1 = int(temp % 10)
45+
temp = temp / 10
46+
digit_2 = int(temp % 10)
47+
temp = temp / 10
48+
digit_3 = int(temp % 10)
49+
if sign :
50+
segment.set_digit_raw(0, 0x40) # - sign
51+
if digit_3 > 0 :
52+
segment.set_digit(0, digit_3) # Hundreds
53+
else:
54+
segment.set_digit_raw(0, 0)
55+
if digit_2 > 0 :
56+
segment.set_digit(1, digit_2) # Tens
57+
else:
58+
segment.set_digit_raw(1, 0)
59+
segment.set_digit(2, digit_1) # Ones
60+
segment.set_digit_raw(3, 0x71) #F # Temp units letter
61+
# segment.set_digit_raw(3, 0x39) #C
62+
63+
def display_time():
64+
now = datetime.datetime.now()
65+
hour = now.hour
66+
minute = now.minute
67+
second = now.second
68+
# Set hours
69+
segment.set_digit(0, int(hour / 10)) # Tens
70+
segment.set_digit(1, hour % 10) # Ones
71+
# Set minutes
72+
segment.set_digit(2, int(minute / 10)) # Tens
73+
segment.set_digit(3, minute % 10) # Ones
74+
# Toggle colon
75+
segment.set_colon(second % 2) # Toggle colon at 1Hz
76+
77+
78+
while True:
79+
segment.clear()
80+
if not switch_pin.value:
81+
display_temp()
82+
else :
83+
display_time()
84+
segment.write_display()
85+
time.sleep(0.5)

PyPortal_Calculator/calculator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ def __init__(self, calc_display, clear_button, label_offset):
2323
self._operand = None
2424
self._all_clear()
2525

26+
def get_current_operator(self):
27+
operator = self._operator
28+
if operator == "*":
29+
operator = "x"
30+
return operator
31+
2632
def _all_clear(self):
2733
self._accumulator = "0"
2834
self._operator = None
@@ -36,6 +42,7 @@ def _clear_entry(self):
3642
self._set_text("0")
3743

3844
def _set_button_ce(self, entry_only):
45+
self._clear_button.selected = False
3946
if entry_only:
4047
self._clear_button.label = "CE"
4148
else:

PyPortal_Calculator/code.py

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@
4646
font = bitmap_font.load_font("/fonts/Arial-12.bdf")
4747
buttons = []
4848

49-
# Some button placement functions
49+
# Some button functions
5050
def button_grid(row, col):
5151
return Coords(BUTTON_MARGIN * (row + 1) + BUTTON_WIDTH * row + 20,
5252
BUTTON_MARGIN * (col + 1) + BUTTON_HEIGHT * col + 40)
5353

54-
def make_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
54+
def add_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
5555
pos = button_grid(row, col)
5656
new_button = Button(x=pos.x, y=pos.y,
5757
width=BUTTON_WIDTH * width + BUTTON_MARGIN * (width - 1),
@@ -60,29 +60,36 @@ def make_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
6060
buttons.append(new_button)
6161
return new_button
6262

63+
def find_button(label):
64+
result = None
65+
for _, btn in enumerate(buttons):
66+
if btn.label == label:
67+
result = btn
68+
return result
69+
6370
border = Rect(20, 8, 280, 35, fill=WHITE, outline=BLACK, stroke=2)
6471
calc_display = Label(font, text="0", color=BLACK, max_glyphs=MAX_DIGITS)
6572
calc_display.y = 25
6673

67-
clear_button = make_button(0, 0, "AC")
68-
make_button(1, 0, "+/-")
69-
make_button(2, 0, "%")
70-
make_button(3, 0, "/", 1, ORANGE, WHITE)
71-
make_button(0, 1, "7")
72-
make_button(1, 1, "8")
73-
make_button(2, 1, "9")
74-
make_button(3, 1, "x", 1, ORANGE, WHITE)
75-
make_button(0, 2, "4")
76-
make_button(1, 2, "5")
77-
make_button(2, 2, "6")
78-
make_button(3, 2, "-", 1, ORANGE, WHITE)
79-
make_button(0, 3, "1")
80-
make_button(1, 3, "2")
81-
make_button(2, 3, "3")
82-
make_button(3, 3, "+", 1, ORANGE, WHITE)
83-
make_button(0, 4, "0", 2)
84-
make_button(2, 4, ".")
85-
make_button(3, 4, "=", 1, ORANGE, WHITE)
74+
clear_button = add_button(0, 0, "AC")
75+
add_button(1, 0, "+/-")
76+
add_button(2, 0, "%")
77+
add_button(3, 0, "/", 1, ORANGE, WHITE)
78+
add_button(0, 1, "7")
79+
add_button(1, 1, "8")
80+
add_button(2, 1, "9")
81+
add_button(3, 1, "x", 1, ORANGE, WHITE)
82+
add_button(0, 2, "4")
83+
add_button(1, 2, "5")
84+
add_button(2, 2, "6")
85+
add_button(3, 2, "-", 1, ORANGE, WHITE)
86+
add_button(0, 3, "1")
87+
add_button(1, 3, "2")
88+
add_button(2, 3, "3")
89+
add_button(3, 3, "+", 1, ORANGE, WHITE)
90+
add_button(0, 4, "0", 2)
91+
add_button(2, 4, ".")
92+
add_button(3, 4, "=", 1, ORANGE, WHITE)
8693

8794
# Add the display and buttons to the main calc group
8895
calc_group.append(border)
@@ -96,15 +103,25 @@ def make_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
96103
while True:
97104
point = ts.touch_point
98105
if point is not None:
99-
for i, b in enumerate(buttons):
106+
# Button Down Events
107+
for _, b in enumerate(buttons):
100108
if b.contains(point) and button == "":
101109
b.selected = True
102110
button = b.label
103-
time.sleep(0.1)
111+
elif button != "":
112+
# Button Up Events
113+
last_op = calculator.get_current_operator()
114+
op_button = find_button(last_op)
115+
# Deselect the last operation when certain buttons are pressed
116+
if op_button is not None:
117+
if button in ('=', 'AC', 'CE'):
118+
op_button.selected = False
119+
elif button in ('+', '-', 'x', '/') and button != last_op:
120+
op_button.selected = False
121+
calculator.add_input(button)
122+
b = find_button(button)
123+
if b is not None:
124+
if button not in ('+', '-', 'x', '/') or button != calculator.get_current_operator():
104125
b.selected = False
105-
break
106-
else:
107-
if button != "":
108-
calculator.add_input(button)
109-
button = ""
126+
button = ""
110127
time.sleep(0.05)

0 commit comments

Comments
 (0)