Skip to content

Commit c1d6450

Browse files
committed
added rate selections without perf mode requirements
1 parent c71f05a commit c1d6450

File tree

2 files changed

+85
-45
lines changed

2 files changed

+85
-45
lines changed

adafruit_lis3mdl.py

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -104,41 +104,37 @@ class Range(CV):
104104
('RANGE_16_GAUSS', 3, 16, 1711)
105105
))
106106

107+
class PerformanceMode(CV):
108+
"""Options for `performance_mode` """
109+
pass #pylint: disable=unnecessary-pass
107110

108-
# lis3mdl_range_t range = getRange();
109-
# float scale = 1; // LSB per gauss
110-
# if (range == LIS3MDL_RANGE_16_GAUSS)
111-
# scale = 1711;
112-
# if (range == LIS3MDL_RANGE_12_GAUSS)
113-
# scale = 2281;
114-
# if (range == LIS3MDL_RANGE_8_GAUSS)
115-
# scale = 3421;
116-
# if (range == LIS3MDL_RANGE_4_GAUSS)
117-
# scale = 6842;
111+
PerformanceMode.add_values((
112+
('MODE_LOW_POWER', 0, 'Low Power', None),
113+
('MODE_MEDIUM', 1, 'Medium Power', None),
114+
('MODE_HIGH', 2, 'High Power', None),
115+
('MODE_ULTRA', 3, 'Ultra-high Power', None)
116+
))
117+
class Rate(CV):
118+
"""Options for `data_rate`"""
119+
pass #pylint: disable=unnecessary-pass
118120

121+
Rate.add_values((
122+
('RATE_0_625_HZ', 0b0000, 0.625, None),
123+
('RATE_1_25_HZ', 0b0010, 1.25, None),
124+
('RATE_2_5_HZ', 0b0100, 2.5, None),
125+
('RATE_5_HZ', 0b0110, 5.0, None),
126+
('RATE_10_HZ', 0b1000, 10.0, None),
127+
('RATE_20_HZ', 0b1010, 20.0, None),
128+
('RATE_40_HZ', 0b1100, 40.0, None),
129+
('RATE_80_HZ', 0b1110, 80.0, None)
130+
))
119131
# /** The magnetometer data rate, includes FAST_ODR bit */
120-
# typedef enum {
121-
# LIS3MDL_DATARATE_0_625_HZ = 0b0000, ///< 0.625 Hz
122-
# LIS3MDL_DATARATE_1_25_HZ = 0b0010, ///< 1.25 Hz
123-
# LIS3MDL_DATARATE_2_5_HZ = 0b0100, ///< 2.5 Hz
124-
# LIS3MDL_DATARATE_5_HZ = 0b0110, ///< 5 Hz
125-
# LIS3MDL_DATARATE_10_HZ = 0b1000, ///< 10 Hz
126-
# LIS3MDL_DATARATE_20_HZ = 0b1010, ///< 20 Hz
127-
# LIS3MDL_DATARATE_40_HZ = 0b1100, ///< 40 Hz
128-
# LIS3MDL_DATARATE_80_HZ = 0b1110, ///< 80 Hz
129-
# LIS3MDL_DATARATE_155_HZ = 0b0001, ///< 155 Hz (FAST_ODR + UHP)
130-
# LIS3MDL_DATARATE_300_HZ = 0b0011, ///< 300 Hz (FAST_ODR + HP)
131-
# LIS3MDL_DATARATE_560_HZ = 0b0101, ///< 560 Hz (FAST_ODR + MP)
132-
# LIS3MDL_DATARATE_1000_HZ = 0b0111, ///< 1000 Hz (FAST_ODR + LP)
133-
# } lis3mdl_dataRate_t;
134132

135-
# /** The magnetometer performance mode */
136-
# typedef enum {
137-
# LIS3MDL_LOWPOWERMODE = 0b00, ///< Low power mode
138-
# LIS3MDL_MEDIUMMODE = 0b01, ///< Medium performance mode
139-
# LIS3MDL_HIGHMODE = 0b10, ///< High performance mode
140-
# LIS3MDL_ULTRAHIGHMODE = 0b11, ///< Ultra-high performance mode
141-
# } lis3mdl_performancemode_t;
133+
# RATE_155_HZ = 0b0001, ///< 155 Hz (FAST_ODR + UHP)
134+
# RATE_300_HZ = 0b0011, ///< 300 Hz (FAST_ODR + HP)
135+
# RATE_560_HZ = 0b0101, ///< 560 Hz (FAST_ODR + MP)
136+
# RATE_1000_HZ = 0b0111, ///< 1000 Hz (FAST_ODR + LP)
137+
# } lis3mdl_dataRate_t;
142138

143139
# /** The magnetometer operation mode */
144140
# typedef enum {
@@ -159,7 +155,7 @@ class LIS3MDL:
159155

160156
_operation_mode = RWBits(2, _LIS3MDL_CTRL_REG3, 0)
161157

162-
_data_rate = RWBits(3, _LIS3MDL_CTRL_REG1, 2)
158+
_data_rate = RWBits(4, _LIS3MDL_CTRL_REG1, 1)
163159

164160
_raw_mag_data = Struct(_LIS3MDL_OUT_X_L, "<hhh")
165161

@@ -173,7 +169,8 @@ def __init__(self, i2c_bus, address=_LIS3MDL_DEFAULT_ADDRESS):
173169
# // set high quality performance mode
174170
# setPerformanceMode(LIS3MDL_ULTRAHIGHMODE);
175171
self._perf_mode = 0b11
176-
self._z_perf_mode = 0b11
172+
self.performance_mode = PerformanceMode.MODE_ULTRA #pylint: disable=no-member
173+
177174
# // 155Hz default rate
178175
# setDataRate(LIS3MDL_DATARATE_155_HZ);
179176

@@ -218,15 +215,37 @@ def range(self, value):
218215

219216
sleep(0.010)
220217

221-
# @property
222-
# def data_rate(self):
223-
# """The rate at which the sensor takes measurements. Must be a ``Rate``"""
224-
# return self._data_rate
225-
226-
# @data_rate.setter
227-
# def data_rate(self, value):
228-
# check value is valid
229-
# set performance mode
230-
# sleep(0.010)
231-
#
232-
# self._data_rate = value
218+
@property
219+
def data_rate(self):
220+
"""The rate at which the sensor takes measurements. Must be a ``Rate``"""
221+
return self._data_rate
222+
223+
@data_rate.setter
224+
def data_rate(self, value):
225+
# if
226+
# if current_data_rate is Rate.RATE_155_HZ:
227+
# self.performance_mode = Mode.MODE_ULTRA
228+
# if current_data_rate is Rate.RATE_300_H:
229+
# self.performance_mode = Mode.MODE_HIGH
230+
# if current_data_rate is Rate.RATE_560_HZ:
231+
# self.performance_mode = Mode.MODE_MEDIUM
232+
# if current_data_rate is Rate.RATE_1000_HZ:
233+
# self.performance_mode = Mode.MODE_LOW_POWER
234+
# sleep(0.010)
235+
if not Rate.is_valid(value):
236+
raise AttributeError("`data_rate` must be a `Rate`")
237+
self._data_rate = value
238+
239+
@property
240+
def performance_mode(self):
241+
"""Sets the 'performance mode' of the sensor. Must be a `PerformanceMode`.
242+
Note that `performance_mode` affects the available data rate and will be
243+
automatically changed by setting ``data_rate`` to certain values."""
244+
245+
return self._data_rate
246+
247+
@performance_mode.setter
248+
def performance_mode(self, value):
249+
#TODO: check value
250+
self._perf_mode = value
251+
self._z_perf_mode = value

examples/lis3mdl_data_rate_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
""" Test Each Data Rate """
2+
import time
3+
import board
4+
import busio
5+
from adafruit_lis3mdl import LIS3MDL, Rate
6+
7+
i2c = busio.I2C(board.SCL, board.SDA)
8+
sensor = LIS3MDL(i2c)
9+
10+
current_rate = Rate.RATE_2_5_HZ #pylint: disable=no-member
11+
12+
sensor.data_rate = current_rate
13+
14+
while True:
15+
mag_x, mag_y, mag_z = sensor.magnetic
16+
17+
print('X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT'.format(mag_x, mag_y, mag_z))
18+
19+
# sleep for enough time so that we'll read the value twice per measurement
20+
sleep_time = (1/(Rate.string[current_rate]*2))
21+
time.sleep(sleep_time)

0 commit comments

Comments
 (0)