Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed Jun 23, 2023
1 parent e7d0785 commit 9175513
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
10 changes: 5 additions & 5 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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-
23 changes: 23 additions & 0 deletions examples/mma8451_high_pass_filter_cutoff.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion examples/mma8451_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
69 changes: 69 additions & 0 deletions micropython_mma8451/mma8451.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
_DATA = const(0x01)
_XYZ_DATA_CFG = const(0x0E)
_CTRL_REG1 = const(0x2A)
_HP_FILTER_CUTOFF = const(0x2F)

_GRAVITY = 9.80665

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 9175513

Please sign in to comment.