#pragma once #include #include #include #include #include "main.h" enum class ConfigType { BYTE, SHORT, INT, BOOL, FLOAT, CHAR, STRING }; using ConfigValue = std::variant< uint8_t, // BYTE unsigned int16_t, // SHORT int32_t, // INT bool, // BOOL float, // FLOAT char, // CHAR String // STRING >; struct ConfigDef { const char* key; ConfigType type; ConfigValue defval; }; // keys have to match names in config.json static const ConfigDef configdefs[] = { {"systemName", ConfigType::STRING, String("OBPkp61")}, {"systemMode", ConfigType::CHAR, 'K'}, {"logLevel", ConfigType::BYTE, uint8_t(1)}, {"adminPassword", ConfigType::STRING, String("obpkp61")}, {"useAdminPass", ConfigType::BOOL, true}, {"apEnable", ConfigType::BOOL, true}, {"apPassword", ConfigType::STRING, String("obpkp61")}, {"apIp", ConfigType::STRING, String("192.168.15.1")}, {"apMask", ConfigType::STRING, String("255.255.255.0")}, {"stopApTime", ConfigType::SHORT, int16_t(0)}, {"cpuSpeed", ConfigType::SHORT, int16_t(160)}, {"ledBrightness", ConfigType::SHORT, int16_t(96)}, {"ledRgbBrightness", ConfigType::SHORT, int16_t(96)}, {"tempFormat", ConfigType::CHAR, 'C'}, {"switchBank", ConfigType::BYTE, uint8_t(0)}, {"key1", ConfigType::BYTE, uint8_t(1)}, {"key1long", ConfigType::BYTE, uint8_t(11)}, {"key2", ConfigType::BYTE, uint8_t(2)}, {"key2long", ConfigType::BYTE, uint8_t(12)}, {"key3", ConfigType::BYTE, uint8_t(3)}, {"key3long", ConfigType::BYTE, uint8_t(13)}, {"key4", ConfigType::BYTE, uint8_t(4)}, {"key4long", ConfigType::BYTE, uint8_t(14)}, {"key5", ConfigType::BYTE, uint8_t(5)}, {"key5long", ConfigType::BYTE, uint8_t(15)}, {"key6", ConfigType::BYTE, uint8_t(6)}, {"key6long", ConfigType::BYTE, uint8_t(16)}, {"n2kDestA", ConfigType::STRING, String("none")}, {"n2kDestB", ConfigType::STRING, String("none")}, {"n2kDestC", ConfigType::STRING, String("none")}, {"envInterval", ConfigType::SHORT, int16_t(5)}, // no user access {"LastNodeId", ConfigType::BYTE, uint8_t(N2K_DEFAULT_NODEID)} }; constexpr size_t configdefsCount = sizeof(configdefs) / sizeof(configdefs[0]); class Config { private: Preferences& prefs; std::map values; std::map defs; [[noreturn]] void error_abort() const; public: explicit Config(Preferences& prefs); void load(); bool save(JsonObject json); void dump(); template T get(const char* key) const; uint8_t getByte(const char* key) const; int16_t getShort(const char* key) const; int32_t getInt(const char* key) const; bool getBool(const char* key) const; float getFloat(const char* key) const; char getChar(const char* key) const; const String& getString(const char* key) const; const char* getCString(const char* key) const; }; extern Config config;