forked from EmbeddedTeam/app_photomagnetic
46 lines
1.0 KiB
C++
46 lines
1.0 KiB
C++
#ifndef THER_NTC_HPP
|
|
#define THER_NTC_HPP
|
|
#include <zephyr/device.h>
|
|
#include <zephyr/drivers/sensor.h>
|
|
#include <zpp/result.hpp>
|
|
#include <zpp/value.hpp>
|
|
|
|
namespace ther {
|
|
|
|
template <device *...kNtcDev> class Ntc {
|
|
public:
|
|
/// Read all NTC sensors, return the maximum temperature value.
|
|
/// Returns error if every sensor read fails.
|
|
static auto GetSensorValue()
|
|
-> zpp::result<zpp::value<zpp::value_type::AMBIENT_TEMP>> {
|
|
bool found = false;
|
|
zpp::value<zpp::value_type::AMBIENT_TEMP> max_val;
|
|
|
|
for (const auto *dev : s_devices) {
|
|
if (0 != sensor_sample_fetch(dev))
|
|
continue;
|
|
|
|
zpp::value<zpp::value_type::AMBIENT_TEMP> val;
|
|
if (0 != sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &val))
|
|
continue;
|
|
|
|
if (!found || val.to_double() > max_val.to_double()) {
|
|
max_val = val;
|
|
found = true;
|
|
}
|
|
}
|
|
|
|
if (found)
|
|
return max_val;
|
|
|
|
return zpp::error_code::k_io;
|
|
}
|
|
|
|
private:
|
|
static constexpr device *s_devices[] = {kNtcDev...};
|
|
};
|
|
|
|
} // namespace ther
|
|
|
|
#endif // THER_NTC_HPP
|