Skip to content

Commit 42ea2c1

Browse files
committed
feat: added store configuration function
1 parent 4ca6a40 commit 42ea2c1

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ This project is an ESP32-based desk clock with functionality of monitoring the t
3232
**Software Setup:**
3333
- Install the PlatformIO IDE or PlatformIO CLI (or the VSCode extension, make sure to add ".platformio\penv\Scripts" in your path).
3434
- Connect the board to your computer.
35-
- Change the configuration in data/config.json.dist and rename it in config.json.
35+
- Change the configuration in ```data/config.json```.
3636
- Build the filesystem and upload it, compile and upload the firmware to your ESP32 board (you can even run the build script instead of doing it manually).
3737

3838
**Hardware Setup:**

lib/Config_Parser/Config_Parser.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#define PATH_TO_CONF "/config.json"
77

88
void loadConfiguration(Config& config) {
9-
File file = SPIFFS.open(PATH_TO_CONF, "r");
9+
File file = SPIFFS.open(PATH_TO_CONF, FILE_READ);
1010
JsonDocument conf;
1111

1212
DeserializationError error = deserializeJson(conf, file);
@@ -67,6 +67,57 @@ void loadConfiguration(Config& config) {
6767
file.close();
6868
}
6969

70+
void storeConfiguration(Config config) {
71+
SPIFFS.remove(PATH_TO_CONF);
72+
73+
File file = SPIFFS.open(PATH_TO_CONF, FILE_WRITE);
74+
if (!file) {
75+
Serial.println(F("Failed to create file"));
76+
return;
77+
}
78+
79+
JsonDocument conf;
80+
81+
conf["STD_TIME"]["abbrev"] = config.std.abbrev;
82+
conf["STD_TIME"]["week"] = config.std.week;
83+
conf["STD_TIME"]["dow"] = config.std.dow;
84+
conf["STD_TIME"]["month"] = config.std.month;
85+
conf["STD_TIME"]["hour"] = config.std.hour;
86+
conf["STD_TIME"]["offset"] = config.std.offset;
87+
88+
conf["DL_TIME"]["abbrev"] = config.dlt.abbrev;
89+
conf["DL_TIME"]["week"] = config.dlt.week;
90+
conf["DL_TIME"]["dow"] = config.dlt.dow;
91+
conf["DL_TIME"]["month"] = config.dlt.month;
92+
conf["DL_TIME"]["hour"] = config.dlt.hour;
93+
conf["DL_TIME"]["offset"] = config.dlt.offset;
94+
95+
conf["I2C_ADDR"]["BME_280"] = config.i2c.BME_280;
96+
conf["I2C_ADDR"]["BH1750"] = config.i2c.BH1750;
97+
conf["I2C_ADDR"]["DS3231"] = config.i2c.DS3231;
98+
99+
conf["MAX7219_PINS"]["CLK_PIN"] = config.max7219.CLK_PIN;
100+
conf["MAX7219_PINS"]["DATA_PIN"] = config.max7219.DATA_PIN;
101+
conf["MAX7219_PINS"]["CS_PIN"] = config.max7219.CS_PIN;
102+
103+
conf["PINS"]["BUTTON_PIN"] = config.pins.BUTTON_PIN;
104+
conf["PINS"]["BUZZER_PIN"] = config.pins.BUZZER_PIN;
105+
106+
conf["NTP_SERVER"] = config.ntpServer;
107+
conf["LANGUAGE"] = config.language;
108+
conf["BUZZ_SOUND"] = config.buzzSound;
109+
conf["FAHRENHEIT"] = config.fahrenheit;
110+
111+
conf["WIFI"]["SSID"] = config.wifi.SSID;
112+
conf["WIFI"]["PASSWORD"] = config.wifi.PASSWORD;
113+
114+
if (serializeJson(conf, file) == 0) {
115+
Serial.println(F("Failed to write to file"));
116+
}
117+
118+
file.close();
119+
}
120+
70121
void printConfiguration(Config config) {
71122
Serial.printf("Standard TimeChangeRule:\n");
72123
Serial.printf(" Abbreviation: %s\n", config.std.abbrev);

lib/Config_Parser/Config_Parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct Config {
4242
};
4343

4444
void loadConfiguration(Config& config);
45+
void storeConfiguration(Config config);
4546
void printConfiguration(Config config);
4647

4748
#endif

0 commit comments

Comments
 (0)