Skip to content

add pedometer functions #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
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
22 changes: 21 additions & 1 deletion adafruit_lsm6ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
_LSM6DS_CTRL_5_C = const(0x14)
_LSM6DS_MASTER_CONFIG = const(0x14)
_LSM6DS_CTRL9_XL = const(0x18)
_LSM6DS_CTRL10_C = const(0x19)
_LSM6DS_OUT_TEMP_L = const(0x20)
_LSM6DS_OUT_TEMP_H = const(0x21)
_LSM6DS_OUTX_L_G = const(0x22)
Expand All @@ -88,6 +89,8 @@
_LSM6DS_OUTY_H_A = const(0x2B)
_LSM6DS_OUTZ_L_A = const(0x2C)
_LSM6DS_OUTZ_H_A = const(0x2D)
_LSM6DS_STEP_COUNTER = const(0x4B)
_LSM6DS_TAP_CFG = const(0x58)

_MILLI_G_TO_ACCEL = 0.00980665

Expand Down Expand Up @@ -209,10 +212,16 @@ class LSM6DS: #pylint: disable=too-many-instance-attributes
_i3c_disable = RWBit(_LSM6DS_CTRL9_XL, 1)

_raw_temp = ROUnaryStruct(_LSM6DS_OUT_TEMP_L, "<h")

_raw_accel_data = Struct(_LSM6DS_OUTX_L_A, "<hhh")
_raw_gyro_data = Struct(_LSM6DS_OUTX_L_G, "<hhh")

pedometer_steps = ROUnaryStruct(_LSM6DS_STEP_COUNTER, "<h")
pedometer_reset = RWBit(_LSM6DS_CTRL10_C, 1)
_ped_enable = RWBit(_LSM6DS_TAP_CFG, 6)
_func_enable = RWBit(_LSM6DS_CTRL10_C, 2)

CHIP_ID = None

def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
self._cached_accel_range = None
self._cached_gyro_range = None
Expand Down Expand Up @@ -340,6 +349,17 @@ def gyro_data_rate(self, value):
self._gyro_data_rate = value
# sleep(.2) # needed to let new range settle

@property
def pedometer_enable(self):
""" Whether the pedometer function on the accelerometer is enabled"""
return self._ped_enable and self._func_enable

@pedometer_enable.setter
def pedometer_enable(self, enable):
self._ped_enable = enable
self._func_enable = enable
self.pedometer_reset = enable

class LSM6DSOX(LSM6DS): #pylint: disable=too-many-instance-attributes

"""Driver for the LSM6DSOX 6-axis accelerometer and gyroscope.
Expand Down
26 changes: 26 additions & 0 deletions examples/lsm6ds_pedometer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
""" This example shows off how to use the step counter built
into the ST LSM6DS series IMUs. The steps are calculated in
the chip so you don't have to do any calculations!"""

import time
import board
import busio
#pylint:disable=no-member
from adafruit_lsm6ds import LSM6DS33, Rate, AccelRange

i2c = busio.I2C(board.SCL, board.SDA)

sensor = LSM6DS33(i2c)

# enable accelerometer sensor @ 2G and 26 Hz
sensor.accelerometer_range = AccelRange.RANGE_2G
sensor.accelerometer_data_rate = Rate.RATE_26_HZ
# no gyro used for step detection
sensor.gyro_data_rate = Rate.RATE_SHUTDOWN

#enable the pedometer
sensor.pedometer_enable = True

while True:
print("Steps: ", sensor.pedometer_steps)
time.sleep(1)