1 #include <twr_hdc2080.h>
4 #define _TWR_HDC2080_DELAY_RUN 50
5 #define _TWR_HDC2080_DELAY_INITIALIZATION 50
6 #define _TWR_HDC2080_DELAY_MEASUREMENT 50
8 static void _twr_hdc2080_task_interval(
void *param);
10 static void _twr_hdc2080_task_measure(
void *param);
14 memset(
self, 0,
sizeof(*
self));
16 self->_i2c_channel = i2c_channel;
17 self->_i2c_address = i2c_address;
22 self->_tick_ready = _TWR_HDC2080_DELAY_RUN;
38 self->_event_handler = event_handler;
39 self->_event_param = event_param;
44 self->_update_interval = interval;
60 if (self->_measurement_active)
65 self->_measurement_active =
true;
74 if (!self->_humidity_valid)
79 *raw =
self->_reg_humidity;
93 *percentage = (float) raw / 65536.f * 100.f;
95 if (*percentage >= 100.f)
105 if (!self->_temperature_valid)
110 *raw =
self->_reg_temperature;
124 *celsius = (float) raw / 65536.f * 165.f - 40.f;
129 static void _twr_hdc2080_task_interval(
void *param)
138 static void _twr_hdc2080_task_measure(
void *param)
144 switch (self->_state)
146 case TWR_HDC2080_STATE_ERROR:
148 self->_humidity_valid =
false;
149 self->_temperature_valid =
false;
151 self->_measurement_active =
false;
153 if (self->_event_handler != NULL)
158 self->_state = TWR_HDC2080_STATE_INITIALIZE;
162 case TWR_HDC2080_STATE_INITIALIZE:
164 self->_state = TWR_HDC2080_STATE_ERROR;
171 self->_state = TWR_HDC2080_STATE_MEASURE;
173 self->_tick_ready =
twr_tick_get() + _TWR_HDC2080_DELAY_INITIALIZATION;
175 if (self->_measurement_active)
182 case TWR_HDC2080_STATE_MEASURE:
184 self->_state = TWR_HDC2080_STATE_ERROR;
191 self->_state = TWR_HDC2080_STATE_READ;
197 case TWR_HDC2080_STATE_READ:
199 self->_state = TWR_HDC2080_STATE_ERROR;
201 uint8_t reg_interrupt;
208 if ((reg_interrupt & 0x80) == 0)
223 self->_reg_temperature =
self->_reg_temperature << 8 |
self->_reg_temperature >> 8;
224 self->_reg_humidity =
self->_reg_humidity << 8 |
self->_reg_humidity >> 8;
226 self->_temperature_valid =
true;
227 self->_humidity_valid =
true;
229 self->_state = TWR_HDC2080_STATE_UPDATE;
233 case TWR_HDC2080_STATE_UPDATE:
235 self->_measurement_active =
false;
237 if (self->_event_handler != NULL)
242 self->_state = TWR_HDC2080_STATE_MEASURE;
248 self->_state = TWR_HDC2080_STATE_ERROR;
twr_hdc2080_event_t
Callback events.
struct twr_hdc2080_t twr_hdc2080_t
HDC2080 instance.
void twr_hdc2080_init(twr_hdc2080_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize HDC2080.
bool twr_hdc2080_get_temperature_celsius(twr_hdc2080_t *self, float *celsius)
Get measured temperature in degrees of Celsius.
bool twr_hdc2080_get_temperature_raw(twr_hdc2080_t *self, uint16_t *raw)
Get measured temperature as raw value.
void twr_hdc2080_deinit(twr_hdc2080_t *self)
Deinitialize HDC2080.
bool twr_hdc2080_measure(twr_hdc2080_t *self)
Start measurement manually.
bool twr_hdc2080_get_humidity_percentage(twr_hdc2080_t *self, float *percentage)
Get measured humidity as percentage.
bool twr_hdc2080_get_humidity_raw(twr_hdc2080_t *self, uint16_t *raw)
Get measured humidity as raw value.
void twr_hdc2080_set_update_interval(twr_hdc2080_t *self, twr_tick_t interval)
Set measurement interval.
void twr_hdc2080_set_event_handler(twr_hdc2080_t *self, void(*event_handler)(twr_hdc2080_t *, twr_hdc2080_event_t, void *), void *event_param)
Set callback function.
@ TWR_HDC2080_EVENT_UPDATE
Update event.
@ TWR_HDC2080_EVENT_ERROR
Error event.
void twr_i2c_init(twr_i2c_channel_t channel, twr_i2c_speed_t speed)
Initialize I2C channel.
bool twr_i2c_memory_write_8b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t data)
Memory write 1 byte to I2C channel.
bool twr_i2c_memory_read_16b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint16_t *data)
Memory read 2 bytes from I2C channel.
bool twr_i2c_memory_read_8b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t *data)
Memory read 1 byte from I2C channel.
twr_i2c_channel_t
I2C channels.
@ TWR_I2C_SPEED_400_KHZ
I2C communication speed is 400 kHz.
void twr_scheduler_plan_current_from_now(twr_tick_t tick)
Schedule current task to tick relative from now.
void twr_scheduler_plan_current_relative(twr_tick_t tick)
Schedule current task to tick relative from current spin.
void twr_scheduler_plan_current_absolute(twr_tick_t tick)
Schedule current task to absolute tick.
void twr_scheduler_plan_absolute(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to absolute tick.
void twr_scheduler_unregister(twr_scheduler_task_id_t task_id)
Unregister specified task.
void twr_scheduler_plan_relative(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to tick relative from current spin.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
#define TWR_TICK_INFINITY
Maximum timestamp value.
twr_tick_t twr_tick_get(void)
Get absolute timestamp since start of program.
uint64_t twr_tick_t
Timestamp data type.