forked from adafruit/Adafruit_MLX90393_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_MLX90393.cpp
489 lines (411 loc) · 13.1 KB
/
Adafruit_MLX90393.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/******************************************************************************
This is a library for the MLX90393 magnetometer.
Designed specifically to work with the MLX90393 breakout from Adafruit:
----> https://www.adafruit.com/products/4022
These sensors use I2C to communicate, 2 pins are required to interface.
Adafruit invests time and resources providing this open source code, please
support Adafruit and open-source hardware by purchasing products from
Adafruit!
Written by Kevin Townsend/ktown for Adafruit Industries.
MIT license, all text above must be included in any redistribution
*****************************************************************************/
#include "Adafruit_MLX90393.h"
/**
* Instantiates a new Adafruit_MLX90393 class instance
*/
Adafruit_MLX90393::Adafruit_MLX90393(void) {}
/*!
* @brief Sets up the hardware and initializes I2C
* @param i2c_addr
* The I2C address to be used.
* @param wire
* The Wire object to be used for I2C connections.
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_MLX90393::begin_I2C(uint8_t i2c_addr, TwoWire *wire) {
if (i2c_dev) {
delete i2c_dev;
}
if (!i2c_dev) {
i2c_dev = new Adafruit_I2CDevice(i2c_addr, wire);
}
spi_dev = NULL;
if (!i2c_dev->begin()) {
return false;
}
return _init();
}
/*!
* @brief Sets up the hardware and initializes hardware SPI
* @param cs_pin The arduino pin # connected to chip select
* @param theSPI The SPI object to be used for SPI connections.
* @return True if initialization was successful, otherwise false.
*/
boolean Adafruit_MLX90393::begin_SPI(uint8_t cs_pin, SPIClass *theSPI) {
i2c_dev = NULL;
if (!spi_dev) {
_cspin = cs_pin;
spi_dev = new Adafruit_SPIDevice(cs_pin,
1000000, // frequency
SPI_BITORDER_MSBFIRST, // bit order
SPI_MODE3, // data mode
theSPI);
}
if (!spi_dev->begin()) {
return false;
}
return _init();
}
bool Adafruit_MLX90393::_init(void) {
if (!exitMode())
return false;
if (!reset())
return false;
/* Set gain and sensor config. */
if (!setGain(MLX90393_GAIN_1X)) {
return false;
}
/* Set resolution. */
if (!setResolution(MLX90393_X, MLX90393_RES_16))
return false;
if (!setResolution(MLX90393_Y, MLX90393_RES_16))
return false;
if (!setResolution(MLX90393_Z, MLX90393_RES_16))
return false;
/* Set oversampling. */
if (!setOversampling(MLX90393_OSR_3))
return false;
/* Set digital filtering. */
if (!setFilter(MLX90393_FILTER_7))
return false;
/* set INT pin to output interrupt */
if (!setTrigInt(false)) {
return false;
}
return true;
}
/**
* Perform a mode exit
* @return True if the operation succeeded, otherwise false.
*/
bool Adafruit_MLX90393::exitMode(void) {
uint8_t tx[1] = {MLX90393_REG_EX};
/* Perform the transaction. */
return (transceive(tx, sizeof(tx), NULL, 0, 0) == MLX90393_STATUS_OK);
}
/**
* Perform a soft reset
* @return True if the operation succeeded, otherwise false.
*/
bool Adafruit_MLX90393::reset(void) {
uint8_t tx[1] = {MLX90393_REG_RT};
/* Perform the transaction. */
if (transceive(tx, sizeof(tx), NULL, 0, 5) != MLX90393_STATUS_RESET) {
return false;
}
return true;
}
/**
* Sets the sensor gain to the specified level.
* @param gain The gain level to set.
* @return True if the operation succeeded, otherwise false.
*/
bool Adafruit_MLX90393::setGain(mlx90393_gain_t gain) {
_gain = gain;
uint16_t data;
readRegister(MLX90393_CONF1, &data);
// mask off gain bits
data &= ~0x0070;
// set gain bits
data |= gain << MLX90393_GAIN_SHIFT;
return writeRegister(MLX90393_CONF1, data);
}
/**
* Gets the current sensor gain.
*
* @return An enum containing the current gain level.
*/
mlx90393_gain_t Adafruit_MLX90393::getGain(void) {
uint16_t data;
readRegister(MLX90393_CONF1, &data);
// mask off gain bits
data &= 0x0070;
return (mlx90393_gain_t)(data >> 4);
}
/**
* Sets the sensor resolution to the specified level.
* @param axis The axis to set.
* @param resolution The resolution level to set.
* @return True if the operation succeeded, otherwise false.
*/
bool Adafruit_MLX90393::setResolution(enum mlx90393_axis axis,
enum mlx90393_resolution resolution) {
uint16_t data;
readRegister(MLX90393_CONF3, &data);
switch (axis) {
case MLX90393_X:
_res_x = resolution;
data &= ~0x0060;
data |= resolution << 5;
break;
case MLX90393_Y:
_res_y = resolution;
data &= ~0x0180;
data |= resolution << 7;
break;
case MLX90393_Z:
_res_z = resolution;
data &= ~0x0600;
data |= resolution << 9;
break;
}
return writeRegister(MLX90393_CONF3, data);
}
/**
* Gets the current sensor resolution.
* @param axis The axis to get.
* @return An enum containing the current resolution.
*/
enum mlx90393_resolution
Adafruit_MLX90393::getResolution(enum mlx90393_axis axis) {
switch (axis) {
case MLX90393_X:
return _res_x;
case MLX90393_Y:
return _res_y;
case MLX90393_Z:
return _res_z;
}
// shouldn't get here, but to make compiler happy...
return _res_x;
}
/**
* Sets the digital filter.
* @param filter The digital filter setting.
* @return True if the operation succeeded, otherwise false.
*/
bool Adafruit_MLX90393::setFilter(enum mlx90393_filter filter) {
_dig_filt = filter;
uint16_t data;
readRegister(MLX90393_CONF3, &data);
data &= ~0x1C;
data |= filter << 2;
return writeRegister(MLX90393_CONF3, data);
}
/**
* Gets the current digital filter setting.
* @return An enum containing the current digital filter setting.
*/
enum mlx90393_filter Adafruit_MLX90393::getFilter(void) { return _dig_filt; }
/**
* Sets the oversampling.
* @param oversampling The oversampling value to use.
* @return True if the operation succeeded, otherwise false.
*/
bool Adafruit_MLX90393::setOversampling(
enum mlx90393_oversampling oversampling) {
_osr = oversampling;
uint16_t data;
readRegister(MLX90393_CONF3, &data);
data &= ~0x03;
data |= oversampling;
return writeRegister(MLX90393_CONF3, data);
}
/**
* Gets the current oversampling setting.
* @return An enum containing the current oversampling setting.
*/
enum mlx90393_oversampling Adafruit_MLX90393::getOversampling(void) {
return _osr;
}
/**
* Sets the TRIG_INT pin to the specified function.
*
* @param state 'true/1' sets the pin to INT, 'false/0' to TRIG.
*
* @return True if the operation succeeded, otherwise false.
*/
bool Adafruit_MLX90393::setTrigInt(bool state) {
uint16_t data;
readRegister(MLX90393_CONF2, &data);
// mask off trigint bit
data &= ~0x8000;
// set trigint bit if desired
if (state) {
/* Set the INT, highest bit */
data |= 0x8000;
}
return writeRegister(MLX90393_CONF2, data);
}
/**
* Begin a single measurement on all axes
*
* @return True on command success
*/
bool Adafruit_MLX90393::startSingleMeasurement(void) {
uint8_t tx[1] = {MLX90393_REG_SM | MLX90393_AXIS_ALL};
/* Set the device to single measurement mode */
uint8_t stat = transceive(tx, sizeof(tx), NULL, 0, 0);
if ((stat == MLX90393_STATUS_OK) || (stat == MLX90393_STATUS_SMMODE)) {
return true;
}
return false;
}
/**
* Reads data from data register & returns the results.
*
* @param x Pointer to where the 'x' value should be stored.
* @param y Pointer to where the 'y' value should be stored.
* @param z Pointer to where the 'z' value should be stored.
*
* @return True on command success
*/
bool Adafruit_MLX90393::readMeasurement(float *x, float *y, float *z) {
uint8_t tx[1] = {MLX90393_REG_RM | MLX90393_AXIS_ALL};
uint8_t rx[6] = {0};
/* Read a single data sample. */
if (transceive(tx, sizeof(tx), rx, sizeof(rx), 0) != MLX90393_STATUS_OK) {
return false;
}
int16_t xi, yi, zi;
/* Convert data to uT and float. */
xi = (rx[0] << 8) | rx[1];
yi = (rx[2] << 8) | rx[3];
zi = (rx[4] << 8) | rx[5];
if (_res_x == MLX90393_RES_18)
xi -= 0x8000;
if (_res_x == MLX90393_RES_19)
xi -= 0x4000;
if (_res_y == MLX90393_RES_18)
yi -= 0x8000;
if (_res_y == MLX90393_RES_19)
yi -= 0x4000;
if (_res_z == MLX90393_RES_18)
zi -= 0x8000;
if (_res_z == MLX90393_RES_19)
zi -= 0x4000;
*x = (float)xi * mlx90393_lsb_lookup[0][_gain][_res_x][0];
*y = (float)yi * mlx90393_lsb_lookup[0][_gain][_res_y][0];
*z = (float)zi * mlx90393_lsb_lookup[0][_gain][_res_z][1];
return true;
}
/**
* Performs a single X/Y/Z conversion and returns the results.
*
* @param x Pointer to where the 'x' value should be stored.
* @param y Pointer to where the 'y' value should be stored.
* @param z Pointer to where the 'z' value should be stored.
*
* @return True if the operation succeeded, otherwise false.
*/
bool Adafruit_MLX90393::readData(float *x, float *y, float *z) {
if (!startSingleMeasurement())
return false;
// See MLX90393 Getting Started Guide for fancy formula
// tconv = f(OSR, DIG_FILT, OSR2, ZYXT)
// For now, using Table 18 from datasheet
// Without +10ms delay measurement doesn't always seem to work
delay(mlx90393_tconv[_dig_filt][_osr] + 10);
return readMeasurement(x, y, z);
}
bool Adafruit_MLX90393::writeRegister(uint8_t reg, uint16_t data) {
uint8_t tx[4] = {MLX90393_REG_WR,
data >> 8, // high byte
data & 0xFF, // low byte
reg << 2}; // the register itself, shift up by 2 bits!
/* Perform the transaction. */
return (transceive(tx, sizeof(tx), NULL, 0, 0) == MLX90393_STATUS_OK);
}
bool Adafruit_MLX90393::readRegister(uint8_t reg, uint16_t *data) {
uint8_t tx[2] = {MLX90393_REG_RR,
reg << 2}; // the register itself, shift up by 2 bits!
uint8_t rx[2];
/* Perform the transaction. */
if (transceive(tx, sizeof(tx), rx, sizeof(rx), 0) != MLX90393_STATUS_OK) {
return false;
}
*data = ((uint16_t)rx[0] << 8) | rx[1];
return true;
}
/**************************************************************************/
/*!
@brief Gets the most recent sensor event, Adafruit Unified Sensor format
@param event Pointer to an Adafruit Unified sensor_event_t object that
we'll fill in
@returns True on successful read
*/
/**************************************************************************/
bool Adafruit_MLX90393::getEvent(sensors_event_t *event) {
/* Clear the event */
memset(event, 0, sizeof(sensors_event_t));
event->version = 1;
event->sensor_id = _sensorID;
event->type = SENSOR_TYPE_MAGNETIC_FIELD;
event->timestamp = millis();
return readData(&event->magnetic.x, &event->magnetic.y, &event->magnetic.z);
}
/**
* Performs a full read/write transaction with the sensor.
*
* @param txbuf Pointer the the buffer containing the data to write.
* @param txlen The number of bytes to write.
* @param rxbuf Pointer to an appropriately large buffer where data read
* back will be written.
* @param rxlen The number of bytes to read back (not including the
* mandatory status byte that is always returned).
*
* @return The status byte from the IC.
*/
uint8_t Adafruit_MLX90393::transceive(uint8_t *txbuf, uint8_t txlen,
uint8_t *rxbuf, uint8_t rxlen,
uint8_t interdelay) {
uint8_t status = 0;
uint8_t i;
uint8_t rxbuf2[rxlen + 2];
if (i2c_dev) {
/* Write stage */
if (!i2c_dev->write(txbuf, txlen)) {
return MLX90393_STATUS_ERROR;
}
delay(interdelay);
/* Read status byte plus any others */
if (!i2c_dev->read(rxbuf2, rxlen + 1)) {
return MLX90393_STATUS_ERROR;
}
status = rxbuf2[0];
for (i = 0; i < rxlen; i++) {
rxbuf[i] = rxbuf2[i + 1];
}
}
if (spi_dev) {
spi_dev->write_then_read(txbuf, txlen, rxbuf2, rxlen + 1, 0x00);
status = rxbuf2[0];
for (i = 0; i < rxlen; i++) {
rxbuf[i] = rxbuf2[i + 1];
}
delay(interdelay);
}
/* Mask out bytes available in the status response. */
return (status >> 2);
}
/**************************************************************************/
/*!
@brief Gets the sensor_t device data, Adafruit Unified Sensor format
@param sensor Pointer to an Adafruit Unified sensor_t object that we'll
fill in
*/
/**************************************************************************/
void Adafruit_MLX90393::getSensor(sensor_t *sensor) {
/* Clear the sensor_t object */
memset(sensor, 0, sizeof(sensor_t));
/* Insert the sensor name in the fixed length char array */
strncpy(sensor->name, "MLX90393", sizeof(sensor->name) - 1);
sensor->name[sizeof(sensor->name) - 1] = 0;
sensor->version = 1;
sensor->sensor_id = _sensorID;
sensor->type = SENSOR_TYPE_MAGNETIC_FIELD;
sensor->min_delay = 0;
sensor->min_value = -50000; // -50 gauss in uTesla
sensor->max_value = 50000; // +50 gauss in uTesla
sensor->resolution = 0.15; // 100/16-bit uTesla per LSB
}