Skip to content

Commit

Permalink
Merge pull request #59 from adafruit/pylint-update
Browse files Browse the repository at this point in the history
Ran black, updated to pylint 2.x
  • Loading branch information
kattni authored Mar 17, 2020
2 parents e730484 + a5a3059 commit 448c5d2
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 181 deletions.
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
3 changes: 2 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ spelling-store-unknown-words=no
[MISCELLANEOUS]

# List of note tags to take in consideration, separated by a comma.
notes=FIXME,XXX,TODO
# notes=FIXME,XXX,TODO
notes=FIXME,XXX


[TYPECHECK]
Expand Down
104 changes: 59 additions & 45 deletions adafruit_lis3dh.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,38 @@

# Register addresses:
# pylint: disable=bad-whitespace
_REG_OUTADC1_L = const(0x08)
_REG_WHOAMI = const(0x0F)
_REG_TEMPCFG = const(0x1F)
_REG_CTRL1 = const(0x20)
_REG_CTRL3 = const(0x22)
_REG_CTRL4 = const(0x23)
_REG_CTRL5 = const(0x24)
_REG_OUT_X_L = const(0x28)
_REG_INT1SRC = const(0x31)
_REG_CLICKCFG = const(0x38)
_REG_CLICKSRC = const(0x39)
_REG_CLICKTHS = const(0x3A)
_REG_TIMELIMIT = const(0x3B)
_REG_OUTADC1_L = const(0x08)
_REG_WHOAMI = const(0x0F)
_REG_TEMPCFG = const(0x1F)
_REG_CTRL1 = const(0x20)
_REG_CTRL3 = const(0x22)
_REG_CTRL4 = const(0x23)
_REG_CTRL5 = const(0x24)
_REG_OUT_X_L = const(0x28)
_REG_INT1SRC = const(0x31)
_REG_CLICKCFG = const(0x38)
_REG_CLICKSRC = const(0x39)
_REG_CLICKTHS = const(0x3A)
_REG_TIMELIMIT = const(0x3B)
_REG_TIMELATENCY = const(0x3C)
_REG_TIMEWINDOW = const(0x3D)
_REG_TIMEWINDOW = const(0x3D)

# Register value constants:
RANGE_16_G = const(0b11) # +/- 16g
RANGE_8_G = const(0b10) # +/- 8g
RANGE_4_G = const(0b01) # +/- 4g
RANGE_2_G = const(0b00) # +/- 2g (default value)
DATARATE_1344_HZ = const(0b1001) # 1.344 KHz
DATARATE_400_HZ = const(0b0111) # 400Hz
DATARATE_200_HZ = const(0b0110) # 200Hz
DATARATE_100_HZ = const(0b0101) # 100Hz
DATARATE_50_HZ = const(0b0100) # 50Hz
DATARATE_25_HZ = const(0b0011) # 25Hz
DATARATE_10_HZ = const(0b0010) # 10 Hz
DATARATE_1_HZ = const(0b0001) # 1 Hz
DATARATE_POWERDOWN = const(0)
DATARATE_LOWPOWER_1K6HZ = const(0b1000)
DATARATE_LOWPOWER_5KHZ = const(0b1001)
RANGE_16_G = const(0b11) # +/- 16g
RANGE_8_G = const(0b10) # +/- 8g
RANGE_4_G = const(0b01) # +/- 4g
RANGE_2_G = const(0b00) # +/- 2g (default value)
DATARATE_1344_HZ = const(0b1001) # 1.344 KHz
DATARATE_400_HZ = const(0b0111) # 400Hz
DATARATE_200_HZ = const(0b0110) # 200Hz
DATARATE_100_HZ = const(0b0101) # 100Hz
DATARATE_50_HZ = const(0b0100) # 50Hz
DATARATE_25_HZ = const(0b0011) # 25Hz
DATARATE_10_HZ = const(0b0010) # 10 Hz
DATARATE_1_HZ = const(0b0001) # 1 Hz
DATARATE_POWERDOWN = const(0)
DATARATE_LOWPOWER_1K6HZ = const(0b1000)
DATARATE_LOWPOWER_5KHZ = const(0b1001)

# Other constants
STANDARD_GRAVITY = 9.806
Expand All @@ -86,11 +86,12 @@

class LIS3DH:
"""Driver base for the LIS3DH accelerometer."""

def __init__(self, int1=None, int2=None):
# Check device ID.
device_id = self._read_register_byte(_REG_WHOAMI)
if device_id != 0x33:
raise RuntimeError('Failed to find LIS3DH!')
raise RuntimeError("Failed to find LIS3DH!")
# Reboot
self._write_register_byte(_REG_CTRL5, 0x80)
time.sleep(0.01) # takes 5ms
Expand Down Expand Up @@ -156,7 +157,7 @@ def acceleration(self):
elif accel_range == RANGE_2_G:
divider = 16380

x, y, z = struct.unpack('<hhh', self._read_register(_REG_OUT_X_L | 0x80, 6))
x, y, z = struct.unpack("<hhh", self._read_register(_REG_OUT_X_L | 0x80, 6))

# convert from Gs to m / s ^ 2 and adjust for the range
x = (x / divider) * STANDARD_GRAVITY
Expand Down Expand Up @@ -201,12 +202,13 @@ def read_adc_raw(self, adc):
value 1, 2, or 3.
"""
if adc < 1 or adc > 3:
raise ValueError('ADC must be a value 1 to 3!')
raise ValueError("ADC must be a value 1 to 3!")

return struct.unpack('<h',
self._read_register((_REG_OUTADC1_L+((adc-1)*2)) | 0x80, 2)[0:2])[0]
return struct.unpack(
"<h", self._read_register((_REG_OUTADC1_L + ((adc - 1) * 2)) | 0x80, 2)[0:2]
)[0]

def read_adc_mV(self, adc): # pylint: disable=invalid-name
def read_adc_mV(self, adc): # pylint: disable=invalid-name
"""Read the specified analog to digital converter value in millivolts.
ADC must be a value 1, 2, or 3. NOTE the ADC can only measure voltages
in the range of ~900-1200mV!
Expand All @@ -222,7 +224,7 @@ def read_adc_mV(self, adc): # pylint: disable=invalid-name
# x1 = 32512
# y0 = 1800
# y1 = 900
return 1800+(raw+32512)*(-900/65024)
return 1800 + (raw + 32512) * (-900 / 65024)

@property
def tapped(self):
Expand Down Expand Up @@ -250,8 +252,16 @@ def tapped(self):
raw = self._read_register_byte(_REG_CLICKSRC)
return raw & 0x40 > 0

def set_tap(self, tap, threshold, *,
time_limit=10, time_latency=20, time_window=255, click_cfg=None):
def set_tap(
self,
tap,
threshold,
*,
time_limit=10,
time_latency=20,
time_window=255,
click_cfg=None
):
"""
The tap detection parameters.
Expand All @@ -271,9 +281,11 @@ def set_tap(self, tap, threshold, *,
:param int click_cfg: CLICK_CFG register value.
"""
if (tap < 0 or tap > 2) and click_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)")

ctrl3 = self._read_register_byte(_REG_CTRL3)
if tap == 0 and click_cfg is None:
Expand Down Expand Up @@ -315,7 +327,8 @@ class LIS3DH_I2C(LIS3DH):
"""Driver for the LIS3DH accelerometer connected over I2C."""

def __init__(self, i2c, *, address=0x18, int1=None, int2=None):
import adafruit_bus_device.i2c_device as i2c_device
import adafruit_bus_device.i2c_device as i2c_device # pylint: disable=import-outside-toplevel

self._i2c = i2c_device.I2CDevice(i2c, address)
self._buffer = bytearray(6)
super().__init__(int1=int1, int2=int2)
Expand All @@ -338,7 +351,8 @@ class LIS3DH_SPI(LIS3DH):
"""Driver for the LIS3DH accelerometer connected over SPI."""

def __init__(self, spi, cs, *, baudrate=100000, int1=None, int2=None):
import adafruit_bus_device.spi_device as spi_device
import adafruit_bus_device.spi_device as spi_device # pylint: disable=import-outside-toplevel

self._spi = spi_device.SPIDevice(spi, cs, baudrate=baudrate)
self._buffer = bytearray(6)
super().__init__(int1=int1, int2=int2)
Expand All @@ -349,12 +363,12 @@ def _read_register(self, register, length):
else:
self._buffer[0] = (register | 0xC0) & 0xFF # Read multiple, bit 6&7 high.
with self._spi as spi:
spi.write(self._buffer, start=0, end=1) # pylint: disable=no-member
spi.readinto(self._buffer, start=0, end=length) # pylint: disable=no-member
spi.write(self._buffer, start=0, end=1) # pylint: disable=no-member
spi.readinto(self._buffer, start=0, end=length) # pylint: disable=no-member
return self._buffer

def _write_register_byte(self, register, value):
self._buffer[0] = register & 0x7F # Write, bit 7 low.
self._buffer[1] = value & 0xFF
with self._spi as spi:
spi.write(self._buffer, start=0, end=2) # pylint: disable=no-member
spi.write(self._buffer, start=0, end=2) # pylint: disable=no-member
Loading

0 comments on commit 448c5d2

Please sign in to comment.