Skip to content

Commit

Permalink
refactor API, begin() (#66)
Browse files Browse the repository at this point in the history
- refactor API, begin()
- update readme.md
- update examples
  • Loading branch information
RobTillaart authored Dec 6, 2023
1 parent 801032e commit 1fd2fd3
Show file tree
Hide file tree
Showing 29 changed files with 272 additions and 249 deletions.
27 changes: 1 addition & 26 deletions ADS1X15.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: ADS1X15.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.13
// VERSION: 0.4.0
// DATE: 2013-03-24
// PUPROSE: Arduino library for ADS1015 and ADS1115
// URL: https://github.com/RobTillaart/ADS1X15
Expand Down Expand Up @@ -140,33 +140,8 @@ void ADS1X15::reset()
}


#if defined (ESP8266) || defined(ESP32)

bool ADS1X15::begin(int sda, int scl)
{
_wire->begin(sda, scl);
if ((_address < 0x48) || (_address > 0x4B)) return false;
if (! isConnected()) return false;
return true;
}

#elif defined (ARDUINO_ARCH_RP2040) && !defined(__MBED__)

bool ADS1X15::begin(int sda, int scl)
{
_wire->setSDA(sda);
_wire->setSCL(scl);
_wire->begin();
if ((_address < 0x48) || (_address > 0x4B)) return false;
if (! isConnected()) return false;
return true;
}

#endif

bool ADS1X15::begin()
{
_wire->begin();
if ((_address < 0x48) || (_address > 0x4B)) return false;
if (! isConnected()) return false;
return true;
Expand Down
10 changes: 2 additions & 8 deletions ADS1X15.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: ADS1X15.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.13
// VERSION: 0.4.0
// DATE: 2013-03-24
// PUPROSE: Arduino library for ADS1015 and ADS1115
// URL: https://github.com/RobTillaart/ADS1X15
Expand All @@ -12,7 +12,7 @@
#include "Arduino.h"
#include "Wire.h"

#define ADS1X15_LIB_VERSION (F("0.3.13"))
#define ADS1X15_LIB_VERSION (F("0.4.0"))

// allow compile time default address
// address in { 0x48, 0x49, 0x4A, 0x4B }, no test...
Expand All @@ -36,12 +36,6 @@ class ADS1X15
public:
void reset();

#if defined (ESP8266) || defined(ESP32)
bool begin(int sda, int scl);
#elif defined (ARDUINO_ARCH_RP2040) && !defined(__MBED__)
bool begin(int sda, int scl);
#endif

bool begin();
bool isConnected();

Expand Down
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.4.0] - 2023-12-06
- refactor API, begin()
- update readme.md
- update examples

----

## [0.3.13] - 2023-09-20
- fix #61 ESP32 begin()


## [0.3.12] - 2023-09-11
- update and add examples
- add **getLastRequest()** to track last type of measurement.
- update readme.md
- minor edits.


## [0.3.11] - 2023-08-31
- update readme.md
- move code from .h to .cpp
Expand Down
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ interesting from functionality point of view as these can also do
differential measurements.


#### 0.4.0 Breaking change

Version 0.4.0 introduced a breaking change.
You cannot set the pins in **begin()** any more.
This reduces the dependency of processor dependent Wire implementations.
The user has to call **Wire.begin()** and can optionally set the Wire pins
before calling **begin()**.


#### Related

- https://github.com/RobTillaart/MCP_ADC (10 & 12 bit ADC, SPI, fast)
Expand Down Expand Up @@ -445,27 +454,14 @@ mean something different see - Comparator Mode above or datasheet.
- **int16_t getComparatorThresholdHigh()** reads value from device.


## RP2040 specific

- **bool begin(int sda, int scl)** begin communication with the ADC.
It has the parameter for selecting on which pins the communication should happen.
Check RP2040 Pinout for compatible pins.
If, "Wire1" is used, you need to add "&Wire1" in the constructor.


## Future ideas & improvements

#### Must

- Improve documentation (always)


#### Should

- investigate of remove the begin(sda, scl) versions
as the responsibility for the Wire configuration
should not be in this library.


#### Could

Expand Down
15 changes: 9 additions & 6 deletions examples/ADS_1114_four/ADS_1114_four.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ void setup()
Serial.print("ADS1X15_LIB_VERSION: ");
Serial.println(ADS1X15_LIB_VERSION);

Wire.begin();

for (uint8_t i = 0; i < 4; i++)
{
uint8_t address = 0x48 + i;
Expand All @@ -36,22 +38,23 @@ void setup()
Serial.print(" ");
Serial.println(ADS[i].begin() ? "connected" : "not connected");

ADS[i].setDataRate(4); // 0 = slow 4 = medium 7 = fast, but more noise
// 0 = slow 4 = medium 7 = fast, but more noise
ADS[i].setDataRate(4);
}
ADS_request_all();
}


void loop()
{
// Serial.println(__FUNCTION__);
// wait until all is read...
// Serial.println(__FUNCTION__);
// wait until all is read...
while(ADS_read_all());

// we have all values, so process (print) them
// we have all values, so process (print) them
ADS_print_all();

delay(1000); // wait a second, comment this line for more samples.
delay(1000); // wait a second, comment this line for more samples.
ADS_request_all();
}

Expand Down Expand Up @@ -107,4 +110,4 @@ void ADS_print_all()
}


// -- END OF FILE --
// -- END OF FILE --
25 changes: 13 additions & 12 deletions examples/ADS_1114_two_continuous/ADS_1114_two_continuous.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,37 @@ void setup()
Serial.print("ADS1X15_LIB_VERSION: ");
Serial.println(ADS1X15_LIB_VERSION);

Wire.begin();

// SETUP FIRST ADS1114
// SETUP FIRST ADS1114
ADS_1.begin();
ADS_1.setGain(0); // 0 == 6.144 volt, default
ADS_1.setDataRate(7); // 0 = slow 4 = medium 7 = fast

// SET ALERT RDY PIN
// SET ALERT RDY PIN
ADS_1.setComparatorThresholdHigh(0x8000);
ADS_1.setComparatorThresholdLow(0x0000);
ADS_1.setComparatorQueConvert(0);

// SET INTERRUPT HANDLER TO CATCH CONVERSION READY
// SET INTERRUPT HANDLER TO CATCH CONVERSION READY
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), adsReady_1, RISING);

ADS_1.setMode(0); // 0 == continuous mode
ADS_1.readADC(); // 0 == default channel, trigger first read


// SETUP SECOND ADS1114
// SETUP SECOND ADS1114
ADS_2.begin();
ADS_2.setGain(0); // 0 == 6.144 volt, default
ADS_2.setDataRate(7); // 7 == highest

// SET ALERT RDY PIN
// SET ALERT RDY PIN
ADS_2.setComparatorThresholdHigh(0x8000);
ADS_2.setComparatorThresholdLow(0x0000);
ADS_2.setComparatorQueConvert(0);

// SET INTERRUPT HANDLER TO CATCH CONVERSION READY
// SET INTERRUPT HANDLER TO CATCH CONVERSION READY
pinMode(3, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(3), adsReady_2, RISING);

Expand All @@ -78,34 +79,34 @@ void loop()
}


// catch interrupt and set flag device 1
// catch interrupt and set flag device 1
void adsReady_1()
{
RDY_1 = true;
}

// catch interrupt and set flag device 1
// catch interrupt and set flag device 1
void adsReady_2()
{
RDY_2 = true;
}


// handle conversions that are ready
// handle conversions that are ready
bool handleConversion()
{
bool rv = false;
if (RDY_1)
{
// save the last value
// save the last value
val_1 = ADS_1.getValue();
ADS_1.readADC(0);
RDY_1 = false;
rv = true;
}
if (RDY_2)
{
// save the last value
// save the last value
val_2 = ADS_2.getValue();
ADS_2.readADC(0);
RDY_2 = false;
Expand All @@ -115,5 +116,5 @@ bool handleConversion()
}


// -- END OF FILE --
// -- END OF FILE --

29 changes: 17 additions & 12 deletions examples/ADS_RP2040_WIRE1/ADS_RP2040_WIRE1.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
// PURPOSE: read analog input
// URL: https://github.com/RobTillaart/ADS1X15

// test
// connect 1 potmeter
// test
// connect 1 potentiometer
//
// GND ---[ x ]------ 5V
// |
// GND ---[ x ]------ 5V
// |
//
// measure at x (connect to AIN0).
// measure at x (connect to AIN0).


#include "ADS1X15.h"
Expand All @@ -32,19 +32,24 @@ void setup()
Serial.print("ADS1X15_LIB_VERSION: ");
Serial.println(ADS1X15_LIB_VERSION);

ADS.begin(26, 27); // SDA (Pin 26), SCL(Pin 27)
ADS.setGain(0); // 6.144 volt
ADS.setDataRate(7); // 0 = slow 4 = medium 7 = fast
ADS.setMode(0); // continuous mode
ADS.readADC(0); // first read to trigger
// SDA (Pin 26), SCL(Pin 27)
Wire1.begin();
Wire1.setSDA(26);
Wire1.setSCL(27);

ADS.begin();
ADS.setGain(0); // 6.144 volt
ADS.setDataRate(7); // 0 = slow 4 = medium 7 = fast
ADS.setMode(0); // continuous mode
ADS.readADC(0); // first read to trigger
}


void loop()
{
Serial.println(ADS.getValue());
// optional other code here
// optional other code here
}


// -- END OF FILE --
// -- END OF FILE --
Loading

0 comments on commit 1fd2fd3

Please sign in to comment.