11 #ifndef __ADV_EEPROM_HEADER
12 #define __ADV_EEPROM_HEADER
17 #include "../ustd/unordered_map.hpp"
18 #include "../ustd/pair.hpp"
19 #include "../util/reset.hpp"
20 #include "../util/checksum.hpp"
43 template<
typename EEPROM_concept, u
int16_t EEPROM_size>
45 typedef uint16_t adress_type;
46 typedef uint8_t version_type;
48 enum{not_corrupt = 0, corrupt = 0xFF, not_set = 2};
50 EEPROMV2_class(EEPROM_concept &
eeprom): oes_(eeprom), ies_(eeprom), current_free_(1), corrupted_(not_set) {
58 if(corrupted_ != corrupt and corrupted_ != not_corrupt) {
59 corrupted_ = ies_.
read();
60 oes_.
write(not_corrupt);
62 if(corrupted_ == corrupt) {
72 DEBUG_MSG(
"eeprom is corrupted! reset and fresh init incomming")
83 version_type &
v = map_[adress_type(&t)].second;
84 adress_type vmv_begin = map_[adress_type(&t)].first + v * (
sizeof(t) + 1);
89 version_type hw_version;
92 oes_.
pos() = map_.
at(adress_type(&t)).
first + (
sizeof(t) + 1) * hw_version;
102 version_type &
v = map_[adress_type(&t)].second;
103 adress_type vmv_begin = map_[adress_type(&t)].first + v * (
sizeof(t) + 1);
108 version_type hw_version;
111 ies_.
pos() = map_.
at(adress_type(&t)).
first + (
sizeof(t) + 1) * hw_version;
122 if(!map_.
contains(adress_type(&t))) {
123 map_[adress_type(&t)].first = current_free_;
127 map_[adress_type(&t)].second =
version();
131 ASSERT(current_free_ < EEPROM_size);
132 pos = map_[adress_type(&t)].first;
135 pos = map_[adress_type(&t)].first;
141 version_type &
v = map_[adress_type(&t)].second;
142 oes_.
pos() = map_[adress_type(&t)].first + v * (
sizeof(t) + 1);
143 for(version_type
i = 0;
i < v; ++
i) {
148 void clear(uint8_t
const & arg = 0xFF) {
150 for(adress_type
i = 0;
i < EEPROM_size; ++
i) {
167 void swap_versions(T & t, version_type
const & v1, version_type
const & v2) {
168 version_type &
v = map_[adress_type(&t)].second;
169 adress_type vmv_begin = map_[adress_type(&t)].first + v * (
sizeof(t) + 1);
171 ASSERT(v1 < v and v2 < v);
174 ies_.
pos() = vmv_begin + v1;
176 ies_.
pos() = vmv_begin + v2;
179 oes_.
pos() = vmv_begin + v1;
181 oes_.
pos() = vmv_begin + v2;
186 version_type &
v = map_[adress_type(&t)].second;
187 adress_type vmv_begin = map_[adress_type(&t)].first + v * (
sizeof(t) + 1);
189 ASSERT(v1 < v and v2 < v);
190 version_type hw_v1, temp;
191 oes_.
pos() = vmv_begin + v1;
192 ies_.
pos() = vmv_begin + v1;
196 for(version_type
i = v1;
i < v2; ++
i) {
204 version_type &
v = map_[adress_type(&t)].second;
211 adress_type current_free_;
215 EEPROMV2_class<EEPROMClass, 1024>
eeprom(EEPROM);
217 #endif //__ADV_EEPROM_HEADER
size_type & pos()
Definition: serializer.hpp:203
void clear(uint8_t const &arg=0xFF)
Definition: adv_eeprom.hpp:148
Definition: serializer.hpp:111
void move_version_to(T &t, version_type const &v1, version_type const &v2)
Definition: adv_eeprom.hpp:185
uint8_t const & operator()() const
Definition: adv_eeprom.hpp:35
EEPROMV2_class< EEPROMClass, 1024 > eeprom(EEPROM)
size_type & pos()
Definition: serializer.hpp:149
#define ASSERT_MSG(exp, msg)
Definition: ard_assert.hpp:34
EEPROMV2_class & operator&(T &t)
Definition: adv_eeprom.hpp:57
EEPROMV2_class & operator<<(T &t)
Definition: adv_eeprom.hpp:82
Definition: adv_eeprom.hpp:44
#define DEBUG_MSG(x)
Definition: ard_assert.hpp:32
EEPROMV2_class & operator<<(version_class &v)
Definition: adv_eeprom.hpp:159
EEPROMV2_class & operator>>(T &t)
Definition: adv_eeprom.hpp:101
version_class()
Definition: adv_eeprom.hpp:29
void reset()
Definition: reset.hpp:15
void swap_versions(T &t, version_type const &v1, version_type const &v2)
Definition: adv_eeprom.hpp:167
class com::version_class version
Definition: unordered_map.hpp:20
bool contains(K const &key) const
Definition: unordered_map.hpp:64
void move_version_to_end(T &t, version_type const &v1)
Definition: adv_eeprom.hpp:203
void reset()
Definition: adv_eeprom.hpp:53
V & at(K const &key)
Definition: unordered_map.hpp:53
EEPROMV2_class & operator>>(version_class &v)
Definition: adv_eeprom.hpp:162
void write(uint8_t const &in)
Definition: serializer.hpp:144
void adjust_pos(T &t, size_type &pos)
Definition: adv_eeprom.hpp:121
uint8_t checksum(T const &t)
Definition: checksum.hpp:17
void init_vmv(T &t)
Definition: adv_eeprom.hpp:140
EEPROMV2_class & operator&(version_class &v)
Definition: adv_eeprom.hpp:156
Definition: adv_eeprom.hpp:27
#define ASSERT(exp)
Definition: ard_assert.hpp:33
first_type first
Definition: pair.hpp:27
version_class & operator()(uint8_t const &nr)
Definition: adv_eeprom.hpp:31
EEPROMV2_class(EEPROM_concept &eeprom)
Definition: adv_eeprom.hpp:50
uint8_t read()
Definition: serializer.hpp:199