LibDriver SEN5X
Loading...
Searching...
No Matches
driver_sen5x_read_test.c
Go to the documentation of this file.
1
36
38
39static sen5x_handle_t gs_handle;
40
50uint8_t sen5x_read_test(sen5x_type_t type, uint32_t times)
51{
52 uint8_t res;
53 uint32_t i;
54 sen5x_info_t info;
55
56 /* link functions */
64
65 /* get information */
66 res = sen5x_info(&info);
67 if (res != 0)
68 {
69 sen5x_interface_debug_print("sen5x: get info failed.\n");
70
71 return 1;
72 }
73 else
74 {
75 /* print chip info */
76 sen5x_interface_debug_print("sen5x: chip is %s.\n", info.chip_name);
77 sen5x_interface_debug_print("sen5x: manufacturer is %s.\n", info.manufacturer_name);
78 sen5x_interface_debug_print("sen5x: interface is %s.\n", info.interface);
79 sen5x_interface_debug_print("sen5x: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
80 sen5x_interface_debug_print("sen5x: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
81 sen5x_interface_debug_print("sen5x: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
82 sen5x_interface_debug_print("sen5x: max current is %0.2fmA.\n", info.max_current_ma);
83 sen5x_interface_debug_print("sen5x: max temperature is %0.1fC.\n", info.temperature_max);
84 sen5x_interface_debug_print("sen5x: min temperature is %0.1fC.\n", info.temperature_min);
85 }
86
87 /* start read test */
88 sen5x_interface_debug_print("sen5x: start read test.\n");
89
90 /* set the type */
91 res = sen5x_set_type(&gs_handle, type);
92 if (res != 0)
93 {
94 sen5x_interface_debug_print("sen5x: set type failed.\n");
95
96 return 1;
97 }
98
99 /* init the chip */
100 res = sen5x_init(&gs_handle);
101 if (res != 0)
102 {
103 sen5x_interface_debug_print("sen5x: init failed.\n");
104
105 return 1;
106 }
107
108 /* start measurement */
109 res = sen5x_start_measurement(&gs_handle);
110 if (res != 0)
111 {
112 sen5x_interface_debug_print("sen5x: start measurement failed.\n");
113 (void)sen5x_deinit(&gs_handle);
114
115 return 1;
116 }
117
118 /* delay 2000 ms */
120
121 /* sen5x_read test */
122 sen5x_interface_debug_print("sen5x: sen5x_read test.\n");
123 for (i = 0; i < times; i++)
124 {
125 if (type == SEN50)
126 {
127 sen50_data_t output;
128
129 /* read data */
130 res = sen50_read(&gs_handle, &output);
131 if (res != 0)
132 {
133 sen5x_interface_debug_print("sen50: read failed.\n");
134 (void)sen5x_deinit(&gs_handle);
135
136 return 1;
137 }
138 if (output.pm_valid != 0)
139 {
140 sen5x_interface_debug_print("sen50: pm1.0 is %0.2f ug/m3.\n", output.pm1p0_ug_m3);
141 sen5x_interface_debug_print("sen50: pm2.5 is %0.2f ug/m3.\n", output.pm2p5_ug_m3);
142 sen5x_interface_debug_print("sen50: pm4.0 is %0.2f ug/m3.\n", output.pm4p0_ug_m3);
143 sen5x_interface_debug_print("sen50: pm10.0 is %0.2f ug/m3.\n", output.pm10_ug_m3);
144 }
145 else
146 {
147 sen5x_interface_debug_print("sen50: pm is invalid.\n");
148 }
149 }
150 else if (type == SEN54)
151 {
152 sen54_data_t output;
153
154 /* read data */
155 res = sen54_read(&gs_handle, &output);
156 if (res != 0)
157 {
158 sen5x_interface_debug_print("sen54: read failed.\n");
159 (void)sen5x_deinit(&gs_handle);
160
161 return 1;
162 }
163 if (output.pm_valid != 0)
164 {
165 sen5x_interface_debug_print("sen54: pm1.0 is %0.2f ug/m3.\n", output.pm1p0_ug_m3);
166 sen5x_interface_debug_print("sen54: pm2.5 is %0.2f ug/m3.\n", output.pm2p5_ug_m3);
167 sen5x_interface_debug_print("sen54: pm4.0 is %0.2f ug/m3.\n", output.pm4p0_ug_m3);
168 sen5x_interface_debug_print("sen54: pm10.0 is %0.2f ug/m3.\n", output.pm10_ug_m3);
169 sen5x_interface_debug_print("sen54: compensated ambient humidity is %0.2f%%.\n", output.compensated_ambient_humidity_percentage);
170 sen5x_interface_debug_print("sen54: compensated ambient temperature is %0.2fC.\n", output.compensated_ambient_temperature_degree);
171 sen5x_interface_debug_print("sen54: voc index is %0.2f.\n", output.voc_index);
172 }
173 else
174 {
175 sen5x_interface_debug_print("sen54: pm is invalid.\n");
176 sen5x_interface_debug_print("sen54: compensated ambient humidity is %0.2f%%.\n", output.compensated_ambient_humidity_percentage);
177 sen5x_interface_debug_print("sen54: compensated ambient temperature is %0.2fC.\n", output.compensated_ambient_temperature_degree);
178 sen5x_interface_debug_print("sen54: voc index is %0.2f.\n", output.voc_index);
179 }
180 }
181 else
182 {
183 sen55_data_t output;
184
185 /* read data */
186 res = sen55_read(&gs_handle, &output);
187 if (res != 0)
188 {
189 sen5x_interface_debug_print("sen55: read failed.\n");
190 (void)sen5x_deinit(&gs_handle);
191
192 return 1;
193 }
194 if (output.pm_valid != 0)
195 {
196 sen5x_interface_debug_print("sen55: pm1.0 is %0.2f ug/m3.\n", output.pm1p0_ug_m3);
197 sen5x_interface_debug_print("sen55: pm2.5 is %0.2f ug/m3.\n", output.pm2p5_ug_m3);
198 sen5x_interface_debug_print("sen55: pm4.0 is %0.2f ug/m3.\n", output.pm4p0_ug_m3);
199 sen5x_interface_debug_print("sen55: pm10.0 is %0.2f ug/m3.\n", output.pm10_ug_m3);
200 sen5x_interface_debug_print("sen55: compensated ambient humidity is %0.2f%%.\n", output.compensated_ambient_humidity_percentage);
201 sen5x_interface_debug_print("sen55: compensated ambient temperature is %0.2fC.\n", output.compensated_ambient_temperature_degree);
202 sen5x_interface_debug_print("sen55: voc index is %0.2f.\n", output.voc_index);
203 sen5x_interface_debug_print("sen55: nox index is %0.2f.\n", output.nox_index);
204 }
205 else
206 {
207 sen5x_interface_debug_print("sen55: pm is invalid.\n");
208 sen5x_interface_debug_print("sen55: compensated ambient humidity is %0.2f%%.\n", output.compensated_ambient_humidity_percentage);
209 sen5x_interface_debug_print("sen55: compensated ambient temperature is %0.2fC.\n", output.compensated_ambient_temperature_degree);
210 sen5x_interface_debug_print("sen55: voc index is %0.2f.\n", output.voc_index);
211 sen5x_interface_debug_print("sen55: nox index is %0.2f.\n", output.nox_index);
212 }
213 }
214
215 /* delay 2000 ms */
217 }
218
219 /* sen5x_read_pm_value test */
220 sen5x_interface_debug_print("sen5x: sen5x_read_pm_value test.\n");
221 for (i = 0; i < times; i++)
222 {
223 sen5x_pm_t pm;
224
225 res = sen5x_read_pm_value(&gs_handle, &pm);
226 if (res != 0)
227 {
228 sen5x_interface_debug_print("sen5x: read failed.\n");
229 (void)sen5x_deinit(&gs_handle);
230
231 return 1;
232 }
233 if (pm.pm_valid != 0)
234 {
235 sen5x_interface_debug_print("sen5x: pm1.0 is %0.2f ug/m3.\n", pm.pm1p0_ug_m3);
236 sen5x_interface_debug_print("sen5x: pm2.5 is %0.2f ug/m3.\n", pm.pm2p5_ug_m3);
237 sen5x_interface_debug_print("sen5x: pm4.0 is %0.2f ug/m3.\n", pm.pm4p0_ug_m3);
238 sen5x_interface_debug_print("sen5x: pm10.0 is %0.2f ug/m3.\n", pm.pm10_ug_m3);
239 sen5x_interface_debug_print("sen5x: pm0.5 is %0.2f cm3.\n", pm.pm0p5_cm3);
240 sen5x_interface_debug_print("sen5x: pm1.0 is %0.2f cm3.\n", pm.pm1p0_cm3);
241 sen5x_interface_debug_print("sen5x: pm2.5 is %0.2f cm3.\n", pm.pm2p5_cm3);
242 sen5x_interface_debug_print("sen5x: pm4.0 is %0.2f cm3.\n", pm.pm4p0_cm3);
243 sen5x_interface_debug_print("sen5x: pm10.0 is %0.2f cm3.\n", pm.pm10_cm3);
244 sen5x_interface_debug_print("sen5x: typical is %0.2f um.\n", pm.typical_particle_um);
245 }
246 else
247 {
248 sen5x_interface_debug_print("sen5x: pm is invalid.\n");
249 }
250
251 /* delay 2000 ms */
253 }
254
255 if (type != SEN50)
256 {
257 /* sen5x_read_raw_value test */
258 sen5x_interface_debug_print("sen5x: sen5x_read_raw_value test.\n");
259 for (i = 0; i < times; i++)
260 {
261 sen5x_raw_t raw;
262
263 /* read raw value */
264 res = sen5x_read_raw_value(&gs_handle, &raw);
265 if (res != 0)
266 {
267 sen5x_interface_debug_print("sen5x: read raw value failed.\n");
268 (void)sen5x_deinit(&gs_handle);
269
270 return 1;
271 }
272 sen5x_interface_debug_print("sen5x: humidity is %0.2f%%.\n", raw.humidity_percentage);
273 sen5x_interface_debug_print("sen5x: temperature is %0.2fC.\n", raw.temperature_degree);
274 sen5x_interface_debug_print("sen5x: voc is %0.2f.\n", raw.voc);
275 sen5x_interface_debug_print("sen5x: nox is %0.2f.\n", raw.nox);
276
277 /* delay 2000 ms */
279 }
280 }
281
282 /* stop measurement */
283 res = sen5x_stop_measurement(&gs_handle);
284 if (res != 0)
285 {
286 sen5x_interface_debug_print("sen5x: stop measurement failed.\n");
287 (void)sen5x_deinit(&gs_handle);
288
289 return 1;
290 }
291
292 /* delay 2000 ms */
294
295 if (type != SEN50)
296 {
297 /* start measurement without pm test */
298 sen5x_interface_debug_print("sen5x: start measurement without pm test.\n");
299
300 /* start measurement without pm */
301 res = sen5x_start_measurement_without_pm(&gs_handle);
302 if (res != 0)
303 {
304 sen5x_interface_debug_print("sen5x: start measurement without pm failed.\n");
305 (void)sen5x_deinit(&gs_handle);
306
307 return 1;
308 }
309
310 /* delay 2000 ms */
312
313 for (i = 0; i < times; i++)
314 {
315 if (type == SEN54)
316 {
317 sen54_data_t output;
318
319 /* read data */
320 res = sen54_read(&gs_handle, &output);
321 if (res != 0)
322 {
323 sen5x_interface_debug_print("sen54: read failed.\n");
324 (void)sen5x_deinit(&gs_handle);
325
326 return 1;
327 }
328 sen5x_interface_debug_print("sen54: compensated ambient humidity is %0.2f%%.\n", output.compensated_ambient_humidity_percentage);
329 sen5x_interface_debug_print("sen54: compensated ambient temperature is %0.2fC.\n", output.compensated_ambient_temperature_degree);
330 sen5x_interface_debug_print("sen54: voc index is %0.2f.\n", output.voc_index);
331 }
332 else
333 {
334 sen55_data_t output;
335
336 /* read data */
337 res = sen55_read(&gs_handle, &output);
338 if (res != 0)
339 {
340 sen5x_interface_debug_print("sen55: read failed.\n");
341 (void)sen5x_deinit(&gs_handle);
342
343 return 1;
344 }
345 sen5x_interface_debug_print("sen55: compensated ambient humidity is %0.2f%%.\n", output.compensated_ambient_humidity_percentage);
346 sen5x_interface_debug_print("sen55: compensated ambient temperature is %0.2fC.\n", output.compensated_ambient_temperature_degree);
347 sen5x_interface_debug_print("sen55: voc index is %0.2f.\n", output.voc_index);
348 sen5x_interface_debug_print("sen55: nox index is %0.2f.\n", output.nox_index);
349 }
350
351 /* delay 2000 ms */
353 }
354
355 /* stop measurement */
356 res = sen5x_stop_measurement(&gs_handle);
357 if (res != 0)
358 {
359 sen5x_interface_debug_print("sen5x: stop measurement failed.\n");
360 (void)sen5x_deinit(&gs_handle);
361
362 return 1;
363 }
364 }
365
366 /* finish read test */
367 sen5x_interface_debug_print("sen5x: finish read test.\n");
368 (void)sen5x_deinit(&gs_handle);
369
370 return 0;
371}
driver sen5x read test header file
struct sen50_data_s sen50_data_t
sen50 data structure definition
uint8_t sen5x_info(sen5x_info_t *info)
get chip information
uint8_t sen5x_read_pm_value(sen5x_handle_t *handle, sen5x_pm_t *pm)
read the pm value
uint8_t sen5x_read_raw_value(sen5x_handle_t *handle, sen5x_raw_t *raw)
read raw value
uint8_t sen55_read(sen5x_handle_t *handle, sen55_data_t *output)
read the result
uint8_t sen5x_set_type(sen5x_handle_t *handle, sen5x_type_t type)
set the chip type
uint8_t sen54_read(sen5x_handle_t *handle, sen54_data_t *output)
read the result
uint8_t sen5x_start_measurement_without_pm(sen5x_handle_t *handle)
start the measurement without pm
uint8_t sen5x_start_measurement(sen5x_handle_t *handle)
start the measurement
uint8_t sen50_read(sen5x_handle_t *handle, sen50_data_t *output)
read the result
sen5x_type_t
sen5x type enumeration definition
uint8_t sen5x_deinit(sen5x_handle_t *handle)
close the chip
struct sen5x_handle_s sen5x_handle_t
sen5x handle structure definition
struct sen55_data_s sen55_data_t
sen55 data structure definition
struct sen5x_raw_s sen5x_raw_t
sen5x raw structure definition
struct sen54_data_s sen54_data_t
sen54 data structure definition
struct sen5x_pm_s sen5x_pm_t
sen5x pm structure definition
struct sen5x_info_s sen5x_info_t
sen5x information structure definition
uint8_t sen5x_init(sen5x_handle_t *handle)
initialize the chip
uint8_t sen5x_stop_measurement(sen5x_handle_t *handle)
stop the measurement
@ SEN50
@ SEN54
uint8_t sen5x_interface_iic_write_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t sen5x_interface_iic_read_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t sen5x_interface_iic_init(void)
interface iic bus init
uint8_t sen5x_interface_iic_deinit(void)
interface iic bus deinit
void sen5x_interface_delay_ms(uint32_t ms)
interface delay ms
void sen5x_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t sen5x_read_test(sen5x_type_t type, uint32_t times)
read test
uint8_t pm_valid
float compensated_ambient_temperature_degree
float compensated_ambient_humidity_percentage
uint8_t pm_valid
float compensated_ambient_temperature_degree
float compensated_ambient_humidity_percentage
uint8_t pm_valid
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]
float pm1p0_cm3
float typical_particle_um
float pm4p0_cm3
float pm1p0_ug_m3
float pm0p5_cm3
float pm4p0_ug_m3
float pm10_ug_m3
uint8_t pm_valid
float pm2p5_cm3
float pm2p5_ug_m3
float humidity_percentage
float temperature_degree