Firmware SDK
twr_sps30.h
1 #ifndef _TWR_SPS30_H
2 #define _TWR_SPS30_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_sps30_t twr_sps30_t;
26 
28 
29 typedef enum
30 {
31  TWR_SPS30_STATE_ERROR = -1,
32  TWR_SPS30_STATE_READY = 0,
33  TWR_SPS30_STATE_INITIALIZE = 1,
34  TWR_SPS30_STATE_GET_SERIAL_NUMBER = 2,
35  TWR_SPS30_STATE_READ_SERIAL_NUMBER = 3,
36  TWR_SPS30_STATE_START_MEASUREMENT = 4,
37  TWR_SPS30_STATE_SET_DATAREADY_FLAG = 5,
38  TWR_SPS30_STATE_READ_DATAREADY_FLAG = 6,
39  TWR_SPS30_STATE_GET_MEASUREMENT_DATA = 7,
40  TWR_SPS30_STATE_READ_MEASUREMENT_DATA = 8,
41  TWR_SPS30_STATE_STOP_MEASUREMENT = 9
42 
43 } twr_sps30_state_t;
44 
46 
47 typedef struct
48 {
50  float mc_1p0;
51 
53  float mc_2p5;
54 
56  float mc_4p0;
57 
59  float mc_10p0;
60 
61 } twr_sps30_mass_concentration_t;
62 
64 
65 typedef struct
66 {
68  float nc_0p5;
69 
71  float nc_1p0;
72 
74  float nc_2p5;
75 
77  float nc_4p0;
78 
80  float nc_10p0;
81 
82 } twr_sps30_number_concentration_t;
83 
84 struct twr_sps30_t
85 {
86  twr_i2c_channel_t _i2c_channel;
87  uint8_t _i2c_address;
88  twr_scheduler_task_id_t _task_id_interval;
89  twr_scheduler_task_id_t _task_id_measure;
90  void (*_event_handler)(twr_sps30_t *, twr_sps30_event_t, void *);
91  void *_event_param;
92  twr_tick_t _update_interval;
93  twr_sps30_state_t _state;
94  bool _measurement_valid;
95  twr_sps30_mass_concentration_t _mass_concentration;
96  twr_sps30_number_concentration_t _number_concentration;
97  float _typical_particle_size;
98  twr_tick_t _startup_time;
99  twr_tick_t _start_time;
100 };
101 
103 
108 
109 void twr_sps30_init(twr_sps30_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address);
110 
115 
116 void twr_sps30_set_event_handler(twr_sps30_t *self, void (*event_handler)(twr_sps30_t *, twr_sps30_event_t, void *), void *event_param);
117 
121 
122 void twr_sps30_set_startup_time(twr_sps30_t *self, twr_tick_t startup_time);
123 
127 
129 
134 
135 bool twr_sps30_measure(twr_sps30_t *self);
136 
142 
143 bool twr_sps30_get_mass_concentration(twr_sps30_t *self, twr_sps30_mass_concentration_t *mass_concentration);
144 
150 
151 bool twr_sps30_get_number_concentration(twr_sps30_t *self, twr_sps30_number_concentration_t *number_concentration);
152 
158 
159 bool twr_sps30_get_typical_particle_size(twr_sps30_t *self, float *typical_particle_size);
160 
162 
163 #endif // _TWR_SPS30_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_sps30_event_t
Callback events.
Definition: twr_sps30.h:14
void twr_sps30_init(twr_sps30_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize SPS30.
Definition: twr_sps30.c:20
struct twr_sps30_t twr_sps30_t
SPS30 instance.
Definition: twr_sps30.h:25
bool twr_sps30_get_mass_concentration(twr_sps30_t *self, twr_sps30_mass_concentration_t *mass_concentration)
Get measured mass concentration in μg/m3.
Definition: twr_sps30.c:75
void twr_sps30_set_startup_time(twr_sps30_t *self, twr_tick_t startup_time)
Set startup time (how long the fan blows air before the measurement)
Definition: twr_sps30.c:41
void twr_sps30_set_update_interval(twr_sps30_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_sps30.c:46
bool twr_sps30_measure(twr_sps30_t *self)
Start measurement manually.
Definition: twr_sps30.c:60
void twr_sps30_set_event_handler(twr_sps30_t *self, void(*event_handler)(twr_sps30_t *, twr_sps30_event_t, void *), void *event_param)
Set callback function.
Definition: twr_sps30.c:35
bool twr_sps30_get_typical_particle_size(twr_sps30_t *self, float *typical_particle_size)
Get measured typical particle size in μm.
Definition: twr_sps30.c:106
bool twr_sps30_get_number_concentration(twr_sps30_t *self, twr_sps30_number_concentration_t *number_concentration)
Get measured number concentration in #/cm3.
Definition: twr_sps30.c:90
@ TWR_SPS30_EVENT_UPDATE
Update event.
Definition: twr_sps30.h:19
@ TWR_SPS30_EVENT_ERROR
Error event.
Definition: twr_sps30.h:16
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16