From b3a79669d05a06c095dfdd040d1d53bfce0b70bd Mon Sep 17 00:00:00 2001 From: caternuson Date: Wed, 10 Oct 2018 10:28:31 -0700 Subject: [PATCH 1/6] fix for SPI class --- adafruit_lsm9ds0.py | 7 ++----- examples/lsm9ds0_simpletest.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/adafruit_lsm9ds0.py b/adafruit_lsm9ds0.py index 8cd89eb..56e62e6 100644 --- a/adafruit_lsm9ds0.py +++ b/adafruit_lsm9ds0.py @@ -397,8 +397,8 @@ class LSM9DS0_SPI(LSM9DS0): """Driver for the LSM9DS0 connected over SPI.""" # pylint: disable=no-member def __init__(self, spi, xmcs, gcs): - self._gyro_device = spi_device.I2CDevice(spi, gcs) - self._xm_device = spi_device.I2CDevice(spi, xmcs) + self._mag_device = spi_device.SPIDevice(spi, mcs, baudrate=200000, phase=1, polarity=1) + self._xg_device = spi_device.SPIDevice(spi, xgcs, baudrate=200000, phase=1, polarity=1) super().__init__() def _read_u8(self, sensor_type, address): @@ -407,7 +407,6 @@ def _read_u8(self, sensor_type, address): else: device = self._xm_device with device as spi: - spi.configure(baudrate=200000, phase=0, polarity=0) self._BUFFER[0] = (address | 0x80) & 0xFF spi.write(self._BUFFER, end=1) spi.readinto(self._BUFFER, end=1) @@ -419,7 +418,6 @@ def _read_bytes(self, sensor_type, address, count, buf): else: device = self._xm_device with device as spi: - spi.configure(baudrate=200000, phase=0, polarity=0) buf[0] = (address | 0x80) & 0xFF spi.write(buf, end=1) spi.readinto(buf, end=count) @@ -430,7 +428,6 @@ def _write_u8(self, sensor_type, address, val): else: device = self._xm_device with device as spi: - spi.configure(baudrate=200000, phase=0, polarity=0) self._BUFFER[0] = (address & 0x7F) & 0xFF self._BUFFER[1] = val & 0xFF spi.write(self._BUFFER, end=2) diff --git a/examples/lsm9ds0_simpletest.py b/examples/lsm9ds0_simpletest.py index 47707a1..c1c37c4 100644 --- a/examples/lsm9ds0_simpletest.py +++ b/examples/lsm9ds0_simpletest.py @@ -12,6 +12,17 @@ i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_lsm9ds0.LSM9DS0_I2C(i2c) +#SPI connection: +# from digitalio import DigitalInOut, Direction +# spi = busio.SPI(board.SCK, board.MOSI, board.MISO) +# csag = DigitalInOut(board.D5) +# csag.direction = Direction.OUTPUT +# csag.value = True +# csm = DigitalInOut(board.D6) +# csm.direction = Direction.OUTPUT +# csm.value = True +# sensor = adafruit_lsm9ds0.LSM9DS0_SPI(spi, csag, csm) + # Main loop will read the acceleration, magnetometer, gyroscope, Temperature # values every second and print them out. while True: From 55cbf04e3d0c3c0da500e13679a4a41ece17604f Mon Sep 17 00:00:00 2001 From: caternuson Date: Wed, 10 Oct 2018 10:33:51 -0700 Subject: [PATCH 2/6] fix copypasta --- adafruit_lsm9ds0.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_lsm9ds0.py b/adafruit_lsm9ds0.py index 56e62e6..b70885e 100644 --- a/adafruit_lsm9ds0.py +++ b/adafruit_lsm9ds0.py @@ -397,8 +397,8 @@ class LSM9DS0_SPI(LSM9DS0): """Driver for the LSM9DS0 connected over SPI.""" # pylint: disable=no-member def __init__(self, spi, xmcs, gcs): - self._mag_device = spi_device.SPIDevice(spi, mcs, baudrate=200000, phase=1, polarity=1) - self._xg_device = spi_device.SPIDevice(spi, xgcs, baudrate=200000, phase=1, polarity=1) + self._mag_device = spi_device.SPIDevice(spi, xmcs, baudrate=200000, phase=1, polarity=1) + self._xg_device = spi_device.SPIDevice(spi, gcs, baudrate=200000, phase=1, polarity=1) super().__init__() def _read_u8(self, sensor_type, address): From d024a3c41f17a753792290d3bb508b2d9b14367e Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 20 Nov 2018 10:15:41 -0800 Subject: [PATCH 3/6] fix class member names --- adafruit_lsm9ds0.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_lsm9ds0.py b/adafruit_lsm9ds0.py index b70885e..f17636d 100644 --- a/adafruit_lsm9ds0.py +++ b/adafruit_lsm9ds0.py @@ -397,8 +397,8 @@ class LSM9DS0_SPI(LSM9DS0): """Driver for the LSM9DS0 connected over SPI.""" # pylint: disable=no-member def __init__(self, spi, xmcs, gcs): - self._mag_device = spi_device.SPIDevice(spi, xmcs, baudrate=200000, phase=1, polarity=1) - self._xg_device = spi_device.SPIDevice(spi, gcs, baudrate=200000, phase=1, polarity=1) + self._gyro_device = spi_device.SPIDevice(spi, gcs, baudrate=200000, phase=1, polarity=1) + self._xm_device = spi_device.SPIDevice(spi, xmcs, baudrate=200000, phase=1, polarity=1) super().__init__() def _read_u8(self, sensor_type, address): From ed4492c5cb41f865ef0139b28a4c22a8a5400cca Mon Sep 17 00:00:00 2001 From: caternuson Date: Fri, 23 Nov 2018 08:26:20 -0800 Subject: [PATCH 4/6] ladyada edits --- adafruit_lsm9ds0.py | 44 +++++++++++++++------------------- examples/lsm9ds0_simpletest.py | 10 +++----- 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/adafruit_lsm9ds0.py b/adafruit_lsm9ds0.py index f17636d..2675d21 100644 --- a/adafruit_lsm9ds0.py +++ b/adafruit_lsm9ds0.py @@ -55,6 +55,7 @@ import adafruit_bus_device.i2c_device as i2c_device import adafruit_bus_device.spi_device as spi_device +from digitalio import DigitalInOut, Direction from micropython import const @@ -269,8 +270,7 @@ def acceleration(self): m/s^2 values. """ raw = self.read_accel_raw() - return map(lambda x: x * self._accel_mg_lsb / 1000.0 * _SENSORS_GRAVITY_STANDARD, - raw) + return (x * self._accel_mg_lsb / 1000.0 * _SENSORS_GRAVITY_STANDARD for x in raw) def read_mag_raw(self): """Read the raw magnetometer sensor values and return it as a @@ -290,7 +290,7 @@ def magnetic(self): gauss values. """ raw = self.read_mag_raw() - return map(lambda x: x * self._mag_mgauss_lsb / 1000.0, raw) + return (x * self._mag_mgauss_lsb / 1000.0 for x in raw) def read_gyro_raw(self): """Read the raw gyroscope sensor values and return it as a @@ -309,8 +309,8 @@ def gyro(self): """The gyroscope X, Y, Z axis values as a 3-tuple of degrees/second values. """ - raw = self.read_mag_raw() - return map(lambda x: x * self._gyro_dps_digit, raw) + raw = self.read_gyro_raw() + return (x * self._gyro_dps_digit for x in raw) def read_temp_raw(self): """Read the raw temperature sensor value and return it as a 16-bit @@ -362,14 +362,7 @@ def __init__(self, i2c): super().__init__() def _read_u8(self, sensor_type, address): - if sensor_type == _GYROTYPE: - device = self._gyro_device - else: - device = self._xm_device - with device as i2c: - self._BUFFER[0] = address & 0xFF - i2c.write(self._BUFFER, end=1, stop=False) - i2c.readinto(self._BUFFER, end=1) + self._read_bytes(sensor_type, address, 1, self._BUFFER) return self._BUFFER[0] def _read_bytes(self, sensor_type, address, count, buf): @@ -379,8 +372,9 @@ def _read_bytes(self, sensor_type, address, count, buf): device = self._xm_device with device as i2c: buf[0] = address & 0xFF - i2c.write(buf, end=1, stop=False) + i2c.write(buf, end=1) i2c.readinto(buf, end=count) + #print("read from %02x: %s" % (address, [hex(i) for i in buf[:count]])) def _write_u8(self, sensor_type, address, val): if sensor_type == _GYROTYPE: @@ -391,25 +385,23 @@ def _write_u8(self, sensor_type, address, val): self._BUFFER[0] = address & 0xFF self._BUFFER[1] = val & 0xFF i2c.write(self._BUFFER, end=2) + #print("write to %02x: %02x" % (address, val)) class LSM9DS0_SPI(LSM9DS0): """Driver for the LSM9DS0 connected over SPI.""" # pylint: disable=no-member def __init__(self, spi, xmcs, gcs): - self._gyro_device = spi_device.SPIDevice(spi, gcs, baudrate=200000, phase=1, polarity=1) - self._xm_device = spi_device.SPIDevice(spi, xmcs, baudrate=200000, phase=1, polarity=1) + gcs.direction = Direction.OUTPUT + gcs.value = True + xmcs.direction = Direction.OUTPUT + xmcs.value = True + self._gyro_device = spi_device.SPIDevice(spi, gcs) + self._xm_device = spi_device.SPIDevice(spi, xmcs) super().__init__() def _read_u8(self, sensor_type, address): - if sensor_type == _GYROTYPE: - device = self._gyro_device - else: - device = self._xm_device - with device as spi: - self._BUFFER[0] = (address | 0x80) & 0xFF - spi.write(self._BUFFER, end=1) - spi.readinto(self._BUFFER, end=1) + self._read_bytes(sensor_type, address, 1, self._BUFFER) return self._BUFFER[0] def _read_bytes(self, sensor_type, address, count, buf): @@ -418,9 +410,10 @@ def _read_bytes(self, sensor_type, address, count, buf): else: device = self._xm_device with device as spi: - buf[0] = (address | 0x80) & 0xFF + buf[0] = (address | 0x80 | 0x40) & 0xFF spi.write(buf, end=1) spi.readinto(buf, end=count) + #print("read from %02x: %s" % (address, [hex(i) for i in buf[:count]])) def _write_u8(self, sensor_type, address, val): if sensor_type == _GYROTYPE: @@ -431,3 +424,4 @@ def _write_u8(self, sensor_type, address, val): self._BUFFER[0] = (address & 0x7F) & 0xFF self._BUFFER[1] = val & 0xFF spi.write(self._BUFFER, end=2) + #print("write to %02x: %02x" % (address, val)) \ No newline at end of file diff --git a/examples/lsm9ds0_simpletest.py b/examples/lsm9ds0_simpletest.py index c1c37c4..9485345 100644 --- a/examples/lsm9ds0_simpletest.py +++ b/examples/lsm9ds0_simpletest.py @@ -15,13 +15,9 @@ #SPI connection: # from digitalio import DigitalInOut, Direction # spi = busio.SPI(board.SCK, board.MOSI, board.MISO) -# csag = DigitalInOut(board.D5) -# csag.direction = Direction.OUTPUT -# csag.value = True -# csm = DigitalInOut(board.D6) -# csm.direction = Direction.OUTPUT -# csm.value = True -# sensor = adafruit_lsm9ds0.LSM9DS0_SPI(spi, csag, csm) +# gcs = DigitalInOut(board.D5) +# xmcs = DigitalInOut(board.D6) +# sensor = adafruit_lsm9ds0.LSM9DS0_SPI(spi, xmcs, gcs) # Main loop will read the acceleration, magnetometer, gyroscope, Temperature # values every second and print them out. From aebd0dd37627af68821b9576afa795b43d5513ea Mon Sep 17 00:00:00 2001 From: caternuson Date: Fri, 23 Nov 2018 09:02:01 -0800 Subject: [PATCH 5/6] lint --- adafruit_lsm9ds0.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_lsm9ds0.py b/adafruit_lsm9ds0.py index 2675d21..9f58b9f 100644 --- a/adafruit_lsm9ds0.py +++ b/adafruit_lsm9ds0.py @@ -55,7 +55,7 @@ import adafruit_bus_device.i2c_device as i2c_device import adafruit_bus_device.spi_device as spi_device -from digitalio import DigitalInOut, Direction +from digitalio import Direction from micropython import const @@ -424,4 +424,4 @@ def _write_u8(self, sensor_type, address, val): self._BUFFER[0] = (address & 0x7F) & 0xFF self._BUFFER[1] = val & 0xFF spi.write(self._BUFFER, end=2) - #print("write to %02x: %02x" % (address, val)) \ No newline at end of file + #print("write to %02x: %02x" % (address, val)) From 5374e18fb36941dc9d0c9716e3cd51ff85f8f9b2 Mon Sep 17 00:00:00 2001 From: caternuson Date: Thu, 29 Nov 2018 14:44:50 -0800 Subject: [PATCH 6/6] spaces --- adafruit_lsm9ds0.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_lsm9ds0.py b/adafruit_lsm9ds0.py index 9f58b9f..14801d7 100644 --- a/adafruit_lsm9ds0.py +++ b/adafruit_lsm9ds0.py @@ -374,7 +374,7 @@ def _read_bytes(self, sensor_type, address, count, buf): buf[0] = address & 0xFF i2c.write(buf, end=1) i2c.readinto(buf, end=count) - #print("read from %02x: %s" % (address, [hex(i) for i in buf[:count]])) + # print("read from %02x: %s" % (address, [hex(i) for i in buf[:count]])) def _write_u8(self, sensor_type, address, val): if sensor_type == _GYROTYPE: @@ -385,7 +385,7 @@ def _write_u8(self, sensor_type, address, val): self._BUFFER[0] = address & 0xFF self._BUFFER[1] = val & 0xFF i2c.write(self._BUFFER, end=2) - #print("write to %02x: %02x" % (address, val)) + # print("write to %02x: %02x" % (address, val)) class LSM9DS0_SPI(LSM9DS0): @@ -413,7 +413,7 @@ def _read_bytes(self, sensor_type, address, count, buf): buf[0] = (address | 0x80 | 0x40) & 0xFF spi.write(buf, end=1) spi.readinto(buf, end=count) - #print("read from %02x: %s" % (address, [hex(i) for i in buf[:count]])) + # print("read from %02x: %s" % (address, [hex(i) for i in buf[:count]])) def _write_u8(self, sensor_type, address, val): if sensor_type == _GYROTYPE: @@ -424,4 +424,4 @@ def _write_u8(self, sensor_type, address, val): self._BUFFER[0] = (address & 0x7F) & 0xFF self._BUFFER[1] = val & 0xFF spi.write(self._BUFFER, end=2) - #print("write to %02x: %02x" % (address, val)) + # print("write to %02x: %02x" % (address, val))