52
52
_BME680_REG_SOFTRESET = const (0xE0 )
53
53
_BME680_REG_CTRL_GAS = const (0x71 )
54
54
_BME680_REG_CTRL_HUM = const (0x72 )
55
- _BME280_REG_STATUS = const (0xF3 )
55
+ _BME680_REG_STATUS = const (0x73 )
56
56
_BME680_REG_CTRL_MEAS = const (0x74 )
57
57
_BME680_REG_CONFIG = const (0x75 )
58
58
59
- _BME680_REG_STATUS = const (0x1D )
59
+ _BME680_REG_MEAS_STATUS = const (0x1D )
60
60
_BME680_REG_PDATA = const (0x1F )
61
61
_BME680_REG_TDATA = const (0x22 )
62
62
_BME680_REG_HDATA = const (0x25 )
@@ -266,7 +266,7 @@ def _perform_reading(self):
266
266
self ._write (_BME680_REG_CTRL_MEAS , [ctrl ])
267
267
new_data = False
268
268
while not new_data :
269
- data = self ._read (_BME680_REG_STATUS , 15 )
269
+ data = self ._read (_BME680_REG_MEAS_STATUS , 15 )
270
270
new_data = data [0 ] & 0x80 != 0
271
271
time .sleep (0.005 )
272
272
self ._last_reading = time .monotonic ()
@@ -324,7 +324,7 @@ class Adafruit_BME680_I2C(Adafruit_BME680):
324
324
will be from the previous reading."""
325
325
def __init__ (self , i2c , address = 0x77 , debug = False , * , refresh_rate = 10 ):
326
326
"""Initialize the I2C device at the 'address' given"""
327
- import adafruit_bus_device . i2c_device as i2c_device
327
+ from adafruit_bus_device import i2c_device
328
328
self ._i2c = i2c_device .I2CDevice (i2c , address )
329
329
self ._debug = debug
330
330
super ().__init__ (refresh_rate = refresh_rate )
@@ -349,3 +349,56 @@ def _write(self, register, values):
349
349
i2c .write (buffer )
350
350
if self ._debug :
351
351
print ("\t $%02X <= %s" % (values [0 ], [hex (i ) for i in values [1 :]]))
352
+
353
+ class Adafruit_BME680_SPI (Adafruit_BME680 ):
354
+ """Driver for SPI connected BME680.
355
+
356
+ :param busio.SPI spi: SPI device
357
+ :param digitalio.DigitalInOut cs: Chip Select
358
+ :param bool debug: Print debug statements when True.
359
+ :param int baudrate: Clock rate, default is 100000
360
+ :param int refresh_rate: Maximum number of readings per second. Faster property reads
361
+ will be from the previous reading.
362
+ """
363
+
364
+ def __init__ (self , spi , cs , baudrate = 100000 , debug = False , * , refresh_rate = 10 ):
365
+ from adafruit_bus_device import spi_device
366
+ self ._spi = spi_device .SPIDevice (spi , cs , baudrate = baudrate )
367
+ self ._debug = debug
368
+ super ().__init__ (refresh_rate = refresh_rate )
369
+
370
+ def _read (self , register , length ):
371
+ if register != _BME680_REG_STATUS :
372
+ #_BME680_REG_STATUS exists in both SPI memory pages
373
+ #For all other registers, we must set the correct memory page
374
+ self ._set_spi_mem_page (register )
375
+
376
+ register = (register | 0x80 ) & 0xFF # Read single, bit 7 high.
377
+ with self ._spi as spi :
378
+ spi .write (bytearray ([register ])) #pylint: disable=no-member
379
+ result = bytearray (length )
380
+ spi .readinto (result ) #pylint: disable=no-member
381
+ if self ._debug :
382
+ print ("\t $%02X => %s" % (register , [hex (i ) for i in result ]))
383
+ return result
384
+
385
+ def _write (self , register , values ):
386
+ if register != _BME680_REG_STATUS :
387
+ #_BME680_REG_STATUS exists in both SPI memory pages
388
+ #For all other registers, we must set the correct memory page
389
+ self ._set_spi_mem_page (register )
390
+ register &= 0x7F # Write, bit 7 low.
391
+ with self ._spi as spi :
392
+ buffer = bytearray (2 * len (values ))
393
+ for i , value in enumerate (values ):
394
+ buffer [2 * i ] = register + i
395
+ buffer [2 * i + 1 ] = value & 0xFF
396
+ spi .write (buffer ) #pylint: disable=no-member
397
+ if self ._debug :
398
+ print ("\t $%02X <= %s" % (values [0 ], [hex (i ) for i in values [1 :]]))
399
+
400
+ def _set_spi_mem_page (self , register ):
401
+ spi_mem_page = 0x00
402
+ if register < 0x80 :
403
+ spi_mem_page = 0x10
404
+ self ._write (_BME680_REG_STATUS , [spi_mem_page ])
0 commit comments