|
43 | 43 | * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
|
44 | 44 | * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
|
45 | 45 | """
|
46 |
| - |
| 46 | +from time import sleep |
47 | 47 | from micropython import const
|
48 | 48 | import adafruit_bus_device.i2c_device as i2c_device
|
49 | 49 | from adafruit_register.i2c_struct import ROUnaryStruct, Struct
|
|
73 | 73 |
|
74 | 74 | _GAUSS_TO_MT = 0.1 #1 Gauss [G] = 0.1 Millitesla [mT]
|
75 | 75 |
|
76 |
| -# /** The magnetometer ranges */ |
77 |
| -# typedef enum { |
78 |
| -# LIS3MDL_RANGE_4_GAUSS = 0b00, ///< +/- 4g (default value) |
79 |
| -# LIS3MDL_RANGE_8_GAUSS = 0b01, ///< +/- 8g |
80 |
| -# LIS3MDL_RANGE_12_GAUSS = 0b10, ///< +/- 12g |
81 |
| -# LIS3MDL_RANGE_16_GAUSS = 0b11, ///< +/- 16g |
82 |
| -# } lis3mdl_range_t; |
| 76 | +class CV: |
| 77 | + """struct helper""" |
| 78 | + |
| 79 | + @classmethod |
| 80 | + def add_values(cls, value_tuples): |
| 81 | + "creates CV entires" |
| 82 | + cls.string = {} |
| 83 | + cls.lsb = {} |
| 84 | + |
| 85 | + for value_tuple in value_tuples: |
| 86 | + name, value, string, lsb = value_tuple |
| 87 | + setattr(cls, name, value) |
| 88 | + cls.string[value] = string |
| 89 | + cls.lsb[value] = lsb |
| 90 | + |
| 91 | + @classmethod |
| 92 | + def is_valid(cls, value): |
| 93 | + "Returns true if the given value is a member of the CV" |
| 94 | + return value in cls.string |
| 95 | + |
| 96 | +class Range(CV): |
| 97 | + """Options for ``accelerometer_range``""" |
| 98 | + pass #pylint: disable=unnecessary-pass |
| 99 | + |
| 100 | +Range.add_values(( |
| 101 | + ('RANGE_4_GAUSS', 0, 4, 6842), |
| 102 | + ('RANGE_8_GAUSS', 1, 8, 3421), |
| 103 | + ('RANGE_12_GAUSS', 2, 12, 2281), |
| 104 | + ('RANGE_16_GAUSS', 3, 16, 1711) |
| 105 | +)) |
83 | 106 |
|
84 | 107 |
|
85 | 108 | # lis3mdl_range_t range = getRange();
|
|
93 | 116 | # if (range == LIS3MDL_RANGE_4_GAUSS)
|
94 | 117 | # scale = 6842;
|
95 | 118 |
|
96 |
| - |
97 | 119 | # /** The magnetometer data rate, includes FAST_ODR bit */
|
98 | 120 | # typedef enum {
|
99 | 121 | # LIS3MDL_DATARATE_0_625_HZ = 0b0000, ///< 0.625 Hz
|
@@ -155,27 +177,56 @@ def __init__(self, i2c_bus, address=_LIS3MDL_DEFAULT_ADDRESS):
|
155 | 177 | # // 155Hz default rate
|
156 | 178 | # setDataRate(LIS3MDL_DATARATE_155_HZ);
|
157 | 179 |
|
158 |
| - # // lowest range |
159 |
| - # setRange(LIS3MDL_RANGE_4_GAUSS); |
160 |
| - self._range = 0 |
| 180 | + |
| 181 | + self.range = Range.RANGE_4_GAUSS #pylint: disable=no-member |
161 | 182 |
|
162 | 183 | # setOperationMode(LIS3MDL_CONTINUOUSMODE);
|
163 |
| - self._operation_mode = 0 |
| 184 | + self._operation_mode = 0 # enable, take out of shutdown |
| 185 | + sleep(0.010) |
164 | 186 | def reset(self): #pylint: disable=no-self-use
|
165 | 187 | """Reset the sensor to the default state set by the library"""
|
166 | 188 |
|
167 |
| - print("reeeset") |
| 189 | + print("called reset") |
| 190 | + # reset |
| 191 | + # sleep(0.010) |
168 | 192 |
|
169 | 193 | @property
|
170 | 194 | def magnetic(self):
|
171 | 195 | """How do they even work?!"""
|
172 | 196 |
|
173 | 197 | raw_mag_data = self._raw_mag_data
|
174 |
| - x = self._scale_mag_data(raw_mag_data[0]) * _GAUSS_TO_MT |
175 |
| - y = self._scale_mag_data(raw_mag_data[1]) * _GAUSS_TO_MT |
176 |
| - z = self._scale_mag_data(raw_mag_data[2]) * _GAUSS_TO_MT |
| 198 | + x = self._scale_mag_data(raw_mag_data[0]) |
| 199 | + y = self._scale_mag_data(raw_mag_data[1]) |
| 200 | + z = self._scale_mag_data(raw_mag_data[2]) |
| 201 | + |
177 | 202 | return(x, y, z)
|
| 203 | + |
178 | 204 | def _scale_mag_data(self, raw_measurement): #pylint: disable=no-self-use
|
| 205 | + return (raw_measurement / Range.lsb[self.range]) * _GAUSS_TO_MT |
179 | 206 |
|
180 |
| - scale = 6842 |
181 |
| - return raw_measurement/scale |
| 207 | + @property |
| 208 | + def range(self): |
| 209 | + """The measurement range for the magnetic sensor. Must be a ``Range``""" |
| 210 | + return self._range |
| 211 | + |
| 212 | + @range.setter |
| 213 | + def range(self, value): |
| 214 | + if not Range.is_valid(value): |
| 215 | + raise AttributeError("``range`` must be a ``Range``") |
| 216 | + |
| 217 | + self._range = value |
| 218 | + |
| 219 | + sleep(0.010) |
| 220 | + |
| 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 |
0 commit comments