Firmware SDK
twr_lp8.h
1 #ifndef _TWR_LP8_H
2 #define _TWR_LP8_H
3 
4 #include <twr_scheduler.h>
5 #include <twr_tick.h>
6 
10 
12 
13 typedef enum
14 {
17 
20 
22 
24 
25 typedef enum
26 {
29 
32 
35 
38 
41 
44 
46 
48 
49 typedef struct twr_lp8_t twr_lp8_t;
50 
52 
53 typedef struct
54 {
56  bool (*init)(void);
57 
59  bool (*charge_enable)(bool state);
60 
62  bool (*device_enable)(bool state);
63 
65  bool (*read_signal_rdy)(int *value);
66 
68  bool (*uart_enable)(bool state);
69 
71  size_t (*uart_write)(uint8_t *buffer, size_t length);
72 
74  size_t (*uart_read)(uint8_t *buffer, size_t length);
75 
77 
79 
80 typedef enum
81 {
82  TWR_LP8_STATE_ERROR = -1,
83  TWR_LP8_STATE_INITIALIZE = 0,
84  TWR_LP8_STATE_READY = 1,
85  TWR_LP8_STATE_PRECHARGE = 2,
86  TWR_LP8_STATE_CHARGE = 3,
87  TWR_LP8_STATE_BOOT = 4,
88  TWR_LP8_STATE_BOOT_READ = 5,
89  TWR_LP8_STATE_MEASURE = 6,
90  TWR_LP8_STATE_MEASURE_READ = 7
91 
92 } twr_lp8_state_t;
93 
94 typedef enum
95 {
96  TWR_LP8_ERROR_INITIALIZE = 0,
97  TWR_LP8_ERROR_PRECHARGE = 1,
98 
99  TWR_LP8_ERROR_CHARGE_CHARGE_ENABLE = 2,
100  TWR_LP8_ERROR_CHARGE_DEVICE_ENABLE = 3,
101 
102  TWR_LP8_ERROR_BOOT_SIGNAL_READY = 4,
103  TWR_LP8_ERROR_BOOT_TIMEOUT = 5,
104  TWR_LP8_ERROR_BOOT_UART_ENABLE = 6,
105  TWR_LP8_ERROR_BOOT_UART_WRITE = 7,
106 
107  TWR_LP8_ERROR_BOOT_READ_UART_ENABLE = 8,
108  TWR_LP8_ERROR_BOOT_READ_DEVICE_ADDRESS = 9,
109  TWR_LP8_ERROR_BOOT_READ_COMMAND = 10,
110  TWR_LP8_ERROR_BOOT_READ_CRC = 11,
111  TWR_LP8_ERROR_BOOT_READ_TIMEOUT = 12,
112 
113  TWR_LP8_ERROR_MEASURE_SIGNAL_RDY = 13,
114  TWR_LP8_ERROR_MEASURE_SIGNAL_RDY_TIMEOUT = 14,
115  TWR_LP8_ERROR_MEASURE_UART_ENABLE = 15,
116  TWR_LP8_ERROR_MEASURE_UART_WRITE = 16,
117 
118  TWR_LP8_ERROR_MEASURE_READ_UART_ENABLE = 17,
119  TWR_LP8_ERROR_MEASURE_READ_DEVICE_ENABLE = 18,
120  TWR_LP8_ERROR_MEASURE_READ_DEVICE_ADDRESS = 19,
121  TWR_LP8_ERROR_MEASURE_READ_COMMAND = 20,
122  TWR_LP8_ERROR_MEASURE_READ_CRC = 21,
123  TWR_LP8_ERROR_MEASURE_READ_CALIBRATION_RUN = 22,
124  TWR_LP8_ERROR_MEASURE_READ_STATUS1 = 23,
125  TWR_LP8_ERROR_MEASURE_READ_TIMEOUT = 24
126 
127 } twr_lp8_error_t;
128 
129 struct twr_lp8_t
130 {
131  const twr_lp8_driver_t *_driver;
132  twr_scheduler_task_id_t _task_id_interval;
133  twr_scheduler_task_id_t _task_id_measure;
134  void (*_event_handler)(twr_lp8_event_t, void *);
135  void *_event_param;
136  twr_tick_t _update_interval;
137  twr_lp8_state_t _state;
138  bool _first_measurement_done;
139  twr_tick_t _tick_timeout;
140  twr_tick_t _tick_calibration;
141  twr_lp8_calibration_t _calibration;
142  bool _calibration_run;
143  uint8_t _rx_buffer[49];
144  size_t _rx_buffer_length;
145  uint8_t _tx_buffer[33];
146  uint8_t _sensor_state[23];
147  bool _valid;
148  int16_t _concentration;
149  uint16_t _pressure;
150  twr_lp8_error_t _error;
151 
152 };
153 
155 
157 
158 void twr_lp8_init(twr_lp8_t *self, const twr_lp8_driver_t *driver);
159 
164 
165 void twr_lp8_set_event_handler(twr_lp8_t *self, void (*event_handler)(twr_lp8_event_t, void *), void *event_param);
166 
170 
171 void twr_lp8_set_update_interval(twr_lp8_t *self, twr_tick_t interval);
172 
177 
178 bool twr_lp8_measure(twr_lp8_t *self);
179 
185 
186 bool twr_lp8_get_concentration_ppm(twr_lp8_t *self, float *ppm);
187 
193 
194 bool twr_lp8_get_error(twr_lp8_t *self, twr_lp8_error_t *error);
195 
199 
200 void twr_lp8_calibration(twr_lp8_t *self, twr_lp8_calibration_t calibration);
201 
203 
204 #endif // _TWR_LP8_H
void twr_lp8_set_event_handler(twr_lp8_t *self, void(*event_handler)(twr_lp8_event_t, void *), void *event_param)
Set callback function.
Definition: twr_lp8.c:36
bool twr_lp8_measure(twr_lp8_t *self)
Start measurement manually.
Definition: twr_lp8.c:58
void twr_lp8_calibration(twr_lp8_t *self, twr_lp8_calibration_t calibration)
Request sensor calibration.
Definition: twr_lp8.c:97
void twr_lp8_set_update_interval(twr_lp8_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_lp8.c:42
void twr_lp8_init(twr_lp8_t *self, const twr_lp8_driver_t *driver)
Initialize LP8.
Definition: twr_lp8.c:20
bool twr_lp8_get_concentration_ppm(twr_lp8_t *self, float *ppm)
Get CO2 concentration in ppm (parts per million)
Definition: twr_lp8.c:76
twr_lp8_event_t
Callback events.
Definition: twr_lp8.h:14
bool twr_lp8_get_error(twr_lp8_t *self, twr_lp8_error_t *error)
Get last error code.
Definition: twr_lp8.c:90
twr_lp8_calibration_t
Calibration.
Definition: twr_lp8.h:26
struct twr_lp8_t twr_lp8_t
LP8 instance.
Definition: twr_lp8.h:49
@ TWR_LP8_EVENT_UPDATE
Update event.
Definition: twr_lp8.h:19
@ TWR_LP8_EVENT_ERROR
Error event.
Definition: twr_lp8.h:16
@ TWR_LP8_CALIBRATION_BACKGROUND_UNFILTERED
Background calibration using unfiltered data.
Definition: twr_lp8.h:28
@ TWR_LP8_CALIBRATION_BACKGROUND_FILTERED
Background calibration using filtered data.
Definition: twr_lp8.h:31
@ TWR_LP8_CALIBRATION_ABC
ABC (based on filtered data)
Definition: twr_lp8.h:40
@ TWR_LP8_CALIBRATION_ATWR_RF
ABC (based on filtered data) + reset filters.
Definition: twr_lp8.h:43
@ TWR_LP8_CALIBRATION_BACKGROUND_FILTERED_RF
Background calibration using filtered data + reset filters.
Definition: twr_lp8.h:37
@ TWR_LP8_CALIBRATION_BACKGROUND_UNFILTERED_RF
Background calibration using unfiltered data + reset filters.
Definition: twr_lp8.h:34
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
LP8 driver.
Definition: twr_lp8.h:54