Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions adafruit_mma8451.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"""

import struct
import time

from adafruit_bus_device import i2c_device
from micropython import const
Expand Down Expand Up @@ -127,8 +128,18 @@ def __init__(self, i2c: I2C, *, address: int = _MMA8451_DEFAULT_ADDRESS) -> None
raise RuntimeError("Failed to find MMA8451, check wiring!")
# Reset and wait for chip to be ready.
self._write_u8(_MMA8451_REG_CTRL_REG2, 0x40)
while self._read_u8(_MMA8451_REG_CTRL_REG2) & 0x40 > 0:
pass
attempts = 10
while attempts:
attempts -= 1
try:
if not self._read_u8(_MMA8451_REG_CTRL_REG2) & 0x40 > 0:
# Continue if chip is ready
break
# Ignore OSError. After reset the device may fail to respond
except OSError as e:
time.sleep(0.001)
if not attempts:
raise e
# Enable 4G range.
self._write_u8(_MMA8451_REG_XYZ_DATA_CFG, RANGE_4G)
# High resolution mode.
Expand Down