diff --git a/adafruit_lsm9ds0.py b/adafruit_lsm9ds0.py index 24bd66f..14801d7 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 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 @@ -310,7 +310,7 @@ def gyro(self): degrees/second values. """ raw = self.read_gyro_raw() - return map(lambda x: x * self._gyro_dps_digit, 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,26 +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.I2CDevice(spi, gcs) - self._xm_device = spi_device.I2CDevice(spi, xmcs) + 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: - 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) + self._read_bytes(sensor_type, address, 1, self._BUFFER) return self._BUFFER[0] def _read_bytes(self, sensor_type, address, count, buf): @@ -419,10 +410,10 @@ 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 + 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: @@ -430,7 +421,7 @@ 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) + # print("write to %02x: %02x" % (address, val)) diff --git a/examples/lsm9ds0_simpletest.py b/examples/lsm9ds0_simpletest.py index 47707a1..9485345 100644 --- a/examples/lsm9ds0_simpletest.py +++ b/examples/lsm9ds0_simpletest.py @@ -12,6 +12,13 @@ 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) +# 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. while True: