diff --git a/firmware/controllers/sensors/sensor.cpp b/firmware/controllers/sensors/sensor.cpp index af260bc869..18e1153201 100644 --- a/firmware/controllers/sensors/sensor.cpp +++ b/firmware/controllers/sensors/sensor.cpp @@ -231,3 +231,18 @@ bool Sensor::Register() { entry->showInfo(getSensorName(type)); } } + +/** + * this is definitely not the fastest implementation possible but good enough for now? + * todo: some sort of hashmap in the future? + */ +SensorType findSensorTypeByName(const char *name) { + for (int i = 0;i<(int)SensorType::PlaceholderLast;i++) { + SensorType type = (SensorType)i; + const char *sensorName = getSensorType(type); + if (strEqualCaseInsensitive(sensorName, name)) { + return type; + } + } + return SensorType::Invalid; +} diff --git a/firmware/controllers/sensors/sensor.h b/firmware/controllers/sensors/sensor.h index 783ad89eb7..a23f02a4d9 100644 --- a/firmware/controllers/sensors/sensor.h +++ b/firmware/controllers/sensors/sensor.h @@ -193,3 +193,5 @@ private: */ static SensorRegistryEntry *getEntryForType(SensorType type); }; + +SensorType findSensorTypeByName(const char *name); diff --git a/unit_tests/tests/sensor/basic_sensor.cpp b/unit_tests/tests/sensor/basic_sensor.cpp index 859e795016..3d09021d00 100644 --- a/unit_tests/tests/sensor/basic_sensor.cpp +++ b/unit_tests/tests/sensor/basic_sensor.cpp @@ -107,3 +107,8 @@ TEST_F(SensorBasic, HasSensorMock) { // Now we should! ASSERT_TRUE(Sensor::hasSensor(SensorType::Clt)); } + + +TEST_F(SensorBasic, FindByName) { + ASSERT_EQ(SensorType::Clt, findSensorTypeByName("Clt")); +}