Skip to content

Commit

Permalink
Added SPI support
Browse files Browse the repository at this point in the history
  • Loading branch information
fxsheep committed Nov 18, 2023
1 parent 83dfdd6 commit d5c9a5b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ https://github.com/vitally/BMP280
https://github.com/micropython-IMU/micropython-bmp180

## Constructor
**BMP280(i2c_bus, addr=0x76, use_case=BMP280_CASE_HANDHELD_DYN)**
* *i2c_bus* - the I2C bus to use
* *addr* - I2C address of the BMP280 (always the same)
**BMP280(bus, addr_cs=0x76, use_case=BMP280_CASE_HANDHELD_DYN)**
* *bus* - the I2C/SPI bus to use
* *addr_cs* - when I2C is used: I2C address of the BMP280 (always the same), when SPI is used: CS pin
* *use_case* - Use case to start the BMP280 with. Set to None to disable measuring on boot.

## Enums
Expand Down
25 changes: 20 additions & 5 deletions bmp280.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@


class BMP280:
def __init__(self, i2c_bus, addr=0x76, use_case=BMP280_CASE_HANDHELD_DYN):
self._bmp_i2c = i2c_bus
self._i2c_addr = addr
# when 'bus' is an I2C object, 'addr_cs' is I2C address
# when 'bus' is an SPI object, 'addr_cs' is Pin object of the CS pin
def __init__(self, bus, addr_cs=0x76, use_case=BMP280_CASE_HANDHELD_DYN):
self._bmp_bus = bus
self._addr_cs = addr_cs

# read calibration data
# < little-endian
Expand Down Expand Up @@ -123,12 +125,25 @@ def __init__(self, i2c_bus, addr=0x76, use_case=BMP280_CASE_HANDHELD_DYN):
self.use_case(use_case)

def _read(self, addr, size=1):
return self._bmp_i2c.readfrom_mem(self._i2c_addr, addr, size)
if hasattr(self._bmp_bus, 'readfrom_mem'):
return self._bmp_bus.readfrom_mem(self._addr_cs, addr, size)
else:
self._addr_cs(0)
self._bmp_bus.write(bytearray([addr]))
dat = self._bmp_bus.read(size)
self._addr_cs(1)
return dat

def _write(self, addr, b_arr):
if not type(b_arr) is bytearray:
b_arr = bytearray([b_arr])
return self._bmp_i2c.writeto_mem(self._i2c_addr, addr, b_arr)
if hasattr(self._bmp_bus, 'writeto_mem'):
return self._bmp_bus.writeto_mem(self._addr_cs, addr, b_arr)
else:
self._addr_cs(0)
ret = self._bmp_bus.write(bytearray([addr & 0x7f]) + b_arr)
self._addr_cs(1)
return ret

def _gauge(self):
# TODO limit new reads
Expand Down

0 comments on commit d5c9a5b

Please sign in to comment.