Skip to content

Commit acdad93

Browse files
authored
Merge pull request #2 from BlitzCityDIY/main
library and example
2 parents 09662da + 6fe989c commit acdad93

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

README.rst

+20-1
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,28 @@ Usage Example
9393

9494
.. code-block:: python
9595
96+
import time
97+
import board
9698
import adafruit_s35710
9799
98-
print("hello s35710")
100+
i2c = board.I2C()
101+
102+
timer = adafruit_s35710.Adafruit_S35710(i2c)
103+
104+
timer.alarm = 5
105+
print(f"The S-35710 alarm is set for {timer.alarm} seconds")
106+
107+
countdown = timer.alarm - timer.clock
108+
109+
while True:
110+
print(f"The S-35710 clock is {timer.clock}")
111+
countdown = timer.alarm - timer.clock
112+
if countdown == 0:
113+
timer.alarm = 5
114+
print("Alarm reached! Resetting..")
115+
else:
116+
print(f"The alarm will expire in {countdown} seconds")
117+
time.sleep(1)
99118
100119
Documentation
101120
=============

adafruit_s35710.py

+69-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,75 @@
2525
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2626
"""
2727

28-
# imports
28+
import adafruit_bus_device.i2c_device as i2cdevice
29+
from micropython import const
30+
31+
try:
32+
import typing # pylint: disable=unused-import
33+
from busio import I2C
34+
except ImportError:
35+
pass
2936

3037
__version__ = "0.0.0+auto.0"
3138
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_S35710.git"
39+
40+
41+
_DEFAULT_I2C_ADDR = const(0x32)
42+
43+
44+
class Adafruit_S35710:
45+
"""
46+
A driver for the S-35710 Low-Power Wake Up Timer
47+
"""
48+
49+
def __init__(self, i2c: typing.Type[I2C], address: int = _DEFAULT_I2C_ADDR):
50+
"""Initialize the S-35710 Wake-Up Timer IC over I2C.
51+
52+
:param i2c: The I2C bus object.
53+
:type i2c: Type[I2C]
54+
:param address: The I2C address of the S-35710, defaults to 0x32.
55+
:type i2c_address: int
56+
"""
57+
self.i2c_device = i2cdevice.I2CDevice(i2c, address)
58+
59+
@property
60+
def alarm(self):
61+
"""Wake-up alarm time register value."""
62+
try:
63+
buffer = bytearray(3)
64+
with self.i2c_device as device:
65+
device.write_then_readinto(bytearray([0x01]), buffer)
66+
value = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2]
67+
return value
68+
except Exception as error:
69+
raise ValueError("Failed to read wake-up time register: ", error) from error
70+
71+
@alarm.setter
72+
def alarm(self, value: int):
73+
"""Wake-up alarm time register value.
74+
75+
:param value: the alarm time in seconds
76+
:type value: int
77+
"""
78+
try:
79+
buffer = bytearray(
80+
[0x81, (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF]
81+
)
82+
with self.i2c_device as device:
83+
device.write(buffer)
84+
except Exception as error:
85+
raise ValueError(
86+
"Failed to write wake-up time register: ", error
87+
) from error
88+
89+
@property
90+
def clock(self):
91+
"""Current time register value."""
92+
try:
93+
buffer = bytearray(3)
94+
with self.i2c_device as device:
95+
device.readinto(buffer)
96+
value = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2]
97+
return value
98+
except Exception as error:
99+
raise ValueError("Failed to read time register: ", error) from error

examples/s35710_simpletest.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
1-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
21
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries
32
#
4-
# SPDX-License-Identifier: Unlicense
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import adafruit_s35710
8+
9+
i2c = board.I2C()
10+
11+
timer = adafruit_s35710.Adafruit_S35710(i2c)
12+
13+
timer.alarm = 5
14+
print(f"The S-35710 alarm is set for {timer.alarm} seconds")
15+
16+
countdown = timer.alarm - timer.clock
17+
18+
while True:
19+
print(f"The S-35710 clock is {timer.clock}")
20+
countdown = timer.alarm - timer.clock
21+
if countdown == 0:
22+
timer.alarm = 5
23+
print("Alarm reached! Resetting..")
24+
else:
25+
print(f"The alarm will expire in {countdown} seconds")
26+
time.sleep(1)

0 commit comments

Comments
 (0)