diff options
| author | Daniel Thompson <daniel@redfelineninja.org.uk> | 2020-06-09 20:29:00 (GMT) |
|---|---|---|
| committer | Daniel Thompson <daniel@redfelineninja.org.uk> | 2020-06-09 20:31:55 (GMT) |
| commit | ccaf12750ba39fe0bc1fa6d2425ef2d1831ded9a (patch) | |
| tree | 4a34126a5afa06c1178527a9e9268396b17811db /wasp/drivers | |
| parent | dea2ba8d65f03c6c203f58d5db4bfdccda6f7c25 (diff) | |
wasp: apps: Step counter application
Currently there's no fancy algorithms to estimate stride length. Just
pure simple step counting directly from the hardware's "intelligence
engine".
Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
Diffstat (limited to 'wasp/drivers')
| -rw-r--r-- | wasp/drivers/bma421.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/wasp/drivers/bma421.py b/wasp/drivers/bma421.py new file mode 100644 index 0000000..aae4b59 --- /dev/null +++ b/wasp/drivers/bma421.py @@ -0,0 +1,53 @@ +# SPDX-License-Identifier: LGPL-3.0-or-later +# Copyright (C) 2020 Daniel Thompson + +"""Bosch BMA421 accelerometer driver +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +""" + +import bma42x +import time + +class BMA421: + """BMA421 driver + + .. automethod:: __init__ + """ + def __init__(self, i2c): + """Configure the driver. + + :param machine.I2C i2c: I2C bus used to access the sensor. + """ + self._dev = bma42x.BMA42X(i2c) + + def reset(self): + """Reset and reinitialize the sensor.""" + dev = self._dev + + # Init, reset, wait for reset, enable I2C watchdog + dev.init() + dev.set_command_register(0xb6) + time.sleep(0.20) + dev.set_reg(bma42x.NV_CONFIG_ADDR, 6); + + # Configure the sensor for basic step counting + dev.write_config_file() + dev.set_accel_enable(True) + dev.set_accel_config(odr=bma42x.OUTPUT_DATA_RATE_100HZ, + range=bma42x.ACCEL_RANGE_2G, + bandwidth=bma42x.ACCEL_NORMAL_AVG4, + perf_mode=bma42x.CIC_AVG_MODE) + dev.feature_enable(bma42x.STEP_CNTR, True) + + @property + def steps(self): + """Report the number of steps counted.""" + return self._dev.step_counter_output() + + @steps.setter + def steps(self, value): + if value != 0: + raise ValueError() + # TODO: There is a more efficient way to reset the step counter + # but I haven't looked it up yet! + self.reset() |
