forked from mrmcwethy/Adafruit_CircuitPython_L3GD20
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Added support for setting data rate #19
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ef2476f
Better documentation in simpletest, new example: write_registers
evaherrada 9cc127c
Formatted with black
evaherrada 45c31c2
Tweaked Hz in comment
evaherrada 4c47e55
Added support for setting data rate
evaherrada b70ab58
Formatted with black
evaherrada ae5d300
Linted
evaherrada 6a65910
More formatting
evaherrada b1d3b10
Made suggested changes
evaherrada d6bb0a3
Reordered parameters
evaherrada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,11 @@ | |
L3DS20_RANGE_500DPS = const(1) | ||
L3DS20_RANGE_2000DPS = const(2) | ||
|
||
L3DS20_RATE_100HZ = const(0x00) | ||
L3DS20_RATE_200HZ = const(0x40) | ||
L3DS20_RATE_400HZ = const(0x80) | ||
L3DS20_RATE_800HZ = const(0xC0) | ||
|
||
_L3GD20_REGISTER_CTRL_REG1 = const(0x20) | ||
_L3GD20_REGISTER_CTRL_REG4 = const(0x23) | ||
|
||
|
@@ -91,9 +96,12 @@ class L3GD20: | |
|
||
:param int rng: a range value one of L3DS20_RANGE_250DPS (default), L3DS20_RANGE_500DPS, or | ||
L3DS20_RANGE_2000DPS | ||
|
||
:param int rate: a rate value one of L3DS20_RATE_100HZ (default), L3DS20_RATE_200HZ, | ||
L3DS20_RATE_400HZ, or L3DS20_RATE_800HZ | ||
""" | ||
|
||
def __init__(self, rng=L3DS20_RANGE_250DPS): | ||
def __init__(self, rng=L3DS20_RANGE_250DPS, rate=L3DS20_RATE_100HZ): | ||
chip_id = self.read_register(_ID_REGISTER) | ||
if chip_id not in (_L3GD20_CHIP_ID, _L3GD20H_CHIP_ID): | ||
raise RuntimeError( | ||
|
@@ -119,7 +127,8 @@ def __init__(self, rng=L3DS20_RANGE_250DPS): | |
# 0 XEN X-axis enable (0 = disabled, 1 = enabled) | ||
|
||
# Switch to normal mode and enable all three channels | ||
self.write_register(_L3GD20_REGISTER_CTRL_REG1, 0x0F) | ||
# self.write_register(_L3GD20_REGISTER_CTRL_REG1, 0x0F) | ||
self.write_register(_L3GD20_REGISTER_CTRL_REG1, rate + 0x0F) | ||
|
||
# Set CTRL_REG2 (0x21) | ||
# ==================================================================== | ||
|
@@ -212,12 +221,14 @@ class L3GD20_I2C(L3GD20): | |
gyro_raw = Struct(_L3GD20_REGISTER_OUT_X_L_X80, "<hhh") | ||
"""Gives the raw gyro readings, in units of rad/s.""" | ||
|
||
def __init__(self, i2c, rng=L3DS20_RANGE_250DPS, address=0x6B): | ||
def __init__( | ||
self, i2c, rng=L3DS20_RANGE_250DPS, rate=L3DS20_RATE_100HZ, address=0x6B | ||
tannewt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): | ||
import adafruit_bus_device.i2c_device as i2c_device # pylint: disable=import-outside-toplevel | ||
|
||
self.i2c_device = i2c_device.I2CDevice(i2c, address) | ||
self.buffer = bytearray(2) | ||
super().__init__(rng) | ||
super().__init__(rng, rate) | ||
|
||
def write_register(self, register, value): | ||
""" | ||
|
@@ -254,13 +265,20 @@ class L3GD20_SPI(L3GD20): | |
:param baudrate: spi baud rate default is 100000 | ||
""" | ||
|
||
def __init__(self, spi_busio, cs, rng=L3DS20_RANGE_250DPS, baudrate=100000): | ||
def __init__( | ||
self, | ||
spi_busio, | ||
cs, | ||
rng=L3DS20_RANGE_250DPS, | ||
rate=L3DS20_RATE_100HZ, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add rate after baudrate to prevent breaking code that relies on position of these args. It wouldn't matter if there was a |
||
baudrate=100000, | ||
): # pylint: disable=too-many-arguments | ||
import adafruit_bus_device.spi_device as spi_device # pylint: disable=import-outside-toplevel | ||
|
||
self._spi = spi_device.SPIDevice(spi_busio, cs, baudrate=baudrate) | ||
self._spi_bytearray1 = bytearray(1) | ||
self._spi_bytearray6 = bytearray(6) | ||
super().__init__(rng) | ||
super().__init__(rng, rate) | ||
|
||
def write_register(self, register, value): | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.