-
Notifications
You must be signed in to change notification settings - Fork 1
add SPI support and example #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
b3f139f
ddfd14a
64692dc
3d90092
5443268
ea61b07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,15 +30,18 @@ | |
import time | ||
|
||
from adafruit_bus_device.i2c_device import I2CDevice | ||
from adafruit_register.i2c_bit import ROBit, RWBit | ||
from adafruit_register.i2c_bits import ROBits, RWBits | ||
from adafruit_register.i2c_struct import ROUnaryStruct | ||
from adafruit_bus_device.spi_device import SPIDevice | ||
from adafruit_register.register_accessor import I2CRegisterAccessor, SPIRegisterAccessor | ||
from adafruit_register.register_bit import ROBit, RWBit | ||
from adafruit_register.register_bits import ROBits, RWBits | ||
from busio import I2C | ||
from micropython import const | ||
|
||
try: | ||
import typing | ||
from typing import Union | ||
|
||
from busio import I2C | ||
from busio import SPI | ||
from digitalio import DigitalInOut | ||
except ImportError: | ||
pass | ||
|
||
|
@@ -152,12 +155,11 @@ | |
} | ||
|
||
|
||
class SPA06_003_I2C: | ||
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: | ||
|
||
|
@@ -386,14 +388,47 @@ class SPA06_003_I2C: | |
24, SPA06_003_REG_PSR_B2, 0, register_width=3, lsb_first=False, signed=True | ||
) | ||
|
||
def __init__(self, i2c_bus: I2C, address: int = SPA06_003_DEFAULT_ADDR): | ||
try: | ||
self.i2c_device = I2CDevice(i2c_bus, address) | ||
except ValueError: | ||
raise ValueError(f"No I2C device found at address 0x{address:02X}") | ||
@staticmethod | ||
def over_spi(spi: SPI, cs: DigitalInOut): | ||
""" | ||
Initialize SPA06_003 breakout over SPI bus. | ||
|
||
:param spi: busio.SPI instance to communicate over | ||
:param cs: DigitalInOut instance to use for chip select | ||
:return: Initialized SPA06_003 object | ||
""" | ||
spi_device = SPIDevice(spi, cs, phase=1, polarity=1) | ||
return SPA06_003(spi_device) | ||
|
||
@staticmethod | ||
def over_i2c(i2c: I2C, address=SPA06_003_DEFAULT_ADDR): | ||
""" | ||
Initialize SPA06_003 breakout over I2C bus. | ||
|
||
:param i2c: busio.I2C instance to communicate over | ||
:param address: The I2C address to use. Defaults to SPA06_003_DEFAULT_ADDR | ||
:return: Initialized SPA06_003 object | ||
""" | ||
i2c_device = I2CDevice(i2c, address) | ||
return SPA06_003(i2c_device) | ||
|
||
def __init__(self, bus_device: Union[I2CDevice, SPIDevice]): | ||
if isinstance(bus_device, SPIDevice): | ||
try: | ||
self.register_accessor = SPIRegisterAccessor(bus_device) | ||
except ValueError: | ||
raise ValueError(f"No SPI device found.") | ||
|
||
elif isinstance(bus_device, I2CDevice): | ||
try: | ||
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() | ||
|
||
|
@@ -513,3 +548,21 @@ 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): | ||
|
||
""" | ||
SPA06_003_I2C Deprecated fallback driver class for warning message. | ||
|
||
:param i2c: busio.I2C instance to communicate over | ||
:param address: I2C address to use. Defaults to SPA06_003_DEFAULT_ADDR | ||
""" | ||
|
||
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: MIT | ||
import time | ||
|
||
import board | ||
from digitalio import DigitalInOut | ||
|
||
from adafruit_spa06_003 import SPA06_003 | ||
|
||
spi = board.SPI() | ||
cs = DigitalInOut(board.D10) | ||
|
||
spa = SPA06_003.over_spi(spi, cs) | ||
|
||
while True: | ||
if spa.temperature_data_ready and spa.pressure_data_ready: | ||
print(f"Temperature: {spa.temperature} °C", end=" ") | ||
print(f"Pressure: {spa.pressure} hPa") | ||
|
||
time.sleep(1.0) |
Uh oh!
There was an error while loading. Please reload this page.