Skip to content

Commit e9048ed

Browse files
authored
Merge pull request #7 from ladyada/master
add pedometer functions
2 parents ac21381 + 971b21f commit e9048ed

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

adafruit_lsm6ds.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
_LSM6DS_CTRL_5_C = const(0x14)
7575
_LSM6DS_MASTER_CONFIG = const(0x14)
7676
_LSM6DS_CTRL9_XL = const(0x18)
77+
_LSM6DS_CTRL10_C = const(0x19)
7778
_LSM6DS_OUT_TEMP_L = const(0x20)
7879
_LSM6DS_OUT_TEMP_H = const(0x21)
7980
_LSM6DS_OUTX_L_G = const(0x22)
@@ -88,6 +89,8 @@
8889
_LSM6DS_OUTY_H_A = const(0x2B)
8990
_LSM6DS_OUTZ_L_A = const(0x2C)
9091
_LSM6DS_OUTZ_H_A = const(0x2D)
92+
_LSM6DS_STEP_COUNTER = const(0x4B)
93+
_LSM6DS_TAP_CFG = const(0x58)
9194

9295
_MILLI_G_TO_ACCEL = 0.00980665
9396

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

211214
_raw_temp = ROUnaryStruct(_LSM6DS_OUT_TEMP_L, "<h")
212-
213215
_raw_accel_data = Struct(_LSM6DS_OUTX_L_A, "<hhh")
214216
_raw_gyro_data = Struct(_LSM6DS_OUTX_L_G, "<hhh")
217+
218+
pedometer_steps = ROUnaryStruct(_LSM6DS_STEP_COUNTER, "<h")
219+
pedometer_reset = RWBit(_LSM6DS_CTRL10_C, 1)
220+
_ped_enable = RWBit(_LSM6DS_TAP_CFG, 6)
221+
_func_enable = RWBit(_LSM6DS_CTRL10_C, 2)
222+
215223
CHIP_ID = None
224+
216225
def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
217226
self._cached_accel_range = None
218227
self._cached_gyro_range = None
@@ -340,6 +349,17 @@ def gyro_data_rate(self, value):
340349
self._gyro_data_rate = value
341350
# sleep(.2) # needed to let new range settle
342351

352+
@property
353+
def pedometer_enable(self):
354+
""" Whether the pedometer function on the accelerometer is enabled"""
355+
return self._ped_enable and self._func_enable
356+
357+
@pedometer_enable.setter
358+
def pedometer_enable(self, enable):
359+
self._ped_enable = enable
360+
self._func_enable = enable
361+
self.pedometer_reset = enable
362+
343363
class LSM6DSOX(LSM6DS): #pylint: disable=too-many-instance-attributes
344364

345365
"""Driver for the LSM6DSOX 6-axis accelerometer and gyroscope.

examples/lsm6ds_pedometer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
""" This example shows off how to use the step counter built
2+
into the ST LSM6DS series IMUs. The steps are calculated in
3+
the chip so you don't have to do any calculations!"""
4+
5+
import time
6+
import board
7+
import busio
8+
#pylint:disable=no-member
9+
from adafruit_lsm6ds import LSM6DS33, Rate, AccelRange
10+
11+
i2c = busio.I2C(board.SCL, board.SDA)
12+
13+
sensor = LSM6DS33(i2c)
14+
15+
# enable accelerometer sensor @ 2G and 26 Hz
16+
sensor.accelerometer_range = AccelRange.RANGE_2G
17+
sensor.accelerometer_data_rate = Rate.RATE_26_HZ
18+
# no gyro used for step detection
19+
sensor.gyro_data_rate = Rate.RATE_SHUTDOWN
20+
21+
#enable the pedometer
22+
sensor.pedometer_enable = True
23+
24+
while True:
25+
print("Steps: ", sensor.pedometer_steps)
26+
time.sleep(1)

0 commit comments

Comments
 (0)