Skip to content
This repository was archived by the owner on Oct 29, 2019. It is now read-only.

Update to method names and linting #3

Merged
merged 2 commits into from
Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Usage Example
=============

.. code-block:: python

import time
import board
import busio
Expand All @@ -38,10 +38,10 @@ Usage Example
sensor = adafruit_lsm303.LSM303(i2c)

while True:
raw_accel_x, raw_accel_y, raw_accel_z = sensor.raw_accelerometer
accel_x, accel_y, accel_z = sensor.accelerometer
raw_mag_x, raw_mag_y, raw_mag_z = sensor.raw_magnetometer
mag_x, mag_y, mag_z = sensor.magnetometer
raw_accel_x, raw_accel_y, raw_accel_z = sensor.raw_acceleration
accel_x, accel_y, accel_z = sensor.acceleration
raw_mag_x, raw_mag_y, raw_mag_z = sensor.raw_magnetic
mag_x, mag_y, mag_z = sensor.magnetic

print('Acceleration raw: ({0:6d}, {1:6d}, {2:6d}), (m/s^2): ({3:10.3f}, {4:10.3f}, {5:10.3f})'.format(raw_accel_x, raw_accel_y, raw_accel_z, accel_x, accel_y, accel_z))
print('Magnetometer raw: ({0:6d}, {1:6d}, {2:6d}), (gauss): ({3:10.3f}, {4:10.3f}, {5:10.3f})'.format(raw_mag_x, raw_mag_y, raw_mag_z, mag_x, mag_y, mag_z))
Expand Down
57 changes: 24 additions & 33 deletions adafruit_lsm303.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@
_LSM303ACCEL_MG_LSB = 16704.0
_GRAVITY_STANDARD = 9.80665 # Earth's gravity in m/s^2
_GAUSS_TO_MICROTESLA = 100.0 # Gauss to micro-Tesla multiplier
# pylint: enable=bad-whitespace
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this meant to be left in? (surprised that's not a default check)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it's meant to be there, it's needed because of the style choice to have all the registers line up, however, at the start it was disabled for the entire file which isn't consistent with the way we've been doing it, so I resolved the white space issues present in the rest of the file and enabled pylint again.

Copy link
Contributor

Choose a reason for hiding this comment

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

the things we do to keep the lint monster happy



class LSM303(object):
"""Driver for the LSM303 accelerometer/magnetometer."""
Expand All @@ -128,122 +130,111 @@ class LSM303(object):
def __init__(self, i2c):
self._accel_device = I2CDevice(i2c, _ADDRESS_ACCEL)
self._mag_device = I2CDevice(i2c, _ADDRESS_MAG)
self._write_u8(self._accel_device, _REG_ACCEL_CTRL_REG1_A, 0x27) # Enable the accelerometer
self._write_u8(self._mag_device, _REG_MAG_MR_REG_M, 0x00) # Enable the magnetometer
self._write_u8(self._accel_device, _REG_ACCEL_CTRL_REG1_A, 0x27) # Enable the accelerometer
self._write_u8(self._mag_device, _REG_MAG_MR_REG_M, 0x00) # Enable the magnetometer
self._lsm303mag_gauss_lsb_xy = 1100.0
self._lsm303mag_gauss_lsb_z = 980.0
self._mag_gain = MAGGAIN_1_3
self._mag_rate = MAGRATE_0_7

@property
def raw_accelerometer(self):
def raw_acceleration(self):
"""The raw accelerometer sensor values.
A 3-tuple of X, Y, Z axis values that are 16-bit signed integers.
"""
self._read_bytes(self._accel_device, _REG_ACCEL_OUT_X_L_A | 0x80, 6, self._BUFFER)
return struct.unpack_from('<hhh', self._BUFFER[0:6])


@property
def accelerometer(self):
def acceleration(self):
"""The processed accelerometer sensor values.
A 3-tuple of X, Y, Z axis values in meters per second squared that are signed floats.
"""
raw_accel_data = self.raw_accelerometer
return [n / _LSM303ACCEL_MG_LSB * _GRAVITY_STANDARD for n in raw_accel_data]

raw_accel_data = self.raw_acceleration
return tuple([n / _LSM303ACCEL_MG_LSB * _GRAVITY_STANDARD for n in raw_accel_data])

@property
def raw_magnetometer(self):
def raw_magnetic(self):
"""The raw magnetometer sensor values.
A 3-tuple of X, Y, Z axis values that are 16-bit signed integers.
"""
self._read_bytes(self._mag_device, _REG_MAG_OUT_X_H_M, 6, self._BUFFER)
raw_values = struct.unpack_from('>hhh', self._BUFFER[0:6])
return [n >> 4 for n in raw_values]
Copy link
Contributor

Choose a reason for hiding this comment

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

Change this to return a tuple also for consistency.



@property
def magnetometer(self):
def magnetic(self):
"""The processed magnetometer sensor values.
A 3-tuple of X, Y, Z axis values in microteslas that are signed floats.
"""
mag_x, mag_y, mag_z = self.raw_magnetometer
mag_x, mag_y, mag_z = self.raw_magnetic
return (mag_x / self._lsm303mag_gauss_lsb_xy * _GAUSS_TO_MICROTESLA,
mag_y / self._lsm303mag_gauss_lsb_xy * _GAUSS_TO_MICROTESLA,
mag_z / self._lsm303mag_gauss_lsb_z * _GAUSS_TO_MICROTESLA)


@property
def mag_gain(self):
"""The magnetometer's gain."""
return self._mag_gain


@mag_gain.setter
def mag_gain(self, value):
# pylint: disable=line-too-long
assert value in (MAGGAIN_1_3, MAGGAIN_1_9, MAGGAIN_2_5, MAGGAIN_4_0, MAGGAIN_4_7, MAGGAIN_5_6, MAGGAIN_8_1)
# pylint: enable=line-too-long
assert value in (MAGGAIN_1_3, MAGGAIN_1_9, MAGGAIN_2_5, MAGGAIN_4_0, MAGGAIN_4_7,
MAGGAIN_5_6, MAGGAIN_8_1)

self._mag_gain = value
self._write_u8(self._mag_device, _REG_MAG_CRB_REG_M, self._mag_gain)
if self._mag_gain == MAGGAIN_1_3:
self._lsm303mag_gauss_lsb_xy = 1100.0
self._lsm303mag_gauss_lsb_z = 980.0
self._lsm303mag_gauss_lsb_z = 980.0
elif self._mag_gain == MAGGAIN_1_9:
self._lsm303mag_gauss_lsb_xy = 855.0
self._lsm303mag_gauss_lsb_z = 760.0
self._lsm303mag_gauss_lsb_z = 760.0
elif self._mag_gain == MAGGAIN_2_5:
self._lsm303mag_gauss_lsb_xy = 670.0
self._lsm303mag_gauss_lsb_z = 600.0
self._lsm303mag_gauss_lsb_z = 600.0
elif self._mag_gain == MAGGAIN_4_0:
self._lsm303mag_gauss_lsb_xy = 450.0
self._lsm303mag_gauss_lsb_z = 400.0
self._lsm303mag_gauss_lsb_z = 400.0
elif self._mag_gain == MAGGAIN_4_7:
self._lsm303mag_gauss_lsb_xy = 400.0
self._lsm303mag_gauss_lsb_z = 355.0
self._lsm303mag_gauss_lsb_z = 355.0
elif self._mag_gain == MAGGAIN_5_6:
self._lsm303mag_gauss_lsb_xy = 330.0
self._lsm303mag_gauss_lsb_z = 295.0
self._lsm303mag_gauss_lsb_z = 295.0
elif self._mag_gain == MAGGAIN_8_1:
self._lsm303mag_gauss_lsb_xy = 230.0
self._lsm303mag_gauss_lsb_z = 205.0

self._lsm303mag_gauss_lsb_z = 205.0

@property
def mag_rate(self):
"""The magnetometer update rate."""
return self._mag_rate


@mag_rate.setter
def mag_rate(self, value):
# pylint: disable=line-too-long
assert value in (MAGRATE_0_7, MAGRATE_1_5, MAGRATE_3_0, MAGRATE_7_5, MAGRATE_15, MAGRATE_30, MAGRATE_75, MAGRATE_220)
# pylint: enable=line-too-long
assert value in (MAGRATE_0_7, MAGRATE_1_5, MAGRATE_3_0, MAGRATE_7_5, MAGRATE_15, MAGRATE_30,
MAGRATE_75, MAGRATE_220)

self._mag_rate = value
reg_m = ((value & 0x07) << 2) & 0xFF
self._write_u8(self._mag_device, _REG_MAG_CRA_REG_M, reg_m)


def _read_u8(self, device, address):
with device as i2c:
self._BUFFER[0] = address & 0xFF
i2c.write(self._BUFFER, end=1, stop=False)
i2c.readinto(self._BUFFER, end=1)
return self._BUFFER[0]


def _write_u8(self, device, address, val):
with device as i2c:
self._BUFFER[0] = address & 0xFF
self._BUFFER[1] = val & 0xFF
i2c.write(self._BUFFER, end=2)

# pylint: disable=no-self-use
def _read_bytes(self, device, address, count, buf):
@staticmethod
def _read_bytes(device, address, count, buf):
with device as i2c:
buf[0] = address & 0xFF
i2c.write(buf, end=1, stop=False)
Expand Down
2 changes: 1 addition & 1 deletion examples/fast_accel/fast_accel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
sensor = adafruit_lsm303.LSM303(i2c)

while True:
accel_x, accel_y, accel_z = sensor.accelerometer
accel_x, accel_y, accel_z = sensor.acceleration
print('{0:10.3f} {1:10.3f} {2:10.3f}'.format(accel_x, accel_y, accel_z))
2 changes: 1 addition & 1 deletion examples/fast_mag/fast_mag.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
sensor = adafruit_lsm303.LSM303(i2c)

while True:
mag_x, mag_y, mag_z = sensor.magnetometer
mag_x, mag_y, mag_z = sensor.magnetic
print('{0:10.3f} {1:10.3f} {2:10.3f}'.format(mag_x, mag_y, mag_z))
15 changes: 8 additions & 7 deletions examples/raw_and_cooked/raw_and_cooked.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
""" Display both accelerometer and magnetometer data once per second """
# pylint: disable=line-too-long

import time
import board
Expand All @@ -11,12 +10,14 @@
sensor = adafruit_lsm303.LSM303(i2c)

while True:
raw_accel_x, raw_accel_y, raw_accel_z = sensor.raw_accelerometer
accel_x, accel_y, accel_z = sensor.accelerometer
raw_mag_x, raw_mag_y, raw_mag_z = sensor.raw_magnetometer
mag_x, mag_y, mag_z = sensor.magnetometer
raw_accel_x, raw_accel_y, raw_accel_z = sensor.raw_acceleration
accel_x, accel_y, accel_z = sensor.acceleration
raw_mag_x, raw_mag_y, raw_mag_z = sensor.raw_magnetic
mag_x, mag_y, mag_z = sensor.magnetic

print('Acceleration raw: ({0:6d}, {1:6d}, {2:6d}), (m/s^2): ({3:10.3f}, {4:10.3f}, {5:10.3f})'.format(raw_accel_x, raw_accel_y, raw_accel_z, accel_x, accel_y, accel_z))
print('Magnetometer raw: ({0:6d}, {1:6d}, {2:6d}), (gauss): ({3:10.3f}, {4:10.3f}, {5:10.3f})'.format(raw_mag_x, raw_mag_y, raw_mag_z, mag_x, mag_y, mag_z))
print('Acceleration raw: ({0:6d}, {1:6d}, {2:6d}), (m/s^2): ({3:10.3f}, {4:10.3f}, {5:10.3f})'
.format(raw_accel_x, raw_accel_y, raw_accel_z, accel_x, accel_y, accel_z))
print('Magnetometer raw: ({0:6d}, {1:6d}, {2:6d}), (gauss): ({3:10.3f}, {4:10.3f}, {5:10.3f})'
.format(raw_mag_x, raw_mag_y, raw_mag_z, mag_x, mag_y, mag_z))
print('')
time.sleep(1.0)
4 changes: 2 additions & 2 deletions examples/slow_both/slow_both.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
sensor = adafruit_lsm303.LSM303(i2c)

while True:
acc_x, acc_y, acc_z = sensor.accelerometer
mag_x, mag_y, mag_z = sensor.magnetometer
acc_x, acc_y, acc_z = sensor.acceleration
mag_x, mag_y, mag_z = sensor.magnetic

print('Acceleration (m/s^2): ({0:10.3f}, {1:10.3f}, {2:10.3f})'.format(acc_x, acc_y, acc_z))
print('Magnetometer (gauss): ({0:10.3f}, {1:10.3f}, {2:10.3f})'.format(mag_x, mag_y, mag_z))
Expand Down