5 #define _TWR_SHT30_DELAY_RUN 20
6 #define _TWR_SHT30_DELAY_INITIALIZATION 50
7 #define _TWR_SHT30_DELAY_MEASUREMENT 20
9 static void _twr_sht30_task_interval(
void *param);
11 static void _twr_sht30_task_measure(
void *param);
13 static bool _twr_sht30_write(
twr_sht30_t *
self,
const uint16_t data);
17 memset(
self, 0,
sizeof(*
self));
19 self->_i2c_channel = i2c_channel;
20 self->_i2c_address = i2c_address;
25 self->_tick_ready = _TWR_SHT30_DELAY_RUN;
32 _twr_sht30_write(
self, 0xa230);
39 self->_event_handler = event_handler;
40 self->_event_param = event_param;
45 self->_update_interval = interval;
61 if (self->_measurement_active)
66 self->_measurement_active =
true;
75 if (!self->_humidity_valid)
80 *raw =
self->_reg_humidity;
94 *percentage = 100.f * (float) raw / (65536.f - 1.f);
96 if (*percentage >= 100.f)
100 else if (*percentage <= 0.f)
110 if (!self->_temperature_valid)
115 *raw =
self->_reg_temperature;
129 *celsius = -45.f + (175.f * (float) raw / (65535.f));
143 *fahrenheit = celsius * 1.8f + 32.f;
157 *kelvin = celsius + 273.15f;
167 static void _twr_sht30_task_interval(
void *param)
176 static void _twr_sht30_task_measure(
void *param)
182 switch (self->_state)
184 case TWR_SHT30_STATE_ERROR:
186 self->_humidity_valid =
false;
187 self->_temperature_valid =
false;
189 self->_measurement_active =
false;
191 if (self->_event_handler != NULL)
196 self->_state = TWR_SHT30_STATE_INITIALIZE;
200 case TWR_SHT30_STATE_INITIALIZE:
202 self->_state = TWR_SHT30_STATE_ERROR;
204 if (!_twr_sht30_write(
self, 0xa230))
209 self->_state = TWR_SHT30_STATE_MEASURE;
211 self->_tick_ready =
twr_tick_get() + _TWR_SHT30_DELAY_INITIALIZATION;
213 if (self->_measurement_active)
220 case TWR_SHT30_STATE_MEASURE:
222 self->_state = TWR_SHT30_STATE_ERROR;
224 if (!_twr_sht30_write(
self, 0x0d2c))
229 self->_state = TWR_SHT30_STATE_READ;
235 case TWR_SHT30_STATE_READ:
237 self->_state = TWR_SHT30_STATE_ERROR;
245 transfer.
length =
sizeof(buffer);
252 if ((
twr_crc8(0x31, buffer, 2, 0xff) != buffer[2]) || (
twr_crc8(0x31, buffer + 3, 2, 0xff) != buffer[5]))
257 self->_reg_humidity = buffer[3] << 8 | buffer[4];
258 self->_reg_temperature = buffer[0] << 8 | buffer[1];
260 self->_humidity_valid =
true;
261 self->_temperature_valid =
true;
263 self->_state = TWR_SHT30_STATE_UPDATE;
267 case TWR_SHT30_STATE_UPDATE:
269 self->_measurement_active =
false;
271 if (self->_event_handler != NULL)
276 self->_state = TWR_SHT30_STATE_MEASURE;
282 self->_state = TWR_SHT30_STATE_ERROR;
289 static bool _twr_sht30_write(
twr_sht30_t *
self,
const uint16_t data)
294 transfer.
buffer = (
void *) &data;
295 transfer.
length =
sizeof(data);
uint8_t twr_crc8(const uint8_t polynomial, const void *buffer, size_t length, const uint8_t initialization)
Calculate CRC8.
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.
void twr_sht30_set_update_interval(twr_sht30_t *self, twr_tick_t interval)
Set measurement interval.
bool twr_sht30_get_temperature_celsius(twr_sht30_t *self, float *celsius)
Get measured temperature in degrees of Celsius.
bool twr_sht30_measure(twr_sht30_t *self)
Start measurement manually.
struct twr_sht30_t twr_sht30_t
SHT30 instance.
void twr_sht30_deinit(twr_sht30_t *self)
Deinitialize SHT30.
bool twr_sht30_get_temperature_kelvin(twr_sht30_t *self, float *kelvin)
Get measured temperature in kelvin.
bool twr_sht30_get_temperature_fahrenheit(twr_sht30_t *self, float *fahrenheit)
Get measured temperature in degrees of Fahrenheit.
twr_sht30_event_t
Callback events.
void twr_sht30_set_event_handler(twr_sht30_t *self, void(*event_handler)(twr_sht30_t *, twr_sht30_event_t, void *), void *event_param)
Set callback function.
bool twr_sht30_get_humidity_percentage(twr_sht30_t *self, float *percentage)
Get measured humidity as percentage.
void twr_sht30_init(twr_sht30_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize SHT30.
bool twr_sht30_get_temperature_raw(twr_sht30_t *self, uint16_t *raw)
Get measured temperature as raw value.
bool twr_sht30_get_humidity_raw(twr_sht30_t *self, uint16_t *raw)
Get measured humidity as raw value.
@ TWR_SHT30_EVENT_ERROR
Error event.
@ TWR_SHT30_EVENT_UPDATE
Update 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.