Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
pass bus_device object instead of bus, address, and cs.
  • Loading branch information
FoamyGuy committed Oct 6, 2025
commit 64692dcfc64318ef4310a557f0b8581827819563
38 changes: 21 additions & 17 deletions adafruit_spa06_003.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from micropython import const

try:
import typing
from typing import Union

from busio import I2C, SPI
from digitalio import DigitalInOut
Expand Down Expand Up @@ -158,8 +158,7 @@ class SPA06_003:
"""
SPA06-003 temperature and pressure sensor breakout CircuitPython driver.

:param busio.I2C i2c: I2C bus
:param int address: I2C address
:param bus_device: Instance of I2CDevice or SPIDevice

.. _measurement-rate-options:

Expand Down Expand Up @@ -388,29 +387,23 @@ class SPA06_003:
24, SPA06_003_REG_PSR_B2, 0, register_width=3, lsb_first=False, signed=True
)

def __init__(
self,
i2c: I2C = None,
address: int = SPA06_003_DEFAULT_ADDR,
spi: SPI = None,
cs: DigitalInOut = None,
):
if spi is not None and cs is not None:
def __init__(self, bus_device: Union[I2CDevice, SPIDevice]):
if isinstance(bus_device, SPIDevice):
try:
self.spi_device = SPIDevice(spi, cs, baudrate=1000000, phase=1, polarity=1)
self.register_accessor = SPIRegisterAccessor(self.spi_device)
self.register_accessor = SPIRegisterAccessor(bus_device)
except ValueError:
raise ValueError(f"No SPI device found.")

elif i2c is not None:
elif isinstance(bus_device, I2CDevice):
try:
i2c_device = I2CDevice(i2c, address)
self.register_accessor = I2CRegisterAccessor(i2c_device)
self.register_accessor = I2CRegisterAccessor(bus_device)
except ValueError:
raise ValueError(f"No I2C device found.")
else:
raise ValueError("bus_device must be an instance of I2CDevice or SPIDevice.")

if self.chip_id != 0x11:
raise ValueError("SPA06_003_I2C device not found")
raise ValueError("Error Reading Chip ID. Device not found.")

self.reset()

Expand Down Expand Up @@ -530,3 +523,14 @@ def reset(self):
"""Performs soft reset on the sensor."""
self.soft_reset_cmd = SPA06_003_CMD_RESET
time.sleep(0.01)


class SPA06_003_I2C(SPA06_003):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need a class? Or can this be a function?

Also, please use the warnings module. I believe most CP builds will have it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to a function and use warnings module in the latest commit.

Thank you

def __init__(self, i2c: I2C, address: int = SPA06_003_DEFAULT_ADDR):
i2c_device = I2CDevice(i2c, address)
print(
"Warning: SPA06_003_I2C class is deprecated and will be removed in a future version. "
"User code should be updated to initialize I2CDevice externally and pass it to "
"SPA06_003 class constructor."
)
super().__init__(i2c_device)
6 changes: 4 additions & 2 deletions examples/spa06_003_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import time

import board
from adafruit_bus_device.i2c_device import I2CDevice

from adafruit_spa06_003 import SPA06_003
from adafruit_spa06_003 import SPA06_003, SPA06_003_DEFAULT_ADDR

i2c = board.I2C()
spa = SPA06_003(i2c)
i2c_device = I2CDevice(i2c, SPA06_003_DEFAULT_ADDR)
spa = SPA06_003(i2c_device)


while True:
Expand Down
4 changes: 3 additions & 1 deletion examples/spa06_003_spi_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import time

import board
from adafruit_bus_device.spi_device import SPIDevice
from digitalio import DigitalInOut

from adafruit_spa06_003 import SPA06_003

spi = board.SPI()
cs = DigitalInOut(board.D10)

spa = SPA06_003(spi=spi, cs=cs)
spi_device = SPIDevice(spi, cs)
spa = SPA06_003(spi_device)


while True:
Expand Down