Firmware SDK
twr_sgpc3.h
1 #ifndef _TWR_SGPC3_H
2 #define _TWR_SGPC3_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_sgpc3_t twr_sgpc3_t;
26 
28 
29 typedef enum
30 {
31  TWR_SGPC3_STATE_ERROR = -1,
32  TWR_SGPC3_STATE_INITIALIZE = 0,
33  TWR_SGPC3_STATE_GET_FEATURE_SET = 1,
34  TWR_SGPC3_STATE_READ_FEATURE_SET = 2,
35  TWR_SGPC3_STATE_SET_POWER_MODE = 3,
36  TWR_SGPC3_STATE_INIT_AIR_QUALITY = 4,
37  TWR_SGPC3_STATE_SET_HUMIDITY = 5,
38  TWR_SGPC3_STATE_MEASURE_AIR_QUALITY = 6,
39  TWR_SGPC3_STATE_READ_AIR_QUALITY = 7
40 
41 } twr_sgpc3_state_t;
42 
43 struct twr_sgpc3_t
44 {
45  twr_i2c_channel_t _i2c_channel;
46  uint8_t _i2c_address;
47  twr_scheduler_task_id_t _task_id_interval;
48  twr_scheduler_task_id_t _task_id_measure;
49  void (*_event_handler)(twr_sgpc3_t *, twr_sgpc3_event_t, void *);
50  void *_event_param;
51  twr_tick_t _update_interval;
52  twr_sgpc3_state_t _state;
53  twr_tick_t _tick_ready;
54  twr_tick_t _tick_last_measurement;
55  bool _hit_error;
56  bool _measurement_valid;
57  uint16_t _tvoc;
58  uint16_t _ah_scaled;
59 };
60 
62 
67 
68 void twr_sgpc3_init(twr_sgpc3_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address);
69 
72 
73 void twr_sgpc3_deinit(twr_sgpc3_t *self);
74 
79 
80 void twr_sgpc3_set_event_handler(twr_sgpc3_t *self, void (*event_handler)(twr_sgpc3_t *, twr_sgpc3_event_t, void *), void *event_param);
81 
85 
87 
92 
93 bool twr_sgpc3_measure(twr_sgpc3_t *self);
94 
100 
101 bool twr_sgpc3_get_tvoc_ppb(twr_sgpc3_t *self, uint16_t *ppb);
102 
108 
109 float twr_sgpc3_set_compensation(twr_sgpc3_t *self, float *t_celsius, float *rh_percentage);
110 
112 
113 #endif // _TWR_SGPC3_H
twr_i2c_channel_t
I2C channels.
Definition: twr_i2c.h:16
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
Definition: twr_scheduler.h:22
twr_sgpc3_event_t
Callback events.
Definition: twr_sgpc3.h:14
void twr_sgpc3_set_update_interval(twr_sgpc3_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_sgpc3.c:46
float twr_sgpc3_set_compensation(twr_sgpc3_t *self, float *t_celsius, float *rh_percentage)
Set sensor compensation (absolute humidity is calculated from temperature and relative humidity)
Definition: twr_sgpc3.c:93
bool twr_sgpc3_measure(twr_sgpc3_t *self)
Start measurement manually.
Definition: twr_sgpc3.c:62
void twr_sgpc3_deinit(twr_sgpc3_t *self)
Deinitialize SGPC3.
Definition: twr_sgpc3.c:33
bool twr_sgpc3_get_tvoc_ppb(twr_sgpc3_t *self, uint16_t *ppb)
Get measured TVOC in ppb (parts per billion)
Definition: twr_sgpc3.c:81
void twr_sgpc3_init(twr_sgpc3_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize SGPC3.
Definition: twr_sgpc3.c:18
void twr_sgpc3_set_event_handler(twr_sgpc3_t *self, void(*event_handler)(twr_sgpc3_t *, twr_sgpc3_event_t, void *), void *event_param)
Set callback function.
Definition: twr_sgpc3.c:40
struct twr_sgpc3_t twr_sgpc3_t
SGPC3 instance.
Definition: twr_sgpc3.h:25
@ TWR_SGPC3_EVENT_UPDATE
Update event.
Definition: twr_sgpc3.h:19
@ TWR_SGPC3_EVENT_ERROR
Error event.
Definition: twr_sgpc3.h:16
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16