Skip to content

Commit 32184fe

Browse files
committed
added hpf enable
1 parent 75ae7dd commit 32184fe

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

adafruit_lis331.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from struct import unpack_from
5252
from time import sleep
5353
from adafruit_register.i2c_bits import RWBits
54+
from adafruit_register.i2c_bit import RWBit
5455
from adafruit_register.i2c_struct import UnaryStruct, ROUnaryStruct
5556
import adafruit_bus_device.i2c_device as i2c_device
5657

@@ -214,6 +215,7 @@ class RateDivisor(CV):
214215

215216

216217
class LIS331:
218+
# pylint:disable=too-many-instance-attributes
217219
"""Base class for the LIS331 family of 3-axis accelerometers.
218220
**Cannot be instantiated directly**
219221
@@ -229,6 +231,12 @@ class LIS331:
229231
_range_bits = RWBits(2, _LIS331_REG_CTRL4, 4)
230232
_raw_acceleration = ROByteArray((_LIS331_REG_OUT_X_L | 0x80), "<hhh", 6)
231233
_reference_value = UnaryStruct(_LIS331_REG_REFERENCE, "<b")
234+
_zero_hpf_reference = ROUnaryStruct(_LIS331_REG_HP_FILTER_RESET, "<b")
235+
236+
_hpf_mode_bits = RWBit(_LIS331_REG_CTRL2, 5)
237+
_hpf_enable_bit = RWBit(_LIS331_REG_CTRL2, 4)
238+
_hpf_cutoff = RWBits(2, _LIS331_REG_CTRL2, 0)
239+
232240
CHIP_ID = None
233241
# Adafruit_BusIO_Register reference_reg = Adafruit_BusIO_Register(
234242
# i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS331_REG_REFERENCE);
@@ -244,6 +252,7 @@ def __init__(self, i2c_bus, address=_LIS331_DEFAULT_ADDRESS):
244252
"Failed to find %s - check your wiring!" % self.__class__.__name__
245253
)
246254
self._range_class = None
255+
self.enable_hpf(False)
247256

248257
@property
249258
def lpf_cutoff(self):
@@ -293,12 +302,32 @@ def hpf_reference(self, reference_value):
293302
raise AttributeError("`hpf_reference` must be from -128 to 127")
294303
self._reference_value = reference_value
295304

296-
# void enableHighPassFilter(bool filter_enabled,
297-
# lis331_hpf_cutoff_t cutoff = LIS331_HPF_0_0025_ODR,
298-
# bool use_reference = false);
299-
# void setHPFReference(int8_t reference);
300-
# int8_t getHPFReference(void);
301-
# void HPFReset(void);
305+
def zero_hpf(self):
306+
"""When the high-pass filter is enabled with ``use_reference=False``, calling ``zero_hpf``
307+
will set all measurements to zero immediately, avoiding the normal settling time seen when
308+
using the high-pass filter without a ``hpf_reference``
309+
"""
310+
311+
def enable_hpf(
312+
self, enabled=True, cutoff=RateDivisor.ODR_DIV_50, use_reference=False
313+
): # pylint: disable=no-member
314+
"""Enable or disable the high-pass filter.
315+
316+
:param enabled: Enable or disable the filter. Default is `True` to enable
317+
:param ~RateDivisor cutoff: A `RateDivisor` to set the high-pass cutoff frequency. Default\
318+
is ``RateDivisor.ODR_DIV_50``. See ``RateDivisor`` for more information
319+
:param use_reference: Determines if the filtered measurements are offset by a reference\
320+
value. Default is false.
321+
322+
See section **4** of the LIS331DLH application note for more information `LIS331DLH application\
323+
note for more information <https://www.st.com/content/ccc/resource/technical/document/\
324+
application_note/b5/8e/58/69/cb/87/45/55/CD00215823.pdf/files/CD00215823.pdf/jcr:content/\
325+
translations/en.CD00215823.pdf>`_
326+
327+
"""
328+
self._hpf_mode_bits = use_reference
329+
self._hpf_cutoff = cutoff
330+
self._hpf_enable_bit = enabled
302331

303332
@property
304333
def data_rate(self):

examples/lis331_filter_test.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
# lis = H3LIS331(i2c)
99
lis = LIS331HH(i2c)
1010

11+
# `data_rate` must be a `LOWPOWER` rate to use the low-pass filter
1112
lis.data_rate = Rate.RATE_LOWPOWER_10_HZ
1213
print("Rate is %sHz" % Rate.string[lis.data_rate])
1314

14-
lis.lpf_cutoff = Frequency.FREQ_37_HZ
15-
print("LPF cutoff frequency is %sHz" % Frequency.string[lis.lpf_cutoff])
15+
# lis.lpf_cutoff = Frequency.FREQ_37_HZ
16+
# print("LPF cutoff frequency is %sHz" % Frequency.string[lis.lpf_cutoff])
1617

1718
# un-comment the range for the sensor you are using
1819
# lis.range = H3LIS331Range.RANGE_100G
@@ -21,6 +22,11 @@
2122
range_string = LIS331HHRange.string[lis.range]
2223
print("Range is +/-%sg" % range_string)
2324

25+
lis.hpf_reference = 127
26+
print("high-pass filter reference value:", lis.hpf_reference)
27+
28+
lis.enable_hpf(True)
2429
while True:
2530
print(lis.acceleration) # plotter friendly printing
26-
time.sleep(0.005)
31+
# print((lis.acceleration[2],)) # plotter friendly printing
32+
time.sleep(0.2)

0 commit comments

Comments
 (0)