Skip to content

Commit d5c9a5b

Browse files
committed
Added SPI support
1 parent 83dfdd6 commit d5c9a5b

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ https://github.com/vitally/BMP280
99
https://github.com/micropython-IMU/micropython-bmp180
1010

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

1717
## Enums

bmp280.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@
8686

8787

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

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

125127
def _read(self, addr, size=1):
126-
return self._bmp_i2c.readfrom_mem(self._i2c_addr, addr, size)
128+
if hasattr(self._bmp_bus, 'readfrom_mem'):
129+
return self._bmp_bus.readfrom_mem(self._addr_cs, addr, size)
130+
else:
131+
self._addr_cs(0)
132+
self._bmp_bus.write(bytearray([addr]))
133+
dat = self._bmp_bus.read(size)
134+
self._addr_cs(1)
135+
return dat
127136

128137
def _write(self, addr, b_arr):
129138
if not type(b_arr) is bytearray:
130139
b_arr = bytearray([b_arr])
131-
return self._bmp_i2c.writeto_mem(self._i2c_addr, addr, b_arr)
140+
if hasattr(self._bmp_bus, 'writeto_mem'):
141+
return self._bmp_bus.writeto_mem(self._addr_cs, addr, b_arr)
142+
else:
143+
self._addr_cs(0)
144+
ret = self._bmp_bus.write(bytearray([addr & 0x7f]) + b_arr)
145+
self._addr_cs(1)
146+
return ret
132147

133148
def _gauge(self):
134149
# TODO limit new reads

0 commit comments

Comments
 (0)