Skip to content

Commit 5dc86c6

Browse files
committedOct 11, 2020
doxklang
1 parent 949424f commit 5dc86c6

File tree

2 files changed

+60
-49
lines changed

2 files changed

+60
-49
lines changed
 

‎Adafruit_MLX90395.cpp

+49-34
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919
#include "Adafruit_MLX90395.h"
2020

21-
Adafruit_MLX90395::Adafruit_MLX90395(void) {
22-
}
21+
Adafruit_MLX90395::Adafruit_MLX90395(void) {}
2322

2423
/*!
2524
* @brief Sets up the hardware and initializes I2C
@@ -63,16 +62,14 @@ bool Adafruit_MLX90395::_init(void) {
6362

6463
_resolution = getResolution();
6564

66-
if (! readRegister(0x26, &uniqueID[0]) ||
67-
! readRegister(0x27, &uniqueID[1]) ||
68-
! readRegister(0x28, &uniqueID[2])) {
65+
if (!readRegister(0x26, &uniqueID[0]) || !readRegister(0x27, &uniqueID[1]) ||
66+
!readRegister(0x28, &uniqueID[2])) {
6967
return false;
7068
}
7169

7270
return true;
7371
}
7472

75-
7673
/**
7774
* Performs a single X/Y/Z conversion and returns the results.
7875
*
@@ -85,13 +82,12 @@ bool Adafruit_MLX90395::_init(void) {
8582
bool Adafruit_MLX90395::readData(float *x, float *y, float *z) {
8683
if (!startSingleMeasurement())
8784
return false;
88-
while (! readMeasurement(x, y, z))
85+
while (!readMeasurement(x, y, z))
8986
delay(1);
9087

9188
return true;
9289
}
9390

94-
9591
/**
9692
* Reads data from data register & returns the results.
9793
*
@@ -103,7 +99,7 @@ bool Adafruit_MLX90395::readData(float *x, float *y, float *z) {
10399
*/
104100
bool Adafruit_MLX90395::readMeasurement(float *x, float *y, float *z) {
105101
uint8_t tx[1] = {0x80}; // Read memory command
106-
uint8_t rx[12] = {0}; // status, crc, X16, Y16, Z16, T16, V16
102+
uint8_t rx[12] = {0}; // status, crc, X16, Y16, Z16, T16, V16
107103

108104
/* Perform the transaction. */
109105
if (i2c_dev) {
@@ -113,7 +109,7 @@ bool Adafruit_MLX90395::readMeasurement(float *x, float *y, float *z) {
113109
}
114110

115111
// check status
116-
Serial.print("Status: "); Serial.println(rx[0], HEX);
112+
// Serial.print("Status: "); Serial.println(rx[0], HEX);
117113
if (rx[0] != MLX90395_STATUS_DRDY) {
118114
return false;
119115
}
@@ -137,8 +133,6 @@ bool Adafruit_MLX90395::readMeasurement(float *x, float *y, float *z) {
137133
return true;
138134
}
139135

140-
141-
142136
/**
143137
* Perform a soft reset
144138
* @return True if the operation succeeded, otherwise false.
@@ -158,7 +152,6 @@ bool Adafruit_MLX90395::exitMode(void) {
158152
return command(MLX90395_REG_EX) == 0;
159153
}
160154

161-
162155
/**
163156
* Begin a single measurement on all axes
164157
*
@@ -168,59 +161,86 @@ bool Adafruit_MLX90395::startSingleMeasurement(void) {
168161
return (command(MLX90395_REG_SM | 0x0F) == MLX90395_STATUS_SMMODE);
169162
}
170163

171-
164+
/**
165+
* Get the current oversampling setting
166+
* @return MLX90395_OSR_1, MLX90395_OSR_2, MLX90395_OSR_4, or MLX90395_OSR_8
167+
*/
172168
mlx90393_osr_t Adafruit_MLX90395::getOSR(void) {
173169
Adafruit_BusIO_Register reg2 =
174-
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
170+
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
175171
Adafruit_BusIO_RegisterBits osr_bits =
176172
Adafruit_BusIO_RegisterBits(&reg2, 2, 0);
177173
return (mlx90393_osr_t)osr_bits.read();
178174
}
179175

176+
/**
177+
* Set the current oversampling setting
178+
* @param osrval MLX90395_OSR_1, MLX90395_OSR_2, MLX90395_OSR_4,
179+
* or MLX90395_OSR_8
180+
* @return True on command success
181+
*/
180182
bool Adafruit_MLX90395::setOSR(mlx90393_osr_t osrval) {
181183
Adafruit_BusIO_Register reg2 =
182-
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
184+
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
183185
Adafruit_BusIO_RegisterBits osr_bits =
184186
Adafruit_BusIO_RegisterBits(&reg2, 2, 0);
185187
return osr_bits.write(osrval);
186188
}
187189

188-
189-
190+
/**
191+
* Get the current gainsel value, see DS 18.6. Sensitivity for values
192+
* @return 0-15 gain selection offset
193+
*/
190194
uint8_t Adafruit_MLX90395::getGain(void) {
191195
Adafruit_BusIO_Register reg0 =
192-
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_0, 2, MSBFIRST);
196+
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_0, 2, MSBFIRST);
193197
Adafruit_BusIO_RegisterBits gain_bits =
194-
Adafruit_BusIO_RegisterBits(&reg0, 4, 4);
198+
Adafruit_BusIO_RegisterBits(&reg0, 4, 4);
195199
return gain_bits.read();
196200
}
197201

202+
/**
203+
* Get the current gainsel value, see DS 18.6. Sensitivity for values
204+
* @param gainval 0-15 gain selection offset
205+
* @return True on command success
206+
*/
198207
bool Adafruit_MLX90395::setGain(uint8_t gainval) {
199208
gainval &= 0xF;
200209
_gain = gainval; // cache it
201210

202211
Adafruit_BusIO_Register reg0 =
203-
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_0, 2, MSBFIRST);
212+
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_0, 2, MSBFIRST);
204213
Adafruit_BusIO_RegisterBits gain_bits =
205-
Adafruit_BusIO_RegisterBits(&reg0, 4, 4);
214+
Adafruit_BusIO_RegisterBits(&reg0, 4, 4);
206215
return gain_bits.write(gainval);
207216
}
208217

209-
210-
218+
/**
219+
* Get the resolution, note that its till 16 bits just offset within the 19
220+
* bit ADC output.
221+
* @return MLX90395_RES_16, MLX90395_RES_17, MLX90395_RES_18 or
222+
* MLX90395_RES_19
223+
*/
211224
mlx90393_res_t Adafruit_MLX90395::getResolution(void) {
212225
Adafruit_BusIO_Register reg2 =
213-
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
226+
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
214227
Adafruit_BusIO_RegisterBits resX_bits =
215228
Adafruit_BusIO_RegisterBits(&reg2, 2, 5);
216229
return (mlx90393_res_t)resX_bits.read();
217230
}
218231

232+
/**
233+
* Set the resolution, note that its till 16 bits just offset within the 19
234+
* bit ADC output.
235+
* @param resval MLX90395_RES_16, MLX90395_RES_17, MLX90395_RES_18 or
236+
* MLX90395_RES_19
237+
* @return True on command success
238+
*/
219239
bool Adafruit_MLX90395::setResolution(mlx90393_res_t resval) {
220240
_resolution = resval; // cache it
221241

222242
Adafruit_BusIO_Register reg2 =
223-
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
243+
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
224244
Adafruit_BusIO_RegisterBits resX_bits =
225245
Adafruit_BusIO_RegisterBits(&reg2, 2, 5);
226246
resX_bits.write(resval);
@@ -234,15 +254,14 @@ bool Adafruit_MLX90395::setResolution(mlx90393_res_t resval) {
234254

235255
/****************************************************************/
236256

237-
238257
uint8_t Adafruit_MLX90395::command(uint8_t cmd) {
239258
uint8_t tx[2] = {0x80, cmd};
240259
uint8_t status = 0xFF;
241260

242261
/* Perform the transaction. */
243262
if (i2c_dev) {
244263
if (!i2c_dev->write_then_read(tx, 2, &status, 1)) {
245-
Serial.println("Failed command");
264+
// Serial.println("Failed command");
246265
return 0xFF;
247266
}
248267
}
@@ -253,15 +272,13 @@ bool Adafruit_MLX90395::readRegister(uint8_t reg, uint16_t *data) {
253272
/* Perform the transaction. */
254273
if (i2c_dev) {
255274
Adafruit_BusIO_Register regis =
256-
Adafruit_BusIO_Register(i2c_dev, reg << 1, 2);
275+
Adafruit_BusIO_Register(i2c_dev, reg << 1, 2);
257276
*data = regis.read();
258277
}
259278

260279
return true;
261280
}
262281

263-
264-
265282
/**************************************************************************/
266283
/*!
267284
@brief Gets the sensor_t device data, Adafruit Unified Sensor format
@@ -282,11 +299,9 @@ void Adafruit_MLX90395::getSensor(sensor_t *sensor) {
282299
sensor->min_delay = 0;
283300
sensor->min_value = -120000; // -120 gauss in uTesla
284301
sensor->max_value = 120000; // +120 gauss in uTesla
285-
sensor->resolution = _uTLSB; // 240/16-bit uTesla per LSB
302+
sensor->resolution = _uTLSB; // 240/16-bit uTesla per LSB
286303
}
287304

288-
289-
290305
/**************************************************************************/
291306
/*!
292307
@brief Gets the most recent sensor event, Adafruit Unified Sensor format

‎Adafruit_MLX90395.h

+11-15
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#define ADAFRUIT_MLX90395_H
33

44
#include "Arduino.h"
5+
#include <Adafruit_BusIO_Register.h>
56
#include <Adafruit_I2CDevice.h>
67
#include <Adafruit_SPIDevice.h>
78
#include <Adafruit_Sensor.h>
8-
#include <Adafruit_BusIO_Register.h>
99

1010
#define MLX90395_STATUS_RESET 0x02
1111
#define MLX90395_STATUS_SMMODE 0x20
@@ -16,9 +16,9 @@
1616

1717
/** Register map. */
1818
enum {
19-
MLX90395_REG_SM = (0x30), /**> Start single-meas mode. */
20-
MLX90395_REG_EX = (0x80), /**> Exit mode. */
21-
MLX90395_REG_RT = (0xF0), /**< Reset. */
19+
MLX90395_REG_SM = (0x30), /**> Start single-meas mode. */
20+
MLX90395_REG_EX = (0x80), /**> Exit mode. */
21+
MLX90395_REG_RT = (0xF0), /**< Reset. */
2222
};
2323

2424
typedef enum mlx90393_osr {
@@ -35,15 +35,15 @@ typedef enum mlx90393_res {
3535
MLX90395_RES_19,
3636
} mlx90393_res_t;
3737

38-
39-
40-
static const float gainMultipliers[16] = {0.2, 0.25, 0.3333, 0.4, 0.5, 0.6, 0.75, 1, 0.1, 0.125, 0.1667, 0.2, 0.25, 0.3, 0.375, 0.5};
41-
38+
static const float gainMultipliers[16] = {
39+
0.2, 0.25, 0.3333, 0.4, 0.5, 0.6, 0.75, 1,
40+
0.1, 0.125, 0.1667, 0.2, 0.25, 0.3, 0.375, 0.5};
4241

4342
#define MLX90395_DEFAULT_ADDR (0x0C) /* Can also be 0x18, depending on IC */
4443

44+
/** Class for interfacing with MLX90395 magnetometer */
4545
class Adafruit_MLX90395 : public Adafruit_Sensor {
46-
public:
46+
public:
4747
Adafruit_MLX90395();
4848
bool begin_I2C(uint8_t i2c_addr = MLX90395_DEFAULT_ADDR,
4949
TwoWire *wire = &Wire);
@@ -60,13 +60,12 @@ class Adafruit_MLX90395 : public Adafruit_Sensor {
6060
uint8_t getGain(void);
6161
bool setGain(uint8_t gainval);
6262

63-
6463
void getSensor(sensor_t *sensor);
6564
bool getEvent(sensors_event_t *event);
6665

67-
uint16_t uniqueID[3];
66+
uint16_t uniqueID[3]; ///< 48 bits of unique identifier, read during init
6867

69-
private:
68+
private:
7069
Adafruit_I2CDevice *i2c_dev = NULL;
7170
Adafruit_SPIDevice *spi_dev = NULL;
7271

@@ -81,6 +80,3 @@ class Adafruit_MLX90395 : public Adafruit_Sensor {
8180
};
8281

8382
#endif
84-
85-
86-

0 commit comments

Comments
 (0)
Please sign in to comment.