diff --git a/docs/examples.rst b/docs/examples.rst index 75fe4ef..27fc0a1 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -34,11 +34,11 @@ Example showing the Data rate setting :caption: examples/mma8451_data_rate.py :lines: 5- -Data rate settings -------------------- +High pass filter cutoff settings +--------------------------------- -Example showing the Data rate setting +Example showing the High pass filter cutoff setting -.. literalinclude:: ../examples/mma8451_data_rate.py - :caption: examples/mma8451_data_rate.py +.. literalinclude:: ../examples/mma8451_high_pass_filter_cutoff.py + :caption: examples/mma8451_high_pass_filter_cutoff.py :lines: 5- diff --git a/examples/mma8451_high_pass_filter_cutoff.py b/examples/mma8451_high_pass_filter_cutoff.py new file mode 100644 index 0000000..e95ca6a --- /dev/null +++ b/examples/mma8451_high_pass_filter_cutoff.py @@ -0,0 +1,23 @@ +# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya +# +# SPDX-License-Identifier: MIT + +import time +from machine import Pin, I2C +from micropython_mma8451 import mma8451 + +i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040 +mma = mma8451.MMA8451(i2c) + +mma.high_pass_filter = mma8451.HPF_ENABLED +mma.high_pass_filter_cutoff = mma8451.CUTOFF_8HZ + +while True: + for high_pass_filter_cutoff in mma8451.high_pass_filter_cutoff_values: + print("Current High pass filter cutoff setting: ", mma.high_pass_filter_cutoff) + for _ in range(10): + accx, accy, accz = mma.acceleration + print("x:{:.2f}m/s2, y:{:.2f}m/s2, z:{:.2f}m/s2".format(accx, accy, accz)) + print() + time.sleep(0.5) + mma.high_pass_filter_cutoff = high_pass_filter_cutoff diff --git a/examples/mma8451_simpletest.py b/examples/mma8451_simpletest.py index e20bbfb..6a88ddc 100644 --- a/examples/mma8451_simpletest.py +++ b/examples/mma8451_simpletest.py @@ -11,6 +11,8 @@ while True: x, y, z = mma.acceleration - print("Acceleration: X={0:0.1f}m/s^2 y={1:0.1f}m/s^2 z={2:0.1f}m/s^2".format(x, y, z)) + print( + "Acceleration: X={0:0.1f}m/s^2 y={1:0.1f}m/s^2 z={2:0.1f}m/s^2".format(x, y, z) + ) print() time.sleep(0.5) diff --git a/micropython_mma8451/mma8451.py b/micropython_mma8451/mma8451.py index cf31cbe..687a3a8 100644 --- a/micropython_mma8451/mma8451.py +++ b/micropython_mma8451/mma8451.py @@ -30,6 +30,7 @@ _DATA = const(0x01) _XYZ_DATA_CFG = const(0x0E) _CTRL_REG1 = const(0x2A) +_HP_FILTER_CUTOFF = const(0x2F) _GRAVITY = 9.80665 @@ -63,6 +64,16 @@ DATARATE_1_56HZ, ) +HPF_DISABLED = const(0b0) +HPF_ENABLED = const(0b1) +high_pass_filter_values = (HPF_DISABLED, HPF_ENABLED) + +CUTOFF_16HZ = const(0b00) +CUTOFF_8HZ = const(0b01) +CUTOFF_4HZ = const(0b10) +CUTOFF_2HZ = const(0b11) +high_pass_filter_cutoff_values = (CUTOFF_16HZ, CUTOFF_8HZ, CUTOFF_4HZ, CUTOFF_2HZ) + class MMA8451: """Driver for the MMA8451 Sensor connected over I2C. @@ -101,6 +112,9 @@ class MMA8451: _scale_range = CBits(2, _XYZ_DATA_CFG, 0) _data_rate = CBits(2, _CTRL_REG1, 4) + _high_pass_filter = CBits(1, _XYZ_DATA_CFG, 4) + _high_pass_filter_cutoff = CBits(2, _HP_FILTER_CUTOFF, 0) + def __init__(self, i2c, address: int = 0x1D) -> None: self._i2c = i2c self._address = address @@ -219,3 +233,58 @@ def data_rate(self, value: int) -> None: self._operation_mode = STANDBY_MODE self._data_rate = value self._operation_mode = ACTIVE_MODE + + @property + def high_pass_filter(self) -> str: + """ + Sensor high_pass_filter + + +----------------------------------+-----------------+ + | Mode | Value | + +==================================+=================+ + | :py:const:`mma8451.HPF_DISABLED` | :py:const:`0b0` | + +----------------------------------+-----------------+ + | :py:const:`mma8451.HPF_ENABLED` | :py:const:`0b1` | + +----------------------------------+-----------------+ + """ + values = ("HPF_DISABLED", "HPF_ENABLED") + return values[self._high_pass_filter] + + @high_pass_filter.setter + def high_pass_filter(self, value: int) -> None: + if value not in high_pass_filter_values: + raise ValueError("Value must be a valid high_pass_filter setting") + self._operation_mode = STANDBY_MODE + self._high_pass_filter = value + self._operation_mode = ACTIVE_MODE + + @property + def high_pass_filter_cutoff(self) -> str: + """ + Sensor high_pass_filter_cutoff sets the high-pass filter cutoff + frequency for removal of the offset and slower changing + acceleration data. In order to filter the acceleration data + :attr:`high_pass_filter` must be enabled. + + +---------------------------------+------------------+ + | Mode | Value | + +=================================+==================+ + | :py:const:`mma8451.CUTOFF_16HZ` | :py:const:`0b00` | + +---------------------------------+------------------+ + | :py:const:`mma8451.CUTOFF_8HZ` | :py:const:`0b01` | + +---------------------------------+------------------+ + | :py:const:`mma8451.CUTOFF_4HZ` | :py:const:`0b10` | + +---------------------------------+------------------+ + | :py:const:`mma8451.CUTOFF_2HZ` | :py:const:`0b11` | + +---------------------------------+------------------+ + """ + values = ("CUTOFF_16HZ", "CUTOFF_8HZ", "CUTOFF_4HZ", "CUTOFF_2HZ") + return values[self._high_pass_filter_cutoff] + + @high_pass_filter_cutoff.setter + def high_pass_filter_cutoff(self, value: int) -> None: + if value not in high_pass_filter_cutoff_values: + raise ValueError("Value must be a valid high_pass_filter_cutoff setting") + self._operation_mode = STANDBY_MODE + self._high_pass_filter_cutoff = value + self._operation_mode = ACTIVE_MODE