GY-63_MS5611/libraries/ADS1x15/ADS1X15.h

241 lines
6.6 KiB
C
Raw Normal View History

2021-01-29 06:31:58 -05:00
#pragma once
//
// FILE: ADS1X15.H
// AUTHOR: Rob Tillaart
2023-01-21 05:46:58 -05:00
// VERSION: 0.3.9
2021-01-29 06:31:58 -05:00
// DATE: 2013-03-24
// PUPROSE: Arduino library for ADS1015 and ADS1115
// URL: https://github.com/RobTillaart/ADS1X15
//
2021-10-08 03:28:06 -04:00
2021-01-29 06:31:58 -05:00
#include "Arduino.h"
#include "Wire.h"
2023-01-21 05:46:58 -05:00
#define ADS1X15_LIB_VERSION (F("0.3.9"))
2021-01-29 06:31:58 -05:00
2023-01-21 05:46:58 -05:00
// allow compile time default address
// address in { 0x48, 0x49, 0x4A, 0x4B }, no test...
2021-01-29 06:31:58 -05:00
#ifndef ADS1015_ADDRESS
2022-01-21 13:23:09 -05:00
#define ADS1015_ADDRESS 0x48
2021-01-29 06:31:58 -05:00
#endif
#ifndef ADS1115_ADDRESS
2022-01-21 13:23:09 -05:00
#define ADS1115_ADDRESS 0x48
2021-01-29 06:31:58 -05:00
#endif
2022-01-21 13:23:09 -05:00
#define ADS1X15_OK 0
#define ADS1X15_INVALID_VOLTAGE -100
#define ADS1X15_INVALID_GAIN 0xFF
#define ADS1X15_INVALID_MODE 0xFE
2021-01-29 06:31:58 -05:00
class ADS1X15
{
public:
2021-04-25 13:56:44 -04:00
void reset();
2021-01-29 06:31:58 -05:00
#if defined (ESP8266) || defined(ESP32)
2022-06-21 04:53:30 -04:00
bool begin(int sda, int scl);
2021-01-29 06:31:58 -05:00
#endif
2022-10-17 05:10:46 -04:00
#if defined (ARDUINO_ARCH_RP2040)
bool begin(int sda, int scl);
#endif
2021-01-29 06:31:58 -05:00
bool begin();
bool isConnected();
2023-01-21 05:46:58 -05:00
// GAIN
// 0 = +- 6.144V default
// 1 = +- 4.096V
// 2 = +- 2.048V
// 4 = +- 1.024V
// 8 = +- 0.512V
// 16 = +- 0.256V
void setGain(uint8_t gain = 0); // invalid values are mapped to 0 (default).
uint8_t getGain(); // 0xFF == invalid gain error.
// both may return ADS1X15_INVALID_VOLTAGE if the gain is invalid.
float toVoltage(int16_t value = 1); // converts raw to voltage
float getMaxVoltage(); // -100 == invalid voltage error
// 0 = CONTINUOUS
// 1 = SINGLE default
2021-01-29 06:31:58 -05:00
void setMode(uint8_t mode = 1); // invalid values are mapped to 1 (default)
uint8_t getMode(); // 0xFE == invalid mode error.
2023-01-21 05:46:58 -05:00
// 0 = slowest
// 7 = fastest
// 4 = default
2021-04-25 13:56:44 -04:00
void setDataRate(uint8_t dataRate = 4); // invalid values are mapped on 4 (default)
uint8_t getDataRate(); // actual speed depends on device
2021-01-29 06:31:58 -05:00
2023-01-21 05:46:58 -05:00
2022-03-15 10:40:30 -04:00
int16_t readADC(uint8_t pin = 0);
2021-01-29 06:31:58 -05:00
int16_t readADC_Differential_0_1();
2023-01-21 05:46:58 -05:00
// used by continuous mode and async mode.
2021-04-25 13:56:44 -04:00
int16_t getLastValue() { return getValue(); }; // will be obsolete in the future 0.4.0
2021-01-29 06:31:58 -05:00
int16_t getValue();
2021-04-25 13:56:44 -04:00
2023-01-21 05:46:58 -05:00
// ASYNC INTERFACE
// requestADC(pin) -> isBusy() or isReady() -> getValue();
// see examples
2022-03-15 10:40:30 -04:00
void requestADC(uint8_t pin = 0);
2021-01-29 06:31:58 -05:00
void requestADC_Differential_0_1();
bool isBusy();
2021-10-08 03:28:06 -04:00
bool isReady();
2021-01-29 06:31:58 -05:00
2023-01-21 05:46:58 -05:00
// COMPARATOR
// 0 = TRADITIONAL > high => on < low => off
// else = WINDOW > high or < low => on between => off
2021-01-29 06:31:58 -05:00
void setComparatorMode(uint8_t mode) { _compMode = mode == 0 ? 0 : 1; };
uint8_t getComparatorMode() { return _compMode; };
2023-01-21 05:46:58 -05:00
// 0 = LOW (default)
// else = HIGH
2021-01-29 06:31:58 -05:00
void setComparatorPolarity(uint8_t pol) { _compPol = pol ? 0 : 1; };
uint8_t getComparatorPolarity() { return _compPol; };
2023-01-21 05:46:58 -05:00
// 0 = NON LATCH
// else = LATCH
2021-01-29 06:31:58 -05:00
void setComparatorLatch(uint8_t latch) { _compLatch = latch ? 0 : 1; };
uint8_t getComparatorLatch() { return _compLatch; };
2023-01-21 05:46:58 -05:00
// 0 = trigger alert after 1 conversion
// 1 = trigger alert after 2 conversions
// 2 = trigger alert after 4 conversions
// 3 = Disable comparator = default, also for all other values.
2021-01-29 06:31:58 -05:00
void setComparatorQueConvert(uint8_t mode) { _compQueConvert = (mode < 3) ? mode : 3; };
uint8_t getComparatorQueConvert() { return _compQueConvert; };
void setComparatorThresholdLow(int16_t lo);
int16_t getComparatorThresholdLow();
void setComparatorThresholdHigh(int16_t hi);
int16_t getComparatorThresholdHigh();
2021-04-25 13:56:44 -04:00
2021-01-29 06:31:58 -05:00
int8_t getError();
2023-01-21 05:46:58 -05:00
// EXPERIMENTAL
// see https://github.com/RobTillaart/ADS1X15/issues/22
2022-01-21 13:23:09 -05:00
void setWireClock(uint32_t clockSpeed = 100000);
2023-01-21 05:46:58 -05:00
// proto - getWireClock returns the value set by setWireClock
// not necessary the actual value
2021-04-25 13:56:44 -04:00
uint32_t getWireClock();
2021-01-29 06:31:58 -05:00
protected:
ADS1X15();
2023-01-21 05:46:58 -05:00
// CONFIGURATION
// BIT DESCRIPTION
// 0 # channels 0 == 1 1 == 4;
// 1 0
// 2 # resolution 0 == 12 1 == 16
// 3 0
// 4 has gain 0 = NO 1 = YES
// 5 has comparator 0 = NO 1 = YES
// 6 0
// 7 0
2021-01-29 06:31:58 -05:00
uint8_t _config;
uint8_t _maxPorts;
uint8_t _address;
uint8_t _conversionDelay;
uint8_t _bitShift;
uint16_t _gain;
uint16_t _mode;
uint16_t _datarate;
2023-01-21 05:46:58 -05:00
// COMPARATOR variables
// TODO merge these into one COMPARATOR MASK? (low priority)
// would speed up code in _requestADC() and save 3 bytes RAM.
// TODO boolean flags for first three, or make it mask value that
// can be or-ed. (low priority)
2021-04-25 13:56:44 -04:00
uint8_t _compMode;
uint8_t _compPol;
uint8_t _compLatch;
uint8_t _compQueConvert;
2021-01-29 06:31:58 -05:00
2021-04-07 07:31:22 -04:00
int16_t _readADC(uint16_t readmode);
void _requestADC(uint16_t readmode);
bool _writeRegister(uint8_t address, uint8_t reg, uint16_t value);
uint16_t _readRegister(uint8_t address, uint8_t reg);
int8_t _err = ADS1X15_OK;
2021-01-29 06:31:58 -05:00
2021-04-07 07:31:22 -04:00
TwoWire* _wire;
2021-04-25 13:56:44 -04:00
uint32_t _clockSpeed = 0;
2021-01-29 06:31:58 -05:00
};
2022-03-15 10:40:30 -04:00
2021-01-29 06:31:58 -05:00
///////////////////////////////////////////////////////////////////////////
//
2023-01-21 05:46:58 -05:00
// DERIVED CLASSES from ADS1X15
2021-01-29 06:31:58 -05:00
//
class ADS1013 : public ADS1X15
{
public:
2021-04-07 07:31:22 -04:00
ADS1013(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire);
2021-01-29 06:31:58 -05:00
};
2022-03-15 10:40:30 -04:00
2021-01-29 06:31:58 -05:00
class ADS1014 : public ADS1X15
{
public:
2021-04-07 07:31:22 -04:00
ADS1014(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire);
2021-01-29 06:31:58 -05:00
};
2022-03-15 10:40:30 -04:00
2021-01-29 06:31:58 -05:00
class ADS1015 : public ADS1X15
{
public:
2021-04-07 07:31:22 -04:00
ADS1015(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire);
2021-01-29 06:31:58 -05:00
int16_t readADC_Differential_0_3();
int16_t readADC_Differential_1_3();
int16_t readADC_Differential_2_3();
2023-01-21 05:46:58 -05:00
int16_t readADC_Differential_0_2(); // not possible in async
int16_t readADC_Differential_1_2(); // not possible in async
2021-01-29 06:31:58 -05:00
void requestADC_Differential_0_3();
void requestADC_Differential_1_3();
void requestADC_Differential_2_3();
};
2022-03-15 10:40:30 -04:00
2021-01-29 06:31:58 -05:00
class ADS1113 : public ADS1X15
{
public:
2021-04-07 07:31:22 -04:00
ADS1113(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire);
2021-01-29 06:31:58 -05:00
};
2022-03-15 10:40:30 -04:00
2021-01-29 06:31:58 -05:00
class ADS1114 : public ADS1X15
{
public:
2021-04-07 07:31:22 -04:00
ADS1114(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire);
2021-01-29 06:31:58 -05:00
};
2022-03-15 10:40:30 -04:00
2021-01-29 06:31:58 -05:00
class ADS1115 : public ADS1X15
{
public:
2021-04-07 07:31:22 -04:00
ADS1115(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire);
2021-01-29 06:31:58 -05:00
int16_t readADC_Differential_0_3();
int16_t readADC_Differential_1_3();
int16_t readADC_Differential_2_3();
2023-01-21 05:46:58 -05:00
int16_t readADC_Differential_0_2(); // not possible in async
int16_t readADC_Differential_1_2(); // not possible in async
2021-01-29 06:31:58 -05:00
void requestADC_Differential_0_3();
void requestADC_Differential_1_3();
void requestADC_Differential_2_3();
};
2022-03-15 10:40:30 -04:00
2023-01-21 05:46:58 -05:00
// -- END OF FILE --
2022-03-15 10:40:30 -04:00