Skip to content

Black reformatting with Python 3 target. #11

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 2 commits into from
Apr 10, 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
142 changes: 84 additions & 58 deletions adafruit_lsm6ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@
from adafruit_register.i2c_struct import ROUnaryStruct, Struct
from adafruit_register.i2c_bits import RWBits
from adafruit_register.i2c_bit import RWBit

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LSM6DSOX.git"


_LSM6DS_DEFAULT_ADDRESS = const(0x6a)
_LSM6DS_DEFAULT_ADDRESS = const(0x6A)

_LSM6DS_CHIP_ID = const(0x6C)
_ISM330DHCT_CHIP_ID = const(0x6B)
Expand Down Expand Up @@ -95,6 +96,7 @@

_MILLI_G_TO_ACCEL = 0.00980665


class CV:
"""struct helper"""

Expand All @@ -115,62 +117,74 @@ def is_valid(cls, value):
"Returns true if the given value is a member of the CV"
return value in cls.string


class AccelRange(CV):
"""Options for ``accelerometer_range``"""
pass #pylint: disable=unnecessary-pass

AccelRange.add_values((
('RANGE_2G', 0, 2, 0.061),
('RANGE_16G', 1, 16, 0.488),
('RANGE_4G', 2, 4, 0.122),
('RANGE_8G', 3, 8, 0.244)
))

AccelRange.add_values(
(
("RANGE_2G", 0, 2, 0.061),
("RANGE_16G", 1, 16, 0.488),
("RANGE_4G", 2, 4, 0.122),
("RANGE_8G", 3, 8, 0.244),
)
)


class GyroRange(CV):
"""Options for ``gyro_data_range``"""
pass #pylint: disable=unnecessary-pass

GyroRange.add_values((
('RANGE_125_DPS', 125, 125, 4.375),
('RANGE_250_DPS', 0, 250, 8.75),
('RANGE_500_DPS', 1, 500, 17.50),
('RANGE_1000_DPS', 2, 1000, 35.0),
('RANGE_2000_DPS', 3, 2000, 70.0),
('RANGE_4000_DPS', 4000, 4000, 140.0)
))

GyroRange.add_values(
(
("RANGE_125_DPS", 125, 125, 4.375),
("RANGE_250_DPS", 0, 250, 8.75),
("RANGE_500_DPS", 1, 500, 17.50),
("RANGE_1000_DPS", 2, 1000, 35.0),
("RANGE_2000_DPS", 3, 2000, 70.0),
("RANGE_4000_DPS", 4000, 4000, 140.0),
)
)


class Rate(CV):
"""Options for ``accelerometer_data_rate`` and ``gyro_data_rate``"""
pass #pylint: disable=unnecessary-pass

Rate.add_values((
('RATE_SHUTDOWN', 0, 0, None),
('RATE_12_5_HZ', 1, 12.5, None),
('RATE_26_HZ', 2, 26.0, None),
('RATE_52_HZ', 3, 52.0, None),
('RATE_104_HZ', 4, 104.0, None),
('RATE_208_HZ', 5, 208.0, None),
('RATE_416_HZ', 6, 416.0, None),
('RATE_833_HZ', 7, 833.0, None),
('RATE_1_66K_HZ', 8, 1066.0, None),
('RATE_3_33K_HZ', 9, 3033.0, None),
('RATE_6_66K_HZ', 10, 6066.0, None),
('RATE_1_6_HZ', 11, 1.6, None)
))


Rate.add_values(
(
("RATE_SHUTDOWN", 0, 0, None),
("RATE_12_5_HZ", 1, 12.5, None),
("RATE_26_HZ", 2, 26.0, None),
("RATE_52_HZ", 3, 52.0, None),
("RATE_104_HZ", 4, 104.0, None),
("RATE_208_HZ", 5, 208.0, None),
("RATE_416_HZ", 6, 416.0, None),
("RATE_833_HZ", 7, 833.0, None),
("RATE_1_66K_HZ", 8, 1066.0, None),
("RATE_3_33K_HZ", 9, 3033.0, None),
("RATE_6_66K_HZ", 10, 6066.0, None),
("RATE_1_6_HZ", 11, 1.6, None),
)
)


class AccelHPF(CV):
"""Options for the accelerometer high pass filter"""
pass #pylint: disable=unnecessary-pass

AccelHPF.add_values((
('SLOPE', 0, 0, None),
('HPF_DIV100', 1, 0, None),
('HPF_DIV9', 2, 0, None),
('HPF_DIV400', 3, 0, None),
))

AccelHPF.add_values(
(
("SLOPE", 0, 0, None),
("HPF_DIV100", 1, 0, None),
("HPF_DIV9", 2, 0, None),
("HPF_DIV400", 3, 0, None),
)
)

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

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

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

Expand All @@ -179,11 +193,11 @@ class LSM6DS: #pylint: disable=too-many-instance-attributes

"""

#ROUnaryStructs:
# ROUnaryStructs:
_chip_id = ROUnaryStruct(_LSM6DS_WHOAMI, "<b")
_temperature = ROUnaryStruct(_LSM6DS_OUT_TEMP_L, "<h")

#RWBits:
# RWBits:
_ois_ctrl_from_ui = RWBit(_LSM6DS_FUNC_CFG_ACCESS, 0)
_shub_reg_access = RWBit(_LSM6DS_FUNC_CFG_ACCESS, 6)
_func_cfg_access = RWBit(_LSM6DS_FUNC_CFG_ACCESS, 7)
Expand Down Expand Up @@ -245,16 +259,18 @@ def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
if self.CHIP_ID is None:
raise AttributeError("LSM6DS Parent Class cannot be directly instantiated")
if self._chip_id != self.CHIP_ID:
raise RuntimeError("Failed to find %s - check your wiring!"%self.__class__.__name__)
raise RuntimeError(
"Failed to find %s - check your wiring!" % self.__class__.__name__
)
self.reset()

self._bdu = True

self.accelerometer_data_rate = Rate.RATE_104_HZ #pylint: disable=no-member
self.gyro_data_rate = Rate.RATE_104_HZ #pylint: disable=no-member
self.accelerometer_data_rate = Rate.RATE_104_HZ # pylint: disable=no-member
self.gyro_data_rate = Rate.RATE_104_HZ # pylint: disable=no-member

self.accelerometer_range = AccelRange.RANGE_4G #pylint: disable=no-member
self.gyro_range = GyroRange.RANGE_250_DPS #pylint: disable=no-member
self.accelerometer_range = AccelRange.RANGE_4G # pylint: disable=no-member
self.gyro_range = GyroRange.RANGE_250_DPS # pylint: disable=no-member

def reset(self):
"Resets the sensor's configuration into an initial state"
Expand All @@ -273,7 +289,7 @@ def acceleration(self):
y = self._scale_xl_data(raw_accel_data[1])
z = self._scale_xl_data(raw_accel_data[2])

return(x, y, z)
return (x, y, z)

@property
def gyro(self):
Expand All @@ -286,7 +302,11 @@ def gyro(self):
return (x, y, z)

def _scale_xl_data(self, raw_measurement):
return raw_measurement * AccelRange.lsb[self._cached_accel_range] * _MILLI_G_TO_ACCEL
return (
raw_measurement
* AccelRange.lsb[self._cached_accel_range]
* _MILLI_G_TO_ACCEL
)

def _scale_gyro_data(self, raw_measurement):
return raw_measurement * GyroRange.lsb[self._cached_gyro_range] / 1000
Expand All @@ -297,14 +317,14 @@ def accelerometer_range(self):
Note that larger ranges will be less accurate. Must be an `AccelRange`"""
return self._cached_accel_range

#pylint: disable=no-member
# pylint: disable=no-member
@accelerometer_range.setter
def accelerometer_range(self, value):
if not AccelRange.is_valid(value):
raise AttributeError("range must be an `AccelRange`")
self._accel_range = value
self._cached_accel_range = value
sleep(.2) # needed to let new range settle
sleep(0.2) # needed to let new range settle

@property
def gyro_range(self):
Expand Down Expand Up @@ -332,9 +352,9 @@ def gyro_range(self, value):
self._gyro_range = value

self._cached_gyro_range = value
sleep(.2) # needed to let new range settle
sleep(0.2) # needed to let new range settle

#pylint: enable=no-member
# pylint: enable=no-member

@property
def accelerometer_data_rate(self):
Expand All @@ -350,7 +370,6 @@ def accelerometer_data_rate(self, value):
self._accel_data_rate = value
# sleep(.2) # needed to let new range settle


@property
def gyro_data_rate(self):
"""Select the rate at which the gyro takes measurements. Must be a `Rate`"""
Expand Down Expand Up @@ -387,38 +406,45 @@ def high_pass_filter(self, value):
self._pass_filter = value


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

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

:param ~busio.I2C i2c_bus: The I2C bus the LSM6DSOX is connected to.
:param address: The I2C slave address of the sensor

"""

CHIP_ID = _LSM6DS_CHIP_ID

def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
super().__init__(i2c_bus, address)
self._i3c_disable = True

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

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

"""Driver for the LSM6DS33 6-axis accelerometer and gyroscope.

:param ~busio.I2C i2c_bus: The I2C bus the LSM6DS33 is connected to.
:param address: The I2C slave address of the sensor

"""

CHIP_ID = _LSM6DS33_CHIP_ID

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

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

"""Driver for the LSM6DS33 6-axis accelerometer and gyroscope.

:param ~busio.I2C i2c_bus: The I2C bus the LSM6DS33 is connected to.
:param address: The I2C slave address of the sensor

"""

CHIP_ID = _ISM330DHCT_CHIP_ID

def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
super().__init__(i2c_bus, address)

Expand Down
Loading