Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrouch committed Dec 12, 2024
1 parent 5997fbc commit aaa343c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 69 deletions.
8 changes: 4 additions & 4 deletions i2c_ads1115.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define M5_UNIT_VMETER_EEPROM_I2C_ADDR 0x53
#define M5_UNIT_VMETER_PRESSURE_COEFFICIENT 0.015918958F

ADS1115 i2c_ads1115_sensor;
ADS_1115 i2c_ads1115_sensor;
bool i2c_ads1115_found = false;

float resolution = 0.0;
Expand All @@ -41,9 +41,9 @@ void i2c_ads1115_try_init() {
if (i2c_ads1115_found) {
gen_nmea0183_msg("$BBTXT,01,01,01,VOLTAGE found ads1115 sensor at address=0x%s", String(M5_UNIT_VMETER_I2C_ADDR, HEX).c_str());
i2c_ads1115_sensor.setEEPROMAddr(M5_UNIT_VMETER_EEPROM_I2C_ADDR);
i2c_ads1115_sensor.setMode(ADS1115_MODE_SINGLESHOT);
i2c_ads1115_sensor.setRate(ADS1115_RATE_8);
i2c_ads1115_sensor.setGain(ADS1115_PGA_512);
i2c_ads1115_sensor.setMode(ADS_1115_MODE_SINGLESHOT);
i2c_ads1115_sensor.setRate(ADS_1115_RATE_8);
i2c_ads1115_sensor.setGain(ADS_1115_PGA_512);
// | PGA | Max Input Voltage(V) |
// | PGA_6144 | 128 |
// | PGA_4096 | 64 |
Expand Down
130 changes: 65 additions & 65 deletions m5_ads1115.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,46 @@
#include <Wire.h>
#include <I2C_Class.h>

#define ADS1115_REG_CONVERSION 0x00
#define ADS1115_REG_CONFIG 0x01
#define ADS1115_I2C_ADDR_0 0x48
#define ADS1115_I2C_ADDR_1 0x49

#define ADS1115_MV_6144 0.187500F
#define ADS1115_MV_4096 0.125000F
#define ADS1115_MV_2048 0.062500F // default
#define ADS1115_MV_1024 0.031250F
#define ADS1115_MV_512 0.015625F
#define ADS1115_MV_256 0.007813F
#define ADS_1115_REG_CONVERSION 0x00
#define ADS_1115_REG_CONFIG 0x01
#define ADS_1115_I2C_ADDR_0 0x48
#define ADS_1115_I2C_ADDR_1 0x49

#define ADS_1115_MV_6144 0.187500F
#define ADS_1115_MV_4096 0.125000F
#define ADS_1115_MV_2048 0.062500F // default
#define ADS_1115_MV_1024 0.031250F
#define ADS_1115_MV_512 0.015625F
#define ADS_1115_MV_256 0.007813F

#define MEASURING_DIRECTION -1

typedef enum {
ADS1115_PGA_6144 = 0,
ADS1115_PGA_4096,
ADS1115_PGA_2048, // default
ADS1115_PGA_1024,
ADS1115_PGA_512,
ADS1115_PGA_256,
ADS_1115_PGA_6144 = 0,
ADS_1115_PGA_4096,
ADS_1115_PGA_2048, // default
ADS_1115_PGA_1024,
ADS_1115_PGA_512,
ADS_1115_PGA_256,
} ads1115_gain_t;

typedef enum {
ADS1115_RATE_8 = 0,
ADS1115_RATE_16,
ADS1115_RATE_32,
ADS1115_RATE_64,
ADS1115_RATE_128, // default
ADS1115_RATE_250,
ADS1115_RATE_475,
ADS1115_RATE_860,
ADS_1115_RATE_8 = 0,
ADS_1115_RATE_16,
ADS_1115_RATE_32,
ADS_1115_RATE_64,
ADS_1115_RATE_128, // default
ADS_1115_RATE_250,
ADS_1115_RATE_475,
ADS_1115_RATE_860,
} ads1115_rate_t;

typedef enum {
ADS1115_MODE_CONTINUOUS = 0,
ADS1115_MODE_SINGLESHOT, // default
ADS_1115_MODE_CONTINUOUS = 0,
ADS_1115_MODE_SINGLESHOT, // default
} ads1115_mode_t;

class ADS1115 {
class ADS_1115 {
private:
I2C_Class _i2c;
uint8_t _addr;
Expand All @@ -61,7 +61,7 @@ class ADS1115 {
float _calibration_factor;

public:
bool begin(TwoWire* wire = &Wire, uint8_t addr = ADS1115_I2C_ADDR_0,
bool begin(TwoWire* wire = &Wire, uint8_t addr = ADS_1115_I2C_ADDR_0,
uint8_t sda = 21, uint8_t scl = 22, long freq = 400000U);

void setGain(ads1115_gain_t gain);
Expand All @@ -85,7 +85,7 @@ class ADS1115 {
bool readCalibration(ads1115_gain_t gain, int16_t* hope, int16_t* actual);
};

bool ADS1115::begin(TwoWire* wire, uint8_t addr, uint8_t sda, uint8_t scl,
bool ADS_1115::begin(TwoWire* wire, uint8_t addr, uint8_t sda, uint8_t scl,
long freq) {
_i2c.begin(wire, sda, scl, freq);
_addr = addr;
Expand All @@ -94,21 +94,21 @@ bool ADS1115::begin(TwoWire* wire, uint8_t addr, uint8_t sda, uint8_t scl,
return _i2c.exist(_addr);
}

void ADS1115::setEEPROMAddr(uint8_t addr) {
void ADS_1115::setEEPROMAddr(uint8_t addr) {
_epprom_addr = addr;
}

float ADS1115::getCoefficient() {
float ADS_1115::getCoefficient() {
return _coefficient;
}

float ADS1115::getFactoryCalibration() {
float ADS_1115::getFactoryCalibration() {
return _calibration_factor;
}

void ADS1115::setGain(ads1115_gain_t gain) {
void ADS_1115::setGain(ads1115_gain_t gain) {
uint16_t reg_value = 0;
bool result = _i2c.readU16(_addr, ADS1115_REG_CONFIG, &reg_value);
bool result = _i2c.readU16(_addr, ADS_1115_REG_CONFIG, &reg_value);

if (result == false) {
return;
Expand All @@ -117,7 +117,7 @@ void ADS1115::setGain(ads1115_gain_t gain) {
reg_value &= ~(0b0111 << 9);
reg_value |= gain << 9;

result = _i2c.writeU16(_addr, ADS1115_REG_CONFIG, reg_value);
result = _i2c.writeU16(_addr, ADS_1115_REG_CONFIG, reg_value);

if (result) {
_gain = gain;
Expand All @@ -128,41 +128,41 @@ void ADS1115::setGain(ads1115_gain_t gain) {
}

switch (_gain) {
case ADS1115_PGA_6144:
_coefficient = ADS1115_MV_6144;
case ADS_1115_PGA_6144:
_coefficient = ADS_1115_MV_6144;
break;
case ADS1115_PGA_4096:
_coefficient = ADS1115_MV_4096;
case ADS_1115_PGA_4096:
_coefficient = ADS_1115_MV_4096;
break;
case ADS1115_PGA_2048:
_coefficient = ADS1115_MV_2048;
case ADS_1115_PGA_2048:
_coefficient = ADS_1115_MV_2048;
break;
case ADS1115_PGA_1024:
_coefficient = ADS1115_MV_1024;
case ADS_1115_PGA_1024:
_coefficient = ADS_1115_MV_1024;
break;
case ADS1115_PGA_512:
_coefficient = ADS1115_MV_512;
case ADS_1115_PGA_512:
_coefficient = ADS_1115_MV_512;
break;
case ADS1115_PGA_256:
_coefficient = ADS1115_MV_256;
case ADS_1115_PGA_256:
_coefficient = ADS_1115_MV_256;
break;
default:
break;
}
}
}

void ADS1115::setRate(ads1115_rate_t rate) {
void ADS_1115::setRate(ads1115_rate_t rate) {
uint16_t reg_value = 0;
bool result = _i2c.readU16(_addr, ADS1115_REG_CONFIG, &reg_value);
bool result = _i2c.readU16(_addr, ADS_1115_REG_CONFIG, &reg_value);
if (result == false) {
return;
}

reg_value &= ~(0b0111 << 5);
reg_value |= rate << 5;

result = _i2c.writeU16(_addr, ADS1115_REG_CONFIG, reg_value);
result = _i2c.writeU16(_addr, ADS_1115_REG_CONFIG, reg_value);

if (result) {
_rate = rate;
Expand All @@ -172,17 +172,17 @@ void ADS1115::setRate(ads1115_rate_t rate) {
}

/*! @brief Set read continuous read/single read data */
void ADS1115::setMode(ads1115_mode_t mode) {
void ADS_1115::setMode(ads1115_mode_t mode) {
uint16_t reg_value = 0;
bool result = _i2c.readU16(_addr, ADS1115_REG_CONFIG, &reg_value);
bool result = _i2c.readU16(_addr, ADS_1115_REG_CONFIG, &reg_value);
if (result == false) {
return;
}

reg_value &= ~(0b0001 << 8);
reg_value |= mode << 8;

result = _i2c.writeU16(_addr, ADS1115_REG_CONFIG, reg_value);
result = _i2c.writeU16(_addr, ADS_1115_REG_CONFIG, reg_value);
if (result) {
_mode = mode;
}
Expand All @@ -192,17 +192,17 @@ void ADS1115::setMode(ads1115_mode_t mode) {

/*! @brief Determine if data is being converted
@return Data being converted returns 1, otherwise 0.. */
bool ADS1115::isInConversion() {
bool ADS_1115::isInConversion() {
uint16_t value = 0x00;
_i2c.readU16(_addr, ADS1115_REG_CONFIG, &value);
_i2c.readU16(_addr, ADS_1115_REG_CONFIG, &value);

return (value & (1 << 15)) ? false : true;
}

/*! @brief Turn on single data conversion */
void ADS1115::startSingleConversion() {
void ADS_1115::startSingleConversion() {
uint16_t reg_value = 0;
bool result = _i2c.readU16(_addr, ADS1115_REG_CONFIG, &reg_value);
bool result = _i2c.readU16(_addr, ADS_1115_REG_CONFIG, &reg_value);

if (result == false) {
return;
Expand All @@ -211,18 +211,18 @@ void ADS1115::startSingleConversion() {
reg_value &= ~(0b0001 << 15);
reg_value |= 0x01 << 15;

_i2c.writeU16(_addr, ADS1115_REG_CONFIG, reg_value);
_i2c.writeU16(_addr, ADS_1115_REG_CONFIG, reg_value);
}

int16_t ADS1115::getAdcRaw() {
int16_t ADS_1115::getAdcRaw() {
uint16_t value = 0x00;
_i2c.readU16(_addr, ADS1115_REG_CONVERSION, &value);
_i2c.readU16(_addr, ADS_1115_REG_CONVERSION, &value);
adc_raw = value;
return value;
}

int16_t ADS1115::getSingleConversion(uint16_t timeout) {
if (_mode == ADS1115_MODE_SINGLESHOT) {
int16_t ADS_1115::getSingleConversion(uint16_t timeout) {
if (_mode == ADS_1115_MODE_SINGLESHOT) {
startSingleConversion();
delay(cover_time);
uint64_t time = millis() + timeout;
Expand All @@ -233,7 +233,7 @@ int16_t ADS1115::getSingleConversion(uint16_t timeout) {
return getAdcRaw() * MEASURING_DIRECTION;
}

bool ADS1115::saveCalibration(ads1115_gain_t gain, int16_t hope,
bool ADS_1115::saveCalibration(ads1115_gain_t gain, int16_t hope,
int16_t actual) {
if (hope == 0 || actual == 0) {
return false;
Expand All @@ -256,7 +256,7 @@ bool ADS1115::saveCalibration(ads1115_gain_t gain, int16_t hope,
return _i2c.writeBytes(_epprom_addr, reg, buff, 8);
}

bool ADS1115::readCalibration(ads1115_gain_t gain, int16_t* hope,
bool ADS_1115::readCalibration(ads1115_gain_t gain, int16_t* hope,
int16_t* actual) {
uint8_t reg = 0xd0 + gain * 8;
uint8_t buff[8];
Expand Down

0 comments on commit aaa343c

Please sign in to comment.