Extract part of functionality into `FunctionalSensorImpl` template class #7180

This commit is contained in:
kifir 2024-12-20 23:41:42 +02:00 committed by rusefillc
parent 1aaea76ac9
commit c8a72d3c76
7 changed files with 88 additions and 49 deletions

View File

@ -2,25 +2,10 @@
* @file functional_sensor.cpp
*/
#include "pch.h"
#include "functional_sensor.h"
void FunctionalSensor::postRawValue(float inputValue, efitick_t timestamp) {
// If no function is set, this sensor isn't valid.
if (!m_function) {
invalidate(UnexpectedCode::Configuration);
return;
}
m_rawValue = inputValue;
auto r = m_function->convert(inputValue);
// This has to happen so that we set the valid bit after
// the value is stored, to prevent the data race of reading
// an old invalid value
if (r.Valid) {
setValidValue(r.Value, timestamp);
} else {
invalidate(r.Code);
}
}
FunctionalSensor::FunctionalSensor(const SensorType type, const efidur_t timeoutPeriod)
: FunctionalSensorImpl(type, timeoutPeriod) {
}

View File

@ -8,9 +8,7 @@
#pragma once
#include "converters/sensor_converter_func.h"
#include "functional_sensor_base.h"
#include <type_traits>
#include "functional_sensor_impl.h"
/**
* @brief Class for sensors that convert from some raw floating point
@ -23,30 +21,9 @@
* Register an instance of the new class with an interface
* that provides and posts raw values so the sensor can update.
*/
class FunctionalSensor : public FunctionalSensorBase {
class FunctionalSensor : public FunctionalSensorImpl<SensorConverter> {
public:
FunctionalSensor(SensorType type, efidur_t timeoutPeriod)
: FunctionalSensorBase(type, timeoutPeriod) { }
void postRawValue(float inputValue, efitick_t timestamp) override;
void setFunction(SensorConverter& func) {
m_function = &func;
}
SensorConverter* getFunction() const {
return m_function;
}
float getRaw() const override final {
return m_rawValue;
}
FunctionalSensor(SensorType type, efidur_t timeoutPeriod);
void showInfo(const char* sensorName) const override;
private:
// Conversion function for this sensor
SensorConverter* m_function = nullptr;
float m_rawValue = 0;
};

View File

@ -0,0 +1,7 @@
//
// Created by kifir on 12/20/24.
//
#include "pch.h"
#include "functional_sensor_impl.h"

View File

@ -0,0 +1,28 @@
//
// Created by kifir on 12/20/24.
//
#pragma once
#include "functional_sensor_base.h"
template<class ConverterType>
class FunctionalSensorImpl : public FunctionalSensorBase {
public:
FunctionalSensorImpl(SensorType type, efidur_t timeoutPeriod);
void postRawValue(float inputValue, efitick_t timestamp) override;
void setFunction(ConverterType& func);
ConverterType* getFunction() const;
float getRaw() const override final;
private:
// Conversion function for this sensor
ConverterType* m_function = nullptr;
float m_rawValue = 0;
};
#include "functional_sensor_impl.hpp"

View File

@ -0,0 +1,41 @@
template<class ConverterType>
FunctionalSensorImpl<ConverterType>::FunctionalSensorImpl(const SensorType type, const efidur_t timeoutPeriod)
: FunctionalSensorBase(type, timeoutPeriod) {
}
template<class ConverterType>
void FunctionalSensorImpl<ConverterType>::setFunction(ConverterType& func) {
m_function = &func;
}
template<class ConverterType>
ConverterType* FunctionalSensorImpl<ConverterType>::getFunction() const {
return m_function;
}
template<class ConverterType>
float FunctionalSensorImpl<ConverterType>::getRaw() const {
return m_rawValue;
}
template<class ConverterType>
void FunctionalSensorImpl<ConverterType>::postRawValue(const float inputValue, const efitick_t timestamp) {
// If no function is set, this sensor isn't valid.
if (!m_function) {
invalidate(UnexpectedCode::Configuration);
return;
}
m_rawValue = inputValue;
auto r = m_function->convert(inputValue);
// This has to happen so that we set the valid bit after
// the value is stored, to prevent the data race of reading
// an old invalid value
if (r.Valid) {
setValidValue(r.Value, timestamp);
} else {
invalidate(r.Code);
}
}

View File

@ -24,11 +24,11 @@ void ProxySensor::showInfo(const char* sensorName) const {
void FunctionalSensor::showInfo(const char* sensorName) const {
const auto value = get();
efiPrintf("Sensor \"%s\": Raw value: %.2f Valid: %s Converted value %.2f", sensorName, m_rawValue, boolToString(value.Valid), value.Value);
efiPrintf("Sensor \"%s\": Raw value: %.2f Valid: %s Converted value %.2f", sensorName, getRaw(), boolToString(value.Valid), value.Value);
// now print out the underlying function's info
if (auto func = m_function) {
func->showInfo(m_rawValue);
if (auto func = getFunction()) {
func->showInfo(getRaw());
}
}

View File

@ -1,6 +1,7 @@
CONTROLLERS_SENSORS_SRC_CPP = \
$(PROJECT_DIR)/controllers/sensors/core/functional_sensor_base.cpp \
$(PROJECT_DIR)/controllers/sensors/core/functional_sensor_impl.cpp \
$(PROJECT_DIR)/controllers/sensors/core/functional_sensor.cpp \
$(PROJECT_DIR)/controllers/sensors/core/sensor.cpp \
$(PROJECT_DIR)/controllers/sensors/thermistors.cpp \