Move data field sorting to NamedField

This commit is contained in:
Jasper Blanckenburg 2023-05-24 01:01:48 +02:00
parent 9466290ff3
commit 1bbac4537d
5 changed files with 55 additions and 26 deletions

View File

@ -1,6 +1,7 @@
#ifndef NAMEDFIELD_HPP #ifndef NAMEDFIELD_HPP
#define NAMEDFIELD_HPP #define NAMEDFIELD_HPP
#include "params.h"
#include "touchgfx/TypedText.hpp" #include "touchgfx/TypedText.hpp"
#include "touchgfx/Unicode.hpp" #include "touchgfx/Unicode.hpp"
#include <stddef.h> #include <stddef.h>
@ -29,6 +30,13 @@ struct NamedFieldDescription {
extern NamedFieldDescription dataFieldDescs[]; extern NamedFieldDescription dataFieldDescs[];
extern NamedFieldDescription paramFieldDescs[]; extern NamedFieldDescription paramFieldDescs[];
extern DataFieldType dataFieldByAlphaIndex[];
extern size_t dataFieldAlphaIndexByField[];
extern ParamType paramByAlphaIndex[];
extern size_t paramAlphaIndexByParam[];
void namedFieldSort();
template <class T> class NamedField { template <class T> class NamedField {
public: public:
NamedField(const NamedFieldDescription *fieldDescs); NamedField(const NamedFieldDescription *fieldDescs);

View File

@ -52,8 +52,6 @@ private:
size_t selectedField; size_t selectedField;
size_t selectedFieldIndex; size_t selectedFieldIndex;
DataFieldType fieldTypes[NUM_FIELDS]; DataFieldType fieldTypes[NUM_FIELDS];
size_t indexByFieldType[DataFieldType_COUNT];
DataFieldType fieldTypeByIndex[DataFieldType_COUNT];
touchgfx::Unicode::UnicodeChar r2dProgBuffer[16]; touchgfx::Unicode::UnicodeChar r2dProgBuffer[16];
}; };

View File

@ -1,7 +1,8 @@
#include <gui/common/FrontendApplication.hpp> #include <gui/common/FrontendApplication.hpp>
FrontendApplication::FrontendApplication(Model& m, FrontendHeap& heap) #include "gui/common/NamedField.hpp"
: FrontendApplicationBase(m, heap)
{
FrontendApplication::FrontendApplication(Model &m, FrontendHeap &heap)
: FrontendApplicationBase(m, heap) {
namedFieldSort();
} }

View File

@ -6,6 +6,9 @@
#include "params.h" #include "params.h"
#include "vehicle_state.h" #include "vehicle_state.h"
#include <algorithm>
#include <cstring>
#define VEH_FIELD(FIELD) []() { return (void *)&vehicle_state.FIELD; } #define VEH_FIELD(FIELD) []() { return (void *)&vehicle_state.FIELD; }
#define VEH_BIT_FIELD(FIELD) \ #define VEH_BIT_FIELD(FIELD) \
[]() { \ []() { \
@ -300,6 +303,42 @@ static_assert(sizeof(paramFieldDescs) / sizeof(paramFieldDescs[0]) ==
ParamType_COUNT, ParamType_COUNT,
"Incorrect number of param field descriptions"); "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 <class T> 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 <class T>
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<T>(i);
}
std::sort(fieldByAlpha, fieldByAlpha + numFields,
NFAlphabeticComp<T>(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 <class T> template <class T>
NamedField<T>::NamedField(const NamedFieldDescription *fieldDescs) NamedField<T>::NamedField(const NamedFieldDescription *fieldDescs)
: fieldDescs{fieldDescs} {} : fieldDescs{fieldDescs} {}

View File

@ -8,30 +8,13 @@
#include "touchgfx/Unicode.hpp" #include "touchgfx/Unicode.hpp"
#include <gui/driverview_screen/DriverViewView.hpp> #include <gui/driverview_screen/DriverViewView.hpp>
#include <algorithm>
#include <cmath> #include <cmath>
#include <cstddef> #include <cstddef>
#include <iterator>
#include "vehicle_state.h" #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() DriverViewView::DriverViewView()
: fieldTypes{DF_MinCellVolt, DF_Speed, DF_TSCurrent} { : fieldTypes{DF_MinCellVolt, DF_Speed, DF_TSCurrent} {}
for (size_t i = 0; i < DataFieldType_COUNT; i++) {
fieldTypeByIndex[i] = static_cast<DataFieldType>(i);
}
std::sort(std::begin(fieldTypeByIndex), std::end(fieldTypeByIndex),
DFAlphabeticComp());
for (size_t i = 0; i < DataFieldType_COUNT; i++) {
indexByFieldType[fieldTypeByIndex[i]] = i;
}
}
void DriverViewView::setupScreen() { void DriverViewView::setupScreen() {
DriverViewViewBase::setupScreen(); DriverViewViewBase::setupScreen();
@ -75,7 +58,7 @@ void DriverViewView::tearDownScreen() { DriverViewViewBase::tearDownScreen(); }
void DriverViewView::fieldTypeSelectionUpdateItem( void DriverViewView::fieldTypeSelectionUpdateItem(
DriverViewFieldSelection &item, int16_t itemIndex) { DriverViewFieldSelection &item, int16_t itemIndex) {
DataFieldType fieldType = fieldTypeByIndex[itemIndex]; DataFieldType fieldType = dataFieldByAlphaIndex[itemIndex];
item.setName(dataFieldDescs[fieldType].title); item.setName(dataFieldDescs[fieldType].title);
item.setSelected(itemIndex == (int)selectedFieldIndex); item.setSelected(itemIndex == (int)selectedFieldIndex);
} }
@ -224,7 +207,7 @@ void DriverViewView::confirmFieldType() {
return; return;
} }
DataFieldType fieldType = fieldTypeByIndex[selectedFieldIndex]; DataFieldType fieldType = dataFieldByAlphaIndex[selectedFieldIndex];
DriverViewField &field = getField(selectedField); DriverViewField &field = getField(selectedField);
presenter->setFieldType(selectedField, fieldType); presenter->setFieldType(selectedField, fieldType);
field.setSelected(false); field.setSelected(false);
@ -256,7 +239,7 @@ void DriverViewView::updateSelectedField(size_t selected) {
} }
void DriverViewView::updateSelectedFieldType(DataFieldType type, bool animate) { void DriverViewView::updateSelectedFieldType(DataFieldType type, bool animate) {
updateSelectedFieldIndex(indexByFieldType[type], animate); updateSelectedFieldIndex(dataFieldAlphaIndexByField[type], animate);
} }
void DriverViewView::updateSelectedFieldIndex(size_t selected, bool animate) { void DriverViewView::updateSelectedFieldIndex(size_t selected, bool animate) {