diff --git a/TouchGFX/gui/include/gui/common/NamedField.hpp b/TouchGFX/gui/include/gui/common/NamedField.hpp index 18bdff4..e6a7585 100644 --- a/TouchGFX/gui/include/gui/common/NamedField.hpp +++ b/TouchGFX/gui/include/gui/common/NamedField.hpp @@ -1,6 +1,7 @@ #ifndef NAMEDFIELD_HPP #define NAMEDFIELD_HPP +#include "params.h" #include "touchgfx/TypedText.hpp" #include "touchgfx/Unicode.hpp" #include @@ -29,6 +30,13 @@ struct NamedFieldDescription { extern NamedFieldDescription dataFieldDescs[]; extern NamedFieldDescription paramFieldDescs[]; +extern DataFieldType dataFieldByAlphaIndex[]; +extern size_t dataFieldAlphaIndexByField[]; +extern ParamType paramByAlphaIndex[]; +extern size_t paramAlphaIndexByParam[]; + +void namedFieldSort(); + template class NamedField { public: NamedField(const NamedFieldDescription *fieldDescs); diff --git a/TouchGFX/gui/include/gui/driverview_screen/DriverViewView.hpp b/TouchGFX/gui/include/gui/driverview_screen/DriverViewView.hpp index ae285f6..d4f3136 100644 --- a/TouchGFX/gui/include/gui/driverview_screen/DriverViewView.hpp +++ b/TouchGFX/gui/include/gui/driverview_screen/DriverViewView.hpp @@ -52,8 +52,6 @@ private: size_t selectedField; size_t selectedFieldIndex; DataFieldType fieldTypes[NUM_FIELDS]; - size_t indexByFieldType[DataFieldType_COUNT]; - DataFieldType fieldTypeByIndex[DataFieldType_COUNT]; touchgfx::Unicode::UnicodeChar r2dProgBuffer[16]; }; diff --git a/TouchGFX/gui/src/common/FrontendApplication.cpp b/TouchGFX/gui/src/common/FrontendApplication.cpp index 5875945..687f272 100644 --- a/TouchGFX/gui/src/common/FrontendApplication.cpp +++ b/TouchGFX/gui/src/common/FrontendApplication.cpp @@ -1,7 +1,8 @@ #include -FrontendApplication::FrontendApplication(Model& m, FrontendHeap& heap) - : FrontendApplicationBase(m, heap) -{ +#include "gui/common/NamedField.hpp" +FrontendApplication::FrontendApplication(Model &m, FrontendHeap &heap) + : FrontendApplicationBase(m, heap) { + namedFieldSort(); } diff --git a/TouchGFX/gui/src/common/NamedField.cpp b/TouchGFX/gui/src/common/NamedField.cpp index 15d8a81..c4961ce 100644 --- a/TouchGFX/gui/src/common/NamedField.cpp +++ b/TouchGFX/gui/src/common/NamedField.cpp @@ -6,6 +6,9 @@ #include "params.h" #include "vehicle_state.h" +#include +#include + #define VEH_FIELD(FIELD) []() { return (void *)&vehicle_state.FIELD; } #define VEH_BIT_FIELD(FIELD) \ []() { \ @@ -300,6 +303,42 @@ static_assert(sizeof(paramFieldDescs) / sizeof(paramFieldDescs[0]) == ParamType_COUNT, "Incorrect number of param field descriptions"); +DataFieldType dataFieldByAlphaIndex[DataFieldType_COUNT]; +size_t dataFieldAlphaIndexByField[DataFieldType_COUNT]; +ParamType paramByAlphaIndex[ParamType_COUNT]; +size_t paramAlphaIndexByParam[ParamType_COUNT]; + +template struct NFAlphabeticComp { + NFAlphabeticComp(const NamedFieldDescription *fieldDescs) + : fieldDescs{fieldDescs} {} + + const NamedFieldDescription *fieldDescs; + + bool operator()(const T &a, const T &b) const { + return strcmp(fieldDescs[a].title, fieldDescs[b].title) < 0; + } +}; + +template +void namedFieldSort(const NamedFieldDescription *fieldDescs, T *fieldByAlpha, + size_t *alphaIndexByField, size_t numFields) { + for (size_t i = 0; i < numFields; i++) { + fieldByAlpha[i] = static_cast(i); + } + std::sort(fieldByAlpha, fieldByAlpha + numFields, + NFAlphabeticComp(fieldDescs)); + for (size_t i = 0; i < numFields; i++) { + alphaIndexByField[fieldByAlpha[i]] = i; + } +} + +void namedFieldSort() { + namedFieldSort(dataFieldDescs, dataFieldByAlphaIndex, + dataFieldAlphaIndexByField, DataFieldType_COUNT); + namedFieldSort(paramFieldDescs, paramByAlphaIndex, paramAlphaIndexByParam, + ParamType_COUNT); +} + template NamedField::NamedField(const NamedFieldDescription *fieldDescs) : fieldDescs{fieldDescs} {} diff --git a/TouchGFX/gui/src/driverview_screen/DriverViewView.cpp b/TouchGFX/gui/src/driverview_screen/DriverViewView.cpp index d8d3bc9..7ddc4c1 100644 --- a/TouchGFX/gui/src/driverview_screen/DriverViewView.cpp +++ b/TouchGFX/gui/src/driverview_screen/DriverViewView.cpp @@ -8,30 +8,13 @@ #include "touchgfx/Unicode.hpp" #include -#include #include #include -#include #include "vehicle_state.h" -struct DFAlphabeticComp { - bool operator()(const DataFieldType &a, const DataFieldType &b) const { - return strcmp(dataFieldDescs[a].title, dataFieldDescs[b].title) < 0; - } -}; - DriverViewView::DriverViewView() - : fieldTypes{DF_MinCellVolt, DF_Speed, DF_TSCurrent} { - for (size_t i = 0; i < DataFieldType_COUNT; i++) { - fieldTypeByIndex[i] = static_cast(i); - } - std::sort(std::begin(fieldTypeByIndex), std::end(fieldTypeByIndex), - DFAlphabeticComp()); - for (size_t i = 0; i < DataFieldType_COUNT; i++) { - indexByFieldType[fieldTypeByIndex[i]] = i; - } -} + : fieldTypes{DF_MinCellVolt, DF_Speed, DF_TSCurrent} {} void DriverViewView::setupScreen() { DriverViewViewBase::setupScreen(); @@ -75,7 +58,7 @@ void DriverViewView::tearDownScreen() { DriverViewViewBase::tearDownScreen(); } void DriverViewView::fieldTypeSelectionUpdateItem( DriverViewFieldSelection &item, int16_t itemIndex) { - DataFieldType fieldType = fieldTypeByIndex[itemIndex]; + DataFieldType fieldType = dataFieldByAlphaIndex[itemIndex]; item.setName(dataFieldDescs[fieldType].title); item.setSelected(itemIndex == (int)selectedFieldIndex); } @@ -224,7 +207,7 @@ void DriverViewView::confirmFieldType() { return; } - DataFieldType fieldType = fieldTypeByIndex[selectedFieldIndex]; + DataFieldType fieldType = dataFieldByAlphaIndex[selectedFieldIndex]; DriverViewField &field = getField(selectedField); presenter->setFieldType(selectedField, fieldType); field.setSelected(false); @@ -256,7 +239,7 @@ void DriverViewView::updateSelectedField(size_t selected) { } void DriverViewView::updateSelectedFieldType(DataFieldType type, bool animate) { - updateSelectedFieldIndex(indexByFieldType[type], animate); + updateSelectedFieldIndex(dataFieldAlphaIndexByField[type], animate); } void DriverViewView::updateSelectedFieldIndex(size_t selected, bool animate) {