Extract part of functionality into `FunctionalSensorImpl` template class #7180
This commit is contained in:
parent
1aaea76ac9
commit
c8a72d3c76
|
@ -2,25 +2,10 @@
|
||||||
* @file functional_sensor.cpp
|
* @file functional_sensor.cpp
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "pch.h"
|
||||||
|
|
||||||
#include "functional_sensor.h"
|
#include "functional_sensor.h"
|
||||||
|
|
||||||
void FunctionalSensor::postRawValue(float inputValue, efitick_t timestamp) {
|
FunctionalSensor::FunctionalSensor(const SensorType type, const efidur_t timeoutPeriod)
|
||||||
// If no function is set, this sensor isn't valid.
|
: FunctionalSensorImpl(type, timeoutPeriod) {
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -8,9 +8,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "converters/sensor_converter_func.h"
|
#include "converters/sensor_converter_func.h"
|
||||||
#include "functional_sensor_base.h"
|
#include "functional_sensor_impl.h"
|
||||||
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Class for sensors that convert from some raw floating point
|
* @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
|
* Register an instance of the new class with an interface
|
||||||
* that provides and posts raw values so the sensor can update.
|
* that provides and posts raw values so the sensor can update.
|
||||||
*/
|
*/
|
||||||
class FunctionalSensor : public FunctionalSensorBase {
|
class FunctionalSensor : public FunctionalSensorImpl<SensorConverter> {
|
||||||
public:
|
public:
|
||||||
FunctionalSensor(SensorType type, efidur_t timeoutPeriod)
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void showInfo(const char* sensorName) const override;
|
void showInfo(const char* sensorName) const override;
|
||||||
|
|
||||||
private:
|
|
||||||
// Conversion function for this sensor
|
|
||||||
SensorConverter* m_function = nullptr;
|
|
||||||
|
|
||||||
float m_rawValue = 0;
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
//
|
||||||
|
// Created by kifir on 12/20/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "pch.h"
|
||||||
|
|
||||||
|
#include "functional_sensor_impl.h"
|
|
@ -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"
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,11 +24,11 @@ void ProxySensor::showInfo(const char* sensorName) const {
|
||||||
|
|
||||||
void FunctionalSensor::showInfo(const char* sensorName) const {
|
void FunctionalSensor::showInfo(const char* sensorName) const {
|
||||||
const auto value = get();
|
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
|
// now print out the underlying function's info
|
||||||
if (auto func = m_function) {
|
if (auto func = getFunction()) {
|
||||||
func->showInfo(m_rawValue);
|
func->showInfo(getRaw());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
CONTROLLERS_SENSORS_SRC_CPP = \
|
CONTROLLERS_SENSORS_SRC_CPP = \
|
||||||
$(PROJECT_DIR)/controllers/sensors/core/functional_sensor_base.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/functional_sensor.cpp \
|
||||||
$(PROJECT_DIR)/controllers/sensors/core/sensor.cpp \
|
$(PROJECT_DIR)/controllers/sensors/core/sensor.cpp \
|
||||||
$(PROJECT_DIR)/controllers/sensors/thermistors.cpp \
|
$(PROJECT_DIR)/controllers/sensors/thermistors.cpp \
|
||||||
|
|
Loading…
Reference in New Issue