rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
stored_value_sensor.h
Go to the documentation of this file.
1/**
2 * @file stored_value_sensor.h
3 * @brief Base class for a sensor that has its value asynchronously
4 * set, then later retrieved by a consumer.
5 *
6 * @date September 12, 2019
7 * @author Matthew Kennedy, (c) 2019-2020
8 */
9
10#pragma once
11
12#include "sensor.h"
13
14#include "efitime.h"
15
16/**
17 * @brief Base class for sensors that compute a value on one thread, and want
18 * to make it available to consumers asynchronously.
19 *
20 * Common examples include sensors that have to do heavy lifting to produce
21 * a reading, and don't want to perform that conversion at the time of
22 * consumption.
23 *
24 * To use this class, create a class for your sensor that inherits StoredValueSensor,
25 * and call Invalidate() and SetValidValue(float) as appropriate when readings are available
26 * (or known to be invalid) for your sensor.
27 *
28 * Consumers will retrieve the last set (or invalidated) value.
29 */
30class StoredValueSensor : public Sensor {
31public:
32 SensorResult get() const final override {
33 auto result = m_result;
34
35 // Timeouts are disabled, return last value
37 return result;
38 }
39
40 // Result is already failed, return that so that we get the real error code instead of a timeout
41 if (!result) {
42 return result;
43 }
44
45 if (m_timeoutPeriod != 0) { // zero m_timeoutPeriod means value lasts forever
47 return UnexpectedCode::Timeout;
48 }
49 }
50
51 return result;
52 }
53
55 : Sensor(type)
56 , m_result(unexpected)
57 , m_timeoutPeriod(timeoutNt)
58 {
59 }
60
61 // Invalidate the stored value.
62 void invalidate() {
63 m_result = unexpected;
64 }
65
66 // Invalidate the stored value with an error code
67 void invalidate(UnexpectedCode why) {
68 m_result = why;
69 }
70
71 // A new reading is available: set and validate a new value for the sensor.
72 void setValidValue(float value, efitick_t timestamp) {
73 // Set value before valid - so we don't briefly have the valid bit set on an invalid value
74 m_result = value;
75 m_lastUpdate = timestamp;
76 }
77
78 void showInfo(const char* sensorName) const override;
79
80 virtual void setTimeout(int timeoutMs) {
81 m_timeoutPeriod = MS2NT(timeoutMs);
82 }
83
84private:
86
88 efitick_t m_lastUpdate = 0;
89};
static bool s_inhibitSensorTimeouts
Definition sensor.h:171
SensorType type() const
Definition sensor.h:162
Base class for sensors that compute a value on one thread, and want to make it available to consumers...
void showInfo(const char *sensorName) const override
void setValidValue(float value, efitick_t timestamp)
StoredValueSensor(SensorType type, efidur_t timeoutNt)
virtual void setTimeout(int timeoutMs)
void invalidate(UnexpectedCode why)
SensorResult get() const final override
efitick_t getTimeNowNt()
Definition efitime.cpp:19
efitick_t efidur_t
Base class for sensors. Inherit this class to implement a new type of sensor.
expected< float > SensorResult
Definition sensor.h:46
SensorType
Definition sensor_type.h:18