55
55
56
56
import adafruit_bus_device .i2c_device as i2c_device
57
57
import adafruit_bus_device .spi_device as spi_device
58
+ from digitalio import Direction
58
59
59
60
from micropython import const
60
61
@@ -269,8 +270,7 @@ def acceleration(self):
269
270
m/s^2 values.
270
271
"""
271
272
raw = self .read_accel_raw ()
272
- return map (lambda x : x * self ._accel_mg_lsb / 1000.0 * _SENSORS_GRAVITY_STANDARD ,
273
- raw )
273
+ return (x * self ._accel_mg_lsb / 1000.0 * _SENSORS_GRAVITY_STANDARD for x in raw )
274
274
275
275
def read_mag_raw (self ):
276
276
"""Read the raw magnetometer sensor values and return it as a
@@ -290,7 +290,7 @@ def magnetic(self):
290
290
gauss values.
291
291
"""
292
292
raw = self .read_mag_raw ()
293
- return map ( lambda x : x * self ._mag_mgauss_lsb / 1000.0 , raw )
293
+ return ( x * self ._mag_mgauss_lsb / 1000.0 for x in raw )
294
294
295
295
def read_gyro_raw (self ):
296
296
"""Read the raw gyroscope sensor values and return it as a
@@ -310,7 +310,7 @@ def gyro(self):
310
310
degrees/second values.
311
311
"""
312
312
raw = self .read_gyro_raw ()
313
- return map ( lambda x : x * self ._gyro_dps_digit , raw )
313
+ return ( x * self ._gyro_dps_digit for x in raw )
314
314
315
315
def read_temp_raw (self ):
316
316
"""Read the raw temperature sensor value and return it as a 16-bit
@@ -362,14 +362,7 @@ def __init__(self, i2c):
362
362
super ().__init__ ()
363
363
364
364
def _read_u8 (self , sensor_type , address ):
365
- if sensor_type == _GYROTYPE :
366
- device = self ._gyro_device
367
- else :
368
- device = self ._xm_device
369
- with device as i2c :
370
- self ._BUFFER [0 ] = address & 0xFF
371
- i2c .write (self ._BUFFER , end = 1 , stop = False )
372
- i2c .readinto (self ._BUFFER , end = 1 )
365
+ self ._read_bytes (sensor_type , address , 1 , self ._BUFFER )
373
366
return self ._BUFFER [0 ]
374
367
375
368
def _read_bytes (self , sensor_type , address , count , buf ):
@@ -379,8 +372,9 @@ def _read_bytes(self, sensor_type, address, count, buf):
379
372
device = self ._xm_device
380
373
with device as i2c :
381
374
buf [0 ] = address & 0xFF
382
- i2c .write (buf , end = 1 , stop = False )
375
+ i2c .write (buf , end = 1 )
383
376
i2c .readinto (buf , end = count )
377
+ # print("read from %02x: %s" % (address, [hex(i) for i in buf[:count]]))
384
378
385
379
def _write_u8 (self , sensor_type , address , val ):
386
380
if sensor_type == _GYROTYPE :
@@ -391,26 +385,23 @@ def _write_u8(self, sensor_type, address, val):
391
385
self ._BUFFER [0 ] = address & 0xFF
392
386
self ._BUFFER [1 ] = val & 0xFF
393
387
i2c .write (self ._BUFFER , end = 2 )
388
+ # print("write to %02x: %02x" % (address, val))
394
389
395
390
396
391
class LSM9DS0_SPI (LSM9DS0 ):
397
392
"""Driver for the LSM9DS0 connected over SPI."""
398
393
# pylint: disable=no-member
399
394
def __init__ (self , spi , xmcs , gcs ):
400
- self ._gyro_device = spi_device .I2CDevice (spi , gcs )
401
- self ._xm_device = spi_device .I2CDevice (spi , xmcs )
395
+ gcs .direction = Direction .OUTPUT
396
+ gcs .value = True
397
+ xmcs .direction = Direction .OUTPUT
398
+ xmcs .value = True
399
+ self ._gyro_device = spi_device .SPIDevice (spi , gcs )
400
+ self ._xm_device = spi_device .SPIDevice (spi , xmcs )
402
401
super ().__init__ ()
403
402
404
403
def _read_u8 (self , sensor_type , address ):
405
- if sensor_type == _GYROTYPE :
406
- device = self ._gyro_device
407
- else :
408
- device = self ._xm_device
409
- with device as spi :
410
- spi .configure (baudrate = 200000 , phase = 0 , polarity = 0 )
411
- self ._BUFFER [0 ] = (address | 0x80 ) & 0xFF
412
- spi .write (self ._BUFFER , end = 1 )
413
- spi .readinto (self ._BUFFER , end = 1 )
404
+ self ._read_bytes (sensor_type , address , 1 , self ._BUFFER )
414
405
return self ._BUFFER [0 ]
415
406
416
407
def _read_bytes (self , sensor_type , address , count , buf ):
@@ -419,18 +410,18 @@ def _read_bytes(self, sensor_type, address, count, buf):
419
410
else :
420
411
device = self ._xm_device
421
412
with device as spi :
422
- spi .configure (baudrate = 200000 , phase = 0 , polarity = 0 )
423
- buf [0 ] = (address | 0x80 ) & 0xFF
413
+ buf [0 ] = (address | 0x80 | 0x40 ) & 0xFF
424
414
spi .write (buf , end = 1 )
425
415
spi .readinto (buf , end = count )
416
+ # print("read from %02x: %s" % (address, [hex(i) for i in buf[:count]]))
426
417
427
418
def _write_u8 (self , sensor_type , address , val ):
428
419
if sensor_type == _GYROTYPE :
429
420
device = self ._gyro_device
430
421
else :
431
422
device = self ._xm_device
432
423
with device as spi :
433
- spi .configure (baudrate = 200000 , phase = 0 , polarity = 0 )
434
424
self ._BUFFER [0 ] = (address & 0x7F ) & 0xFF
435
425
self ._BUFFER [1 ] = val & 0xFF
436
426
spi .write (self ._BUFFER , end = 2 )
427
+ # print("write to %02x: %02x" % (address, val))
0 commit comments