REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
persistent.cpp
Go to the documentation of this file.
1#include "nvmconfig/persistent.h"
2
3#include <ArduinoJson.h>
4
5#include "utils/error.h"
6#include "utils/logging.h"
7#include "utils/streaming_json.h" // only for debugging
8
9/*
10FLASHMEM void debug_print_json(JsonObjectConst thing) {
11 utils::StreamingJson s(Serial);
12 s.begin_dict();
13 s.kv("type", "log");
14 s.kv("source", "manual_debug_log");
15 s.key("thing");
16 serializeJson(thing, Serial);
17 s.end_dict();
18 s.endln();
19}*/
20
21FLASHMEM void nvmconfig::PersistentSettingsWriter::info(JsonObject msg_out) {
22 // read_from_json(serialized_conf);
23 // write_to_json(msg_out.createNestedObject("updated_config"));
24 msg_out["available_bytes"] = eeprom_size;
25 msg_out["consumed_bytes"] = write_to_eeprom();
26}
27
28FLASHMEM void nvmconfig::PersistentSettingsWriter::toJson(JsonObject target, Context c) {
29 target["version"] = version;
30 for (auto const &sys : subsystems) {
31 sys->toJson(target.createNestedObject(sys->name()), c);
32 if (!target[sys->name()].size())
33 target.remove(sys->name());
34 }
35}
36
37FLASHMEM void nvmconfig::PersistentSettingsWriter::fromJson(JsonObjectConst target, Context c) {
38 for (auto const &sys : subsystems) {
39 if (target.containsKey(sys->name()))
40 sys->fromJson(target[sys->name()], c);
41 }
42}
43
44FLASHMEM void nvmconfig::PersistentSettingsWriter::reset_defaults(bool do_write_to_eeprom) {
45 version = required_magic;
46 for (auto const &sys : subsystems)
47 sys->reset_defaults();
48 if (do_write_to_eeprom)
49 write_to_eeprom();
50}
51
52/*
53FLASHMEM size_t nvmconfig::PersistentSettingsWriter::write_to_eeprom() {
54 DynamicJsonDocument serialized_conf_doc(eeprom_size);
55 auto serialized_conf = serialized_conf_doc.to<JsonObject>();
56 toJson(serialized_conf, Context::Flash);
57 StreamUtils::EepromStream eepromStream(eeprom_address, eeprom_size);
58 size_t consumed_size = use_messagepack ? serializeMsgPack(serialized_conf, eepromStream)
59 : serializeJson(serialized_conf, eepromStream);
60 eepromStream.flush();
61
62 //LOG_ALWAYS("PersistentSettingsWriter::write_to_eeprom has written to EEPROM...");
63 //debug_print_json(serialized_conf);
64
65 LOGMEV("Consumed %d Bytes from %d available ones (%.2f%%); Serialization: %s\n", consumed_size, eeprom_size,
66 100.0 * consumed_size / eeprom_size, use_messagepack ? "Messagepack" : "JSON");
67
68 return consumed_size;
69}
70*/