LibDriver SGP40
Loading...
Searching...
No Matches
driver_sgp40_read_test.c
Go to the documentation of this file.
1
36
39
40static sgp40_handle_t gs_handle;
41static sgp40_gas_index_algorithm_t gs_voc_handle;
42
51uint8_t sgp40_read_test(uint32_t times)
52{
53 uint8_t res;
54 uint32_t i;
55 sgp40_info_t info;
56
57 /* link functions */
65
66 /* sgp40 info */
67 res = sgp40_info(&info);
68 if (res != 0)
69 {
70 sgp40_interface_debug_print("sgp40: get info failed.\n");
71
72 return 1;
73 }
74 else
75 {
76 /* print chip information */
77 sgp40_interface_debug_print("sgp40: chip is %s.\n", info.chip_name);
78 sgp40_interface_debug_print("sgp40: manufacturer is %s.\n", info.manufacturer_name);
79 sgp40_interface_debug_print("sgp40: interface is %s.\n", info.interface);
80 sgp40_interface_debug_print("sgp40: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
81 sgp40_interface_debug_print("sgp40: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
82 sgp40_interface_debug_print("sgp40: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
83 sgp40_interface_debug_print("sgp40: max current is %0.2fmA.\n", info.max_current_ma);
84 sgp40_interface_debug_print("sgp40: max temperature is %0.1fC.\n", info.temperature_max);
85 sgp40_interface_debug_print("sgp40: min temperature is %0.1fC.\n", info.temperature_min);
86 }
87
88 /* start read test */
89 sgp40_interface_debug_print("sgp40: start read test.\n");
90
91 /* sgp40 init */
92 res = sgp40_init(&gs_handle);
93 if (res != 0)
94 {
95 sgp40_interface_debug_print("sgp40: init failed.\n");
96
97 return 1;
98 }
99
100 /* soft reset */
101 res = sgp40_soft_reset(&gs_handle);
102 if (res != 0)
103 {
104 sgp40_interface_debug_print("sgp40: soft failed.\n");
105 (void)sgp40_deinit(&gs_handle);
106
107 return 1;
108 }
109
110 /* voc algorithm init */
112
113 /* delay 1000ms */
115
116 /* measure raw test */
117 sgp40_interface_debug_print("sgp40: measure raw test.\n");
118
119 /* run times */
120 for (i = 0; i < times; i++)
121 {
122 uint16_t raw_humidity;
123 uint16_t raw_temperature;
124 uint16_t sraw_voc;
125 int32_t voc_gas_index;
126
127 /* humidity convert to register */
128 res = sgp40_humidity_convert_to_register(&gs_handle, 50.0f, &raw_humidity);
129 if (res != 0)
130 {
131 sgp40_interface_debug_print("sgp40: humidity convert to register failed.\n");
132 (void)sgp40_deinit(&gs_handle);
133
134 return 1;
135 }
136
137 /* temperature convert to register */
138 res = sgp40_temperature_convert_to_register(&gs_handle, 25.0f, &raw_temperature);
139 if (res != 0)
140 {
141 sgp40_interface_debug_print("sgp40: temperature convert to register failed.\n");
142 (void)sgp40_deinit(&gs_handle);
143
144 return 1;
145 }
146
147 /* get measure raw */
148 res = sgp40_get_measure_raw(&gs_handle, raw_humidity, raw_temperature, &sraw_voc);
149 if (res != 0)
150 {
151 sgp40_interface_debug_print("sgp40: get measure raw failed.\n");
152 (void)sgp40_deinit(&gs_handle);
153
154 return 1;
155 }
156
157 /* algorithm process */
158 sgp40_algorithm_process(&gs_voc_handle, sraw_voc, &voc_gas_index);
159
160 /* output */
161 sgp40_interface_debug_print("sgp40: raw voc is 0x%04X and voc gas index is %d.\n", sraw_voc, voc_gas_index);
162
163 /* delay 1000ms */
165 }
166
167 /* measure raw without compensation test */
168 sgp40_interface_debug_print("sgp40: measure raw without compensation test.\n");
169
170 /* run times */
171 for (i = 0; i < times; i++)
172 {
173 uint16_t sraw_voc;
174 int32_t voc_gas_index;
175
176 /* get measure raw */
177 res = sgp40_get_measure_raw_without_compensation(&gs_handle, &sraw_voc);
178 if (res != 0)
179 {
180 sgp40_interface_debug_print("sgp40: get measure raw without compensation failed.\n");
181 (void)sgp40_deinit(&gs_handle);
182
183 return 1;
184 }
185
186 /* algorithm process */
187 sgp40_algorithm_process(&gs_voc_handle, sraw_voc, &voc_gas_index);
188
189 /* output */
190 sgp40_interface_debug_print("sgp40: raw voc is 0x%04X and voc gas index is %d.\n", sraw_voc, voc_gas_index);
191
192 /* delay 1000ms */
194 }
195
196 /* finish read test */
197 sgp40_interface_debug_print("sgp40: finish read test.\n");
198 (void)sgp40_deinit(&gs_handle);
199
200 return 0;
201}
driver sgp40 algorithm header file
driver sgp40 read test header file
void sgp40_algorithm_init(sgp40_gas_index_algorithm_t *params, int32_t algorithm_type)
algorithm init
struct sgp40_gas_index_algorithm_s sgp40_gas_index_algorithm_t
sgp40 gas index algorithm structure definition
#define SGP40_ALGORITHM_TYPE_VOC
sgp40 algorithm type definition
void sgp40_algorithm_process(sgp40_gas_index_algorithm_t *params, int32_t sraw, int32_t *gas_index)
algorithm process
uint8_t sgp40_get_measure_raw(sgp40_handle_t *handle, uint16_t raw_humidity, uint16_t raw_temperature, uint16_t *sraw_voc)
get the measure raw result
uint8_t sgp40_init(sgp40_handle_t *handle)
initialize the chip
uint8_t sgp40_soft_reset(sgp40_handle_t *handle)
soft reset the chip
uint8_t sgp40_get_measure_raw_without_compensation(sgp40_handle_t *handle, uint16_t *sraw_voc)
get the measure raw result without compensation
uint8_t sgp40_info(sgp40_info_t *info)
get chip information
uint8_t sgp40_humidity_convert_to_register(sgp40_handle_t *handle, float rh, uint16_t *reg)
convert the humidity to the register data
uint8_t sgp40_deinit(sgp40_handle_t *handle)
close the chip
struct sgp40_handle_s sgp40_handle_t
sgp40 handle structure definition
uint8_t sgp40_temperature_convert_to_register(sgp40_handle_t *handle, float temp, uint16_t *reg)
convert the temperature to the register data
struct sgp40_info_s sgp40_info_t
sgp40 information structure definition
uint8_t sgp40_interface_iic_read_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus read command
uint8_t sgp40_interface_iic_deinit(void)
interface iic bus deinit
void sgp40_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t sgp40_interface_iic_init(void)
interface iic bus init
uint8_t sgp40_interface_iic_write_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus write command
void sgp40_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t sgp40_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]