-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFRobot_STCC4.h
More file actions
279 lines (244 loc) · 8.71 KB
/
DFRobot_STCC4.h
File metadata and controls
279 lines (244 loc) · 8.71 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*!
* @file DFRobot_STCC4.h
* @brief Declaration the basic structure of class DFRobot_STCC4
* @n It is possible to measure the concentration of carbon dioxide in the air.
* @n If the humidity and temperature sensor is connected, temperature and humidity can also be obtained.
* @n Only supports IIC communication interface
* @copyright Copyright (c) 2025 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [fary](feng.yang@dfrobot.com)
* @version V1.0
* @date 2025-08-15
* @url https://github.com/DFRobot/DFRobot_STCC4
*/
#ifndef __DFROBOT_STCC4_H
#define __DFROBOT_STCC4_H
#include <Arduino.h>
#include <Wire.h>
//#define ENABLE_DBG ///< Enable this macro to see the detailed execution process of the program
#ifdef ENABLE_DBG
#define DBG(...) {Serial.print("[");Serial.print(__FUNCTION__); Serial.print("(): "); Serial.print(__LINE__); Serial.print(" ] "); Serial.println(__VA_ARGS__);}
#else
#define DBG(...)
#endif
#define DFRobot_STCC4_I2C_ADDR 0x64
#define STCC4_GET_ID 0x365B // Get the ID of the sensor
#define STCC4_START_CONT_MEASURE 0x218B // Start continuous measurement
#define STCC4_STOP_CONT_MEASURE 0x3F86 // Stop continuous measurement
#define STCC4_READ_MEASURE 0xEC05 // Read measurement data
#define STCC4_SET_RHT_COMPENSATION 0xE000 // Set temperature and humidity compensation
#define STCC4_SET_PRESSURE_COMPENSATION 0xE016 // Set pressure compensation
#define STCC4_SINGLE_SHOT 0x219D // Single shot measurement
#define STCC4_SLEEP 0x3650 // Sleep mode
#define STCC4_WAKEUP 0x00 // Wake up from sleep mode
#define STCC4_SOFT_RESET 0x06 // Soft reset
#define STCC4_FACTORY_RESET 0x3632 // Factory reset
#define STCC4_ENABLE_TESTING_MODE 0x3FBC // Enable testing mode
#define STCC4_DISABLE_TESTING_MODE 0x3F3D // Disable testing mode
#define STCC4_FORC_CALIBRATION 0x362F // Force calibration
class DFRobot_STCC4
{
public:
#define ERR_OK 0 ///< success
#define ERR_DATA_BUS 1 ///< bus error
#define ERR_DATA_READ 2 ///< read error
#define ERR_DATA_WRITE 3 ///< write error
#define ERR_IC_VERSION 4 ///< The chip versions do not match
public:
/**
* @fn DFRobot_STCC4
* @brief Constructor of DFRobot_STCC4 class
* @retunr None
*/
DFRobot_STCC4(void);
~DFRobot_STCC4();
/**
* @fn calculationCRC
* @brief Calculate the CRC of the data
* @param data Pointer to the data array
* @param length Length of the data array
* @return Calculated CRC value
*/
uint8_t calculationCRC(uint16_t *data, size_t length);
/**
* @fn getID
* @brief Get the ID of the sensor
* @return The ID of the sensor.
*/
uint32_t getID(void);
/**
* @fn startMeasurement
* @brief Start continuous measurement
* @return true if successful, false otherwise
*/
bool startMeasurement(void);
/**
* @fn stopMeasurement
* @brief Stop continuous measurement
* @n The sensor needs 1200 milliseconds to execute this instruction.
* @return true if successful, false otherwise
*/
bool stopMeasurement(void);
/**
* @fn measurement
* @brief Read measurement data
* @param co2Concentration Pointer to store CO2 concentration
* @param temperature Pointer to store temperature
* @param humidity Pointer to store humidity
* @param sensorStatus Pointer to store sensor status
* @return true if successful, false otherwise
*/
bool measurement(uint16_t* co2Concentration,
float* temperature,
float* humidity,
uint16_t* sensorStatus);
/**
* @fn setRHTcompensation
* @brief Set temperature and humidity compensation
* @param temperature Temperature compensation value, range of 10 to 40 degrees Celsius.
* @param humidity Humidity compensation value, range of 20 to 80%RH.
* @return true if successful; false otherwise, maybe the temperature/humidity value is out of range.
*/
bool setRHTcompensation(uint16_t temperature, uint16_t humidity);
/**
* @fn setPressureCompensation
* @brief Set pressure compensation
* @param pressure Pressure compensation value, range of 400 to 1100 hPa
* @return true if successful; false otherwise, maybe the pressure value is out of range.
*/
bool setPressureCompensation(uint16_t pressure);
/**
* @fn singleMeasurement
* @brief Perform a single shot measurement
* @n The sensor needs 500 milliseconds to execute this instruction.
* @return true if successful, false otherwise
*/
bool singleMeasurement(void);
/**
* @fn sleep
* @brief Put the sensor into sleep mode
* @return true if successful, false otherwise
*/
bool sleep(void);
/**
* @fn wakeup
* @brief Wake up the sensor from sleep mode
* @return true if successful, false otherwise
*/
bool wakeup(void);
/**
* @fn softRest
* @brief Perform a soft reset of the sensor
* @return true if successful, false otherwise
*/
bool softRest(void);
/**
* @fn factoryReset
* @brief Perform a factory reset of the sensor
* @return true if successful, false otherwise
*/
bool factoryReset(void);
/**
* @fn enableTestingMode
* @brief Enable testing mode
* @return true if successful, false otherwise
*/
bool enableTestingMode(void);
/**
* @fn disableTestingMode
* @brief Disable testing mode
* @return true if successful, false otherwise
*/
bool disableTestingMode(void);
/**
* @fn forcedRecalibration
* @brief Perform forced recalibration
* @param targetPpm Target PPM value for recalibration, must be between 0 and 32000 ppm.
* @param frcCorrection Pointer to store the correction value
* @return true if successful; false otherwise, maybe the target PPM value is out of range.
*/
bool forcedRecalibration(uint16_t targetPpm, uint16_t* frcCorrection);
private:
/**
* @fn writeData
* @brief Write data to the sensor
* @param cmd Command to write
* @param pBuf Pointer to the data buffer
* @param size Size of the data buffer
* @return true if successful, false otherwise
*/
virtual bool writeData(uint16_t cmd, uint16_t * pBuf, size_t size) = 0;
/**
* @fn writeCMD8
* @brief Write an 8-bit command to the sensor
* @param cmd Command to write
* @return true if successful, false otherwise
*/
virtual bool writeCMD8(uint8_t cmd) = 0;
/**
* @fn writeCMD16
* @brief Write a 16-bit command to the sensor
* @param cmd Command to write
* @return true if successful, false otherwise
*/
virtual bool writeCMD16(uint16_t cmd) = 0;
/**
* @fn readData
* @brief Read data from the sensor
* @param pBuf Pointer to the data buffer
* @param size Size of the data buffer
* @return Number of bytes read, or 0 if an error occurred
*/
virtual size_t readData(uint8_t * pBuf, size_t size) = 0;
};
class DFRobot_STCC4_I2C:public DFRobot_STCC4{
public:
/**
* @fn DFRobot_STCC4_I2C
* @brief Constructor of DFRobot_STCC4_I2C class
* @param pWire Pointer to the TwoWire object for I2C communication
* @param addr I2C address of the sensor
*/
DFRobot_STCC4_I2C(TwoWire *pWire = &Wire, uint8_t addr = DFRobot_STCC4_I2C_ADDR);
/**
* @fn begin
* @brief Initialize the sensor
* @return true if successful, false otherwise
*/
bool begin();
/**
* @fn writeData
* @brief Write data to the sensor
* @param cmd Command to write
* @param pBuf Pointer to the data buffer
* @param size Size of the data buffer
* @return true if successful, false otherwise
*/
bool writeData(uint16_t cmd, uint16_t * pBuf, size_t size);
/**
* @fn writeCMD8
* @brief Write an 8-bit command to the sensor
* @param cmd Command to write
* @return true if successful, false otherwise
*/
bool writeCMD8(uint8_t cmd);
/**
* @fn writeCMD16
* @brief Write a 16-bit command to the sensor
* @param cmd Command to write
* @return true if successful, false otherwise
*/
bool writeCMD16(uint16_t cmd);
/**
* @fn readData
* @brief Read data from the sensor
* @param pBuf Pointer to the data buffer
* @param size Size of the data buffer
* @return Number of bytes read, or 0 if an error occurred
*/
size_t readData(uint8_t * pBuf, size_t size);
private:
TwoWire *_pWire; ///< Pointer to the TwoWire object for I2C communication
uint8_t _deviceAddr; ///< I2C address of the sensor
};
#endif