1 #include <twr_tmp112.h>
3 #define _TWR_TMP112_DELAY_RUN 50
4 #define _TWR_TMP112_DELAY_INITIALIZATION 50
5 #define _TWR_TMP112_DELAY_MEASUREMENT 50
7 static void _twr_tmp112_task_interval(
void *param);
9 static void _twr_tmp112_task_measure(
void *param);
13 memset(
self, 0,
sizeof(*
self));
15 self->_i2c_channel = i2c_channel;
16 self->_i2c_address = i2c_address;
21 self->_tick_ready = _TWR_TMP112_DELAY_RUN;
37 self->_event_handler = event_handler;
38 self->_event_param = event_param;
43 self->_update_interval = interval;
59 if (self->_measurement_active)
64 self->_measurement_active =
true;
73 if (!self->_temperature_valid)
78 *raw = (int16_t) self->_reg_temperature >> 4;
92 *celsius = (float) raw / 16.f;
106 *fahrenheit = celsius * 1.8f + 32.f;
120 *kelvin = celsius + 273.15f;
130 static void _twr_tmp112_task_interval(
void *param)
139 static void _twr_tmp112_task_measure(
void *param)
145 switch (self->_state)
147 case TWR_TMP112_STATE_ERROR:
149 self->_temperature_valid =
false;
151 self->_measurement_active =
false;
153 if (self->_event_handler != NULL)
158 self->_state = TWR_TMP112_STATE_INITIALIZE;
162 case TWR_TMP112_STATE_INITIALIZE:
164 self->_state = TWR_TMP112_STATE_ERROR;
171 self->_state = TWR_TMP112_STATE_MEASURE;
173 self->_tick_ready =
twr_tick_get() + _TWR_TMP112_DELAY_INITIALIZATION;
175 if (self->_measurement_active)
182 case TWR_TMP112_STATE_MEASURE:
184 self->_state = TWR_TMP112_STATE_ERROR;
191 self->_state = TWR_TMP112_STATE_READ;
197 case TWR_TMP112_STATE_READ:
199 self->_state = TWR_TMP112_STATE_ERROR;
201 uint8_t reg_configuration;
208 if ((reg_configuration & 0x81) != 0x81)
218 self->_temperature_valid =
true;
220 self->_state = TWR_TMP112_STATE_UPDATE;
224 case TWR_TMP112_STATE_UPDATE:
226 self->_measurement_active =
false;
228 if (self->_event_handler != NULL)
233 self->_state = TWR_TMP112_STATE_MEASURE;
239 self->_state = TWR_TMP112_STATE_ERROR;
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_write_16b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint16_t data)
Memory write 2 bytes 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.
bool twr_tmp112_get_temperature_kelvin(twr_tmp112_t *self, float *kelvin)
Get measured temperature in kelvin.
twr_tmp112_event_t
Callback events.
void twr_tmp112_set_update_interval(twr_tmp112_t *self, twr_tick_t interval)
Set measurement interval.
bool twr_tmp112_get_temperature_raw(twr_tmp112_t *self, int16_t *raw)
Get measured temperature as raw value.
void twr_tmp112_set_event_handler(twr_tmp112_t *self, void(*event_handler)(twr_tmp112_t *, twr_tmp112_event_t, void *), void *event_param)
Set callback function.
bool twr_tmp112_measure(twr_tmp112_t *self)
Start measurement manually.
struct twr_tmp112_t twr_tmp112_t
TMP112 instance.
void twr_tmp112_deinit(twr_tmp112_t *self)
Deinitialize TMP112.
bool twr_tmp112_get_temperature_celsius(twr_tmp112_t *self, float *celsius)
Get measured temperature in degrees of Celsius.
bool twr_tmp112_get_temperature_fahrenheit(twr_tmp112_t *self, float *fahrenheit)
Get measured temperature in degrees of Fahrenheit.
void twr_tmp112_init(twr_tmp112_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TMP112.
@ TWR_TMP112_EVENT_ERROR
Error event.
@ TWR_TMP112_EVENT_UPDATE
Update event.