Skip to content

Commit

Permalink
library and example
Browse files Browse the repository at this point in the history
  • Loading branch information
BlitzCityDIY committed May 9, 2024
1 parent c4c6eac commit 6fe989c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 4 deletions.
21 changes: 20 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=============
Expand Down
70 changes: 69 additions & 1 deletion adafruit_s35710.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 24 additions & 2 deletions examples/s35710_simpletest.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6fe989c

Please sign in to comment.