Skip to content

Commit 78471b1

Browse files
authored
Merge pull request #11 from caternuson/iss10
Fixes for SPI class
2 parents 11a7497 + 5374e18 commit 78471b1

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

adafruit_lsm9ds0.py

+18-27
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555

5656
import adafruit_bus_device.i2c_device as i2c_device
5757
import adafruit_bus_device.spi_device as spi_device
58+
from digitalio import Direction
5859

5960
from micropython import const
6061

@@ -269,8 +270,7 @@ def acceleration(self):
269270
m/s^2 values.
270271
"""
271272
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)
274274

275275
def read_mag_raw(self):
276276
"""Read the raw magnetometer sensor values and return it as a
@@ -290,7 +290,7 @@ def magnetic(self):
290290
gauss values.
291291
"""
292292
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)
294294

295295
def read_gyro_raw(self):
296296
"""Read the raw gyroscope sensor values and return it as a
@@ -310,7 +310,7 @@ def gyro(self):
310310
degrees/second values.
311311
"""
312312
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)
314314

315315
def read_temp_raw(self):
316316
"""Read the raw temperature sensor value and return it as a 16-bit
@@ -362,14 +362,7 @@ def __init__(self, i2c):
362362
super().__init__()
363363

364364
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)
373366
return self._BUFFER[0]
374367

375368
def _read_bytes(self, sensor_type, address, count, buf):
@@ -379,8 +372,9 @@ def _read_bytes(self, sensor_type, address, count, buf):
379372
device = self._xm_device
380373
with device as i2c:
381374
buf[0] = address & 0xFF
382-
i2c.write(buf, end=1, stop=False)
375+
i2c.write(buf, end=1)
383376
i2c.readinto(buf, end=count)
377+
# print("read from %02x: %s" % (address, [hex(i) for i in buf[:count]]))
384378

385379
def _write_u8(self, sensor_type, address, val):
386380
if sensor_type == _GYROTYPE:
@@ -391,26 +385,23 @@ def _write_u8(self, sensor_type, address, val):
391385
self._BUFFER[0] = address & 0xFF
392386
self._BUFFER[1] = val & 0xFF
393387
i2c.write(self._BUFFER, end=2)
388+
# print("write to %02x: %02x" % (address, val))
394389

395390

396391
class LSM9DS0_SPI(LSM9DS0):
397392
"""Driver for the LSM9DS0 connected over SPI."""
398393
# pylint: disable=no-member
399394
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)
402401
super().__init__()
403402

404403
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)
414405
return self._BUFFER[0]
415406

416407
def _read_bytes(self, sensor_type, address, count, buf):
@@ -419,18 +410,18 @@ def _read_bytes(self, sensor_type, address, count, buf):
419410
else:
420411
device = self._xm_device
421412
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
424414
spi.write(buf, end=1)
425415
spi.readinto(buf, end=count)
416+
# print("read from %02x: %s" % (address, [hex(i) for i in buf[:count]]))
426417

427418
def _write_u8(self, sensor_type, address, val):
428419
if sensor_type == _GYROTYPE:
429420
device = self._gyro_device
430421
else:
431422
device = self._xm_device
432423
with device as spi:
433-
spi.configure(baudrate=200000, phase=0, polarity=0)
434424
self._BUFFER[0] = (address & 0x7F) & 0xFF
435425
self._BUFFER[1] = val & 0xFF
436426
spi.write(self._BUFFER, end=2)
427+
# print("write to %02x: %02x" % (address, val))

examples/lsm9ds0_simpletest.py

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
i2c = busio.I2C(board.SCL, board.SDA)
1313
sensor = adafruit_lsm9ds0.LSM9DS0_I2C(i2c)
1414

15+
#SPI connection:
16+
# from digitalio import DigitalInOut, Direction
17+
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
18+
# gcs = DigitalInOut(board.D5)
19+
# xmcs = DigitalInOut(board.D6)
20+
# sensor = adafruit_lsm9ds0.LSM9DS0_SPI(spi, xmcs, gcs)
21+
1522
# Main loop will read the acceleration, magnetometer, gyroscope, Temperature
1623
# values every second and print them out.
1724
while True:

0 commit comments

Comments
 (0)