Skip to content

Commit 5e83ae1

Browse files
authored
Register dumping feature (#16)
* POC: register dumping * Rename to RegisterData, early return on read/CRC error, return bool * Clean up unused function * Version bump * Document readRegister in readme * Make CRC check optional for readRegister method.
1 parent a7df5ee commit 5e83ae1

File tree

6 files changed

+162
-6
lines changed

6 files changed

+162
-6
lines changed

AGS02MA.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// FILE: AGS02MA.cpp
33
// AUTHOR: Rob Tillaart, Viktor Balint, Beanow
44
// DATE: 2021-08-12
5-
// VERSION: 0.2.0
5+
// VERSION: 0.3.0
66
// PURPOSE: Arduino library for AGS02MA TVOC
77
// URL: https://github.com/RobTillaart/AGS02MA
88

@@ -243,6 +243,22 @@ int AGS02MA::lastError()
243243
return e;
244244
}
245245

246+
bool AGS02MA::readRegister(uint8_t address, AGS02MA::RegisterData &reg) {
247+
if (!_readRegister(address))
248+
{
249+
return false;
250+
}
251+
252+
_error = AGS02MA_OK;
253+
// Don't pollute the struct given to us, until we've handled all error cases.
254+
reg.data[0] = _buffer[0];
255+
reg.data[1] = _buffer[1];
256+
reg.data[2] = _buffer[2];
257+
reg.data[3] = _buffer[3];
258+
reg.crc = _buffer[4];
259+
reg.crcValid = _CRC8(_buffer, 5) == 0;
260+
return true;
261+
}
246262

247263
/////////////////////////////////////////////////////////
248264
//

AGS02MA.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// FILE: AGS02MA.h
44
// AUTHOR: Rob Tillaart, Viktor Balint, Beanow
55
// DATE: 2021-08-12
6-
// VERSION: 0.2.0
6+
// VERSION: 0.3.0
77
// PURPOSE: Arduino library for AGS02MA TVOC
88
// URL: https://github.com/RobTillaart/AGS02MA
99
//
@@ -13,7 +13,7 @@
1313
#include "Wire.h"
1414

1515

16-
#define AGS02MA_LIB_VERSION (F("0.2.0"))
16+
#define AGS02MA_LIB_VERSION (F("0.3.0"))
1717

1818
#define AGS02MA_OK 0
1919
#define AGS02MA_ERROR -10
@@ -28,6 +28,13 @@
2828
class AGS02MA
2929
{
3030
public:
31+
struct RegisterData
32+
{
33+
uint8_t data[4];
34+
uint8_t crc;
35+
bool crcValid;
36+
};
37+
3138
struct ZeroCalibrationData
3239
{
3340
/**
@@ -104,6 +111,9 @@ class AGS02MA
104111
uint8_t lastStatus() { return _status; };
105112
uint8_t dataReady() { return _status & 0x01; };
106113

114+
// Reading registers
115+
bool readRegister(uint8_t address, RegisterData &reg);
116+
107117

108118
private:
109119
uint32_t _readSensor();

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,12 @@ See example sketch.
174174
To be called after at least 5 minutes in fresh air.
175175
For v117: 0-65535 = automatic calibration.
176176
For v118: 0 = automatic calibration, 1-65535 manual calibration.
177-
- **bool getZeroCalibrationData(ZeroCalibrationData &data);** fills a data struct with the current zero calibration status and value.
177+
- **bool getZeroCalibrationData(ZeroCalibrationData &data)** fills a data struct with the current zero calibration status and value.
178178
Returns true on success.
179+
- **bool readRegister(uint8_t address, RegisterData &reg)** fills a data struct with the chip's register data at that address.
180+
Primarily intended for troubleshooting and analysis of the sensor. Not recommended to build applications on top of this method's raw data.
181+
Returns true when the struct is filled, false when the data could not be read.
182+
Note: unlike other public methods, CRC errors don't return false or show up in `lastError()`, instead the CRC result is stored in `RegisterData.crcValid`.
179183
- **int lastError()** returns last error.
180184
- **uint8_t lastStatus()** returns status byte from last read.
181185
Read datasheet or table below for details. A new read is needed to update this.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
//
2+
// FILE: AGS02MA_readRegister.ino
3+
// AUTHOR: Rob Tillaart, Beanow
4+
// VERSION: 0.3.0
5+
// PURPOSE: test application
6+
// DATE: 2022-04-22
7+
// URL: https://github.com/RobTillaart/AGS02MA
8+
//
9+
10+
11+
#include "AGS02MA.h"
12+
13+
14+
const uint8_t addresses[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 0x11, 0x20, 0x21};
15+
16+
17+
AGS02MA::RegisterData reg;
18+
AGS02MA AGS(26);
19+
20+
21+
void setup()
22+
{
23+
// ESP devices typically mis the first serial log lines after flashing.
24+
// Delay somewhat to include all output.
25+
delay(1000);
26+
27+
Serial.begin(115200);
28+
Serial.println(__FILE__);
29+
30+
Wire.begin();
31+
32+
Serial.print("AGS02MA_LIB_VERSION: ");
33+
Serial.println(AGS02MA_LIB_VERSION);
34+
Serial.println();
35+
36+
bool b = AGS.begin();
37+
Serial.print("BEGIN:\t");
38+
Serial.println(b);
39+
}
40+
41+
42+
void loop()
43+
{
44+
delay(3000);
45+
for (auto address : addresses)
46+
{
47+
bool b = AGS.readRegister(address, reg);
48+
Serial.print("REG[0x");
49+
Serial.print(address, HEX);
50+
Serial.print("]");
51+
52+
if(b)
53+
{
54+
printRegister(address, reg);
55+
}
56+
else
57+
{
58+
Serial.print("\tError:\t");
59+
Serial.println(AGS.lastError());
60+
}
61+
delay(50);
62+
}
63+
Serial.println();
64+
}
65+
66+
67+
void printRegister(uint8_t address, AGS02MA::RegisterData &reg) {
68+
// Raw bytes first for any register.
69+
for (auto b : reg.data)
70+
{
71+
Serial.print("\t");
72+
Serial.print(b);
73+
}
74+
75+
Serial.print("\tCRC: ");
76+
Serial.print(reg.crcValid ? "OK " : "ERR ");
77+
Serial.print(reg.crc);
78+
79+
// Specific interpretations
80+
switch (address)
81+
{
82+
case 0x00:
83+
Serial.print("\tSensor data:\t");
84+
Serial.print(reg.data[0]);
85+
Serial.print("\t");
86+
Serial.print(
87+
(reg.data[1] << 16) +
88+
(reg.data[2] << 8) +
89+
reg.data[3]
90+
);
91+
break;
92+
93+
case 0x01:
94+
case 0x02:
95+
case 0x03:
96+
case 0x04:
97+
Serial.print("\tCalibration:\t");
98+
Serial.print(
99+
(reg.data[0] << 8) +
100+
reg.data[1]
101+
);
102+
Serial.print("\t");
103+
Serial.print(
104+
(reg.data[2] << 8) +
105+
reg.data[3]
106+
);
107+
break;
108+
109+
case 0x11:
110+
Serial.print("\tVersion:\t");
111+
Serial.print(reg.data[3]);
112+
break;
113+
114+
case 0x21:
115+
Serial.print("\tI2C address:\t0x");
116+
Serial.print(reg.data[0], HEX);
117+
break;
118+
119+
default:
120+
break;
121+
}
122+
Serial.println();
123+
}
124+
125+
126+
// -- END OF FILE --

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"type": "git",
2222
"url": "https://github.com/RobTillaart/AGS02MA.git"
2323
},
24-
"version": "0.2.0",
24+
"version": "0.3.0",
2525
"license": "MIT",
2626
"frameworks": "arduino",
2727
"platforms": "*",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=AGS02MA
2-
version=0.2.0
2+
version=0.3.0
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for AGS02MA - TVOC sensor

0 commit comments

Comments
 (0)