Skip to content

Commit

Permalink
doxklang
Browse files Browse the repository at this point in the history
  • Loading branch information
ladyada committed Oct 11, 2020
1 parent 949424f commit 5dc86c6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 49 deletions.
83 changes: 49 additions & 34 deletions Adafruit_MLX90395.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

#include "Adafruit_MLX90395.h"

Adafruit_MLX90395::Adafruit_MLX90395(void) {
}
Adafruit_MLX90395::Adafruit_MLX90395(void) {}

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

_resolution = getResolution();

if (! readRegister(0x26, &uniqueID[0]) ||
! readRegister(0x27, &uniqueID[1]) ||
! readRegister(0x28, &uniqueID[2])) {
if (!readRegister(0x26, &uniqueID[0]) || !readRegister(0x27, &uniqueID[1]) ||
!readRegister(0x28, &uniqueID[2])) {
return false;
}

return true;
}


/**
* Performs a single X/Y/Z conversion and returns the results.
*
Expand All @@ -85,13 +82,12 @@ bool Adafruit_MLX90395::_init(void) {
bool Adafruit_MLX90395::readData(float *x, float *y, float *z) {
if (!startSingleMeasurement())
return false;
while (! readMeasurement(x, y, z))
while (!readMeasurement(x, y, z))
delay(1);

return true;
}


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

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

// check status
Serial.print("Status: "); Serial.println(rx[0], HEX);
// Serial.print("Status: "); Serial.println(rx[0], HEX);
if (rx[0] != MLX90395_STATUS_DRDY) {
return false;
}
Expand All @@ -137,8 +133,6 @@ bool Adafruit_MLX90395::readMeasurement(float *x, float *y, float *z) {
return true;
}



/**
* Perform a soft reset
* @return True if the operation succeeded, otherwise false.
Expand All @@ -158,7 +152,6 @@ bool Adafruit_MLX90395::exitMode(void) {
return command(MLX90395_REG_EX) == 0;
}


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


/**
* Get the current oversampling setting
* @return MLX90395_OSR_1, MLX90395_OSR_2, MLX90395_OSR_4, or MLX90395_OSR_8
*/
mlx90393_osr_t Adafruit_MLX90395::getOSR(void) {
Adafruit_BusIO_Register reg2 =
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
Adafruit_BusIO_RegisterBits osr_bits =
Adafruit_BusIO_RegisterBits(&reg2, 2, 0);
return (mlx90393_osr_t)osr_bits.read();
}

/**
* Set the current oversampling setting
* @param osrval MLX90395_OSR_1, MLX90395_OSR_2, MLX90395_OSR_4,
* or MLX90395_OSR_8
* @return True on command success
*/
bool Adafruit_MLX90395::setOSR(mlx90393_osr_t osrval) {
Adafruit_BusIO_Register reg2 =
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
Adafruit_BusIO_RegisterBits osr_bits =
Adafruit_BusIO_RegisterBits(&reg2, 2, 0);
return osr_bits.write(osrval);
}



/**
* Get the current gainsel value, see DS 18.6. Sensitivity for values
* @return 0-15 gain selection offset
*/
uint8_t Adafruit_MLX90395::getGain(void) {
Adafruit_BusIO_Register reg0 =
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_0, 2, MSBFIRST);
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_0, 2, MSBFIRST);
Adafruit_BusIO_RegisterBits gain_bits =
Adafruit_BusIO_RegisterBits(&reg0, 4, 4);
Adafruit_BusIO_RegisterBits(&reg0, 4, 4);
return gain_bits.read();
}

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

Adafruit_BusIO_Register reg0 =
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_0, 2, MSBFIRST);
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_0, 2, MSBFIRST);
Adafruit_BusIO_RegisterBits gain_bits =
Adafruit_BusIO_RegisterBits(&reg0, 4, 4);
Adafruit_BusIO_RegisterBits(&reg0, 4, 4);
return gain_bits.write(gainval);
}



/**
* Get the resolution, note that its till 16 bits just offset within the 19
* bit ADC output.
* @return MLX90395_RES_16, MLX90395_RES_17, MLX90395_RES_18 or
* MLX90395_RES_19
*/
mlx90393_res_t Adafruit_MLX90395::getResolution(void) {
Adafruit_BusIO_Register reg2 =
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
Adafruit_BusIO_RegisterBits resX_bits =
Adafruit_BusIO_RegisterBits(&reg2, 2, 5);
return (mlx90393_res_t)resX_bits.read();
}

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

Adafruit_BusIO_Register reg2 =
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
Adafruit_BusIO_Register(i2c_dev, MLX90395_REG_2, 2, MSBFIRST);
Adafruit_BusIO_RegisterBits resX_bits =
Adafruit_BusIO_RegisterBits(&reg2, 2, 5);
resX_bits.write(resval);
Expand All @@ -234,15 +254,14 @@ bool Adafruit_MLX90395::setResolution(mlx90393_res_t resval) {

/****************************************************************/


uint8_t Adafruit_MLX90395::command(uint8_t cmd) {
uint8_t tx[2] = {0x80, cmd};
uint8_t status = 0xFF;

/* Perform the transaction. */
if (i2c_dev) {
if (!i2c_dev->write_then_read(tx, 2, &status, 1)) {
Serial.println("Failed command");
// Serial.println("Failed command");
return 0xFF;
}
}
Expand All @@ -253,15 +272,13 @@ bool Adafruit_MLX90395::readRegister(uint8_t reg, uint16_t *data) {
/* Perform the transaction. */
if (i2c_dev) {
Adafruit_BusIO_Register regis =
Adafruit_BusIO_Register(i2c_dev, reg << 1, 2);
Adafruit_BusIO_Register(i2c_dev, reg << 1, 2);
*data = regis.read();
}

return true;
}



/**************************************************************************/
/*!
@brief Gets the sensor_t device data, Adafruit Unified Sensor format
Expand All @@ -282,11 +299,9 @@ void Adafruit_MLX90395::getSensor(sensor_t *sensor) {
sensor->min_delay = 0;
sensor->min_value = -120000; // -120 gauss in uTesla
sensor->max_value = 120000; // +120 gauss in uTesla
sensor->resolution = _uTLSB; // 240/16-bit uTesla per LSB
sensor->resolution = _uTLSB; // 240/16-bit uTesla per LSB
}



/**************************************************************************/
/*!
@brief Gets the most recent sensor event, Adafruit Unified Sensor format
Expand Down
26 changes: 11 additions & 15 deletions Adafruit_MLX90395.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#define ADAFRUIT_MLX90395_H

#include "Arduino.h"
#include <Adafruit_BusIO_Register.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_SPIDevice.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BusIO_Register.h>

#define MLX90395_STATUS_RESET 0x02
#define MLX90395_STATUS_SMMODE 0x20
Expand All @@ -16,9 +16,9 @@

/** Register map. */
enum {
MLX90395_REG_SM = (0x30), /**> Start single-meas mode. */
MLX90395_REG_EX = (0x80), /**> Exit mode. */
MLX90395_REG_RT = (0xF0), /**< Reset. */
MLX90395_REG_SM = (0x30), /**> Start single-meas mode. */
MLX90395_REG_EX = (0x80), /**> Exit mode. */
MLX90395_REG_RT = (0xF0), /**< Reset. */
};

typedef enum mlx90393_osr {
Expand All @@ -35,15 +35,15 @@ typedef enum mlx90393_res {
MLX90395_RES_19,
} mlx90393_res_t;



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};

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};

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

/** Class for interfacing with MLX90395 magnetometer */
class Adafruit_MLX90395 : public Adafruit_Sensor {
public:
public:
Adafruit_MLX90395();
bool begin_I2C(uint8_t i2c_addr = MLX90395_DEFAULT_ADDR,
TwoWire *wire = &Wire);
Expand All @@ -60,13 +60,12 @@ class Adafruit_MLX90395 : public Adafruit_Sensor {
uint8_t getGain(void);
bool setGain(uint8_t gainval);


void getSensor(sensor_t *sensor);
bool getEvent(sensors_event_t *event);

uint16_t uniqueID[3];
uint16_t uniqueID[3]; ///< 48 bits of unique identifier, read during init

private:
private:
Adafruit_I2CDevice *i2c_dev = NULL;
Adafruit_SPIDevice *spi_dev = NULL;

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

#endif



0 comments on commit 5dc86c6

Please sign in to comment.