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#include <StreamUtils.hpp>
5
6#include "utils/logging.h"
7#include "utils/streaming_json.h" // only for debugging
8
9FLASHMEM void debug_print_json(JsonObjectConst thing) {
10 utils::StreamingJson s(Serial);
11 s.begin_dict();
12 s.kv("type", "log");
13 s.kv("source", "manual_debug_log");
14 s.key("thing");
15 serializeJson(thing, Serial);
16 s.end_dict();
17 s.endln();
18}
19
20FLASHMEM void nvmconfig::PersistentSettingsWriter::read_from_eeprom() {
21 DynamicJsonDocument deserialized_conf_doc(eeprom_size);
22 StreamUtils::EepromStream eepromStream(eeprom_address, eeprom_size);
23 auto error = use_messagepack ? deserializeMsgPack(deserialized_conf_doc, eepromStream)
24 : deserializeJson(deserialized_conf_doc, eepromStream);
25 if (error) {
26 LOG_ERROR(
27 "nvmconfig::PersistentSettingsWriter::read_from_eeprom(): Failure, will fall back to default values.");
28 LOG4("DeserializationError code: ", error.code(), " and explanation: ", error.c_str());
29 }
30
31 if (deserialized_conf_doc.isNull()) {
32 LOG_ERROR("EEPROM JSON Document is empty");
33 }
34
35 auto deserialized_conf = deserialized_conf_doc.as<JsonObject>();
36
37 LOG_ALWAYS("PersistentSettingsWriter::read_from_eeprom has read this data...");
38 debug_print_json(deserialized_conf);
39
40 version = deserialized_conf["version"];
41
42 if (!error && version >= required_magic) {
43 fromJson(deserialized_conf, Context::Flash);
44 } else {
45 LOG_ERROR("PersistentSettingsWriter::read_from_eeprom(): Invalid Magic, falling back to default values.");
46 LOG4("Required magic byte values: ", required_magic, " Read magic byte values: ", version);
47 reset_defaults(/*write_to_eeprom*/ false);
48 }
49}
50
51FLASHMEM size_t nvmconfig::PersistentSettingsWriter::write_to_eeprom() {
52 DynamicJsonDocument serialized_conf_doc(eeprom_size);
53 auto serialized_conf = serialized_conf_doc.to<JsonObject>();
54 toJson(serialized_conf, Context::Flash);
55 StreamUtils::EepromStream eepromStream(eeprom_address, eeprom_size);
56 size_t consumed_size = use_messagepack ? serializeMsgPack(serialized_conf, eepromStream)
57 : serializeJson(serialized_conf, eepromStream);
58 eepromStream.flush();
59
60 // LOG_ALWAYS("PersistentSettingsWriter::write_to_eeprom has written to EEPROM...");
61 // debug_print_json(serialized_conf);
62
63 LOGMEV("Consumed %d Bytes from %d available ones (%.2f%%); Serialization: %s\n", consumed_size, eeprom_size,
64 100.0 * consumed_size / eeprom_size, use_messagepack ? "Messagepack" : "JSON");
65
66 return consumed_size;
67}
FLASHMEM void debug_print_json(JsonObjectConst thing)
Definition persistent.cpp:9