embedded_raumsenor_lorawan/src/sensors/sht4x.c
2025-05-23 10:48:05 +02:00

55 lines
1.2 KiB
C

#include <sensors/sht4x.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/sensor/sht4x.h>
static struct device *sht;
void init_sht4x()
{
if (!DT_HAS_COMPAT_STATUS_OKAY(sensirion_sht4x)) {
LOG_ERR("No sensirion,sht4x compatible node found in the device tree");
return;
}
sht = DEVICE_DT_GET_ANY(sensirion_sht4x);
if (!device_is_ready(sht))
{
LOG_ERR("SHT4X-Device %s is not ready.\n", sht->name);
return;
}
}
void request_sensor_data_sht4x()
{
for (int i = 0; i < SHT4X_RETRY_COUNT; i++)
{
if (sensor_sample_fetch(sht) == 0)
{
LOG_INF("SHT4X sensor values successfully read after %d tries", i + 1);
return;
}
else
{
k_sleep(K_MSEC(100));
}
}
LOG_ERR("SHT4X reading sensor values failed %d times!", SHT4X_RETRY_COUNT);
}
float get_value_from_current_sample_sht4x(enum sensor_channel sensor)
{
struct sensor_value value;
float result;
if (sensor_channel_get(sht, sensor, &value) == 0) {
result = sensor_value_to_double(&value);
LOG_INF("Value '%s' successfully read [%f]", sensor, result);
} else {
LOG_ERR("Can't read sensor value '%s' from sample!", sensor);
return 0.0;
}
return value;
}