Configuration and code improvements

This commit is contained in:
2026-01-26 20:06:05 +01:00
parent af98a52fd3
commit 107339b3d3
8 changed files with 623 additions and 213 deletions

View File

@@ -1,9 +1,95 @@
#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(0)},
{"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::BYTE, uint8_t(96)},
{"ledRgbBrightness", ConfigType::BYTE, uint8_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("")},
{"n2kDestB", ConfigType::STRING, String("")},
{"n2kDestC", ConfigType::STRING, String("")},
{"envInterval", ConfigType::SHORT, int16_t(5000)},
// 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;
Preferences& prefs;
std::map<String, ConfigValue> values;
std::map<String, const ConfigDef*> defs;
public:
Config();
bool loadConfig();
explicit Config(Preferences& prefs);
void load();
bool save(JsonObject json);
void dump();
template<typename T>
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;
};

View File

@@ -1,4 +1,5 @@
#pragma once
#include <Arduino.h>
#define STRINGIFY_IMPL(x) #x
#define STRINGIFY(x) STRINGIFY_IMPL(x)
@@ -87,14 +88,24 @@
#define BUTTON_6 5
#define BUTTON_DST 6
enum class ButtonPressType : uint8_t
{
enum class ButtonPressType : uint8_t {
SHORT, // < 1 second
MEDIUM, // >= 1 second and < 3 seconds
LONG // >= 3 seconds
};
struct ButtonEvent
{
struct ButtonEvent {
uint8_t buttonId;
ButtonPressType pressType;
};
extern uint64_t chipid;
extern uint8_t led_brightness;
extern uint8_t rgb_brightness;
extern uint8_t keycode[6];
extern uint8_t longcode[6];
extern float temp;
extern float hum;
String uptime_with_unit();

11
include/webserver.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
extern AsyncWebServer server;
void webserver_init();
void send_embedded_file(String name, AsyncWebServerRequest *request);