OBD CAN sensors #1733

refactoring getters/setters
This commit is contained in:
rusefi 2020-08-29 16:22:57 -04:00
parent 8d0555e205
commit fff58289ee
1 changed files with 51 additions and 21 deletions

View File

@ -6,10 +6,43 @@
// It stores whether the sensor should use a mock value, // It stores whether the sensor should use a mock value,
// the value to use, and if not a pointer to the sensor that // the value to use, and if not a pointer to the sensor that
// can provide a real value. // can provide a real value.
struct SensorRegistryEntry { class SensorRegistryEntry {
bool useMock; 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; float mockValue;
Sensor *sensor; Sensor *sensor = nullptr;
}; };
static SensorRegistryEntry s_sensorRegistry[static_cast<size_t>(SensorType::PlaceholderLast)] = {}; static SensorRegistryEntry s_sensorRegistry[static_cast<size_t>(SensorType::PlaceholderLast)] = {};
@ -46,12 +79,12 @@ bool Sensor::Register() {
auto &entry = s_sensorRegistry[getIndex()]; auto &entry = s_sensorRegistry[getIndex()];
// If there's somebody already here - a consumer tried to double-register a sensor // 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. // This sensor has already been registered. Don't re-register it.
return false; return false;
} else { } else {
// put ourselves in the registry // put ourselves in the registry
entry.sensor = this; entry.setSensor(this);
return true; return true;
} }
} }
@ -61,9 +94,7 @@ bool Sensor::Register() {
for (size_t i = 0; i < efi::size(s_sensorRegistry); i++) { for (size_t i = 0; i < efi::size(s_sensorRegistry); i++) {
auto &entry = s_sensorRegistry[i]; auto &entry = s_sensorRegistry[i];
entry.sensor = nullptr; entry.reset();
entry.useMock = false;
entry.mockValue = 0.0f;
} }
} }
@ -79,7 +110,7 @@ bool Sensor::Register() {
/*static*/ const Sensor *Sensor::getSensorOfType(SensorType type) { /*static*/ const Sensor *Sensor::getSensorOfType(SensorType type) {
auto entry = getEntryForType(type); auto entry = getEntryForType(type);
return entry ? entry->sensor : nullptr; return entry ? entry->getSensor() : nullptr;
} }
/*static*/ SensorResult Sensor::get(SensorType type) { /*static*/ SensorResult Sensor::get(SensorType type) {
@ -91,12 +122,12 @@ bool Sensor::Register() {
} }
// Next check for mock // Next check for mock
if (entry->useMock) { if (entry->isMock()) {
return entry->mockValue; return entry->getMockValue();
} }
// Get the sensor out of the entry // Get the sensor out of the entry
const Sensor *s = entry->sensor; const Sensor *s = entry->getSensor();
if (s) { if (s) {
// If we found the sensor, ask it for a result. // If we found the sensor, ask it for a result.
return s->get(); return s->get();
@ -114,7 +145,7 @@ bool Sensor::Register() {
return 0; return 0;
} }
const auto s = entry->sensor; const auto s = entry->getSensor();
if (s) { if (s) {
return s->getRaw(); return s->getRaw();
} }
@ -130,15 +161,14 @@ bool Sensor::Register() {
return false; return false;
} }
return entry->useMock || entry->sensor; return entry->isMock() || entry->getSensor();
} }
/*static*/ void Sensor::setMockValue(SensorType type, float value) { /*static*/ void Sensor::setMockValue(SensorType type, float value) {
auto entry = getEntryForType(type); auto entry = getEntryForType(type);
if (entry) { if (entry) {
entry->mockValue = value; entry->setMockValue(value);
entry->useMock = true;
} }
} }
@ -155,7 +185,7 @@ bool Sensor::Register() {
auto entry = getEntryForType(type); auto entry = getEntryForType(type);
if (entry) { 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++) { for (size_t i = 0; i < efi::size(s_sensorRegistry); i++) {
auto &entry = 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]; auto& entry = s_sensorRegistry[i];
const char* name = s_sensorNames[i]; const char* name = s_sensorNames[i];
if (entry.useMock) { if (entry.isMock()) {
scheduleMsg(logger, "Sensor \"%s\" mocked with value %.2f", name, entry.mockValue); scheduleMsg(logger, "Sensor \"%s\" mocked with value %.2f", name, entry.getMockValue());
} else { } else {
const auto sensor = entry.sensor; const auto sensor = entry.getSensor();
if (sensor) { if (sensor) {
sensor->showInfo(logger, name); sensor->showInfo(logger, name);