Skip to content

Commit

Permalink
Fix #80, setComparatorPolarity() et al.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Aug 20, 2024
1 parent ebf49f3 commit fd3a812
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 11 deletions.
10 changes: 5 additions & 5 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.4.5
// VERSION: 0.5.0
// DATE: 2013-03-24
// PURPOSE: Arduino library for ADS1015 and ADS1115
// URL: https://github.com/RobTillaart/ADS1X15
Expand Down Expand Up @@ -342,7 +342,7 @@ uint8_t ADS1X15::getComparatorMode()

void ADS1X15::setComparatorPolarity(uint8_t pol)
{
_compPol = pol ? 0 : 1;
_compPol = pol == 0 ? 0 : 1;
}


Expand All @@ -354,7 +354,7 @@ uint8_t ADS1X15::getComparatorPolarity()

void ADS1X15::setComparatorLatch(uint8_t latch)
{
_compLatch = latch ? 0 : 1;
_compLatch = latch == 0 ? 0 : 1;
}


Expand Down Expand Up @@ -502,7 +502,7 @@ bool ADS1X15::_writeRegister(uint8_t address, uint8_t reg, uint16_t value)
_wire->write((uint8_t)(value >> 8));
_wire->write((uint8_t)(value & 0xFF));
int rv = _wire->endTransmission();
if (rv != 0)
if (rv != 0)
{
_error = ADS1X15_ERROR_I2C;
return false;
Expand All @@ -516,7 +516,7 @@ uint16_t ADS1X15::_readRegister(uint8_t address, uint8_t reg)
_wire->beginTransmission(address);
_wire->write(reg);
int rv = _wire->endTransmission();
if (rv == 0)
if (rv == 0)
{
rv = _wire->requestFrom((int) address, (int) 2);
if (rv == 2)
Expand Down
4 changes: 2 additions & 2 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.4.5
// VERSION: 0.5.0
// DATE: 2013-03-24
// PURPOSE: 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.4.5"))
#define ADS1X15_LIB_VERSION (F("0.5.0"))

// allow compile time default address
// address in { 0x48, 0x49, 0x4A, 0x4B }, no test...
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.5.0] - 2024-08-20
- Fix #80, setComparatorPolarity() and setComparatorLatch() inverting.
- add test example to test parameters.
- add unit tests to test parameters.

----

## [0.4.5] - 2024-07-03
- Fix #78, prevent infinite loop. (Thanks to devmirek).
- Fix #76 (again), update readme.md Comparator Polarity.
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ This pin can be used both for interrupts or polling, see table of examples below
| ADS_read_RDY.ino | polling |


### 0.5.0 Breaking change

Fixed #80, setComparatorPolarity() and setComparatorLatch() as these inverted
the setting.


### 0.4.0 Breaking change

Version 0.4.0 introduced a breaking change.
Expand Down
95 changes: 95 additions & 0 deletions examples/ADS_test_config/ADS_test_config.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// FILE: ADS_test_config.ino
// AUTHOR: Rob.Tillaart
// PURPOSE: test config flags
// URL: https://github.com/RobTillaart/ADS1X15
// triggered by issue 80


#include "ADS1X15.h"

ADS1115 ADS(0x48);

void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("ADS1X15_LIB_VERSION: ");
Serial.println(ADS1X15_LIB_VERSION);

Wire.begin();

ADS.begin();

Serial.println("\nTEST GAIN");
int gain = 16;
for (int i = 0; i < 6; i++)
{
Serial.print(gain);
ADS.setGain(gain);
if (ADS.getGain() == gain) Serial.println("\tOK");
else Serial.println("\tFAIL");
}

Serial.println("\nTEST DATARATE");
for (int i = 0; i < 7; i++)
{
Serial.print(i);
ADS.setDataRate(i);
if (ADS.getDataRate() == i) Serial.println("\tOK");
else Serial.println("\tFAIL");
}

Serial.println("\nTEST MODE");
for (int i = 0; i < 2; i++)
{
Serial.print(i);
ADS.setMode(i);
if (ADS.getMode() == i) Serial.println("\tOK");
else Serial.println("\tFAIL");
}

Serial.println("\nTEST COMP MODE");
for (int i = 0; i < 2; i++)
{
Serial.print(i);
ADS.setComparatorMode(i);
if (ADS.getComparatorMode() == i) Serial.println("\tOK");
else Serial.println("\tFAIL");
}

Serial.println("\nTEST COMP POLARITY");
for (int i = 0; i < 2; i++)
{
Serial.print(i);
ADS.setComparatorPolarity(i);
if (ADS.getComparatorPolarity() == i) Serial.println("\tOK");
else Serial.println("\tFAIL");
}

Serial.println("\nTEST COMP LATCH");
for (int i = 0; i < 2; i++)
{
Serial.print(i);
ADS.setComparatorLatch(i);
if (ADS.getComparatorLatch() == i) Serial.println("\tOK");
else Serial.println("\tFAIL");
}

Serial.println("\nTEST COMP QUECONVERT");
for (int i = 0; i < 2; i++)
{
Serial.print(i);
ADS.setComparatorQueConvert(i);
if (ADS.getComparatorQueConvert() == i) Serial.println("\tOK");
else Serial.println("\tFAIL");
}

Serial.println("\ndone...");
}

void loop()
{
}

// -- END OF FILE --
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/ADS1X15"
},
"version": "0.4.5",
"version": "0.5.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ADS1X15
version=0.4.5
version=0.5.0
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for ADS1015 - I2C 12 bit ADC and ADS1115 I2C 16 bit ADC
Expand Down
130 changes: 128 additions & 2 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ unittest_teardown()
}


unittest(test_constants)
unittest(test_constants_I)
{
assertEqual(0x48, ADS1015_ADDRESS);
assertEqual(0x48, ADS1115_ADDRESS);

assertEqual( 0, ADS1X15_OK);
assertEqual(-100, ADS1X15_INVALID_VOLTAGE);
assertEqual(-101, ADS1X15_ERROR_TIMEOUT);
assertEqual(-102, ADS1X15_ERROR_I2C);
assertEqual(0xFF, ADS1X15_INVALID_GAIN);
assertEqual(0xFE, ADS1X15_INVALID_MODE);
}
Expand Down Expand Up @@ -103,7 +106,7 @@ unittest(test_gain_ADS1113)
}


unittest(test_Voltage)
unittest(test_voltage)
{
ADS1115 ADS(0x48);

Expand Down Expand Up @@ -144,6 +147,129 @@ unittest(test_Voltage)
}


unittest(test_mode)
{
ADS1115 ADS(0x48);

Wire.begin();
assertTrue(ADS.begin());

// test default
assertEqual(1, ADS.getMode());
// test valid values
ADS.setMode(0);
assertEqual(0, ADS.getMode());
ADS.setMode(1);
assertEqual(1, ADS.getMode());
// test out of range
ADS.setMode(2);
assertEqual(1, ADS.getMode());
}


unittest(test_datarate)
{
ADS1115 ADS(0x48);

Wire.begin();
assertTrue(ADS.begin());

// test default
assertEqual(4, ADS.getDataRate());
// test valid values
for (int i = 0; i < 8; i++)
{
ADS.setDataRate(i);
assertEqual(1, ADS.getDataRate());
}
// test out of range
ADS.setDataRate(8);
assertEqual(4, ADS.getDataRate());
}


unittest(test_comparator_mode)
{
ADS1115 ADS(0x48);

Wire.begin();
assertTrue(ADS.begin());

// test default
assertEqual(0, ADS.getComparatorMode());
// test valid values
ADS.setComparatorMode(0);
assertEqual(0, ADS.getComparatorMode());
ADS.setComparatorMode(1);
assertEqual(1, ADS.getComparatorMode());
// test out of range
ADS.setComparatorMode(2);
assertEqual(1, ADS.getComparatorMode());
}


unittest(test_comparator_polarity)
{
ADS1115 ADS(0x48);

Wire.begin();
assertTrue(ADS.begin());

// test default
assertEqual(1, ADS.getComparatorPolarity());
// test valid values
ADS.setComparatorPolarity(0);
assertEqual(0, ADS.getComparatorPolarity());
ADS.setComparatorPolarity(1);
assertEqual(1, ADS.getComparatorPolarity());
// test out of range
ADS.setComparatorPolarity(2);
assertEqual(1, ADS.getComparatorPolarity());
}


unittest(test_comparator_latch)
{
ADS1115 ADS(0x48);

Wire.begin();
assertTrue(ADS.begin());

// test default
assertEqual(0, ADS.getComparatorLatch());
// test valid values
ADS.setComparatorLatch(0);
assertEqual(0, ADS.getComparatorLatch());
ADS.setComparatorLatch(1);
assertEqual(1, ADS.getComparatorLatch());
// test out of range
ADS.setComparatorLatch(2);
assertEqual(1, ADS.getComparatorLatch());
}


unittest(test_comparator_que_convert)
{
ADS1115 ADS(0x48);

Wire.begin();
assertTrue(ADS.begin());

// test default
assertEqual(3, ADS.getComparatorQueConvert());
// test valid values
for (int i = 0; i < 4; i++)
{
ADS.setComparatorQueConvert(i);
assertEqual(1, ADS.getComparatorQueConvert());
}
// test out of range
ADS.setComparatorQueConvert(4);
assertEqual(3, ADS.getComparatorQueConvert());
}



unittest_main()


Expand Down

0 comments on commit fd3a812

Please sign in to comment.