Firmware SDK
twr_sgp30.h
1 #ifndef _TWR_SGP30_H
2 #define _TWR_SGP30_H
3 
4 #include <twr_i2c.h>
5 #include <twr_scheduler.h>
6 
10 
12 
13 typedef enum
14 {
17 
20 
22 
24 
25 typedef struct twr_sgp30_t twr_sgp30_t;
26 
28 
29 typedef enum
30 {
31  TWR_SGP30_STATE_ERROR = -1,
32  TWR_SGP30_STATE_INITIALIZE = 0,
33  TWR_SGP30_STATE_GET_FEATURE_SET = 1,
34  TWR_SGP30_STATE_READ_FEATURE_SET = 2,
35  TWR_SGP30_STATE_INIT_AIR_QUALITY = 3,
36  TWR_SGP30_STATE_SET_HUMIDITY = 4,
37  TWR_SGP30_STATE_MEASURE_AIR_QUALITY = 5,
38  TWR_SGP30_STATE_READ_AIR_QUALITY = 6
39 
40 } twr_sgp30_state_t;
41 
42 struct twr_sgp30_t
43 {
44  twr_i2c_channel_t _i2c_channel;
45  uint8_t _i2c_address;
46  twr_scheduler_task_id_t _task_id_interval;
47  twr_scheduler_task_id_t _task_id_measure;
48  void (*_event_handler)(twr_sgp30_t *, twr_sgp30_event_t, void *);
49  void *_event_param;
50  twr_tick_t _update_interval;
51  twr_sgp30_state_t _state;
52  twr_tick_t _tick_ready;
53  twr_tick_t _tick_last_measurement;
54  bool _hit_error;
55  bool _measurement_valid;
56  uint16_t _co2eq;
57  uint16_t _tvoc;
58  uint16_t _ah_scaled;
59 };
60 
62 
67 
68 void twr_sgp30_init(twr_sgp30_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address);
69 
72 
73 void twr_sgp30_deinit(twr_sgp30_t *self);
74 
79 
80 void twr_sgp30_set_event_handler(twr_sgp30_t *self, void (*event_handler)(twr_sgp30_t *, twr_sgp30_event_t, void *), void *event_param);
81 
85 
87 
92 
93 bool twr_sgp30_measure(twr_sgp30_t *self);
94 
100 
101 bool twr_sgp30_get_co2eq_ppm(twr_sgp30_t *self, uint16_t *ppm);
102 
108 
109 bool twr_sgp30_get_tvoc_ppb(twr_sgp30_t *self, uint16_t *ppb);
110 
116 
117 float twr_sgp30_set_compensation(twr_sgp30_t *self, float *t_celsius, float *rh_percentage);
118 
120 
121 #endif // _TWR_SGP30_H
void twr_sgp30_init(twr_sgp30_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize SGP30.
Definition: twr_sgp30.c:17
struct twr_sgp30_t twr_sgp30_t
SGP30 instance.
Definition: twr_sgp30.h:25
twr_sgp30_event_t
Callback events.
Definition: twr_sgp30.h:13
bool twr_sgp30_get_co2eq_ppm(twr_sgp30_t *self, uint16_t *ppm)
Get measured CO2eq in ppm (parts per million)
Definition: twr_sgp30.c:80
void twr_sgp30_set_update_interval(twr_sgp30_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_sgp30.c:45
void twr_sgp30_deinit(twr_sgp30_t *self)
Deinitialize SGP30.
Definition: twr_sgp30.c:32
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
Definition: twr_scheduler.h:22
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16
Error event.
Definition: twr_sgp30.h:16
twr_i2c_channel_t
I2C channels.
Definition: twr_i2c.h:15
void twr_sgp30_set_event_handler(twr_sgp30_t *self, void(*event_handler)(twr_sgp30_t *, twr_sgp30_event_t, void *), void *event_param)
Set callback function.
Definition: twr_sgp30.c:39
bool twr_sgp30_measure(twr_sgp30_t *self)
Start measurement manually.
Definition: twr_sgp30.c:61
Update event.
Definition: twr_sgp30.h:19
float twr_sgp30_set_compensation(twr_sgp30_t *self, float *t_celsius, float *rh_percentage)
Set sensor compensation (absolute humidity is calculated from temperature and relative humidity) ...
Definition: twr_sgp30.c:104
bool twr_sgp30_get_tvoc_ppb(twr_sgp30_t *self, uint16_t *ppb)
Get measured TVOC in ppb (parts per billion)
Definition: twr_sgp30.c:92