Skip to content

Fixes for SPI class #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 18 additions & 27 deletions adafruit_lsm9ds0.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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]]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove print statement. Seems like it's left over from debugging, shouldn't be needed in the final release.


def _write_u8(self, sensor_type, address, val):
if sensor_type == _GYROTYPE:
Expand All @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.



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):
Expand All @@ -419,18 +410,18 @@ 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]]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.


def _write_u8(self, sensor_type, address, val):
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 & 0x7F) & 0xFF
self._BUFFER[1] = val & 0xFF
spi.write(self._BUFFER, end=2)
#print("write to %02x: %02x" % (address, val))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

7 changes: 7 additions & 0 deletions examples/lsm9ds0_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example copy-pasted from LSM9DS1 is probably all wrong also. For one thing, the CS pins are taken in a different order for the LSM9DS0:

def __init__(self, spi, xmcs, gcs):

# Main loop will read the acceleration, magnetometer, gyroscope, Temperature
# values every second and print them out.
while True:
Expand Down