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
#define NAMEDFIELD_HPP
#include "params.h"
#include "touchgfx/TypedText.hpp"
#include "touchgfx/Unicode.hpp"
#include <stddef.h>
@ -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 T> class NamedField {
public:
NamedField(const NamedFieldDescription *fieldDescs);

View File

@ -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];
};

View File

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

View File

@ -6,6 +6,9 @@
#include "params.h"
#include "vehicle_state.h"
#include <algorithm>
#include <cstring>
#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 <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>
NamedField<T>::NamedField(const NamedFieldDescription *fieldDescs)
: fieldDescs{fieldDescs} {}

View File

@ -8,30 +8,13 @@
#include "touchgfx/Unicode.hpp"
#include <gui/driverview_screen/DriverViewView.hpp>
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <iterator>
#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<DataFieldType>(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) {