Skip to content

Commit 760f89f

Browse files
authored
Fix #80, comparator polarity and latch (#81)
- Fix #80, setComparatorPolarity() and setComparatorLatch() inverting. - add test example to test parameters. - add unit tests to test parameters.
1 parent ebf49f3 commit 760f89f

File tree

8 files changed

+245
-11
lines changed

8 files changed

+245
-11
lines changed

ADS1X15.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: ADS1X15.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.4.5
4+
// VERSION: 0.5.0
55
// DATE: 2013-03-24
66
// PURPOSE: Arduino library for ADS1015 and ADS1115
77
// URL: https://github.com/RobTillaart/ADS1X15
@@ -342,7 +342,7 @@ uint8_t ADS1X15::getComparatorMode()
342342

343343
void ADS1X15::setComparatorPolarity(uint8_t pol)
344344
{
345-
_compPol = pol ? 0 : 1;
345+
_compPol = pol == 0 ? 0 : 1;
346346
}
347347

348348

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

355355
void ADS1X15::setComparatorLatch(uint8_t latch)
356356
{
357-
_compLatch = latch ? 0 : 1;
357+
_compLatch = latch == 0 ? 0 : 1;
358358
}
359359

360360

@@ -502,7 +502,7 @@ bool ADS1X15::_writeRegister(uint8_t address, uint8_t reg, uint16_t value)
502502
_wire->write((uint8_t)(value >> 8));
503503
_wire->write((uint8_t)(value & 0xFF));
504504
int rv = _wire->endTransmission();
505-
if (rv != 0)
505+
if (rv != 0)
506506
{
507507
_error = ADS1X15_ERROR_I2C;
508508
return false;
@@ -516,7 +516,7 @@ uint16_t ADS1X15::_readRegister(uint8_t address, uint8_t reg)
516516
_wire->beginTransmission(address);
517517
_wire->write(reg);
518518
int rv = _wire->endTransmission();
519-
if (rv == 0)
519+
if (rv == 0)
520520
{
521521
rv = _wire->requestFrom((int) address, (int) 2);
522522
if (rv == 2)

ADS1X15.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: ADS1X15.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.4.5
5+
// VERSION: 0.5.0
66
// DATE: 2013-03-24
77
// PURPOSE: Arduino library for ADS1015 and ADS1115
88
// URL: https://github.com/RobTillaart/ADS1X15
@@ -12,7 +12,7 @@
1212
#include "Arduino.h"
1313
#include "Wire.h"
1414

15-
#define ADS1X15_LIB_VERSION (F("0.4.5"))
15+
#define ADS1X15_LIB_VERSION (F("0.5.0"))
1616

1717
// allow compile time default address
1818
// address in { 0x48, 0x49, 0x4A, 0x4B }, no test...

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

77

8+
## [0.5.0] - 2024-08-20
9+
- Fix #80, setComparatorPolarity() and setComparatorLatch() inverting.
10+
- add test example to test parameters.
11+
- add unit tests to test parameters.
12+
13+
----
14+
815
## [0.4.5] - 2024-07-03
916
- Fix #78, prevent infinite loop. (Thanks to devmirek).
1017
- Fix #76 (again), update readme.md Comparator Polarity.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ This pin can be used both for interrupts or polling, see table of examples below
5555
| ADS_read_RDY.ino | polling |
5656

5757

58+
### 0.5.0 Breaking change
59+
60+
Fixed #80, setComparatorPolarity() and setComparatorLatch() as these inverted
61+
the setting.
62+
63+
5864
### 0.4.0 Breaking change
5965

6066
Version 0.4.0 introduced a breaking change.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
// FILE: ADS_test_config.ino
3+
// AUTHOR: Rob.Tillaart
4+
// PURPOSE: test config flags
5+
// URL: https://github.com/RobTillaart/ADS1X15
6+
// triggered by issue 80
7+
8+
9+
#include "ADS1X15.h"
10+
11+
ADS1115 ADS(0x48);
12+
13+
void setup()
14+
{
15+
Serial.begin(115200);
16+
Serial.println(__FILE__);
17+
Serial.print("ADS1X15_LIB_VERSION: ");
18+
Serial.println(ADS1X15_LIB_VERSION);
19+
20+
Wire.begin();
21+
22+
ADS.begin();
23+
24+
Serial.println("\nTEST GAIN");
25+
int gain = 16;
26+
for (int i = 0; i < 6; i++)
27+
{
28+
Serial.print(gain);
29+
ADS.setGain(gain);
30+
if (ADS.getGain() == gain) Serial.println("\tOK");
31+
else Serial.println("\tFAIL");
32+
}
33+
34+
Serial.println("\nTEST DATARATE");
35+
for (int i = 0; i < 7; i++)
36+
{
37+
Serial.print(i);
38+
ADS.setDataRate(i);
39+
if (ADS.getDataRate() == i) Serial.println("\tOK");
40+
else Serial.println("\tFAIL");
41+
}
42+
43+
Serial.println("\nTEST MODE");
44+
for (int i = 0; i < 2; i++)
45+
{
46+
Serial.print(i);
47+
ADS.setMode(i);
48+
if (ADS.getMode() == i) Serial.println("\tOK");
49+
else Serial.println("\tFAIL");
50+
}
51+
52+
Serial.println("\nTEST COMP MODE");
53+
for (int i = 0; i < 2; i++)
54+
{
55+
Serial.print(i);
56+
ADS.setComparatorMode(i);
57+
if (ADS.getComparatorMode() == i) Serial.println("\tOK");
58+
else Serial.println("\tFAIL");
59+
}
60+
61+
Serial.println("\nTEST COMP POLARITY");
62+
for (int i = 0; i < 2; i++)
63+
{
64+
Serial.print(i);
65+
ADS.setComparatorPolarity(i);
66+
if (ADS.getComparatorPolarity() == i) Serial.println("\tOK");
67+
else Serial.println("\tFAIL");
68+
}
69+
70+
Serial.println("\nTEST COMP LATCH");
71+
for (int i = 0; i < 2; i++)
72+
{
73+
Serial.print(i);
74+
ADS.setComparatorLatch(i);
75+
if (ADS.getComparatorLatch() == i) Serial.println("\tOK");
76+
else Serial.println("\tFAIL");
77+
}
78+
79+
Serial.println("\nTEST COMP QUECONVERT");
80+
for (int i = 0; i < 2; i++)
81+
{
82+
Serial.print(i);
83+
ADS.setComparatorQueConvert(i);
84+
if (ADS.getComparatorQueConvert() == i) Serial.println("\tOK");
85+
else Serial.println("\tFAIL");
86+
}
87+
88+
Serial.println("\ndone...");
89+
}
90+
91+
void loop()
92+
{
93+
}
94+
95+
// -- END OF FILE --

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/ADS1X15"
1717
},
18-
"version": "0.4.5",
18+
"version": "0.5.0",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ADS1X15
2-
version=0.4.5
2+
version=0.5.0
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for ADS1015 - I2C 12 bit ADC and ADS1115 I2C 16 bit ADC

test/unit_test_001.cpp

Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ unittest_teardown()
3535
}
3636

3737

38-
unittest(test_constants)
38+
unittest(test_constants_I)
3939
{
4040
assertEqual(0x48, ADS1015_ADDRESS);
4141
assertEqual(0x48, ADS1115_ADDRESS);
42+
4243
assertEqual( 0, ADS1X15_OK);
4344
assertEqual(-100, ADS1X15_INVALID_VOLTAGE);
45+
assertEqual(-101, ADS1X15_ERROR_TIMEOUT);
46+
assertEqual(-102, ADS1X15_ERROR_I2C);
4447
assertEqual(0xFF, ADS1X15_INVALID_GAIN);
4548
assertEqual(0xFE, ADS1X15_INVALID_MODE);
4649
}
@@ -103,7 +106,7 @@ unittest(test_gain_ADS1113)
103106
}
104107

105108

106-
unittest(test_Voltage)
109+
unittest(test_voltage)
107110
{
108111
ADS1115 ADS(0x48);
109112

@@ -144,6 +147,129 @@ unittest(test_Voltage)
144147
}
145148

146149

150+
unittest(test_mode)
151+
{
152+
ADS1115 ADS(0x48);
153+
154+
Wire.begin();
155+
assertTrue(ADS.begin());
156+
157+
// test default
158+
assertEqual(1, ADS.getMode());
159+
// test valid values
160+
ADS.setMode(0);
161+
assertEqual(0, ADS.getMode());
162+
ADS.setMode(1);
163+
assertEqual(1, ADS.getMode());
164+
// test out of range
165+
ADS.setMode(2);
166+
assertEqual(1, ADS.getMode());
167+
}
168+
169+
170+
unittest(test_datarate)
171+
{
172+
ADS1115 ADS(0x48);
173+
174+
Wire.begin();
175+
assertTrue(ADS.begin());
176+
177+
// test default
178+
assertEqual(4, ADS.getDataRate());
179+
// test valid values
180+
for (int i = 0; i < 8; i++)
181+
{
182+
ADS.setDataRate(i);
183+
assertEqual(i, ADS.getDataRate());
184+
}
185+
// test out of range
186+
ADS.setDataRate(8);
187+
assertEqual(4, ADS.getDataRate());
188+
}
189+
190+
191+
unittest(test_comparator_mode)
192+
{
193+
ADS1115 ADS(0x48);
194+
195+
Wire.begin();
196+
assertTrue(ADS.begin());
197+
198+
// test default
199+
assertEqual(0, ADS.getComparatorMode());
200+
// test valid values
201+
ADS.setComparatorMode(0);
202+
assertEqual(0, ADS.getComparatorMode());
203+
ADS.setComparatorMode(1);
204+
assertEqual(1, ADS.getComparatorMode());
205+
// test out of range
206+
ADS.setComparatorMode(2);
207+
assertEqual(1, ADS.getComparatorMode());
208+
}
209+
210+
211+
unittest(test_comparator_polarity)
212+
{
213+
ADS1115 ADS(0x48);
214+
215+
Wire.begin();
216+
assertTrue(ADS.begin());
217+
218+
// test default
219+
assertEqual(1, ADS.getComparatorPolarity());
220+
// test valid values
221+
ADS.setComparatorPolarity(0);
222+
assertEqual(0, ADS.getComparatorPolarity());
223+
ADS.setComparatorPolarity(1);
224+
assertEqual(1, ADS.getComparatorPolarity());
225+
// test out of range
226+
ADS.setComparatorPolarity(2);
227+
assertEqual(1, ADS.getComparatorPolarity());
228+
}
229+
230+
231+
unittest(test_comparator_latch)
232+
{
233+
ADS1115 ADS(0x48);
234+
235+
Wire.begin();
236+
assertTrue(ADS.begin());
237+
238+
// test default
239+
assertEqual(0, ADS.getComparatorLatch());
240+
// test valid values
241+
ADS.setComparatorLatch(0);
242+
assertEqual(0, ADS.getComparatorLatch());
243+
ADS.setComparatorLatch(1);
244+
assertEqual(1, ADS.getComparatorLatch());
245+
// test out of range
246+
ADS.setComparatorLatch(2);
247+
assertEqual(1, ADS.getComparatorLatch());
248+
}
249+
250+
251+
unittest(test_comparator_que_convert)
252+
{
253+
ADS1115 ADS(0x48);
254+
255+
Wire.begin();
256+
assertTrue(ADS.begin());
257+
258+
// test default
259+
assertEqual(3, ADS.getComparatorQueConvert());
260+
// test valid values
261+
for (int i = 0; i < 4; i++)
262+
{
263+
ADS.setComparatorQueConvert(i);
264+
assertEqual(i, ADS.getComparatorQueConvert());
265+
}
266+
// test out of range
267+
ADS.setComparatorQueConvert(4);
268+
assertEqual(3, ADS.getComparatorQueConvert());
269+
}
270+
271+
272+
147273
unittest_main()
148274

149275

0 commit comments

Comments
 (0)