diff --git a/.gitignore b/.gitignore index f97c8f4..ca224f7 100644 --- a/.gitignore +++ b/.gitignore @@ -171,3 +171,4 @@ cython_debug/ # PyPI configuration file .pypirc +.DS_Store diff --git a/rv3028/rv3028.py b/rv3028/rv3028.py old mode 100644 new mode 100755 index 7b1d177..04552c0 --- a/rv3028/rv3028.py +++ b/rv3028/rv3028.py @@ -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: @@ -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: diff --git a/tests/mocks/i2cMock.py b/tests/mocks/i2cMock.py index 5e78799..713b783 100644 --- a/tests/mocks/i2cMock.py +++ b/tests/mocks/i2cMock.py @@ -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 diff --git a/tests/stubs/i2c_device.py b/tests/stubs/i2c_device.py index c4d0af1..b6e8eb4 100644 --- a/tests/stubs/i2c_device.py +++ b/tests/stubs/i2c_device.py @@ -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): ...