Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,4 @@ cython_debug/

# PyPI configuration file
.pypirc
.DS_Store
14 changes: 11 additions & 3 deletions rv3028/rv3028.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
)

try:
from tests.stubs.i2c_device import I2CDevice
from tests.stubs.i2c_device import I2C, I2CDevice
except ImportError:
from adafruit_bus_device.i2c_device import I2CDevice
from busio import I2C

_RV3028_DEFAULT_ADDRESS = 0x52


class WEEKDAY:
Expand All @@ -36,8 +39,13 @@ class WEEKDAY:


class RV3028:
def __init__(self, i2c_device: I2CDevice):
self.i2c_device = i2c_device
def __init__(self, i2c, address: int = _RV3028_DEFAULT_ADDRESS):
if isinstance(i2c, I2C):
self.i2c_device = I2CDevice(i2c, address)
elif isinstance(i2c, I2CDevice):
self.i2c_device = i2c
else:
raise TypeError("i2c should be an i2c bus or device!")

def _read_register(self, register, length=1):
with self.i2c_device as i2c:
Expand Down
7 changes: 5 additions & 2 deletions tests/mocks/i2cMock.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
class MockI2C:
from tests.stubs.i2c_device import I2C, I2CDevice


class MockI2C(I2C):
def __init__(self):
self.registers = [0x00] * 256 # 256 8 bit registers


class MockI2CDevice:
class MockI2CDevice(I2CDevice):
def __init__(self, i2c: MockI2C, address):
self.i2c = i2c
self.address = address
Expand Down
4 changes: 4 additions & 0 deletions tests/stubs/i2c_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ def __enter__(self): ...
def __exit__(self, exc_type, exc_value, traceback): ...
def write(self, data): ...
def readinto(self, buffer): ...


class I2C:
def __init__(self): ...