test_led_strip/include/infrared.hpp

50 lines
1.2 KiB
C++

#ifndef __THER_INFRARED_HPP__
#define __THER_INFRARED_HPP__
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zpp/result.hpp>
#include <zpp/value.hpp>
#include <zpp/work_queue.hpp>
namespace ther {
template <device *kDev> class Infrared {
public:
static auto Init() -> zpp::error {
static sensor_trigger tri{.type = SENSOR_TRIG_DATA_READY,
.chan = SENSOR_CHAN_AMBIENT_TEMP};
if (auto r = sensor_trigger_set(
kDev, &tri,
[](const device *dev, const sensor_trigger *trig) {
s_is_data_ready = true;
});
r != 0) {
return -ENODEV;
};
}
static auto GetSensorValue()
-> zpp::result<zpp::value<zpp::value_type::AMBIENT_TEMP>> {
if (!s_is_data_ready) {
return zpp::error_code::k_nodata;
}
if (0 != sensor_sample_fetch(kDev)) {
return zpp::error_code::k_io;
}
zpp::value<zpp::value_type::AMBIENT_TEMP> val;
if (0 != sensor_channel_get(kDev, SENSOR_CHAN_AMBIENT_TEMP, &val)) {
return zpp::error_code::k_io;
}
s_is_data_ready = false;
return val;
}
private:
inline static bool s_is_data_ready{false};
};
} // namespace ther
#endif