REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
persistent.cpp
Go to the documentation of this file.
2
3#ifdef ARDUINO
4
5#include <ArduinoJson.h>
6#include <StreamUtils.hpp>
7
8#include "utils/logging.h"
9#include "utils/streaming_json.h" // only for debugging
10
11FLASHMEM void debug_print_json(JsonObjectConst thing) {
12 utils::StreamingJson s(Serial);
13 s.begin_dict();
14 s.kv("type", "log");
15 s.kv("source", "manual_debug_log");
16 s.key("thing");
17 serializeJson(thing, Serial);
18 s.end_dict();
19 s.endln();
20}
21
22FLASHMEM void nvmconfig::PersistentSettingsWriter::info(JsonObject msg_out) {
23 // read_from_json(serialized_conf);
24 // write_to_json(msg_out.createNestedObject("updated_config"));
25 msg_out["available_bytes"] = eeprom_size;
26 msg_out["consumed_bytes"] = write_to_eeprom();
27}
28
29FLASHMEM void nvmconfig::PersistentSettingsWriter::toJson(JsonObject target, Context c) {
30 target["version"] = version;
31 for (auto const &sys : subsystems) {
32 sys->toJson(target.createNestedObject(sys->name()), c);
33 if (!target[sys->name()].size())
34 target.remove(sys->name());
35 }
36}
37
38FLASHMEM void nvmconfig::PersistentSettingsWriter::fromJson(JsonObjectConst target, Context c) {
39 for (auto const &sys : subsystems) {
40 if (target.containsKey(sys->name()))
41 sys->fromJson(target[sys->name()], c);
42 }
43}
44
45FLASHMEM void nvmconfig::PersistentSettingsWriter::reset_defaults(bool do_write_to_eeprom) {
46 version = required_magic;
47 for (auto const &sys : subsystems)
48 sys->reset_defaults();
49 if (do_write_to_eeprom)
50 write_to_eeprom();
51}
52
54 DynamicJsonDocument deserialized_conf_doc(eeprom_size);
55 StreamUtils::EepromStream eepromStream(eeprom_address, eeprom_size);
56 auto error = use_messagepack ? deserializeMsgPack(deserialized_conf_doc, eepromStream)
57 : deserializeJson(deserialized_conf_doc, eepromStream);
58 if (error) {
60 "nvmconfig::PersistentSettingsWriter::read_from_eeprom(): Failure, will fall back to default values.");
61 LOG4("DeserializationError code: ", error.code(), " and explanation: ", error.c_str());
62 }
63
64 if(deserialized_conf_doc.isNull()) {
65 LOG_ERROR("EEPROM JSON Document is empty");
66 }
67
68 auto deserialized_conf = deserialized_conf_doc.as<JsonObject>();
69
70 //LOG_ALWAYS("PersistentSettingsWriter::read_from_eeprom has read this data...");
71 //debug_print_json(deserialized_conf);
72
73 version = deserialized_conf["version"];
74
75 if (!error && version >= required_magic) {
76 fromJson(deserialized_conf, Context::Flash);
77 } else {
78 LOG_ERROR("PersistentSettingsWriter::read_from_eeprom(): Invalid Magic, falling back to default values.");
79 LOG4("Required magic byte values: ", required_magic, " Read magic byte values: ", version);
80 reset_defaults(/*write_to_eeprom*/ false);
81 }
82}
83
85 DynamicJsonDocument serialized_conf_doc(eeprom_size);
86 auto serialized_conf = serialized_conf_doc.to<JsonObject>();
87 toJson(serialized_conf, Context::Flash);
88 StreamUtils::EepromStream eepromStream(eeprom_address, eeprom_size);
89 size_t consumed_size = use_messagepack ? serializeMsgPack(serialized_conf, eepromStream)
90 : serializeJson(serialized_conf, eepromStream);
91 eepromStream.flush();
92
93 //LOG_ALWAYS("PersistentSettingsWriter::write_to_eeprom has written to EEPROM...");
94 //debug_print_json(serialized_conf);
95
96 LOGMEV("Consumed %d Bytes from %d available ones (%.2f%%); Serialization: %s\n", consumed_size, eeprom_size,
97 100.0 * consumed_size / eeprom_size, use_messagepack ? "Messagepack" : "JSON");
98
99 return consumed_size;
100}
101
102#endif // ARDUINO
void fromJson(JsonObjectConst src, Context c)
This will only call the respective fromJson call in the subsystem if its key is given in the object.
void info(JsonObject target)
size_t write_to_eeprom()
serialize configuration to eeprom,
void toJson(JsonObject target, Context c)
Note that empty subsystems get removed, i.e.
void reset_defaults(bool write_to_eeprom=true)
void read_from_eeprom()
read configuration from eeprom
The Streaming JSON API provides a way of constructing (writing) JSON messages without RAM overhead.
void kv(const char *_key, V _val)
void key(const char *str, char quote='"')
#define LOG_ERROR(message)
Definition logging.h:37
#define LOGMEV(message,...)
Definition logging.h:93
#define LOG4(a, b, c, d)
Definition logging.h:110
static constexpr int eeprom_size
Definition persistent.h:42
static constexpr uint32_t required_magic
Definition persistent.h:48
static constexpr int eeprom_address
Definition persistent.h:42
@ Flash
Flash-Facing (writing/reading)
FLASHMEM void debug_print_json(JsonObjectConst thing)