Skip to content

Ran black, updated to pylint 2.x #7

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 1 commit into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
source actions-ci/install.sh
- name: Pip install pylint, black, & Sphinx
run: |
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
- name: Library version
run: git describe --dirty --always --tags
- name: PyLint
Expand Down
157 changes: 88 additions & 69 deletions adafruit_lsm303_accel.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,80 +61,87 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LSM303_Accel.git"

# pylint: disable=bad-whitespace
_ADDRESS_ACCEL = const(0x19) # (0x32 >> 1) // 0011001x
_ADDRESS_MAG = const(0x1E) # (0x3C >> 1) // 0011110x
_ID = const(0xD4) # (0b11010100)
_ADDRESS_ACCEL = const(0x19) # (0x32 >> 1) // 0011001x
_ADDRESS_MAG = const(0x1E) # (0x3C >> 1) // 0011110x
_ID = const(0xD4) # (0b11010100)

# Accelerometer registers

_REG_ACCEL_WHO_AM_I = const(0x0F)
_REG_ACCEL_CTRL_REG1_A = const(0x20)
_REG_ACCEL_CTRL_REG2_A = const(0x21)
_REG_ACCEL_CTRL_REG3_A = const(0x22)
_REG_ACCEL_CTRL_REG4_A = const(0x23)
_REG_ACCEL_CTRL_REG5_A = const(0x24)
_REG_ACCEL_CTRL_REG6_A = const(0x25)
_REG_ACCEL_REFERENCE_A = const(0x26)
_REG_ACCEL_STATUS_REG_A = const(0x27)
_REG_ACCEL_OUT_X_L_A = const(0x28)
_REG_ACCEL_OUT_X_H_A = const(0x29)
_REG_ACCEL_OUT_Y_L_A = const(0x2A)
_REG_ACCEL_OUT_Y_H_A = const(0x2B)
_REG_ACCEL_OUT_Z_L_A = const(0x2C)
_REG_ACCEL_OUT_Z_H_A = const(0x2D)
_REG_ACCEL_WHO_AM_I = const(0x0F)
_REG_ACCEL_CTRL_REG1_A = const(0x20)
_REG_ACCEL_CTRL_REG2_A = const(0x21)
_REG_ACCEL_CTRL_REG3_A = const(0x22)
_REG_ACCEL_CTRL_REG4_A = const(0x23)
_REG_ACCEL_CTRL_REG5_A = const(0x24)
_REG_ACCEL_CTRL_REG6_A = const(0x25)
_REG_ACCEL_REFERENCE_A = const(0x26)
_REG_ACCEL_STATUS_REG_A = const(0x27)
_REG_ACCEL_OUT_X_L_A = const(0x28)
_REG_ACCEL_OUT_X_H_A = const(0x29)
_REG_ACCEL_OUT_Y_L_A = const(0x2A)
_REG_ACCEL_OUT_Y_H_A = const(0x2B)
_REG_ACCEL_OUT_Z_L_A = const(0x2C)
_REG_ACCEL_OUT_Z_H_A = const(0x2D)
_REG_ACCEL_FIFO_CTRL_REG_A = const(0x2E)
_REG_ACCEL_FIFO_SRC_REG_A = const(0x2F)
_REG_ACCEL_INT1_CFG_A = const(0x30)
_REG_ACCEL_INT1_SOURCE_A = const(0x31)
_REG_ACCEL_INT1_THS_A = const(0x32)
_REG_ACCEL_FIFO_SRC_REG_A = const(0x2F)
_REG_ACCEL_INT1_CFG_A = const(0x30)
_REG_ACCEL_INT1_SOURCE_A = const(0x31)
_REG_ACCEL_INT1_THS_A = const(0x32)
_REG_ACCEL_INT1_DURATION_A = const(0x33)
_REG_ACCEL_INT2_CFG_A = const(0x34)
_REG_ACCEL_INT2_SOURCE_A = const(0x35)
_REG_ACCEL_INT2_THS_A = const(0x36)
_REG_ACCEL_INT2_CFG_A = const(0x34)
_REG_ACCEL_INT2_SOURCE_A = const(0x35)
_REG_ACCEL_INT2_THS_A = const(0x36)
_REG_ACCEL_INT2_DURATION_A = const(0x37)
_REG_ACCEL_CLICK_CFG_A = const(0x38)
_REG_ACCEL_CLICK_SRC_A = const(0x39)
_REG_ACCEL_CLICK_THS_A = const(0x3A)
_REG_ACCEL_TIME_LIMIT_A = const(0x3B)
_REG_ACCEL_TIME_LATENCY_A = const(0x3C)
_REG_ACCEL_TIME_WINDOW_A = const(0x3D)
_REG_ACCEL_ACT_THS_A = const(0x3E)
_REG_ACCEL_ACT_DUR_A = const(0x3F)
_REG_ACCEL_CLICK_CFG_A = const(0x38)
_REG_ACCEL_CLICK_SRC_A = const(0x39)
_REG_ACCEL_CLICK_THS_A = const(0x3A)
_REG_ACCEL_TIME_LIMIT_A = const(0x3B)
_REG_ACCEL_TIME_LATENCY_A = const(0x3C)
_REG_ACCEL_TIME_WINDOW_A = const(0x3D)
_REG_ACCEL_ACT_THS_A = const(0x3E)
_REG_ACCEL_ACT_DUR_A = const(0x3F)
# note:: Tap related registers are called ``CLICK_`` in the datasheet
# Conversion constants
_LSM303ACCEL_MG_LSB = 16704.0 # magic!
_GRAVITY_STANDARD = 9.80665 # Earth's gravity in m/s^2
_SMOLLER_GRAVITY = 0.00980665
#pylint:disable=too-few-public-methods
_LSM303ACCEL_MG_LSB = 16704.0 # magic!
_GRAVITY_STANDARD = 9.80665 # Earth's gravity in m/s^2
_SMOLLER_GRAVITY = 0.00980665
# pylint:disable=too-few-public-methods
class Rate:
"""Options for `data_rate`"""

RATE_SHUTDOWN = const(0)
RATE_1_HZ = const(1)
RATE_10_HZ = const(2)
RATE_25_HZ = const(3)
RATE_50_HZ = const(4)
RATE_100_HZ = const(5)
RATE_200_HZ = const(6)
RATE_400_HZ = const(7)
RATE_1620_HZ = const(8)
RATE_1344_HZ = const(9)
RATE_1_HZ = const(1)
RATE_10_HZ = const(2)
RATE_25_HZ = const(3)
RATE_50_HZ = const(4)
RATE_100_HZ = const(5)
RATE_200_HZ = const(6)
RATE_400_HZ = const(7)
RATE_1620_HZ = const(8)
RATE_1344_HZ = const(9)


class Mode:
"""Options for `mode`"""
MODE_NORMAL = const(0)

MODE_NORMAL = const(0)
MODE_HIGH_RESOLUTION = const(1)
MODE_LOW_POWER = const(2)
MODE_LOW_POWER = const(2)


class Range:
"""Options for `range`"""

RANGE_2G = const(0)
RANGE_4G = const(1)
RANGE_8G = const(2)
RANGE_16G = const(3)


# pylint: enable=bad-whitespace,too-few-public-methods

class LSM303_Accel: #pylint:disable=too-many-instance-attributes

class LSM303_Accel: # pylint:disable=too-many-instance-attributes
"""Driver for the LSM303's accelerometer."""

# Class-level buffer for reading and writing data with the sensor.
Expand Down Expand Up @@ -189,6 +196,7 @@ class LSM303_Accel: #pylint:disable=too-many-instance-attributes
_tap_time_window = UnaryStruct(_REG_ACCEL_TIME_WINDOW_A, "B")

_BUFFER = bytearray(6)

def __init__(self, i2c):
self._accel_device = I2CDevice(i2c, _ADDRESS_ACCEL)
self.i2c_device = self._accel_device
Expand All @@ -203,8 +211,16 @@ def __init__(self, i2c):
self._cached_mode = 0
self._cached_range = 0

def set_tap(self, tap, threshold, *,
time_limit=10, time_latency=20, time_window=255, tap_cfg=None):
def set_tap(
self,
tap,
threshold,
*,
time_limit=10,
time_latency=20,
time_window=255,
tap_cfg=None
):
"""
The tap detection parameters.

Expand All @@ -221,10 +237,11 @@ def set_tap(self, tap, threshold, *,
"""

if (tap < 0 or tap > 2) and tap_cfg is None:
raise ValueError('Tap must be 0 (disabled), 1 (single tap), or 2 (double tap)!')
raise ValueError(
"Tap must be 0 (disabled), 1 (single tap), or 2 (double tap)!"
)
if threshold > 127 or threshold < 0:
raise ValueError('Threshold out of range (0-127)')

raise ValueError("Threshold out of range (0-127)")

if tap == 0 and tap_cfg is None:
# Disable click interrupt.
Expand All @@ -242,7 +259,7 @@ def set_tap(self, tap, threshold, *,
# Or, if a custom tap configuration register value specified, use it.
self._tap_config = tap_cfg

self._tap_threshold = threshold # why and?
self._tap_threshold = threshold # why and?
self._tap_time_limit = time_limit
self._tap_time_latency = time_latency
self._tap_time_window = time_window
Expand All @@ -259,8 +276,10 @@ def tapped(self):

@property
def _raw_acceleration(self):
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])
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 acceleration(self):
Expand All @@ -279,14 +298,14 @@ def acceleration(self):
def _scale_data(self, raw_measurement):
lsb, shift = self._lsb_shift()

return(raw_measurement >> shift) * lsb * _SMOLLER_GRAVITY
return (raw_measurement >> shift) * lsb * _SMOLLER_GRAVITY

def _lsb_shift(self): #pylint:disable=too-many-branches
def _lsb_shift(self): # pylint:disable=too-many-branches
# the bit depth of the data depends on the mode, and the lsb value
# depends on the mode and range
lsb = -1 # the default, normal mode @ 2G
lsb = -1 # the default, normal mode @ 2G

if self._cached_mode is Mode.MODE_HIGH_RESOLUTION: # 12-bit
if self._cached_mode is Mode.MODE_HIGH_RESOLUTION: # 12-bit
shift = 4
if self._cached_range is Range.RANGE_2G:
lsb = 0.98
Expand All @@ -296,7 +315,7 @@ def _lsb_shift(self): #pylint:disable=too-many-branches
lsb = 3.9
elif self._cached_range is Range.RANGE_16G:
lsb = 11.72
elif self._cached_mode is Mode.MODE_NORMAL: # 10-bit
elif self._cached_mode is Mode.MODE_NORMAL: # 10-bit
shift = 6
if self._cached_range is Range.RANGE_2G:
lsb = 3.9
Expand All @@ -307,8 +326,7 @@ def _lsb_shift(self): #pylint:disable=too-many-branches
elif self._cached_range is Range.RANGE_16G:
lsb = 46.9


elif self._cached_mode is Mode.MODE_LOW_POWER: # 8-bit
elif self._cached_mode is Mode.MODE_LOW_POWER: # 8-bit
shift = 8
if self._cached_range is Range.RANGE_2G:
lsb = 15.63
Expand All @@ -320,8 +338,10 @@ def _lsb_shift(self): #pylint:disable=too-many-branches
lsb = 187.58

if lsb is -1:
raise AttributeError("'impossible' range or mode detected: range: %d mode: %d"%
(self._cached_range, self._cached_mode))
raise AttributeError(
"'impossible' range or mode detected: range: %d mode: %d"
% (self._cached_range, self._cached_mode)
)
return (lsb, shift)

@property
Expand Down Expand Up @@ -360,14 +380,13 @@ def mode(self, value):
if value < 0 or value > 2:
raise AttributeError("mode must be a `Mode`")
self._high_resolution = value & 0b01
self._low_power = (value & 0b10) >>1
self._low_power = (value & 0b10) >> 1
self._cached_mode = value

def _read_u8(self, device, address):
with device as i2c:
self._BUFFER[0] = address & 0xFF
i2c.write_then_readinto(self._BUFFER, self._BUFFER,
out_end=1, in_end=1)
i2c.write_then_readinto(self._BUFFER, self._BUFFER, out_end=1, in_end=1)
return self._BUFFER[0]

def _write_u8(self, device, address, val):
Expand Down
Loading