-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAGS02MA.h
146 lines (110 loc) · 3.96 KB
/
AGS02MA.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#pragma once
//
// FILE: AGS02MA.h
// AUTHOR: Rob Tillaart, Viktor Balint, Beanow
// DATE: 2021-08-12
// VERSION: 0.4.2
// PURPOSE: Arduino library for AGS02MA TVOC sensor
// URL: https://github.com/RobTillaart/AGS02MA
//
#include "Arduino.h"
#include "Wire.h"
#define AGS02MA_LIB_VERSION (F("0.4.2"))
#define AGS02MA_OK 0
#define AGS02MA_ERROR -10
#define AGS02MA_ERROR_CRC -11
#define AGS02MA_ERROR_READ -12
#define AGS02MA_ERROR_NOT_READY -13
#define AGS02MA_ERROR_REQUEST -14
#define AGS02MA_I2C_CLOCK 25000 // max 30000
class AGS02MA
{
public:
struct RegisterData
{
uint8_t data[4];
uint8_t crc;
bool crcValid;
};
struct ZeroCalibrationData
{
/**
* Warning, the exact meaning of this status is not fully documented.
* It seems like it's a bit mask:
* 0000 1100 | 0x0C | 12 | Typical value
* 0000 1101 | 0x0D | 13 | Sometimes seen on v117
* 0111 1101 | 0x7D | 125 | Seen on v118, after power-off (gives different data than 12!)
*/
uint16_t status;
uint16_t value;
};
// address 26 = 0x1A
explicit AGS02MA(const uint8_t deviceAddress = 26, TwoWire *wire = &Wire);
bool begin();
bool isConnected();
void reset();
bool isHeated() { return (millis() - _startTime) > 120000UL; };
// CONFIGURATION
bool setAddress(const uint8_t deviceAddress);
uint8_t getAddress() { return _address; };
uint8_t getSensorVersion();
uint32_t getSensorDate();
// to set the speed the I2C bus should return to
// as the device operates at very low bus speed of 30 kHz.
void setI2CResetSpeed(uint32_t speed) { _I2CResetSpeed = speed; };
uint32_t getI2CResetSpeed() { return _I2CResetSpeed; };
// to be called after at least 5 minutes in fresh air.
bool zeroCalibration() { return manualZeroCalibration(0); };
/**
* Set the zero calibration value manually.
* To be called after at least 5 minutes in fresh air.
* For v117: 0-65535 = automatic calibration.
* For v118: 0 = automatic calibration, 1-65535 manual calibration.
*/
bool manualZeroCalibration(uint16_t value = 0);
bool getZeroCalibrationData(ZeroCalibrationData &data);
// MODE
bool setPPBMode();
bool setUGM3Mode();
uint8_t getMode() { return _mode; };
// READ functions
uint32_t readPPB(); // parts per billion 10^9
uint32_t readUGM3(); // microgram per cubic meter
// derived read functions
float readPPM() { return readPPB() * 0.001; }; // parts per million
float readMGM3() { return readUGM3() * 0.001; }; // milligram per cubic meter
float readUGF3() { return readUGM3() * 0.0283168466; }; // microgram per cubic feet
float lastPPM() { return _lastPPB * 0.001; };
uint32_t lastPPB() { return _lastPPB; }; // fetch last PPB measurement
uint32_t lastUGM3() { return _lastUGM3; }; // fetch last UGM3 measurement
// STATUS
uint32_t lastRead() { return _lastRead; }; // timestamp last measurement
int lastError();
uint8_t lastStatus() { return _status; };
uint8_t dataReady() { return _status & 0x01; };
// Reading registers
bool readRegister(uint8_t address, RegisterData ®);
private:
uint32_t _readSensor();
bool _readRegister(uint8_t reg);
bool _writeRegister(uint8_t reg);
uint32_t _I2CResetSpeed = 100000;
uint32_t _startTime = 0;
uint32_t _lastRead = 0;
uint32_t _lastRegTime = 0;
uint32_t _lastPPB = 0;
uint32_t _lastUGM3 = 0;
uint8_t _address = 0;
uint8_t _mode = 255;
uint8_t _status = 0;
uint8_t _buffer[5];
void _setI2CLowSpeed();
void _setI2CHighSpeed();
uint16_t _getDataMSB();
uint16_t _getDataLSB();
uint8_t _CRC8(uint8_t * buf, uint8_t size);
uint8_t _bin2bcd(uint8_t val);
int _error = AGS02MA_OK;
TwoWire* _wire;
};
// -- END OF FILE --