|
37 | 37 | from micropython import const |
38 | 38 |
|
39 | 39 | try: |
40 | | - import typing |
| 40 | + from typing import Union |
41 | 41 |
|
42 | 42 | from busio import I2C, SPI |
43 | 43 | from digitalio import DigitalInOut |
@@ -158,8 +158,7 @@ class SPA06_003: |
158 | 158 | """ |
159 | 159 | SPA06-003 temperature and pressure sensor breakout CircuitPython driver. |
160 | 160 |
|
161 | | - :param busio.I2C i2c: I2C bus |
162 | | - :param int address: I2C address |
| 161 | + :param bus_device: Instance of I2CDevice or SPIDevice |
163 | 162 |
|
164 | 163 | .. _measurement-rate-options: |
165 | 164 |
|
@@ -388,29 +387,23 @@ class SPA06_003: |
388 | 387 | 24, SPA06_003_REG_PSR_B2, 0, register_width=3, lsb_first=False, signed=True |
389 | 388 | ) |
390 | 389 |
|
391 | | - def __init__( |
392 | | - self, |
393 | | - i2c: I2C = None, |
394 | | - address: int = SPA06_003_DEFAULT_ADDR, |
395 | | - spi: SPI = None, |
396 | | - cs: DigitalInOut = None, |
397 | | - ): |
398 | | - if spi is not None and cs is not None: |
| 390 | + def __init__(self, bus_device: Union[I2CDevice, SPIDevice]): |
| 391 | + if isinstance(bus_device, SPIDevice): |
399 | 392 | try: |
400 | | - self.spi_device = SPIDevice(spi, cs, baudrate=1000000, phase=1, polarity=1) |
401 | | - self.register_accessor = SPIRegisterAccessor(self.spi_device) |
| 393 | + self.register_accessor = SPIRegisterAccessor(bus_device) |
402 | 394 | except ValueError: |
403 | 395 | raise ValueError(f"No SPI device found.") |
404 | 396 |
|
405 | | - elif i2c is not None: |
| 397 | + elif isinstance(bus_device, I2CDevice): |
406 | 398 | try: |
407 | | - i2c_device = I2CDevice(i2c, address) |
408 | | - self.register_accessor = I2CRegisterAccessor(i2c_device) |
| 399 | + self.register_accessor = I2CRegisterAccessor(bus_device) |
409 | 400 | except ValueError: |
410 | 401 | raise ValueError(f"No I2C device found.") |
| 402 | + else: |
| 403 | + raise ValueError("bus_device must be an instance of I2CDevice or SPIDevice.") |
411 | 404 |
|
412 | 405 | if self.chip_id != 0x11: |
413 | | - raise ValueError("SPA06_003_I2C device not found") |
| 406 | + raise ValueError("Error Reading Chip ID. Device not found.") |
414 | 407 |
|
415 | 408 | self.reset() |
416 | 409 |
|
@@ -530,3 +523,14 @@ def reset(self): |
530 | 523 | """Performs soft reset on the sensor.""" |
531 | 524 | self.soft_reset_cmd = SPA06_003_CMD_RESET |
532 | 525 | time.sleep(0.01) |
| 526 | + |
| 527 | + |
| 528 | +class SPA06_003_I2C(SPA06_003): |
| 529 | + def __init__(self, i2c: I2C, address: int = SPA06_003_DEFAULT_ADDR): |
| 530 | + i2c_device = I2CDevice(i2c, address) |
| 531 | + print( |
| 532 | + "Warning: SPA06_003_I2C class is deprecated and will be removed in a future version. " |
| 533 | + "User code should be updated to initialize I2CDevice externally and pass it to " |
| 534 | + "SPA06_003 class constructor." |
| 535 | + ) |
| 536 | + super().__init__(i2c_device) |
0 commit comments