From fff58289ee791e9832dd963b4cd33f7afa61f7f7 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sat, 29 Aug 2020 16:22:57 -0400 Subject: [PATCH] OBD CAN sensors #1733 refactoring getters/setters --- firmware/controllers/sensors/sensor.cpp | 72 +++++++++++++++++-------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/firmware/controllers/sensors/sensor.cpp b/firmware/controllers/sensors/sensor.cpp index b328fe7bfb..ba08588aa5 100644 --- a/firmware/controllers/sensors/sensor.cpp +++ b/firmware/controllers/sensors/sensor.cpp @@ -6,10 +6,43 @@ // It stores whether the sensor should use a mock value, // the value to use, and if not a pointer to the sensor that // can provide a real value. -struct SensorRegistryEntry { - bool useMock; +class SensorRegistryEntry { +public: + Sensor *getSensor() { + return sensor; + } + + void setSensor(Sensor * sensor) { + this->sensor = sensor; + } + + bool isMock() { + return useMock; + } + + void setMockValue(float value) { + mockValue = value; + useMock = true; + } + + float getMockValue() { + return mockValue; + } + + void resetMock() { + useMock = false; + mockValue = 0.0f; + } + + void reset() { + sensor = nullptr; + resetMock(); + } + +private: + bool useMock = false; float mockValue; - Sensor *sensor; + Sensor *sensor = nullptr; }; static SensorRegistryEntry s_sensorRegistry[static_cast(SensorType::PlaceholderLast)] = {}; @@ -46,12 +79,12 @@ bool Sensor::Register() { auto &entry = s_sensorRegistry[getIndex()]; // If there's somebody already here - a consumer tried to double-register a sensor - if (entry.sensor) { + if (entry.getSensor()) { // This sensor has already been registered. Don't re-register it. return false; } else { // put ourselves in the registry - entry.sensor = this; + entry.setSensor(this); return true; } } @@ -61,9 +94,7 @@ bool Sensor::Register() { for (size_t i = 0; i < efi::size(s_sensorRegistry); i++) { auto &entry = s_sensorRegistry[i]; - entry.sensor = nullptr; - entry.useMock = false; - entry.mockValue = 0.0f; + entry.reset(); } } @@ -79,7 +110,7 @@ bool Sensor::Register() { /*static*/ const Sensor *Sensor::getSensorOfType(SensorType type) { auto entry = getEntryForType(type); - return entry ? entry->sensor : nullptr; + return entry ? entry->getSensor() : nullptr; } /*static*/ SensorResult Sensor::get(SensorType type) { @@ -91,12 +122,12 @@ bool Sensor::Register() { } // Next check for mock - if (entry->useMock) { - return entry->mockValue; + if (entry->isMock()) { + return entry->getMockValue(); } // Get the sensor out of the entry - const Sensor *s = entry->sensor; + const Sensor *s = entry->getSensor(); if (s) { // If we found the sensor, ask it for a result. return s->get(); @@ -114,7 +145,7 @@ bool Sensor::Register() { return 0; } - const auto s = entry->sensor; + const auto s = entry->getSensor(); if (s) { return s->getRaw(); } @@ -130,15 +161,14 @@ bool Sensor::Register() { return false; } - return entry->useMock || entry->sensor; + return entry->isMock() || entry->getSensor(); } /*static*/ void Sensor::setMockValue(SensorType type, float value) { auto entry = getEntryForType(type); if (entry) { - entry->mockValue = value; - entry->useMock = true; + entry->setMockValue(value); } } @@ -155,7 +185,7 @@ bool Sensor::Register() { auto entry = getEntryForType(type); if (entry) { - entry->useMock = false; + entry->resetMock(); } } @@ -164,7 +194,7 @@ bool Sensor::Register() { for (size_t i = 0; i < efi::size(s_sensorRegistry); i++) { auto &entry = s_sensorRegistry[i]; - entry.useMock = false; + entry.resetMock(); } } @@ -178,10 +208,10 @@ bool Sensor::Register() { auto& entry = s_sensorRegistry[i]; const char* name = s_sensorNames[i]; - if (entry.useMock) { - scheduleMsg(logger, "Sensor \"%s\" mocked with value %.2f", name, entry.mockValue); + if (entry.isMock()) { + scheduleMsg(logger, "Sensor \"%s\" mocked with value %.2f", name, entry.getMockValue()); } else { - const auto sensor = entry.sensor; + const auto sensor = entry.getSensor(); if (sensor) { sensor->showInfo(logger, name);