Skip to content

Commit 6d53cf9

Browse files
mrmcwethytannewt
authored andcommitted
added examples folder and .py file. Also change read_into to readinto (#3)
1 parent 102c350 commit 6d53cf9

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

adafruit_ds1307.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(self, i2c_bus):
7979
buf[0] = 0x07
8080
with self.i2c_device as i2c:
8181
i2c.write(buf, end=1, stop=False)
82-
i2c.read_into(buf, start=1)
82+
i2c.readinto(buf, start=1)
8383

8484
if (buf[1] & 0b00000011) != 0b00000011:
8585
raise ValueError("Unable to find DS1307 at i2c address 0x68.")

examples/demo.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Simple demo of reading and writing the time for the DS1307 real-time clock.
2+
# Change the if False to if True below to set the time, otherwise it will just
3+
# print the current date and time every second. Notice also comments to adjust
4+
# for working with hardware vs. software I2C.
5+
6+
import time
7+
import board
8+
# For hardware I2C (M0 boards) use this line:
9+
import busio as io
10+
# Or for software I2C (ESP8266) use this line instead:
11+
#import bitbangio as io
12+
13+
import adafruit_ds1307
14+
15+
# Change to the appropriate I2C clock & data pins here!
16+
i2c_bus = io.I2C(board.SCL, board.SDA)
17+
18+
# Create the RTC instance:
19+
rtc = adafruit_ds1307.DS1307(i2c_bus)
20+
21+
# Lookup table for names of days (nicer printing).
22+
days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
23+
24+
25+
#pylint: disable-msg=bad-whitespace
26+
#pylint: disable-msg=using-constant-test
27+
if False: # change to True if you want to set the time!
28+
# year, mon, date, hour, min, sec, wday, yday, isdst
29+
t = time.struct_time((2017, 10, 29, 15, 14, 15, 0, -1, -1))
30+
# you must set year, mon, date, hour, min, sec and weekday
31+
# yearday is not supported, isdst can be set but we don't do anything with it at this time
32+
print("Setting time to:", t) # uncomment for debugging
33+
rtc.datetime = t
34+
print()
35+
#pylint: enable-msg=using-constant-test
36+
#pylint: enable-msg=bad-whitespace
37+
38+
# Main loop:
39+
while True:
40+
t = rtc.datetime
41+
#print(t) # uncomment for debugging
42+
print("The date is {} {}/{}/{}".format(days[int(t.tm_wday)], t.tm_mday, t.tm_mon, t.tm_year))
43+
print("The time is {}:{:02}:{:02}".format(t.tm_hour, t.tm_min, t.tm_sec))
44+
time.sleep(1) # wait a second

0 commit comments

Comments
 (0)