LibDriver SGP41
Loading...
Searching...
No Matches
driver_sgp41_read_test.c
Go to the documentation of this file.
1
36
39
40static sgp41_handle_t gs_handle;
41static sgp41_gas_index_algorithm_t gs_voc_handle;
42static sgp41_gas_index_algorithm_t gs_nox_handle;
43
52uint8_t sgp41_read_test(uint32_t times)
53{
54 uint8_t res;
55 uint16_t raw;
56 uint32_t i;
57 sgp41_info_t info;
58
59 /* link functions */
67
68 /* sgp41 info */
69 res = sgp41_info(&info);
70 if (res != 0)
71 {
72 sgp41_interface_debug_print("sgp41: get info failed.\n");
73
74 return 1;
75 }
76 else
77 {
78 /* print chip information */
79 sgp41_interface_debug_print("sgp41: chip is %s.\n", info.chip_name);
80 sgp41_interface_debug_print("sgp41: manufacturer is %s.\n", info.manufacturer_name);
81 sgp41_interface_debug_print("sgp41: interface is %s.\n", info.interface);
82 sgp41_interface_debug_print("sgp41: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
83 sgp41_interface_debug_print("sgp41: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
84 sgp41_interface_debug_print("sgp41: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
85 sgp41_interface_debug_print("sgp41: max current is %0.2fmA.\n", info.max_current_ma);
86 sgp41_interface_debug_print("sgp41: max temperature is %0.1fC.\n", info.temperature_max);
87 sgp41_interface_debug_print("sgp41: min temperature is %0.1fC.\n", info.temperature_min);
88 }
89
90 /* start read test */
91 sgp41_interface_debug_print("sgp41: start read test.\n");
92
93 /* sgp41 init */
94 res = sgp41_init(&gs_handle);
95 if (res != 0)
96 {
97 sgp41_interface_debug_print("sgp41: init failed.\n");
98
99 return 1;
100 }
101
102 /* soft reset */
103 res = sgp41_soft_reset(&gs_handle);
104 if (res != 0)
105 {
106 sgp41_interface_debug_print("sgp41: soft failed.\n");
107 (void)sgp41_deinit(&gs_handle);
108
109 return 1;
110 }
111
112 /* voc algorithm init */
114
115 /* nox algorithm init */
117
118 /* execute conditioning test */
119 sgp41_interface_debug_print("sgp41: execute conditioning test.\n");
120
121 /* delay 1000ms */
123
124 /* get data */
125 (void)sgp41_get_execute_conditioning(&gs_handle, &raw);
126
127 /* run times */
128 for (i = 0; i < times; i++)
129 {
130 uint16_t sraw_voc;
131 int32_t gas_index;
132
133 /* get execute conditioning */
134 res = sgp41_get_execute_conditioning(&gs_handle, &sraw_voc);
135 if (res != 0)
136 {
137 sgp41_interface_debug_print("sgp41: get execute conditioning failed.\n");
138 (void)sgp41_deinit(&gs_handle);
139
140 return 1;
141 }
142
143 /* algorithm process */
144 sgp41_algorithm_process(&gs_voc_handle, sraw_voc, &gas_index);
145
146 /* output */
147 sgp41_interface_debug_print("sgp41: raw voc is 0x%04X and gas index is %d.\n", sraw_voc, gas_index);
148
149 /* delay 1000ms */
151 }
152
153 /* measure raw test */
154 sgp41_interface_debug_print("sgp41: measure raw test.\n");
155
156 /* run times */
157 for (i = 0; i < times; i++)
158 {
159 uint16_t raw_humidity;
160 uint16_t raw_temperature;
161 uint16_t sraw_voc;
162 uint16_t sraw_nox;
163 int32_t voc_gas_index;
164 int32_t nox_gas_index;
165
166 /* humidity convert to register */
167 res = sgp41_humidity_convert_to_register(&gs_handle, 50.0f, &raw_humidity);
168 if (res != 0)
169 {
170 sgp41_interface_debug_print("sgp41: humidity convert to register failed.\n");
171 (void)sgp41_deinit(&gs_handle);
172
173 return 1;
174 }
175
176 /* temperature convert to register */
177 res = sgp41_temperature_convert_to_register(&gs_handle, 25.0f, &raw_temperature);
178 if (res != 0)
179 {
180 sgp41_interface_debug_print("sgp41: temperature convert to register failed.\n");
181 (void)sgp41_deinit(&gs_handle);
182
183 return 1;
184 }
185
186 /* get measure raw */
187 res = sgp41_get_measure_raw(&gs_handle, raw_humidity, raw_temperature, &sraw_voc, &sraw_nox);
188 if (res != 0)
189 {
190 sgp41_interface_debug_print("sgp41: get measure raw failed.\n");
191 (void)sgp41_deinit(&gs_handle);
192
193 return 1;
194 }
195
196 /* algorithm process */
197 sgp41_algorithm_process(&gs_voc_handle, sraw_voc, &voc_gas_index);
198
199 /* algorithm process */
200 sgp41_algorithm_process(&gs_nox_handle, sraw_nox, &nox_gas_index);
201
202 /* output */
203 sgp41_interface_debug_print("sgp41: raw voc is 0x%04X and voc gas index is %d.\n", sraw_voc, voc_gas_index);
204 sgp41_interface_debug_print("sgp41: raw nox is 0x%04X and nox gas index is %d.\n", sraw_nox, nox_gas_index);
205
206 /* delay 1000ms */
208 }
209
210 /* measure raw without compensation test */
211 sgp41_interface_debug_print("sgp41: measure raw without compensation test.\n");
212
213 /* run times */
214 for (i = 0; i < times; i++)
215 {
216 uint16_t sraw_voc;
217 uint16_t sraw_nox;
218 int32_t voc_gas_index;
219 int32_t nox_gas_index;
220
221 /* get measure raw */
222 res = sgp41_get_measure_raw_without_compensation(&gs_handle, &sraw_voc, &sraw_nox);
223 if (res != 0)
224 {
225 sgp41_interface_debug_print("sgp41: get measure raw without compensation failed.\n");
226 (void)sgp41_deinit(&gs_handle);
227
228 return 1;
229 }
230
231 /* algorithm process */
232 sgp41_algorithm_process(&gs_voc_handle, sraw_voc, &voc_gas_index);
233
234 /* algorithm process */
235 sgp41_algorithm_process(&gs_nox_handle, sraw_nox, &nox_gas_index);
236
237 /* output */
238 sgp41_interface_debug_print("sgp41: raw voc is 0x%04X and voc gas index is %d.\n", sraw_voc, voc_gas_index);
239 sgp41_interface_debug_print("sgp41: raw nox is 0x%04X and nox gas index is %d.\n", sraw_nox, nox_gas_index);
240
241 /* delay 1000ms */
243 }
244
245 /* finish read test */
246 sgp41_interface_debug_print("sgp41: finish read test.\n");
247 (void)sgp41_deinit(&gs_handle);
248
249 return 0;
250}
driver sgp41 algorithm header file
driver sgp41 read test header file
void sgp41_algorithm_process(sgp41_gas_index_algorithm_t *params, int32_t sraw, int32_t *gas_index)
algorithm process
struct sgp41_gas_index_algorithm_s sgp41_gas_index_algorithm_t
sgp41 gas index algorithm structure definition
void sgp41_algorithm_init(sgp41_gas_index_algorithm_t *params, int32_t algorithm_type)
algorithm init
#define SGP41_ALGORITHM_TYPE_VOC
sgp41 algorithm type definition
#define SGP41_ALGORITHM_TYPE_NOX
uint8_t sgp41_temperature_convert_to_register(sgp41_handle_t *handle, float temp, uint16_t *reg)
convert the temperature to the register data
uint8_t sgp41_get_measure_raw(sgp41_handle_t *handle, uint16_t raw_humidity, uint16_t raw_temperature, uint16_t *sraw_voc, uint16_t *sraw_nox)
get the measure raw result
uint8_t sgp41_get_execute_conditioning(sgp41_handle_t *handle, uint16_t *sraw_voc)
get execute conditioning
uint8_t sgp41_info(sgp41_info_t *info)
get chip information
uint8_t sgp41_deinit(sgp41_handle_t *handle)
close the chip
struct sgp41_info_s sgp41_info_t
sgp41 information structure definition
uint8_t sgp41_soft_reset(sgp41_handle_t *handle)
soft reset the chip
struct sgp41_handle_s sgp41_handle_t
sgp41 handle structure definition
uint8_t sgp41_init(sgp41_handle_t *handle)
initialize the chip
uint8_t sgp41_get_measure_raw_without_compensation(sgp41_handle_t *handle, uint16_t *sraw_voc, uint16_t *sraw_nox)
get the measure raw result without compensation
uint8_t sgp41_humidity_convert_to_register(sgp41_handle_t *handle, float rh, uint16_t *reg)
convert the humidity to the register data
uint8_t sgp41_interface_iic_read_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus read command
void sgp41_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t sgp41_interface_iic_write_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus write command
void sgp41_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t sgp41_interface_iic_deinit(void)
interface iic bus deinit
uint8_t sgp41_interface_iic_init(void)
interface iic bus init
uint8_t sgp41_read_test(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]