Skip to content

Commit cecbdcb

Browse files
authored
Merge pull request #29 from SquirtleSquadLeader/Clock-Source-Integration
Clock source integration
2 parents 99bcfa5 + 805cad6 commit cecbdcb

File tree

1 file changed

+65
-26
lines changed

1 file changed

+65
-26
lines changed

adafruit_mpu6050.py

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
3030
* Adafruit's Register library:
3131
https://github.com/adafruit/Adafruit_CircuitPython_Register
32+
3233
"""
3334

3435
# imports
@@ -74,13 +75,36 @@
7475
STANDARD_GRAVITY = 9.80665
7576

7677

78+
class ClockSource: # pylint: disable=too-few-public-methods
79+
"""Allowed values for :py:attr:`clock_source`.
80+
81+
* :py:attr:'ClockSource.CLKSEL_INTERNAL_8MHz
82+
* :py:attr:'ClockSource.CLKSEL_INTERNAL_X
83+
* :py:attr:'ClockSource.CLKSEL_INTERNAL_Y
84+
* :py:attr:'ClockSource.CLKSEL_INTERNAL_Z
85+
* :py:attr:'ClockSource.CLKSEL_EXTERNAL_32
86+
* :py:attr:'ClockSource.CLKSEL_EXTERNAL_19
87+
* :py:attr:'ClockSource.CLKSEL_RESERVED
88+
* :py:attr:'ClockSource.CLKSEL_STOP
89+
"""
90+
91+
CLKSEL_INTERNAL_8MHz = 0 # Internal 8MHz oscillator
92+
CLKSEL_INTERNAL_X = 1 # PLL with X Axis gyroscope reference
93+
CLKSEL_INTERNAL_Y = 2 # PLL with Y Axis gyroscope reference
94+
CLKSEL_INTERNAL_Z = 3 # PLL with Z Axis gyroscope reference
95+
CLKSEL_EXTERNAL_32 = 4 # External 32.768 kHz reference
96+
CLKSEL_EXTERNAL_19 = 5 # External 19.2 MHz reference
97+
CLKSEL_RESERVED = 6 # Reserved
98+
CLKSEL_STOP = 7 # Stops the clock, constant reset mode
99+
100+
77101
class Range: # pylint: disable=too-few-public-methods
78-
"""Allowed values for `accelerometer_range`.
102+
"""Allowed values for :py:attr:`accelerometer_range`.
79103
80-
* :attr:`Range.RANGE_2_G`
81-
* :attr:`Range.RANGE_4_G`
82-
* :attr:`Range.RANGE_8_G`
83-
* :attr:`Range.RANGE_16_G`
104+
* :py:attr:`Range.RANGE_2_G`
105+
* :py:attr:`Range.RANGE_4_G`
106+
* :py:attr:`Range.RANGE_8_G`
107+
* :py:attr:`Range.RANGE_16_G`
84108
85109
"""
86110

@@ -91,12 +115,12 @@ class Range: # pylint: disable=too-few-public-methods
91115

92116

93117
class GyroRange: # pylint: disable=too-few-public-methods
94-
"""Allowed values for `gyro_range`.
118+
"""Allowed values for :py:attr:`gyro_range`.
95119
96-
* :attr:`GyroRange.RANGE_250_DPS`
97-
* :attr:`GyroRange.RANGE_500_DPS`
98-
* :attr:`GyroRange.RANGE_1000_DPS`
99-
* :attr:`GyroRange.RANGE_2000_DPS`
120+
* :py:attr:`GyroRange.RANGE_250_DPS`
121+
* :py:attr:`GyroRange.RANGE_500_DPS`
122+
* :py:attr:`GyroRange.RANGE_1000_DPS`
123+
* :py:attr:`GyroRange.RANGE_2000_DPS`
100124
101125
"""
102126

@@ -107,15 +131,15 @@ class GyroRange: # pylint: disable=too-few-public-methods
107131

108132

109133
class Bandwidth: # pylint: disable=too-few-public-methods
110-
"""Allowed values for `filter_bandwidth`.
134+
"""Allowed values for :py:attr:`filter_bandwidth`.
111135
112-
* :attr:`Bandwidth.BAND_260_HZ`
113-
* :attr:`Bandwidth.BAND_184_HZ`
114-
* :attr:`Bandwidth.BAND_94_HZ`
115-
* :attr:`Bandwidth.BAND_44_HZ`
116-
* :attr:`Bandwidth.BAND_21_HZ`
117-
* :attr:`Bandwidth.BAND_10_HZ`
118-
* :attr:`Bandwidth.BAND_5_HZ`
136+
* :py:attr:`Bandwidth.BAND_260_HZ`
137+
* :py:attr:`Bandwidth.BAND_184_HZ`
138+
* :py:attr:`Bandwidth.BAND_94_HZ`
139+
* :py:attr:`Bandwidth.BAND_44_HZ`
140+
* :py:attr:`Bandwidth.BAND_21_HZ`
141+
* :py:attr:`Bandwidth.BAND_10_HZ`
142+
* :py:attr:`Bandwidth.BAND_5_HZ`
119143
120144
"""
121145

@@ -129,12 +153,12 @@ class Bandwidth: # pylint: disable=too-few-public-methods
129153

130154

131155
class Rate: # pylint: disable=too-few-public-methods
132-
"""Allowed values for `cycle_rate`.
156+
"""Allowed values for :py:attr:`cycle_rate`.
133157
134-
* :attr:`Rate.CYCLE_1_25_HZ`
135-
* :attr:`Rate.CYCLE_5_HZ`
136-
* :attr:`Rate.CYCLE_20_HZ`
137-
* :attr:`Rate.CYCLE_40_HZ`
158+
* :py:attr:`Rate.CYCLE_1_25_HZ`
159+
* :py:attr:`Rate.CYCLE_5_HZ`
160+
* :py:attr:`Rate.CYCLE_20_HZ`
161+
* :py:attr:`Rate.CYCLE_40_HZ`
138162
139163
"""
140164

@@ -175,7 +199,6 @@ class MPU6050:
175199
acc_x, acc_y, acc_z = sensor.acceleration
176200
gyro_x, gyro_y, gyro_z = sensor.gyro
177201
temperature = sensor.temperature
178-
179202
"""
180203

181204
def __init__(self, i2c_bus: I2C, address: int = _MPU6050_DEFAULT_ADDRESS) -> None:
@@ -191,7 +214,9 @@ def __init__(self, i2c_bus: I2C, address: int = _MPU6050_DEFAULT_ADDRESS) -> Non
191214
self._gyro_range = GyroRange.RANGE_500_DPS
192215
self._accel_range = Range.RANGE_2_G
193216
sleep(0.100)
194-
self._clock_source = 1 # set to use gyro x-axis as reference
217+
self.clock_source = (
218+
ClockSource.CLKSEL_INTERNAL_X
219+
) # set to use gyro x-axis as reference
195220
sleep(0.100)
196221
self.sleep = False
197222
sleep(0.010)
@@ -206,7 +231,7 @@ def reset(self) -> None:
206231
_signal_path_reset = 0b111 # reset all sensors
207232
sleep(0.100)
208233

209-
_clock_source = RWBits(3, _MPU6050_PWR_MGMT_1, 0)
234+
_clksel = RWBits(3, _MPU6050_PWR_MGMT_1, 0)
210235
_device_id = ROUnaryStruct(_MPU6050_WHO_AM_I, ">B")
211236

212237
_reset = RWBit(_MPU6050_PWR_MGMT_1, 7, 1)
@@ -347,3 +372,17 @@ def cycle_rate(self, value: int) -> None:
347372
raise ValueError("cycle_rate must be a Rate")
348373
self._cycle_rate = value
349374
sleep(0.01)
375+
376+
@property
377+
def clock_source(self) -> int:
378+
"""The clock source for the sensor"""
379+
return self._clksel
380+
381+
@clock_source.setter
382+
def clock_source(self, value: int) -> None:
383+
"""Select between Internal/External clock sources"""
384+
if value not in range(8):
385+
raise ValueError(
386+
"clock_source must be ClockSource value, integer from 0 - 7."
387+
)
388+
self._clksel = value

0 commit comments

Comments
 (0)