Skip to content

Commit

Permalink
clean up - black
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryneedell committed Mar 9, 2022
1 parent a3319a1 commit 039c91a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
24 changes: 10 additions & 14 deletions adafruit_lsm6ds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ class LSM6DS: # pylint: disable=too-many-instance-attributes
_raw_temp_data = Struct(_LSM6DS_OUT_TEMP_L, "<h")
_emb_func_en_a = Struct(_LSM6DS_EMB_FUNC_EN_A, "<b")
_emb_func_en_b = Struct(_LSM6DS_EMB_FUNC_EN_B, "<b")
_mlc0_src = Struct(_LSM6DS_MLC0_SRC, "<b")
# _all_int = Struct(_LSM6DS_ALL_INT_SRC, "<bbbbbbbb")
_mlc0_src = Struct(_LSM6DS_MLC0_SRC, "<bbbbbbbb")
# RWBits:
_accel_range = RWBits(2, _LSM6DS_CTRL1_XL, 2)
_accel_data_rate = RWBits(4, _LSM6DS_CTRL1_XL, 4)
Expand Down Expand Up @@ -241,7 +240,6 @@ def __init__(
self.accelerometer_range = AccelRange.RANGE_4G # pylint: disable=no-member
self.gyro_range = GyroRange.RANGE_250_DPS # pylint: disable=no-member
# Load and configure MLC if UCF file is provided
print(ucf)
if ucf is not None:
self.load_mlc(ucf)

Expand Down Expand Up @@ -406,17 +404,15 @@ def temperature(self) -> float:

return temp * 0.0625

def set_embedded_functions(self, enable, emb_ab=None):
"""DocString Here"""
def _set_embedded_functions(self, enable, emb_ab=None):
"""Enable/disable embedded functions - returns prior settings when disabled"""
self._mem_bank = 1
if enable:
self._emb_func_en_a = emb_ab[0]
self._emb_func_en_b = emb_ab[1]
else:
emb_a = self._emb_func_en_a
emb_b = self._emb_func_en_b
print(emb_a)
print(emb_b)
self._emb_func_en_a = (emb_a[0] & 0xC7,)
self._emb_func_en_b = (emb_b[0] & 0xE6,)
emb_ab = (emb_a, emb_b)
Expand All @@ -425,20 +421,20 @@ def set_embedded_functions(self, enable, emb_ab=None):
return emb_ab

def load_mlc(self, ucf):
"""DOCString Here"""
"""Load MLC configuration file into sensor"""
buf = bytearray(2)
with self.i2c_device as i2c:
# Load MLC config from file
with open(ucf, "r") as ucf_file:
for line in ucf_file:
if line.startswith("Ac"):
command = [int(v, 16) for v in line.strip().split(" ")[1:3]]
print(line)
buf[0] = command[0]
buf[1] = command[1]
i2c.write(buf)

emb_ab = self.set_embedded_functions(False)
# Disable embudded function -- save current settings
emb_ab = self._set_embedded_functions(False)

# Disable I3C interface
self._i3c_disable = 1
Expand All @@ -449,19 +445,19 @@ def load_mlc(self, ucf):
# Route signals on interrupt pin 1
self._mem_bank = 1
self._route_int1 &= 1
self._mem_bank = 1
self._mem_bank = 0

# Configure interrupt pin mode
self._tap_latch = 1
self._tap_clear = 1

self.set_embedded_functions(True, emb_ab)
# Enabble Embedded Functions using previously stored settings
self._set_embedded_functions(True, emb_ab)

def read_mlc_output(self):
"""DOCString here"""
"""Read MLC results"""
buf = None
if self._mlc_status:
# junk = self._all_int
self._mem_bank = 1
buf = self._mlc0_src
self._mem_bank = 0
Expand Down
24 changes: 23 additions & 1 deletion examples/lsm6ds_lsm6dsox_mlc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# NOTE: The pre-trained models (UCF files) for the examples can be found here:
# https://github.com/STMicroelectronics/STMems_Machine_Learning_Core/tree/master/application_examples/lsm6dsox

import time
import board
from adafruit_lsm6ds.lsm6dsox import LSM6DSOX
from adafruit_lsm6ds import Rate, AccelRange, GyroRange
Expand All @@ -23,15 +24,36 @@
lsm.accelerometer_range = AccelRange.RANGE_4G
lsm.accelerometer_data_rate = Rate.RATE_26_HZ
lsm.gyro_data_rate = Rate.RATE_26_HZ


# Head gestures example
# UCF_FILE = "lsm6dsox_head_gestures.ucf"
# UCF_LABELS = {0:"Nod", 1:"Shake", 2:"Stationary", 3:"Swing", 4:"Walk"}
# NOTE: Selected data rate and scale must match the MLC data rate and scale.
# lsm = LSM6DSOX(i2c, gyro_odr=26, accel_odr=26, gyro_scale=250, accel_scale=2, ucf=UCF_FILE)
# lsm = LSM6DSOX(i2c, ucf=UCF_FILE)
# lsm.gyro_range = GyroRange.RANGE_250_DPS
# lsm.accelerometer_range = AccelRange.RANGE_2G
# lsm.accelerometer_data_rate = Rate.RATE_26_HZ
# lsm.gyro_data_rate = Rate.RATE_26_HZ

# 6 DOF Position example
# UCF_FILE = "lsm6dsox_six_d_position.ucf"
# UCF_LABELS = {0:"None", 1:"X-UP", 2:"X-DOWN", 3:"Y-UP", 4:"Y-DOWN", 5:"Z-UP", 6:"Z-DOWN"}
# NOTE: Selected data rate and scale must match the MLC data rate and scale.
# lsm = LSM6DSOX(i2c, ucf=UCF_FILE)
# lsm.gyro_range = GyroRange.RANGE_250_DPS
# lsm.accelerometer_range = AccelRange.RANGE_2G
# lsm.accelerometer_data_rate = Rate.RATE_26_HZ
# lsm.gyro_data_rate = Rate.RATE_26_HZ


print("MLC configured...")

while True:
buf = lsm.read_mlc_output()
if buf is not None:
print(UCF_LABELS[buf[0]])
# delay to allow interrupt flag to clear
# interrupt stays high for one sample interval
# (38.4 ms @ 26 Hz ot 19.2 ms @ 52Hz)
time.sleep(0.05)
2 changes: 1 addition & 1 deletion examples/lsm6ds_lsm6dsox_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import board
from adafruit_lsm6ds.lsm6dsox import LSM6DSOX

i2c = board.STEMMA_I2C() # uses board.SCL and board.SDA
i2c = board.I2C() # uses board.SCL and board.SDA
sensor = LSM6DSOX(i2c)

while True:
Expand Down

0 comments on commit 039c91a

Please sign in to comment.