REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
persistent.h
Go to the documentation of this file.
1// Copyright (c) 2023 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
4
5#pragma once
6
7#ifdef ARDUINO
8
9#include "utils/json.h"
10#include "utils/singleton.h"
11#include <list>
12
13namespace nvmconfig {
14
23enum class Context {
24 User,
25 Flash
26};
27
29 virtual std::string name() const = 0;
30 virtual void reset_defaults() = 0;
31 virtual void fromJson(JsonObjectConst src, Context c) = 0;
32 virtual void toJson(JsonObject target, Context c) const = 0;
33
36 virtual void clear() {}
37};
38
39// Teensy EEPROM is 4KB in size. This class claims the beginning
40// of this storage. The required size is generously estimated, may be much less.
41// Remember increasing the storage when more data have to be stored.
42static constexpr int eeprom_address = 0, eeprom_size = 2'000;
43
44// This number checks as a proof that the EEPROM stores something senseful. If it
45// does not store a number >= this constant, it is resetted.
46// Must not start from 0 in order to distinguish from "value not found" errors,
47// thus the arbitrary nonzero 0xAA prefix.
48static constexpr uint32_t required_magic = 0xAA03;
49
63class PersistentSettingsWriter : public utils::HeapSingleton<PersistentSettingsWriter> {
64 uint32_t version;
65 static constexpr bool use_messagepack = false;
66
67public:
68 std::list<PersistentSettings *> subsystems;
69
70 size_t write_to_eeprom();
71 void read_from_eeprom();
72
73 // these methods just thread over subsystems
74 void reset_defaults(bool write_to_eeprom = true);
75
80 void fromJson(JsonObjectConst src, Context c);
81
86 void toJson(JsonObject target, Context c);
87
88 void info(JsonObject target);
89
90 // Update configuration with a user call.
91 // void set(JsonObjectConst serialized_conf, JsonObject& msg_out);
92};
93
94} // namespace nvmconfig
95
96#endif // ARDUINO
Persistent user-changable configuration of the Teensy Microcontroller.
Definition persistent.h:63
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
std::list< PersistentSettings * > subsystems
Definition persistent.h:68
Define singletons which are not static-space allocated (and thus consume valuable ICTM space).
Definition singleton.h:46
uint32_t
Definition flasher.cpp:195
uint32_t src
Definition flasher.cpp:63
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
@ User
User-Facing (writing/reading)
@ Flash
Flash-Facing (writing/reading)
virtual void reset_defaults()=0
virtual void clear()
Clear local memory in order to save RAM if an object is not needed during runtime but only at startup...
Definition persistent.h:36
virtual std::string name() const =0
virtual void fromJson(JsonObjectConst src, Context c)=0
virtual void toJson(JsonObject target, Context c) const =0