config file
This commit is contained in:
parent
be288f6a5a
commit
f40483873a
|
|
@ -0,0 +1,26 @@
|
||||||
|
# HannaBox Configuration File
|
||||||
|
# Values are in the format: key=value
|
||||||
|
# Lines starting with # are comments
|
||||||
|
|
||||||
|
# Audio Settings
|
||||||
|
initialVolume=7
|
||||||
|
maxVolume=15
|
||||||
|
|
||||||
|
# Power Management (times in milliseconds)
|
||||||
|
sleepTime=1800000
|
||||||
|
sleepDelay=1800000
|
||||||
|
sleepMessageDelay=1798000
|
||||||
|
|
||||||
|
# Battery Settings (voltage in millivolts)
|
||||||
|
minVoltage=3200
|
||||||
|
voltage100Percent=4200
|
||||||
|
|
||||||
|
# RFID Settings
|
||||||
|
rfidLoopInterval=25
|
||||||
|
|
||||||
|
# Playback Settings
|
||||||
|
startAtStoredProgress=true
|
||||||
|
|
||||||
|
# WiFi Settings (leave empty to use current WiFiManager)
|
||||||
|
wifiSSID=
|
||||||
|
wifiPassword=
|
||||||
|
|
@ -0,0 +1,151 @@
|
||||||
|
#include "config.h"
|
||||||
|
#include "globals.h"
|
||||||
|
#include <SD.h>
|
||||||
|
|
||||||
|
// Global config instance
|
||||||
|
Config config;
|
||||||
|
|
||||||
|
const String getConfigFilePath() {
|
||||||
|
return "/" + sys_dir + "/config.txt";
|
||||||
|
}
|
||||||
|
|
||||||
|
void setDefaultConfig() {
|
||||||
|
config.initialVolume = 7;
|
||||||
|
config.maxVolume = 15;
|
||||||
|
config.sleepTime = 1800000;
|
||||||
|
config.minVoltage = 3200;
|
||||||
|
config.voltage100Percent = 4200;
|
||||||
|
config.sleepDelay = 1800000;
|
||||||
|
config.sleepMessageDelay = 1798000;
|
||||||
|
config.rfidLoopInterval = 25;
|
||||||
|
config.startAtStoredProgress = true;
|
||||||
|
config.wifiSSID[0] = '\0';
|
||||||
|
config.wifiPassword[0] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
bool loadConfig() {
|
||||||
|
String configPath = getConfigFilePath();
|
||||||
|
|
||||||
|
if (!SD.exists(configPath)) {
|
||||||
|
Serial.println("Config file not found, using defaults");
|
||||||
|
setDefaultConfig();
|
||||||
|
return saveConfig(); // Create config file with defaults
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = SD.open(configPath, FILE_READ);
|
||||||
|
if (!file) {
|
||||||
|
Serial.println("Failed to open config file");
|
||||||
|
setDefaultConfig();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set defaults first
|
||||||
|
setDefaultConfig();
|
||||||
|
|
||||||
|
// Parse config file line by line
|
||||||
|
while (file.available()) {
|
||||||
|
String line = file.readStringUntil('\n');
|
||||||
|
line.trim();
|
||||||
|
|
||||||
|
// Skip empty lines and comments
|
||||||
|
if (line.length() == 0 || line.startsWith("#")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int separatorIndex = line.indexOf('=');
|
||||||
|
if (separatorIndex == -1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String key = line.substring(0, separatorIndex);
|
||||||
|
String value = line.substring(separatorIndex + 1);
|
||||||
|
key.trim();
|
||||||
|
value.trim();
|
||||||
|
|
||||||
|
// Parse each configuration value
|
||||||
|
if (key == "initialVolume") {
|
||||||
|
config.initialVolume = constrain(value.toInt(), 0, 21);
|
||||||
|
} else if (key == "maxVolume") {
|
||||||
|
config.maxVolume = constrain(value.toInt(), 1, 21);
|
||||||
|
} else if (key == "sleepTime") {
|
||||||
|
config.sleepTime = value.toInt();
|
||||||
|
} else if (key == "minVoltage") {
|
||||||
|
config.minVoltage = value.toInt();
|
||||||
|
} else if (key == "voltage100Percent") {
|
||||||
|
config.voltage100Percent = value.toInt();
|
||||||
|
} else if (key == "sleepDelay") {
|
||||||
|
config.sleepDelay = value.toInt();
|
||||||
|
} else if (key == "sleepMessageDelay") {
|
||||||
|
config.sleepMessageDelay = value.toInt();
|
||||||
|
} else if (key == "rfidLoopInterval") {
|
||||||
|
config.rfidLoopInterval = constrain(value.toInt(), 1, 255);
|
||||||
|
} else if (key == "startAtStoredProgress") {
|
||||||
|
config.startAtStoredProgress = (value == "1" || value.equalsIgnoreCase("true"));
|
||||||
|
} else if (key == "wifiSSID") {
|
||||||
|
strncpy(config.wifiSSID, value.c_str(), sizeof(config.wifiSSID) - 1);
|
||||||
|
config.wifiSSID[sizeof(config.wifiSSID) - 1] = '\0';
|
||||||
|
} else if (key == "wifiPassword") {
|
||||||
|
strncpy(config.wifiPassword, value.c_str(), sizeof(config.wifiPassword) - 1);
|
||||||
|
config.wifiPassword[sizeof(config.wifiPassword) - 1] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
Serial.println("Config loaded successfully");
|
||||||
|
Serial.print("Initial Volume: "); Serial.println(config.initialVolume);
|
||||||
|
Serial.print("Max Volume: "); Serial.println(config.maxVolume);
|
||||||
|
Serial.print("Sleep Delay: "); Serial.println(config.sleepDelay);
|
||||||
|
Serial.print("RFID Interval: "); Serial.println(config.rfidLoopInterval);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool saveConfig() {
|
||||||
|
String configPath = getConfigFilePath();
|
||||||
|
|
||||||
|
File file = SD.open(configPath, FILE_WRITE);
|
||||||
|
if (!file) {
|
||||||
|
Serial.println("Failed to create config file");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write config file with comments for user reference
|
||||||
|
file.println("# HannaBox Configuration File");
|
||||||
|
file.println("# Values are in the format: key=value");
|
||||||
|
file.println("# Lines starting with # are comments");
|
||||||
|
file.println("");
|
||||||
|
|
||||||
|
file.println("# Audio Settings");
|
||||||
|
file.print("initialVolume="); file.println(config.initialVolume);
|
||||||
|
file.print("maxVolume="); file.println(config.maxVolume);
|
||||||
|
file.println("");
|
||||||
|
|
||||||
|
file.println("# Power Management (times in milliseconds)");
|
||||||
|
file.print("sleepTime="); file.println(config.sleepTime);
|
||||||
|
file.print("sleepDelay="); file.println(config.sleepDelay);
|
||||||
|
file.print("sleepMessageDelay="); file.println(config.sleepMessageDelay);
|
||||||
|
file.println("");
|
||||||
|
|
||||||
|
file.println("# Battery Settings (voltage in millivolts)");
|
||||||
|
file.print("minVoltage="); file.println(config.minVoltage);
|
||||||
|
file.print("voltage100Percent="); file.println(config.voltage100Percent);
|
||||||
|
file.println("");
|
||||||
|
|
||||||
|
file.println("# RFID Settings");
|
||||||
|
file.print("rfidLoopInterval="); file.println(config.rfidLoopInterval);
|
||||||
|
file.println("");
|
||||||
|
|
||||||
|
file.println("# Playback Settings");
|
||||||
|
file.print("startAtStoredProgress="); file.println(config.startAtStoredProgress ? "true" : "false");
|
||||||
|
file.println("");
|
||||||
|
|
||||||
|
file.println("# WiFi Settings (leave empty to use current WiFiManager)");
|
||||||
|
file.print("wifiSSID="); file.println(config.wifiSSID);
|
||||||
|
file.print("wifiPassword="); file.println(config.wifiPassword);
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
Serial.println("Config saved successfully");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef CONFIG_H_
|
||||||
|
#define CONFIG_H_
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
// Configuration structure - keep it minimal for memory efficiency
|
||||||
|
struct Config {
|
||||||
|
uint8_t initialVolume = 7;
|
||||||
|
uint8_t maxVolume = 15;
|
||||||
|
uint32_t sleepTime = 1800000; // 30 minutes in ms
|
||||||
|
uint16_t minVoltage = 3200; // mV - minimum voltage before sleep
|
||||||
|
uint16_t voltage100Percent = 4200; // mV - voltage representing 100% battery
|
||||||
|
uint32_t sleepDelay = 1800000; // 30 minutes in ms
|
||||||
|
uint32_t sleepMessageDelay = 1798000; // 2 seconds before sleep
|
||||||
|
uint8_t rfidLoopInterval = 25; // RFID check interval
|
||||||
|
bool startAtStoredProgress = true; // Resume from last position
|
||||||
|
char wifiSSID[32] = ""; // WiFi SSID (empty = use current mechanism)
|
||||||
|
char wifiPassword[64] = ""; // WiFi password (empty = use current mechanism)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Global config instance
|
||||||
|
extern Config config;
|
||||||
|
|
||||||
|
// Function declarations
|
||||||
|
bool loadConfig();
|
||||||
|
bool saveConfig();
|
||||||
|
void setDefaultConfig();
|
||||||
|
const String getConfigFilePath();
|
||||||
|
|
||||||
|
#endif
|
||||||
557
src/main.cpp
557
src/main.cpp
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue