111 lines
3.4 KiB
C++
111 lines
3.4 KiB
C++
#pragma once
|
|
#include <Preferences.h>
|
|
#include <ArduinoJson.h>
|
|
#include <variant>
|
|
#include <map>
|
|
#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(4)},
|
|
{"adminPassword", ConfigType::STRING, String("obpkp61")},
|
|
{"useAdminPass", ConfigType::BOOL, true},
|
|
{"instDesc1", ConfigType::STRING, String("")},
|
|
{"instDesc2", ConfigType::STRING, String("")},
|
|
{"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)},
|
|
{"apHidden", ConfigType::BOOL, false},
|
|
{"cpuSpeed", ConfigType::SHORT, int16_t(160)},
|
|
{"ledBrightness", ConfigType::SHORT, int16_t(96)},
|
|
{"rgbBrightness", 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<String, ConfigValue> values;
|
|
std::map<String, const ConfigDef*> defs;
|
|
void updateValue(const char* key, ConfigType type, ConfigValue newvalue);
|
|
ConfigValue getValue(const ConfigDef* def);
|
|
[[noreturn]] void error_abort() const;
|
|
public:
|
|
explicit Config(Preferences& prefs);
|
|
void load();
|
|
void loadValue(const char* key);
|
|
void save();
|
|
void dump();
|
|
|
|
bool hasKey(const char* key);
|
|
|
|
template<typename T>
|
|
T get(const char* key) const;
|
|
ConfigValue 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;
|
|
|
|
bool setValue(const char* key, const char* value);
|
|
|
|
};
|
|
|
|
extern Config config;
|