Skip to content

Commit

Permalink
Merge pull request #62 from rsbohn/dexter0819
Browse files Browse the repository at this point in the history
Provide proxies for I2C and SPI
  • Loading branch information
FoamyGuy authored Sep 26, 2022
2 parents 69a07bc + e521dfc commit c7d4e1c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 81 deletions.
46 changes: 5 additions & 41 deletions adafruit_bme280/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"""
from micropython import const
from adafruit_bme280.basic import Adafruit_BME280
from adafruit_bme280.protocol import I2C_Impl, SPI_Impl

try:
import typing # pylint: disable=unused-import
Expand Down Expand Up @@ -141,14 +142,14 @@ class Adafruit_BME280_Advanced(Adafruit_BME280):
"""

# pylint: disable=too-many-instance-attributes
def __init__(self) -> None:
def __init__(self, proxy: typing.Union[I2C_Impl, SPI_Impl]) -> None:
"""Check the BME280 was found, read the coefficients and enable the sensor"""
self._overscan_humidity = OVERSCAN_X1
self._overscan_temperature = OVERSCAN_X1
self._overscan_pressure = OVERSCAN_X16
self._mode = MODE_SLEEP
self._t_standby = STANDBY_TC_125
super().__init__()
super().__init__(proxy)

@property
def standby_period(self) -> int:
Expand Down Expand Up @@ -317,25 +318,7 @@ class Adafruit_BME280_I2C(Adafruit_BME280_Advanced):
"""

def __init__(self, i2c: I2C, address: int = _BME280_ADDRESS) -> None:
from adafruit_bus_device import ( # pylint: disable=import-outside-toplevel
i2c_device,
)

self._i2c = i2c_device.I2CDevice(i2c, address)
super().__init__()

def _read_register(self, register: int, length: int) -> bytearray:
with self._i2c as i2c:
i2c.write(bytes([register & 0xFF]))
result = bytearray(length)
i2c.readinto(result)
# print("$%02X => %s" % (register, [hex(i) for i in result]))
return result

def _write_register_byte(self, register: int, value: int) -> None:
with self._i2c as i2c:
i2c.write(bytes([register & 0xFF, value & 0xFF]))
# print("$%02X <= 0x%02X" % (register, value))
super().__init__(I2C_Impl(i2c, address))


class Adafruit_BME280_SPI(Adafruit_BME280_Advanced):
Expand Down Expand Up @@ -387,23 +370,4 @@ class Adafruit_BME280_SPI(Adafruit_BME280_Advanced):
"""

def __init__(self, spi: SPI, cs: DigitalInOut, baudrate: int = 100000) -> None:
from adafruit_bus_device import ( # pylint: disable=import-outside-toplevel
spi_device,
)

self._spi = spi_device.SPIDevice(spi, cs, baudrate=baudrate)
super().__init__()

def _read_register(self, register: int, length: int) -> bytearray:
register = (register | 0x80) & 0xFF # Read single, bit 7 high.
with self._spi as spi:
spi.write(bytearray([register])) # pylint: disable=no-member
result = bytearray(length)
spi.readinto(result) # pylint: disable=no-member
# print("$%02X => %s" % (register, [hex(i) for i in result]))
return result

def _write_register_byte(self, register: int, value: int) -> None:
register &= 0x7F # Write, bit 7 low.
with self._spi as spi:
spi.write(bytes([register, value & 0xFF])) # pylint: disable=no-member
super().__init__(SPI_Impl(spi, cs, baudrate))
47 changes: 7 additions & 40 deletions adafruit_bme280/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from time import sleep

from micropython import const
from adafruit_bme280.protocol import I2C_Impl, SPI_Impl

try:
import typing # pylint: disable=unused-import
Expand Down Expand Up @@ -88,9 +89,10 @@ class Adafruit_BME280:
"""

# pylint: disable=too-many-instance-attributes
def __init__(self) -> None:
def __init__(self, bus_implementation: typing.Union[I2C_Impl, SPI_Impl]) -> None:
"""Check the BME280 was found, read the coefficients and enable the sensor"""
# Check device ID.
self._bus_implementation = bus_implementation
chip_id = self._read_byte(_BME280_REGISTER_CHIPID)
if _BME280_CHIPID != chip_id:
raise RuntimeError("Failed to find BME280! Chip ID 0x%x" % chip_id)
Expand Down Expand Up @@ -309,10 +311,10 @@ def _read24(self, register: int) -> float:
return ret

def _read_register(self, register: int, length: int) -> bytearray:
raise NotImplementedError()
return self._bus_implementation.read_register(register, length)

def _write_register_byte(self, register: int, value: int) -> None:
raise NotImplementedError()
self._bus_implementation.write_register_byte(register, value)


class Adafruit_BME280_I2C(Adafruit_BME280):
Expand Down Expand Up @@ -363,24 +365,7 @@ class Adafruit_BME280_I2C(Adafruit_BME280):
"""

def __init__(self, i2c: I2C, address: int = 0x77) -> None: # BME280_ADDRESS
from adafruit_bus_device import ( # pylint: disable=import-outside-toplevel
i2c_device,
)

self._i2c = i2c_device.I2CDevice(i2c, address)
super().__init__()

def _read_register(self, register: int, length: int) -> bytearray:
with self._i2c as i2c:
i2c.write(bytes([register & 0xFF]))
result = bytearray(length)
i2c.readinto(result)
return result

def _write_register_byte(self, register: int, value: int) -> None:
with self._i2c as i2c:
i2c.write(bytes([register & 0xFF, value & 0xFF]))
# print("$%02X <= 0x%02X" % (register, value))
super().__init__(I2C_Impl(i2c, address))


class Adafruit_BME280_SPI(Adafruit_BME280):
Expand Down Expand Up @@ -433,22 +418,4 @@ class Adafruit_BME280_SPI(Adafruit_BME280):
"""

def __init__(self, spi: SPI, cs: DigitalInOut, baudrate: int = 100000) -> None:
from adafruit_bus_device import ( # pylint: disable=import-outside-toplevel
spi_device,
)

self._spi = spi_device.SPIDevice(spi, cs, baudrate=baudrate)
super().__init__()

def _read_register(self, register: int, length: int) -> bytearray:
register = (register | 0x80) & 0xFF # Read single, bit 7 high.
with self._spi as spi:
spi.write(bytearray([register])) # pylint: disable=no-member
result = bytearray(length)
spi.readinto(result) # pylint: disable=no-member
return result

def _write_register_byte(self, register: int, value: int) -> None:
register &= 0x7F # Write, bit 7 low.
with self._spi as spi:
spi.write(bytes([register, value & 0xFF])) # pylint: disable=no-member
super().__init__(SPI_Impl(spi, cs, baudrate))
58 changes: 58 additions & 0 deletions adafruit_bme280/protocol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# SPDX-FileCopyrightText: 2022 Randall Bohn (dexter)
#
# SPDX-License-Identifier: MIT
"Provides the protocol objects for I2C and SPI"

from busio import I2C, SPI
from digitalio import DigitalInOut


class I2C_Impl:
"Protocol implementation for the I2C bus."

def __init__(self, i2c: I2C, address: int) -> None:
from adafruit_bus_device import ( # pylint: disable=import-outside-toplevel
i2c_device,
)

self._i2c = i2c_device.I2CDevice(i2c, address)

def read_register(self, register: int, length: int) -> bytearray:
"Read from the device register."
with self._i2c as i2c:
i2c.write(bytes([register & 0xFF]))
result = bytearray(length)
i2c.readinto(result)
return result

def write_register_byte(self, register: int, value: int) -> None:
"Write to the device register."
with self._i2c as i2c:
i2c.write(bytes([register & 0xFF, value & 0xFF]))


class SPI_Impl:
"Protocol implemenation for the SPI bus."

def __init__(self, spi: SPI, cs: DigitalInOut, baudrate: int = 100000) -> None:
from adafruit_bus_device import ( # pylint: disable=import-outside-toplevel
spi_device,
)

self._spi = spi_device.SPIDevice(spi, cs, baudrate=baudrate)

def read_register(self, register: int, length: int) -> bytearray:
"Read from the device register."
register = (register | 0x80) & 0xFF # Read single, bit 7 high.
with self._spi as spi:
spi.write(bytearray([register])) # pylint: disable=no-member
result = bytearray(length)
spi.readinto(result) # pylint: disable=no-member
# print("$%02X => %s" % (register, [hex(i) for i in result]))
return result

def write_register_byte(self, register: int, value: int) -> None:
"Write value to the device register."
register &= 0x7F # Write, bit 7 low.
with self._spi as spi:
spi.write(bytes([register, value & 0xFF])) # pylint: disable=no-member

0 comments on commit c7d4e1c

Please sign in to comment.