LibDriver SCD4X
Loading...
Searching...
No Matches
driver_scd4x_read_test.c
Go to the documentation of this file.
1
36
38
39static scd4x_handle_t gs_handle;
40
50uint8_t scd4x_read_test(scd4x_t type, uint32_t times)
51{
52 uint8_t res;
53 uint32_t i;
54 scd4x_info_t info;
55
56 /* link functions */
64
65 /* scd4x info */
66 res = scd4x_info(&info);
67 if (res != 0)
68 {
69 scd4x_interface_debug_print("scd4x: get info failed.\n");
70
71 return 1;
72 }
73 else
74 {
75 /* print chip information */
76 scd4x_interface_debug_print("scd4x: chip is %s.\n", info.chip_name);
77 scd4x_interface_debug_print("scd4x: manufacturer is %s.\n", info.manufacturer_name);
78 scd4x_interface_debug_print("scd4x: interface is %s.\n", info.interface);
79 scd4x_interface_debug_print("scd4x: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
80 scd4x_interface_debug_print("scd4x: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
81 scd4x_interface_debug_print("scd4x: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
82 scd4x_interface_debug_print("scd4x: max current is %0.2fmA.\n", info.max_current_ma);
83 scd4x_interface_debug_print("scd4x: max temperature is %0.1fC.\n", info.temperature_max);
84 scd4x_interface_debug_print("scd4x: min temperature is %0.1fC.\n", info.temperature_min);
85 }
86
87 /* start read test */
88 scd4x_interface_debug_print("scd4x: start read test.\n");
89
90 /* set chip type */
91 res = scd4x_set_type(&gs_handle, type);
92 if (res != 0)
93 {
94 scd4x_interface_debug_print("scd4x: set type failed.\n");
95
96 return 1;
97 }
98
99 /* scd4x init */
100 res = scd4x_init(&gs_handle);
101 if (res != 0)
102 {
103 scd4x_interface_debug_print("scd4x: init failed.\n");
104
105 return 1;
106 }
107
108 /* continuous read test */
109 scd4x_interface_debug_print("scd4x: continuous read test.\n");
110
111 /* start */
112 res = scd4x_start_periodic_measurement(&gs_handle);
113 if (res != 0)
114 {
115 scd4x_interface_debug_print("scd4x: start periodic measurement failed.\n");
116 (void)scd4x_deinit(&gs_handle);
117
118 return 1;
119 }
120
121 /* loop */
122 for (i = 0; i < times; i++)
123 {
124 uint16_t co2_raw;
125 uint16_t co2_ppm;
126 uint16_t temperature_raw;
127 float temperature_s;
128 uint16_t humidity_raw;
129 float humidity_s;
130
131 /* delay 5000ms */
133
134 /* read data */
135 res = scd4x_read(&gs_handle, &co2_raw, &co2_ppm,
136 &temperature_raw, &temperature_s,
137 &humidity_raw, &humidity_s);
138 if (res != 0)
139 {
140 scd4x_interface_debug_print("scd4x: read failed.\n");
141 (void)scd4x_deinit(&gs_handle);
142
143 return 1;
144 }
145
146 /* output */
147 scd4x_interface_debug_print("scd4x: co2 is %02dppm.\n", co2_ppm);
148 scd4x_interface_debug_print("scd4x: temperature is %0.2fC.\n", temperature_s);
149 scd4x_interface_debug_print("scd4x: humidity is %0.2f%%.\n", humidity_s);
150 }
151
152 /* stop */
153 res = scd4x_stop_periodic_measurement(&gs_handle);
154 if (res != 0)
155 {
156 scd4x_interface_debug_print("scd4x: stop periodic measurement failed.\n");
157 (void)scd4x_deinit(&gs_handle);
158
159 return 1;
160 }
161
162 /* low power continuous read test */
163 scd4x_interface_debug_print("scd4x: low power continuous read test.\n");
164
165 /* start */
167 if (res != 0)
168 {
169 scd4x_interface_debug_print("scd4x: start low power periodic measurement failed.\n");
170 (void)scd4x_deinit(&gs_handle);
171
172 return 1;
173 }
174
175 /* loop */
176 for (i = 0; i < times; i++)
177 {
178 uint16_t co2_raw;
179 uint16_t co2_ppm;
180 uint16_t temperature_raw;
181 float temperature_s;
182 uint16_t humidity_raw;
183 float humidity_s;
184
185 /* delay 30000ms */
187
188 /* read data */
189 res = scd4x_read(&gs_handle, &co2_raw, &co2_ppm,
190 &temperature_raw, &temperature_s,
191 &humidity_raw, &humidity_s);
192 if (res != 0)
193 {
194 scd4x_interface_debug_print("scd4x: read failed.\n");
195 (void)scd4x_deinit(&gs_handle);
196
197 return 1;
198 }
199
200 /* output */
201 scd4x_interface_debug_print("scd4x: co2 is %02dppm.\n", co2_ppm);
202 scd4x_interface_debug_print("scd4x: temperature is %0.2fC.\n", temperature_s);
203 scd4x_interface_debug_print("scd4x: humidity is %0.2f%%.\n", humidity_s);
204 }
205
206 /* stop */
207 res = scd4x_stop_periodic_measurement(&gs_handle);
208 if (res != 0)
209 {
210 scd4x_interface_debug_print("scd4x: stop periodic measurement failed.\n");
211 (void)scd4x_deinit(&gs_handle);
212
213 return 1;
214 }
215
216 /* scd41 && scd43 */
217 if (type != SCD40)
218 {
219 uint32_t timeout;
220 scd4x_bool_t enable;
221
222 /* measure single shot test */
223 scd4x_interface_debug_print("scd4x: measure single shot test.\n");
224
225 /* loop */
226 for (i = 0; i < times; i++)
227 {
228 /* measure single shot */
229 res = scd4x_measure_single_shot(&gs_handle);
230 if (res != 0)
231 {
232 scd4x_interface_debug_print("scd4x: measure single shot failed.\n");
233 (void)scd4x_deinit(&gs_handle);
234
235 return 1;
236 }
237
238 /* set 30s */
239 timeout = 30;
240
241 /* check timeout */
242 while (timeout != 0)
243 {
244 /* delay 1000ms */
246
247 /* timeout-- */
248 timeout--;
249
250 /* read data */
251 res = scd4x_get_data_ready_status(&gs_handle, &enable);
252 if (res != 0)
253 {
254 scd4x_interface_debug_print("scd4x: measure single shot failed.\n");
255 (void)scd4x_deinit(&gs_handle);
256
257 return 1;
258 }
259 if (enable == SCD4X_BOOL_TRUE)
260 {
261 uint16_t co2_raw;
262 uint16_t co2_ppm;
263 uint16_t temperature_raw;
264 float temperature_s;
265 uint16_t humidity_raw;
266 float humidity_s;
267
268 /* read data */
269 res = scd4x_read(&gs_handle, &co2_raw, &co2_ppm,
270 &temperature_raw, &temperature_s,
271 &humidity_raw, &humidity_s);
272 if (res != 0)
273 {
274 scd4x_interface_debug_print("scd4x: read failed.\n");
275 (void)scd4x_deinit(&gs_handle);
276
277 return 1;
278 }
279
280 /* output */
281 scd4x_interface_debug_print("scd4x: co2 is %02dppm.\n", co2_ppm);
282 scd4x_interface_debug_print("scd4x: temperature is %0.2fC.\n", temperature_s);
283 scd4x_interface_debug_print("scd4x: humidity is %0.2f%%.\n", humidity_s);
284
285 break;
286 }
287 }
288 if (timeout == 0)
289 {
290 scd4x_interface_debug_print("scd4x: timeout.\n");
291 (void)scd4x_deinit(&gs_handle);
292
293 return 1;
294 }
295 }
296
297 /* measure single shot rht only test */
298 scd4x_interface_debug_print("scd4x: measure single shot rht only test.\n");
299
300 /* loop */
301 for (i = 0; i < times; i++)
302 {
303 /* measure single shot rht only */
304 res = scd4x_measure_single_shot_rht_only(&gs_handle);
305 if (res != 0)
306 {
307 scd4x_interface_debug_print("scd4x: measure single shot rht only failed.\n");
308 (void)scd4x_deinit(&gs_handle);
309
310 return 1;
311 }
312
313 /* set 30s */
314 timeout = 30;
315
316 /* check timeout */
317 while (timeout != 0)
318 {
319 /* delay 1000ms */
321
322 /* timeout-- */
323 timeout--;
324
325 /* read data */
326 res = scd4x_get_data_ready_status(&gs_handle, &enable);
327 if (res != 0)
328 {
329 scd4x_interface_debug_print("scd4x: measure single shot failed.\n");
330 (void)scd4x_deinit(&gs_handle);
331
332 return 1;
333 }
334 if (enable == SCD4X_BOOL_TRUE)
335 {
336 uint16_t co2_raw;
337 uint16_t co2_ppm;
338 uint16_t temperature_raw;
339 float temperature_s;
340 uint16_t humidity_raw;
341 float humidity_s;
342
343 /* read data */
344 res = scd4x_read(&gs_handle, &co2_raw, &co2_ppm,
345 &temperature_raw, &temperature_s,
346 &humidity_raw, &humidity_s);
347 if (res != 0)
348 {
349 scd4x_interface_debug_print("scd4x: read failed.\n");
350 (void)scd4x_deinit(&gs_handle);
351
352 return 1;
353 }
354
355 /* output */
356 scd4x_interface_debug_print("scd4x: humidity is %0.2f%%.\n", humidity_s);
357
358 break;
359 }
360 }
361 if (timeout == 0)
362 {
363 scd4x_interface_debug_print("scd4x: timeout.\n");
364 (void)scd4x_deinit(&gs_handle);
365
366 return 1;
367 }
368 }
369 }
370
371 /* finish read test */
372 scd4x_interface_debug_print("scd4x: finish read test.\n");
373 (void)scd4x_deinit(&gs_handle);
374
375 return 0;
376}
driver scd4x read test header file
uint8_t scd4x_measure_single_shot(scd4x_handle_t *handle)
measure single shot
uint8_t scd4x_measure_single_shot_rht_only(scd4x_handle_t *handle)
measure single shot rht only
struct scd4x_info_s scd4x_info_t
scd4x information structure definition
uint8_t scd4x_get_data_ready_status(scd4x_handle_t *handle, scd4x_bool_t *enable)
get data ready status
uint8_t scd4x_stop_periodic_measurement(scd4x_handle_t *handle)
stop periodic measurement
uint8_t scd4x_deinit(scd4x_handle_t *handle)
close the chip
uint8_t scd4x_read(scd4x_handle_t *handle, uint16_t *co2_raw, uint16_t *co2_ppm, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
read data
uint8_t scd4x_set_type(scd4x_handle_t *handle, scd4x_t type)
set type
uint8_t scd4x_init(scd4x_handle_t *handle)
initialize the chip
uint8_t scd4x_start_periodic_measurement(scd4x_handle_t *handle)
start periodic measurement
struct scd4x_handle_s scd4x_handle_t
scd4x handle structure definition
uint8_t scd4x_info(scd4x_info_t *info)
get chip information
scd4x_t
scd4x type enumeration definition
scd4x_bool_t
scd4x bool enumeration definition
uint8_t scd4x_start_low_power_periodic_measurement(scd4x_handle_t *handle)
start low power periodic measurement
@ SCD40
@ SCD4X_BOOL_TRUE
uint8_t scd4x_interface_iic_init(void)
interface iic bus init
void scd4x_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t scd4x_interface_iic_deinit(void)
interface iic bus deinit
uint8_t scd4x_interface_iic_read_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus read command
void scd4x_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t scd4x_interface_iic_write_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus write command
uint8_t scd4x_read_test(scd4x_t type, uint32_t times)
read test
float temperature_max
float supply_voltage_max_v
uint32_t driver_version
float temperature_min
float max_current_ma
char manufacturer_name[32]
float supply_voltage_min_v
char interface[8]
char chip_name[32]