From 2fff1ea4427188d60d0bd99dec414d888a64993d Mon Sep 17 00:00:00 2001 From: mrmcwethy Date: Fri, 19 Jan 2018 10:37:25 -0700 Subject: [PATCH 1/2] added examples folder and .py file. Also change read_into to readinto --- adafruit_ds1307.py | 2 +- examples/demo.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 examples/demo.py diff --git a/adafruit_ds1307.py b/adafruit_ds1307.py index e2dc746..13ed3ba 100644 --- a/adafruit_ds1307.py +++ b/adafruit_ds1307.py @@ -79,7 +79,7 @@ def __init__(self, i2c_bus): buf[0] = 0x07 with self.i2c_device as i2c: i2c.write(buf, end=1, stop=False) - i2c.read_into(buf, start=1) + i2c.readinto(buf, start=1) if (buf[1] & 0b00000011) != 0b00000011: raise ValueError("Unable to find DS1307 at i2c address 0x68.") diff --git a/examples/demo.py b/examples/demo.py new file mode 100644 index 0000000..8af0ba9 --- /dev/null +++ b/examples/demo.py @@ -0,0 +1,44 @@ +# Simple demo of reading and writing the time for the DS1307 real-time clock. +# Change the if False to if True below to set the time, otherwise it will just +# print the current date and time every second. Notice also comments to adjust +# for working with hardware vs. software I2C. + +import time +import board +# For hardware I2C (M0 boards) use this line: +import busio as io +# Or for software I2C (ESP8266) use this line instead: +#import bitbangio as io + +import adafruit_ds1307 + +# Change to the appropriate I2C clock & data pins here! +i2c_bus = io.I2C(board.SCL, board.SDA) + +# Create the RTC instance: +rtc = adafruit_ds1307.DS1307(i2c_bus) + +# Lookup table for names of days (nicer printing). +days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") + + +#pylint: disable-msg=bad-whitespace +#pylint: disable-msg=using-constant-test +if False: # change to True if you want to set the time! + # year, mon, date, hour, min, sec, wday, yday, isdst + t = time.struct_time((2017, 10, 29, 15, 14, 15, 0, -1, -1)) + # you must set year, mon, date, hour, min, sec and weekday + # yearday is not supported, isdst can be set but we don't do anything with it at this time + print("Setting time to:", t) # uncomment for debugging + rtc.datetime = t + print() +#pylint: enable-msg=using-constant-test +#pylint: enable-msg=bad-whitespace + +# Main loop: +while True: + t = rtc.datetime + #print(t) # uncomment for debugging + print("The date is {} {}/{}/{}".format(days[int(t.tm_wday)], t.tm_mday, t.tm_mon, t.tm_year)) + print("The time is {}:{:02}:{:02}".format(t.tm_hour, t.tm_min, t.tm_sec)) + time.sleep(1) # wait a second From 5c8599853cd96317c224657577a7d212ab0ac40c Mon Sep 17 00:00:00 2001 From: mrmcwethy Date: Fri, 19 Jan 2018 10:44:23 -0700 Subject: [PATCH 2/2] fixed pylint problem --- examples/demo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/demo.py b/examples/demo.py index 8af0ba9..82bfd30 100644 --- a/examples/demo.py +++ b/examples/demo.py @@ -13,7 +13,7 @@ import adafruit_ds1307 # Change to the appropriate I2C clock & data pins here! -i2c_bus = io.I2C(board.SCL, board.SDA) +i2c_bus = io.I2C(board.SCL, board.SDA) # Create the RTC instance: rtc = adafruit_ds1307.DS1307(i2c_bus)