diff --git a/README.rst b/README.rst index c75a7ef..c07ca73 100644 --- a/README.rst +++ b/README.rst @@ -93,9 +93,28 @@ Usage Example .. code-block:: python + import time + import board import adafruit_s35710 - print("hello s35710") + i2c = board.I2C() + + timer = adafruit_s35710.Adafruit_S35710(i2c) + + timer.alarm = 5 + print(f"The S-35710 alarm is set for {timer.alarm} seconds") + + countdown = timer.alarm - timer.clock + + while True: + print(f"The S-35710 clock is {timer.clock}") + countdown = timer.alarm - timer.clock + if countdown == 0: + timer.alarm = 5 + print("Alarm reached! Resetting..") + else: + print(f"The alarm will expire in {countdown} seconds") + time.sleep(1) Documentation ============= diff --git a/adafruit_s35710.py b/adafruit_s35710.py index a20dea3..b735eb4 100644 --- a/adafruit_s35710.py +++ b/adafruit_s35710.py @@ -25,7 +25,75 @@ * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice """ -# imports +import adafruit_bus_device.i2c_device as i2cdevice +from micropython import const + +try: + import typing # pylint: disable=unused-import + from busio import I2C +except ImportError: + pass __version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_S35710.git" + + +_DEFAULT_I2C_ADDR = const(0x32) + + +class Adafruit_S35710: + """ + A driver for the S-35710 Low-Power Wake Up Timer + """ + + def __init__(self, i2c: typing.Type[I2C], address: int = _DEFAULT_I2C_ADDR): + """Initialize the S-35710 Wake-Up Timer IC over I2C. + + :param i2c: The I2C bus object. + :type i2c: Type[I2C] + :param address: The I2C address of the S-35710, defaults to 0x32. + :type i2c_address: int + """ + self.i2c_device = i2cdevice.I2CDevice(i2c, address) + + @property + def alarm(self): + """Wake-up alarm time register value.""" + try: + buffer = bytearray(3) + with self.i2c_device as device: + device.write_then_readinto(bytearray([0x01]), buffer) + value = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2] + return value + except Exception as error: + raise ValueError("Failed to read wake-up time register: ", error) from error + + @alarm.setter + def alarm(self, value: int): + """Wake-up alarm time register value. + + :param value: the alarm time in seconds + :type value: int + """ + try: + buffer = bytearray( + [0x81, (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF] + ) + with self.i2c_device as device: + device.write(buffer) + except Exception as error: + raise ValueError( + "Failed to write wake-up time register: ", error + ) from error + + @property + def clock(self): + """Current time register value.""" + try: + buffer = bytearray(3) + with self.i2c_device as device: + device.readinto(buffer) + value = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2] + return value + except Exception as error: + raise ValueError("Failed to read time register: ", error) from error diff --git a/examples/s35710_simpletest.py b/examples/s35710_simpletest.py index b3cb397..e7d1e7e 100644 --- a/examples/s35710_simpletest.py +++ b/examples/s35710_simpletest.py @@ -1,4 +1,26 @@ -# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries # SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries # -# SPDX-License-Identifier: Unlicense +# SPDX-License-Identifier: MIT + +import time +import board +import adafruit_s35710 + +i2c = board.I2C() + +timer = adafruit_s35710.Adafruit_S35710(i2c) + +timer.alarm = 5 +print(f"The S-35710 alarm is set for {timer.alarm} seconds") + +countdown = timer.alarm - timer.clock + +while True: + print(f"The S-35710 clock is {timer.clock}") + countdown = timer.alarm - timer.clock + if countdown == 0: + timer.alarm = 5 + print("Alarm reached! Resetting..") + else: + print(f"The alarm will expire in {countdown} seconds") + time.sleep(1)