3 #define _TWR_SHT20_DELAY_RUN 20
4 #define _TWR_SHT20_DELAY_INITIALIZATION 20
5 #define _TWR_SHT20_DELAY_MEASUREMENT_RH 50
6 #define _TWR_SHT20_DELAY_MEASUREMENT_T 100
8 static void _twr_sht20_task_interval(
void *param);
10 static void _twr_sht20_task_measure(
void *param);
12 static bool _twr_sht20_write(
twr_sht20_t *
self,
const uint8_t data);
18 memset(
self, 0,
sizeof(*
self));
20 self->_i2c_channel = i2c_channel;
21 self->_i2c_address = i2c_address;
26 self->_tick_ready = _TWR_SHT20_DELAY_RUN;
33 _twr_sht20_write(
self, 0xfe);
40 self->_event_handler = event_handler;
41 self->_event_param = event_param;
46 self->_update_interval = interval;
62 if (self->_measurement_active)
67 self->_measurement_active =
true;
76 if (!self->_humidity_valid)
81 *raw =
self->_reg_humidity;
95 *percentage = -6.f + 125.f * (float) raw / 65536.f;
97 if (*percentage >= 100.f)
101 else if (*percentage <= 0.f)
111 if (!self->_temperature_valid)
116 *raw =
self->_reg_temperature;
130 *celsius = -46.85f + 175.72f * (float) raw / 65536.f;
144 *fahrenheit = celsius * 1.8f + 32.f;
158 *kelvin = celsius + 273.15f;
168 static void _twr_sht20_task_interval(
void *param)
177 static void _twr_sht20_task_measure(
void *param)
183 switch (self->_state)
185 case TWR_SHT20_STATE_ERROR:
187 self->_humidity_valid =
false;
188 self->_temperature_valid =
false;
190 self->_measurement_active =
false;
192 if (self->_event_handler != NULL)
197 self->_state = TWR_SHT20_STATE_INITIALIZE;
201 case TWR_SHT20_STATE_INITIALIZE:
203 self->_state = TWR_SHT20_STATE_ERROR;
205 if (!_twr_sht20_write(
self, 0xfe))
210 self->_state = TWR_SHT20_STATE_MEASURE_RH;
212 self->_tick_ready =
twr_tick_get() + _TWR_SHT20_DELAY_INITIALIZATION;
214 if (self->_measurement_active)
221 case TWR_SHT20_STATE_MEASURE_RH:
223 self->_state = TWR_SHT20_STATE_ERROR;
225 if (!_twr_sht20_write(
self, 0xf5))
230 self->_state = TWR_SHT20_STATE_READ_RH;
236 case TWR_SHT20_STATE_READ_RH:
238 self->_state = TWR_SHT20_STATE_ERROR;
246 transfer.
length =
sizeof(buffer);
253 self->_reg_humidity = buffer[0] << 8 | buffer[1];
254 self->_reg_humidity &= ~0x3;
256 self->_humidity_valid =
true;
258 self->_state = TWR_SHT20_STATE_MEASURE_T;
262 case TWR_SHT20_STATE_MEASURE_T:
264 self->_state = TWR_SHT20_STATE_ERROR;
266 if (!_twr_sht20_write(
self, 0xf3))
271 self->_state = TWR_SHT20_STATE_READ_T;
277 case TWR_SHT20_STATE_READ_T:
279 self->_state = TWR_SHT20_STATE_ERROR;
287 transfer.
length =
sizeof(buffer);
294 self->_reg_temperature = buffer[0] << 8 | buffer[1];
295 self->_reg_temperature &= ~0x3;
297 self->_temperature_valid =
true;
299 self->_state = TWR_SHT20_STATE_UPDATE;
303 case TWR_SHT20_STATE_UPDATE:
305 self->_measurement_active =
false;
307 if (self->_event_handler != NULL)
312 self->_state = TWR_SHT20_STATE_MEASURE_RH;
318 self->_state = TWR_SHT20_STATE_ERROR;
325 static bool _twr_sht20_write(
twr_sht20_t *
self,
const uint8_t data)
330 transfer.
buffer = (
void *) &data;
331 transfer.
length =
sizeof(data);
void twr_i2c_init(twr_i2c_channel_t channel, twr_i2c_speed_t speed)
Initialize I2C channel.
bool twr_i2c_read(twr_i2c_channel_t channel, const twr_i2c_transfer_t *transfer)
Read from I2C channel.
bool twr_i2c_write(twr_i2c_channel_t channel, const twr_i2c_transfer_t *transfer)
Write to 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.
bool twr_sht20_get_temperature_celsius(twr_sht20_t *self, float *celsius)
Get measured temperature in degrees of Celsius.
struct twr_sht20_t twr_sht20_t
SHT20 instance.
void twr_sht20_init(twr_sht20_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize SHT20.
bool twr_sht20_measure(twr_sht20_t *self)
Start measurement manually.
void twr_sht20_set_update_interval(twr_sht20_t *self, twr_tick_t interval)
Set measurement interval.
bool twr_sht20_get_temperature_raw(twr_sht20_t *self, uint16_t *raw)
Get measured temperature as raw value.
bool twr_sht20_get_humidity_raw(twr_sht20_t *self, uint16_t *raw)
Get measured humidity as raw value.
bool twr_sht20_get_temperature_kelvin(twr_sht20_t *self, float *kelvin)
Get measured temperature in kelvin.
void twr_sht20_deinit(twr_sht20_t *self)
Deinitialize SHT20.
bool twr_sht20_get_humidity_percentage(twr_sht20_t *self, float *percentage)
Get measured humidity as percentage.
void twr_sht20_set_event_handler(twr_sht20_t *self, void(*event_handler)(twr_sht20_t *, twr_sht20_event_t, void *), void *event_param)
Set callback function.
twr_sht20_event_t
Callback events.
bool twr_sht20_get_temperature_fahrenheit(twr_sht20_t *self, float *fahrenheit)
Get measured temperature in degrees of Fahrenheit.
@ TWR_SHT20_EVENT_UPDATE
Update event.
@ TWR_SHT20_EVENT_ERROR
Error event.
#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.
void * buffer
Pointer to buffer which is being written or read.
uint8_t device_address
7-bit I2C device address
size_t length
Length of buffer which is being written or read.