|
25 | 25 | * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
|
26 | 26 | """
|
27 | 27 |
|
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 |
29 | 36 |
|
30 | 37 | __version__ = "0.0.0+auto.0"
|
31 | 38 | __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 |
0 commit comments