Converted menus to a better menu for typesafe enums

This commit is contained in:
CommanderRedYT
2022-02-08 16:49:04 +01:00
parent a87599c7ea
commit e52cf82218
13 changed files with 41 additions and 150 deletions

View File

@ -49,7 +49,6 @@ set(headers
changevaluedisplay_bobbyquickactions.h
changevaluedisplay_controlmode.h
changevaluedisplay_controltype.h
changevaluedisplay_handbremsmode.h
changevaluedisplay_larsmmode_mode.h
changevaluedisplay_unifiedmodelmode.h
cloud.h
@ -120,7 +119,6 @@ set(headers
displays/menus/networksettingsmenu.h
displays/menus/otamenu.h
displays/menus/profilesmenu.h
displays/menus/selectbatterytypemenu.h
displays/menus/selectbuildserverbranch.h
displays/menus/selectbuildservermenu.h
displays/menus/selectmodemenu.h
@ -282,7 +280,6 @@ set(sources
changevaluedisplay_bobbyquickactions.cpp
changevaluedisplay_controlmode.cpp
changevaluedisplay_controltype.cpp
changevaluedisplay_handbremsmode.cpp
changevaluedisplay_larsmmode_mode.cpp
changevaluedisplay_unifiedmodelmode.cpp
cloud.cpp
@ -351,7 +348,6 @@ set(sources
displays/menus/networksettingsmenu.cpp
displays/menus/otamenu.cpp
displays/menus/profilesmenu.cpp
displays/menus/selectbatterytypemenu.cpp
displays/menus/selectbuildserverbranch.cpp
displays/menus/selectbuildservermenu.cpp
displays/menus/selectmodemenu.cpp

View File

@ -2,10 +2,10 @@
// 3rdparty lib includes
#include <buttonsinterface.h>
#include <cpptypesafeenum.h>
// local includes
#include "bobbybuttons.h"
#include "bobbytypesafeenum.h"
#define BobbyQuickActionsValues(x) \
x(NONE) \
@ -14,7 +14,7 @@
x(HANDBREMSE) \
x(OPEN_GARAGE) \
x(WIFI_SCAN)
DECLARE_BOBBYTYPESAFE_ENUM(BobbyQuickActions, : uint8_t, BobbyQuickActionsValues)
DECLARE_TYPESAFE_ENUM(BobbyQuickActions, : uint8_t, BobbyQuickActionsValues)
namespace quickactions {

View File

@ -6,7 +6,9 @@
template<typename T>
struct iterateEnum;
#define DECLARE_TYPESAFE_ENUM_TYPE(Name) \
#define DECLARE_BOBBYTYPESAFE_ENUM(Name, Derivation, Values) \
DECLARE_TYPESAFE_ENUM(Name, Derivation, Values) \
\
template<> \
struct iterateEnum<Name> { \
template<typename T> \
@ -15,7 +17,3 @@ struct iterateEnum;
return iterate##Name(std::forward<T>(cb)); \
} \
};
#define DECLARE_BOBBYTYPESAFE_ENUM(Name, Derivation, Values) \
DECLARE_TYPESAFE_ENUM(Name, Derivation, Values) \
DECLARE_TYPESAFE_ENUM_TYPE(Name)

View File

@ -5,6 +5,7 @@
#include <configutils_priv_enum.h>
// local includes
#include "battery.h"
#include "ledstrip.h"
#include "handbremse.h"
#include "bobbyquickactions.h"
@ -13,3 +14,4 @@ IMPLEMENT_NVS_GET_SET_ENUM(OtaAnimationModes)
IMPLEMENT_NVS_GET_SET_ENUM(HandbremseMode)
IMPLEMENT_NVS_GET_SET_ENUM(LedstripAnimation)
IMPLEMENT_NVS_GET_SET_ENUM(BobbyQuickActions)
IMPLEMENT_NVS_GET_SET_ENUM(BatteryCellType)

View File

@ -8,3 +8,4 @@ INSTANTIATE_CONFIGWRAPPER_TEMPLATES(OtaAnimationModes)
INSTANTIATE_CONFIGWRAPPER_TEMPLATES(HandbremseMode)
INSTANTIATE_CONFIGWRAPPER_TEMPLATES(LedstripAnimation)
INSTANTIATE_CONFIGWRAPPER_TEMPLATES(BobbyQuickActions)
INSTANTIATE_CONFIGWRAPPER_TEMPLATES(BatteryCellType)

View File

@ -12,29 +12,21 @@
#include <tftinstance.h>
// Local includes
#include "utils.h"
#include "icons/settings.h"
#include "battery.h"
#include "selectbatterytypemenu.h"
#include "displays/bobbychangevaluedisplay.h"
#include "mainmenu.h"
#include "displays/calibratevoltagedisplay.h"
#include "accessors/settingsaccessors.h"
#include "battery.h"
#include "displays/bobbychangevaluedisplay.h"
#include "displays/calibratevoltagedisplay.h"
#include "icons/settings.h"
#include "mainmenu.h"
#include "typesafeenumchangemenu.h"
#include "utils.h"
namespace {
constexpr char TEXT_BATTERY[] = "Battery";
constexpr char TEXT_CELL_SERIES[] = "Cells (Series)";
constexpr char TEXT_CELL_PARALLEL[] = "Cells (Parallel)";
constexpr char TEXT_SELECT_CELL_TYPE[] = "Select Cell Type";
constexpr char TEXT_CELL_TYPE[] = "Cell Type";
constexpr char TEXT_BATTERY_CALIBRATE[] = "Calibrate Voltages";
constexpr char TEXT_BATTERY_TYPE_22P[] = "22P cells";
constexpr char TEXT_BATTERY_TYPE_HG2[] = "HG2 cells";
constexpr char TEXT_BATTERY_TYPE_MH1[] = "MH1 cells";
constexpr char TEXT_BATTERY_TYPE_VTC5[] = "VTC5 cells";
constexpr char TEXT_BATTERY_TYPE_BAK_25R[] = "BAK / 25R cells";
constexpr char TEXT_BATTERY_TYPE_HE4[] = "HE4 cells";
constexpr char TEXT_BATTERY_WHKM[] = "Wh per km";
constexpr char TEXT_BATTERY_APPLYCALIB[] = "Apply calibration";
constexpr char TEXT_VOLTAGECALIBRATION_RESET[] = "Reset calibration";
@ -85,7 +77,7 @@ BatteryMenu::BatteryMenu()
constructMenuItem<makeComponent<MenuItem, TextWithValueHelper<TEXT_CELL_SERIES, BatterySeriesCellsAccessor>, SwitchScreenAction<BatteryCellSeriesChangeScreen>>>();
constructMenuItem<makeComponent<MenuItem, TextWithValueHelper<TEXT_CELL_PARALLEL, BatteryParallelCellsAccessor>, SwitchScreenAction<BatteryCellParallelChangeScreen>>>();
constructMenuItem<makeComponent<MenuItem, TextWithValueHelper<TEXT_BATTERY_WHKM, BatteryWHperKMAccessor>, SwitchScreenAction<BatteryWHperKMChangeScreen>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_SELECT_CELL_TYPE>, SwitchScreenAction<BatteryTypeMenu>>>();
constructMenuItem<SwitchScreenTypeSafeChangeMenuItem<BatteryCellType, BatteryMenu, TEXT_SELECT_CELL_TYPE>>(&configs.battery.cellType);
constructMenuItem<makeComponent<MenuItem, EmptyText, DummyAction>>();
constructMenuItem<makeComponent<MenuItem, WhStatisticsText, DummyAction>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BATTERY_CALIBRATE>, SwitchScreenAction<CalibrateVoltageDisplay>, StaticMenuItemIcon<&bobbyicons::settings>>>();

View File

@ -9,6 +9,7 @@
// local includes
#include "displays/bobbychangevaluedisplay.h"
#include "displays/menus/typesafeenumchangemenu.h"
#include "accessors/settingsaccessors.h"
#include "changevaluedisplay_handbremsmode.h"
#include "displays/menus/defaultmodesettingsmenu.h"
@ -30,21 +31,6 @@ using HandBremsTriggerTimeoutChangeValueDisplay = espgui::makeComponent<
espgui::ConfirmActionInterface<espgui::SwitchScreenAction<HandbremsSettingsMenu>>,
espgui::BackActionInterface<espgui::SwitchScreenAction<HandbremsSettingsMenu>>
>;
using HandBremsModeChangeValueDisplay = espgui::makeComponent<
BobbyChangeValueDisplay<HandbremseMode>,
espgui::StaticText<TEXT_HANDBREMSE_MODE>,
HandbremsModeAccessor,
espgui::ConfirmActionInterface<espgui::SwitchScreenAction<HandbremsSettingsMenu>>,
espgui::BackActionInterface<espgui::SwitchScreenAction<HandbremsSettingsMenu>>
>;
class HandBremsModeText : public virtual espgui::TextInterface
{
public:
std::string text() const override
{
return fmt::format("Mode: &2{}", configs.handbremse.mode.valueAsString());
}
};
}
HandbremsSettingsMenu::HandbremsSettingsMenu()
@ -53,7 +39,7 @@ HandbremsSettingsMenu::HandbremsSettingsMenu()
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_HANDBREMSE_ENABLE>, BobbyCheckbox, HandbremsEnabledAccessor>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_HANDBREMSE_AUTOMATIC>, BobbyCheckbox, HandbremsAutomaticAccessor>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_HANDBREMSE_VISUALIZE>, BobbyCheckbox, HandbremsVisualizeAccessor>>();
constructMenuItem<makeComponent<MenuItem, HandBremsModeText, SwitchScreenAction<HandBremsModeChangeValueDisplay>>>();
constructMenuItem<SwitchScreenTypeSafeChangeMenuItem<HandbremseMode, HandbremsSettingsMenu, TEXT_HANDBREMSE_MODE>>(&configs.handbremse.mode);
constructMenuItem<makeComponent<MenuItem, TextWithValueHelper<TEXT_HANDBREMSE_TRIGGERTIMEOUT, HandbremsTimeoutAccessor>, SwitchScreenAction<HandBremsTriggerTimeoutChangeValueDisplay>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<DefaultModeSettingsMenu>, StaticMenuItemIcon<&espgui::icons::back>>>();
}

View File

@ -11,16 +11,16 @@
#include <textwithvaluehelper.h>
// local includes
#include "displays/bobbychangevaluedisplay.h"
#include "accessors/settingsaccessors.h"
#include "bobbycheckbox.h"
#include "displays/bobbychangevaluedisplay.h"
#include "displays/ledstripcolorsdisplay.h"
#include "displays/menus/ledstripselectotamode.h"
#include "displays/menus/mainmenu.h"
#include "displays/menus/typesafeenumchangemenu.h"
#include "globals.h"
#include "ledstrip.h"
#include "displays/menus/typesafeenumchangemenu.h"
#include "ledstripselectblinkmenu.h"
#include "bobbycheckbox.h"
// clang-format off

View File

@ -1,7 +1,9 @@
#pragma once
// 3rdparty lib includes
#include <cpptypesafeenum.h>
// local includes
#include "bobbytypesafeenum.h"
#include "displays/bobbymenudisplay.h"
#include "modes/ignoreinputmode.h"
@ -10,7 +12,7 @@
x(Copy) \
x(Swap) \
x(__END__)
DECLARE_BOBBYTYPESAFE_ENUM(Actions, : uint8_t, ActionValues)
DECLARE_TYPESAFE_ENUM(Actions, : uint8_t, ActionValues)
class ManageProfilesMenu : public BobbyMenuDisplay
{

View File

@ -1,73 +0,0 @@
#include "selectbatterytypemenu.h"
// 3rdparty lib includes
#include <menuitem.h>
#include <icons/back.h>
#include <actions/switchscreenaction.h>
#include <actioninterface.h>
#include <actions/dummyaction.h>
// local includes
#include "battery.h"
#include "newsettings.h"
#include "utils.h"
#include "displays/menus/batterymenu.h"
#include "batterymenu.h"
#include "displays/menus/mainmenu.h"
namespace {
constexpr char TEXT_CELL_SERIES[] = "Cells (Series)";
constexpr char TEXT_CELL_PARALLEL[] = "Cells (Parallel)";
constexpr char TEXT_SELECT_CELL_TYPE[] = "Select Cell Type";
constexpr char TEXT_CELL_TYPE[] = "Cell Type";
constexpr char TEXT_BATTERY_CALIBRATE[] = "Calibrate Voltages";
constexpr char TEXT_BATTERY_TYPE_22P[] = "22P cells";
constexpr char TEXT_BATTERY_TYPE_HG2[] = "HG2 cells";
constexpr char TEXT_BATTERY_TYPE_MH1[] = "MH1 cells";
constexpr char TEXT_BATTERY_TYPE_VTC5[] = "VTC5 cells";
constexpr char TEXT_BATTERY_TYPE_BAK_25R[] = "BAK / 25R cells";
constexpr char TEXT_BATTERY_TYPE_HE4[] = "HE4 cells";
constexpr char TEXT_BATTERY_WHKM[] = "Wh per km";
constexpr char TEXT_BATTERY_APPLYCALIB[] = "Apply calibration";
constexpr char TEXT_VOLTAGECALIBRATION_RESET[] = "Reset calibration";
constexpr char TEXT_BACK[] = "Back";
class CurrentBatteryTypeText : public virtual espgui::TextInterface
{
public:
std::string text() const override
{
return getBatteryCellTypeString();
}
};
template<BatteryCellType T>
class BatterySelectTypeAction : public virtual espgui::ActionInterface
{
public:
void triggered() override { configs.write_config(configs.battery.cellType, uint8_t(T)); }
};
} // namespace
BatteryTypeMenu::BatteryTypeMenu()
{
using namespace espgui;
constructMenuItem<makeComponent<MenuItem, CurrentBatteryTypeText, DisabledColor, DummyAction>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BATTERY_TYPE_22P>, BatterySelectTypeAction<BatteryCellType::_22P>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BATTERY_TYPE_HG2>, BatterySelectTypeAction<BatteryCellType::HG2>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BATTERY_TYPE_MH1>, BatterySelectTypeAction<BatteryCellType::MH1>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BATTERY_TYPE_VTC5>, BatterySelectTypeAction<BatteryCellType::VTC5>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BATTERY_TYPE_BAK_25R>, BatterySelectTypeAction<BatteryCellType::BAK_25R>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BATTERY_TYPE_HE4>, BatterySelectTypeAction<BatteryCellType::HE4>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<MainMenu>, StaticMenuItemIcon<&espgui::icons::back>>>();
}
std::string BatteryTypeMenu::text() const
{
return TEXT_SELECT_CELL_TYPE;
}
void BatteryTypeMenu::back()
{
espgui::switchScreen<BatteryMenu>();
}

View File

@ -1,14 +0,0 @@
#pragma once
// Local includes
#include "displays/bobbymenudisplay.h"
class BatteryTypeMenu : public BobbyMenuDisplay
{
public:
BatteryTypeMenu();
std::string text() const override;
void back() override;
};

View File

@ -14,17 +14,16 @@
namespace typesafeenumchangemenu {
constexpr char TEXT_BACK[] = "Back";
} // namespace typesafeenumchangemenu
template<typename TEnum>
class TypesafeEnumCurrentValueMenuItem :
public espgui::MenuItem
{
public:
TypesafeEnumCurrentValueMenuItem(const ConfigWrapper<TEnum> &config) : m_config{config} {}
TypesafeEnumCurrentValueMenuItem(ConfigWrapper<TEnum>* config) : m_config{config} {}
std::string text() const override
{
return toString(m_config.value);
return toString(m_config->value);
}
void triggered() override {}
@ -33,7 +32,7 @@ public:
return TFT_DARKGREY;
}
private:
const ConfigWrapper<TEnum> &m_config;
ConfigWrapper<TEnum>* m_config;
};
template<typename TEnum>
@ -41,7 +40,7 @@ class TypesafeEnumSetterMenuItem :
public espgui::MenuItem
{
public:
TypesafeEnumSetterMenuItem(TEnum value, const ConfigWrapper<TEnum> &config) : m_value{value}, m_config{config} {}
TypesafeEnumSetterMenuItem(TEnum value, ConfigWrapper<TEnum>* config) : m_value{value}, m_config{config} {}
std::string text() const override
{
@ -50,12 +49,12 @@ public:
void triggered() override
{
if (auto result = configs.write_config(m_config, m_value); !result)
if (auto result = m_config->write(configs.nvs_handle_user, m_value); !result)
BobbyErrorHandler{}.errorOccured(std::move(result).error());
}
private:
const TEnum m_value;
const ConfigWrapper<TEnum> &m_config;
ConfigWrapper<TEnum>* m_config;
};
template<typename TEnum, typename TMenu>
@ -63,13 +62,13 @@ class TypeSafeChangeMenu :
public BobbyMenuDisplay
{
public:
TypeSafeChangeMenu(const ConfigWrapper<TEnum> &config) : m_config{config}
TypeSafeChangeMenu(ConfigWrapper<TEnum>* config) : m_config{config}
{
using namespace espgui;
using namespace typesafeenumchangemenu;
constructMenuItem<TypesafeEnumCurrentValueMenuItem<TEnum>>(*m_config);
constructMenuItem<TypesafeEnumCurrentValueMenuItem<TEnum>>(m_config);
constructMenuItem<makeComponent<MenuItem, EmptyText, DummyAction>>();
iterateEnum<TEnum>([&](TEnum enum_value, const auto &string_value){
iterateEnum<TEnum>::iterate([&](TEnum enum_value, const auto &string_value){
constructMenuItem<TypesafeEnumSetterMenuItem<TEnum>>(enum_value, m_config);
});
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<TMenu>, StaticMenuItemIcon<&espgui::icons::back>>>();
@ -77,7 +76,7 @@ public:
std::string text() const override
{
return fmt::format("Select {}", m_config.nvsName());
return fmt::format("Select {}", m_config->nvsName());
}
void back() override
@ -85,14 +84,15 @@ public:
espgui::switchScreen<TMenu>();
}
private:
const ConfigWrapper<TEnum> &m_config;
ConfigWrapper<TEnum>* m_config;
};
} // namespace typesafeenumchangemenu
template<typename TEnum, typename TMenu, const char *Ttext>
class SwitchScreenTypeSafeChangeMenuItem : public espgui::MenuItem
{
public:
SwitchScreenTypeSafeChangeMenuItem(const ConfigWrapper<TEnum> &config) : m_config{config} {}
SwitchScreenTypeSafeChangeMenuItem(ConfigWrapper<TEnum>* config) : m_config{config} {}
static constexpr const char *STATIC_TEXT = Ttext;
std::string text() const override
@ -102,8 +102,8 @@ public:
void triggered() override
{
espgui::switchScreen<TypeSafeChangeMenu<TEnum, TMenu>>(*m_config);
espgui::switchScreen<typesafeenumchangemenu::TypeSafeChangeMenu<TEnum, TMenu>>(m_config);
}
private:
const ConfigWrapper<TEnum> &m_config;
ConfigWrapper<TEnum>* m_config;
};

View File

@ -21,6 +21,7 @@
#include <makearray.h>
// local includes
#include "battery.h"
#include "ledstrip.h"
#include "unifiedmodelmode.h"
#include "displays/lockscreen.h"
@ -253,7 +254,7 @@ public:
struct {
ConfigWrapper<uint8_t> cellsSeries {12, DoReset, {}, "batteryCS" };
ConfigWrapper<uint8_t> cellsParallel {10, DoReset, {}, "batteryCP" };
ConfigWrapper<uint8_t> cellType {0, DoReset, {}, "batteryType" };
ConfigWrapper<BatteryCellType> cellType {BatteryCellType::_22P, DoReset, {}, "batteryType" };
ConfigWrapper<uint16_t> watthoursPerKilometer{25, DoReset, {}, "whkm" };
ConfigWrapper<int16_t> front30VoltCalibration{3000, DoReset, {}, "batF30VCal" };
ConfigWrapper<int16_t> back30VoltCalibration {3000, DoReset, {}, "batB30VCal" };