Skip to content

Commit b43cfec

Browse files
committed
ready for release
1 parent 73c73da commit b43cfec

File tree

4 files changed

+62
-50
lines changed

4 files changed

+62
-50
lines changed

adafruit_lis331.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,13 @@ class LIS331:
230230
_data_rate_lpf_bits = RWBits(2, _LIS331_REG_CTRL1, 3)
231231
_range_bits = RWBits(2, _LIS331_REG_CTRL4, 4)
232232
_raw_acceleration = ROByteArray((_LIS331_REG_OUT_X_L | 0x80), "<hhh", 6)
233-
_reference_value = UnaryStruct(_LIS331_REG_REFERENCE, "<b")
234-
_zero_hpf_reference = ROUnaryStruct(_LIS331_REG_HP_FILTER_RESET, "<b")
235233

234+
_reference_value = UnaryStruct(_LIS331_REG_REFERENCE, "<b")
235+
_zero_hpf = ROUnaryStruct(_LIS331_REG_HP_FILTER_RESET, "<b")
236236
_hpf_mode_bits = RWBit(_LIS331_REG_CTRL2, 5)
237237
_hpf_enable_bit = RWBit(_LIS331_REG_CTRL2, 4)
238238
_hpf_cutoff = RWBits(2, _LIS331_REG_CTRL2, 0)
239239

240-
CHIP_ID = None
241-
# Adafruit_BusIO_Register reference_reg = Adafruit_BusIO_Register(
242-
# i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS331_REG_REFERENCE);
243-
# reference_reg.write(reference);
244240
def __init__(self, i2c_bus, address=_LIS331_DEFAULT_ADDRESS):
245241
if (not isinstance(self, LIS331HH)) and (not isinstance(self, H3LIS331)):
246242
raise RuntimeError(
@@ -307,6 +303,7 @@ def zero_hpf(self):
307303
will set all measurements to zero immediately, avoiding the normal settling time seen when
308304
using the high-pass filter without a ``hpf_reference``
309305
"""
306+
self._zero_hpf # pylint: disable=pointless-statement
310307

311308
def enable_hpf(
312309
self, enabled=True, cutoff=RateDivisor.ODR_DIV_50, use_reference=False
@@ -332,25 +329,19 @@ def enable_hpf(
332329
@property
333330
def data_rate(self):
334331
"""Select the rate at which the accelerometer takes measurements. Must be a `Rate`"""
335-
# because both the power mode[pm] and data rate[dr] bits determine the data rate
336-
# we'll report the whole bunch
337332
return self._cached_data_rate
338333

339334
@data_rate.setter
340335
def data_rate(self, new_rate_bits):
341336
if not Rate.is_valid(new_rate_bits):
342337
raise AttributeError("data_rate must be a `Rate`")
343-
# like `data_rate` we'll receive the whole group of pm/dr bits to determine what to be set
344-
# print("(entry)new_rate_bits", bin(new_rate_bits))
338+
339+
# to determine what to be set we'll receive the whole group of pm/dr bits
340+
# to make sure we don't overwrite the filter
345341
new_mode, adjusted_rate_bits = self._mode_and_rate(new_rate_bits)
346-
# print("new mode:", bin(new_mode), "adjusted_rate_bits:", bin(adjusted_rate_bits))
347342
if new_mode == Mode.NORMAL: # pylint: disable=no-member
348-
# print("Mode is NORMAL")
349-
# print("setting self._mode_and_odr_bits to new_rate_bits", bin(new_rate_bits))
350343
self._mode_and_odr_bits = new_rate_bits
351344
else:
352-
# print("Mode is ", Mode.string[new_mode], "and not NORMAL")
353-
# print("setting self._power_mode_bits to new_mode", bin(new_mode))
354345
self._power_mode_bits = new_mode
355346

356347
self._cached_data_rate = new_mode << 2 | new_rate_bits
@@ -365,10 +356,10 @@ def mode(self):
365356
def _mode_and_rate(self, data_rate=None):
366357
if data_rate is None:
367358
data_rate = self._cached_data_rate
368-
# pylint: disable=no-member
359+
369360
pm_value = (data_rate & 0x1C) >> 2
370361
dr_value = data_rate & 0x3
371-
if pm_value is Mode.LOW_POWER:
362+
if pm_value is Mode.LOW_POWER: # pylint: disable=no-member
372363
dr_value = 0
373364
return (pm_value, dr_value)
374365

@@ -402,9 +393,9 @@ def acceleration(self):
402393

403394
def _scale_acceleration(self, value):
404395
# The measurements are 12 bits left justified to preserve the sign bit
396+
# so we'll shift them back to get the real value
405397
right_justified = value >> 4
406398
lsb_value = self._range_class.lsb[self._cached_accel_range]
407-
# print("v:", value, "cr:", self._cached_accel_range, "l:", lsb_value)
408399
return right_justified * lsb_value
409400

410401

examples/lis331_filter_test.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

examples/lis331_high_pass_filter.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import time
2+
import board
3+
import busio
4+
from adafruit_lis331 import *
5+
6+
i2c = busio.I2C(board.SCL, board.SDA)
7+
# un-comment the sensor you are using
8+
# lis = H3LIS331(i2c)
9+
lis = LIS331HH(i2c)
10+
11+
# use a nice fast data rate to for maximum resolution
12+
lis.data_rate = Rate.RATE_1000_HZ
13+
14+
# enable the high pass filter without a reference or offset
15+
lis.enable_hpf(True, cutoff=RateDivisor.ODR_DIV_100, use_reference=False)
16+
17+
# you can also uncomment this section to set and use a reference to offset the measurements
18+
# lis.hpf_reference = 50
19+
# lis.enable_hpf(True, cutoff=RateDivisor.ODR_DIV_100, use_reference=True)
20+
21+
22+
# watch in the serial plotter with the sensor still and you will see the
23+
# z-axis value go from the normal around 9.8 with the filter off to near zero with it
24+
# enabled. If you have a reference enabled and set, that will determind the center point.
25+
26+
# If you shake the sensor, you'll still see the acceleration values change! This is the
27+
# Filter removing slow or non-changing values and letting through ones that move more quickly
28+
29+
while True:
30+
print(lis.acceleration) # plotter friendly printing
31+
time.sleep(0.02)

examples/lis331_low_pass_filter.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import time
2+
import board
3+
import busio
4+
from adafruit_lis331 import *
5+
6+
i2c = busio.I2C(board.SCL, board.SDA)
7+
8+
# un-comment the sensor you are using
9+
# lis = H3LIS331(i2c)
10+
lis = LIS331HH(i2c)
11+
12+
# `data_rate` must be a `LOWPOWER` rate to use the low-pass filter
13+
lis.data_rate = Rate.RATE_LOWPOWER_10_HZ
14+
# next set the cutoff frequency. Anything changing faster than the specified frequency will be filtered out
15+
lis.lpf_cutoff = Frequency.FREQ_74_HZ
16+
17+
# Once you've seen the filter do its thing, you can comment out the lines above to use the default data rate
18+
# without the low pass filter and see the difference it makes
19+
20+
while True:
21+
print(lis.acceleration) # plotter friendly printing
22+
time.sleep(0.002)

0 commit comments

Comments
 (0)