1 #include <twr_module_co2.h>
2 #include <twr_scheduler.h>
4 #include <twr_tca9534a.h>
5 #include <twr_sc16is740.h>
7 #define _TWR_MODULE_CO2_I2C_GPIO_EXPANDER_ADDRESS 0x38
8 #define _TWR_MODULE_CO2_I2C_UART_ADDRESS 0x4d
9 #define _TWR_MODULE_CO2_PIN_DEFAULT (~(1 << 0) & ~(1 << 4))
10 #define _TWR_MODULE_CO2_PIN_CAP_ON (~(1 << 1))
11 #define _TWR_MODULE_CO2_PIN_VDD2_ON (~(1 << 2))
12 #define _TWR_MODULE_CO2_PIN_EN (~(1 << 3))
13 #define _TWR_MODULE_CO2_PIN_UART_RESET (~(1 << 6))
14 #define _TWR_MODULE_CO2_PIN_RDY TWR_TCA9534A_PIN_P7
18 TWR_MODULE_CO2_STATE_ERROR = -1,
19 TWR_MODULE_CO2_STATE_INITIALIZE = 0,
20 TWR_MODULE_CO2_STATE_INITIALIZE1,
21 TWR_MODULE_CO2_STATE_INITIALIZE2,
22 TWR_MODULE_CO2_STATE_READY,
23 TWR_MODULE_CO2_STATE_CHARGE,
24 TWR_MODULE_CO2_STATE_BOOT,
25 TWR_MODULE_CO2_STATE_BOOT_READ,
26 TWR_MODULE_CO2_STATE_MEASURE,
27 TWR_MODULE_CO2_STATE_MEASURE_READ,
29 } twr_module_co2_state_t;
31 static bool _twr_module_co2_init(
void);
32 static bool _twr_module_co2_charge_enable(
bool state);
33 static bool _twr_module_co2_device_enable(
bool state);
34 static bool _twr_module_co2_read_signal_rdy(
int *value);
35 static bool _twr_module_co2_uart_enable(
bool state);
36 static size_t _twr_module_co2_uart_write(uint8_t *buffer,
size_t length);
37 static size_t _twr_module_co2_uart_read(uint8_t *buffer,
size_t length);
42 twr_sc16is740_t sc16is740;
48 .init = _twr_module_co2_init,
49 .charge_enable = _twr_module_co2_charge_enable,
50 .device_enable = _twr_module_co2_device_enable,
51 .read_signal_rdy = _twr_module_co2_read_signal_rdy,
52 .uart_enable = _twr_module_co2_uart_enable,
53 .uart_write = _twr_module_co2_uart_write,
54 .uart_read = _twr_module_co2_uart_read
60 twr_lp8_init(&_twr_module_co2.sensor, &_twr_module_co2.driver);
93 static bool _twr_module_co2_init(
void)
112 for (
volatile int i = 0; i < 100; i++)
123 for (
volatile int i = 0; i < 1000; i++)
136 static bool _twr_module_co2_charge_enable(
bool state)
138 uint8_t direction = _TWR_MODULE_CO2_PIN_DEFAULT;
142 direction &= _TWR_MODULE_CO2_PIN_VDD2_ON & _TWR_MODULE_CO2_PIN_CAP_ON;
148 static bool _twr_module_co2_device_enable(
bool state)
150 uint8_t direction = _TWR_MODULE_CO2_PIN_DEFAULT;
154 direction &= _TWR_MODULE_CO2_PIN_VDD2_ON & _TWR_MODULE_CO2_PIN_CAP_ON & _TWR_MODULE_CO2_PIN_EN;
160 static bool _twr_module_co2_read_signal_rdy(
int *value)
170 static bool _twr_module_co2_uart_enable(
bool state)
180 static size_t _twr_module_co2_uart_write(uint8_t *buffer,
size_t length)
185 static size_t _twr_module_co2_uart_read(uint8_t *buffer,
size_t length)
@ TWR_I2C_I2C0
I2C channel I2C0.
void twr_lp8_set_event_handler(twr_lp8_t *self, void(*event_handler)(twr_lp8_event_t, void *), void *event_param)
Set callback function.
bool twr_lp8_measure(twr_lp8_t *self)
Start measurement manually.
void twr_lp8_calibration(twr_lp8_t *self, twr_lp8_calibration_t calibration)
Request sensor calibration.
void twr_lp8_set_update_interval(twr_lp8_t *self, twr_tick_t interval)
Set measurement interval.
void twr_lp8_init(twr_lp8_t *self, const twr_lp8_driver_t *driver)
Initialize LP8.
bool twr_lp8_get_concentration_ppm(twr_lp8_t *self, float *ppm)
Get CO2 concentration in ppm (parts per million)
twr_lp8_event_t
Callback events.
bool twr_lp8_get_error(twr_lp8_t *self, twr_lp8_error_t *error)
Get last error code.
twr_lp8_calibration_t
Calibration.
struct twr_lp8_t twr_lp8_t
LP8 instance.
void twr_module_co2_init(void)
Initialize HARDWARIO CO2 Module.
bool twr_module_co2_get_concentration_ppm(float *ppm)
Get CO2 concentration in ppm (parts per million)
void twr_module_co2_set_update_interval(twr_tick_t interval)
Set measurement interval.
twr_module_co2_event_t
Callback events.
bool twr_module_co2_measure(void)
Start measurement manually.
void twr_module_co2_calibration(twr_lp8_calibration_t calibration)
Request sensor calibration.
bool twr_module_co2_get_error(twr_lp8_error_t *error)
Get last error code.
void twr_module_co2_set_event_handler(void(*event_handler)(twr_module_co2_event_t, void *), void *event_param)
Set callback function.
bool twr_sc16is740_reset_fifo(twr_sc16is740_t *self, twr_sc16is740_fifo_t fifo)
Reset FIFO.
size_t twr_sc16is740_write(twr_sc16is740_t *self, uint8_t *buffer, size_t length)
Write.
size_t twr_sc16is740_read(twr_sc16is740_t *self, uint8_t *buffer, size_t length, twr_tick_t timeout)
Read.
bool twr_sc16is740_init(twr_sc16is740_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
SC16IS740 instance.
bool twr_tca9534a_write_port(twr_tca9534a_t *self, uint8_t state)
Write state to all pins.
bool twr_tca9534a_read_pin(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, int *state)
Read pin state.
bool twr_tca9534a_init(twr_tca9534a_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TCA9534A.
bool twr_tca9534a_set_port_direction(twr_tca9534a_t *self, uint8_t direction)
Set direction of all pins.
uint64_t twr_tick_t
Timestamp data type.