LibDriver SGP41
Loading...
Searching...
No Matches
driver_sgp41_basic.c
Go to the documentation of this file.
1
36
37#include "driver_sgp41_basic.h"
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
51uint8_t sgp41_basic_init(void)
52{
53 uint8_t res;
54
55 /* link functions */
63
64 /* sgp41 init */
65 res = sgp41_init(&gs_handle);
66 if (res != 0)
67 {
68 sgp41_interface_debug_print("sgp41: init failed.\n");
69
70 return 1;
71 }
72
73 /* soft reset */
74 res = sgp41_soft_reset(&gs_handle);
75 if (res != 0)
76 {
77 sgp41_interface_debug_print("sgp41: soft failed.\n");
78 (void)sgp41_deinit(&gs_handle);
79
80 return 1;
81 }
82
83 /* voc algorithm init */
85
86 /* nox algorithm init */
88
89 return 0;
90}
91
99uint8_t sgp41_basic_deinit(void)
100{
101 /* close sgp41 */
102 if (sgp41_deinit(&gs_handle) != 0)
103 {
104 return 1;
105 }
106
107 return 0;
108}
109
121uint8_t sgp41_basic_read(float temperature, float humidity, int32_t *voc_gas_index, int32_t *nox_gas_index)
122{
123 uint8_t res;
124 uint16_t raw_humidity;
125 uint16_t raw_temperature;
126 uint16_t sraw_voc;
127 uint16_t sraw_nox;
128
129 /* humidity convert to register */
130 res = sgp41_humidity_convert_to_register(&gs_handle, humidity, &raw_humidity);
131 if (res != 0)
132 {
133 sgp41_interface_debug_print("sgp41: humidity convert to register failed.\n");
134
135 return 1;
136 }
137
138 /* temperature convert to register */
139 res = sgp41_temperature_convert_to_register(&gs_handle, temperature, &raw_temperature);
140 if (res != 0)
141 {
142 sgp41_interface_debug_print("sgp41: temperature convert to register failed.\n");
143
144 return 1;
145 }
146
147 /* get measure raw */
148 res = sgp41_get_measure_raw(&gs_handle, raw_humidity, raw_temperature, &sraw_voc, &sraw_nox);
149 if (res != 0)
150 {
151 sgp41_interface_debug_print("sgp41: get measure raw failed.\n");
152
153 return 1;
154 }
155
156 /* algorithm process */
157 sgp41_algorithm_process(&gs_voc_handle, sraw_voc, voc_gas_index);
158
159 /* algorithm process */
160 sgp41_algorithm_process(&gs_nox_handle, sraw_nox, nox_gas_index);
161
162 return 0;
163}
164
174uint8_t sgp41_basic_read_without_compensation(int32_t *voc_gas_index, int32_t *nox_gas_index)
175{
176 uint8_t res;
177 uint16_t sraw_voc;
178 uint16_t sraw_nox;
179
180 /* get measure raw */
181 res = sgp41_get_measure_raw_without_compensation(&gs_handle, &sraw_voc, &sraw_nox);
182 if (res != 0)
183 {
184 sgp41_interface_debug_print("sgp41: get measure raw without compensation failed.\n");
185
186 return 1;
187 }
188
189 /* algorithm process */
190 sgp41_algorithm_process(&gs_voc_handle, sraw_voc, voc_gas_index);
191
192 /* algorithm process */
193 sgp41_algorithm_process(&gs_nox_handle, sraw_nox, nox_gas_index);
194
195 return 0;
196}
197
206uint8_t sgp41_basic_get_serial_id(uint16_t id[3])
207{
208 uint8_t res;
209
210 /* get serial id */
211 res = sgp41_get_serial_id(&gs_handle, id);
212 if (res != 0)
213 {
214 sgp41_interface_debug_print("sgp41: get serial id failed.\n");
215
216 return 1;
217 }
218
219 return 0;
220}
driver sgp41 algorithm header file
driver sgp41 basic 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_deinit(sgp41_handle_t *handle)
close the chip
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_get_serial_id(sgp41_handle_t *handle, uint16_t id[3])
get the chip serial id
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_basic_read_without_compensation(int32_t *voc_gas_index, int32_t *nox_gas_index)
basic example read without compensation
uint8_t sgp41_basic_read(float temperature, float humidity, int32_t *voc_gas_index, int32_t *nox_gas_index)
basic example read
uint8_t sgp41_basic_get_serial_id(uint16_t id[3])
basic example get serial id
uint8_t sgp41_basic_init(void)
basic example init
uint8_t sgp41_basic_deinit(void)
basic example deinit
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