rusefi-1/firmware/controllers/sensors/function_pointer_sensor.h

41 lines
925 B
C++

/**
* @file function_pointer_sensor.h
* @brief A sensor to provide a bridge from old getX()-style functions to the new sensor registry.
*
* @date September 12, 2019
* @author Matthew Kennedy, (c) 2019
*/
#pragma once
#include "sensor.h"
/* This class is intended as a bridge to bridge from old getMySensor() functions
* to the new system. This way, producers and consumers can be independently
* updated to the new system, with sensors being usable either way for some time.
*/
class FunctionPointerSensor final : public Sensor {
public:
FunctionPointerSensor(SensorType type, float (*func)())
: Sensor(type)
, m_func(func) {}
SensorResult get() const final {
float result = m_func();
// check for NaN
bool valid = !(result != result);
if (!valid) {
return unexpected;
}
return result;
}
void showInfo(const char* /*sensorName*/) const override {}
private:
float (*m_func)();
};