Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
eeprom.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 11.06.2013 21:14:49 EDT
3 // File: eeprom.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __EEPROM_HEADER
12 #define __EEPROM_HEADER
13 
14 //DO NOT INCLUDE THIS FILE AND adv_eeprom.hpp AT THE SAME TIME
15 
16 #include "serializer.hpp"
17 #include "../ustd/unordered_map.hpp"
18 #include "../util/reset.hpp"
19 #include "../util/checksum.hpp"
20 
21 #include <EEPROM.h>
22 
23 namespace com {
24  template<typename EEPROM_concept, unsigned int EEPROM_size>
25  class EEPROMV2_class {
26  typedef uint16_t adress_type;
27  typedef typename oss_class<EEPROM_concept, EEPROM_size>::size_type size_type;
28  enum{not_corrupt = 0, corrupt = 0xFF, not_set = 2};
29  public:
30  EEPROMV2_class(EEPROM_concept & eeprom): oes_(eeprom), ies_(eeprom), current_free_(1), corrupted_(not_set) {
31  }
32  //------------------- load from eeprom and reset if corrupted-------------------
33  void reset() { //force a reset without clear
34  corrupted_ = corrupt;
35  }
36  template<typename T>
38  if(corrupted_ != corrupt and corrupted_ != not_corrupt) {
39  corrupted_ = ies_.read();
40  oes_.write(not_corrupt);
41  }
42  if(corrupted_ == corrupt) {
43  adjust_pos(t, oes_.pos());
44  oes_ & t;
45  oes_.write(util::checksum(t));
46  }
47  else {
48  adjust_pos(t, ies_.pos());
49  ies_ & t;
50  if(ies_.read() != util::checksum(t)) {
51  DEBUG_MSG("eeprom is corrupted! reset and fresh init incomming")
52  oes_.pos() = 0;
53  oes_.write(corrupt);
54  util::reset();
55  }
56  }
57  return (*this);
58  }
59  //------------------- write to eeprom -------------------
60  template<typename T>
62  oes_.pos() = map_.at(adress_type(&t));
63  oes_ & t;
64  oes_.write(util::checksum(t));
65  return (*this);
66  }
67  //------------------- read from eeprom -------------------
68  template<typename T>
70  ies_.pos() = map_.at(adress_type(&t));
71  ies_ & t;
72  return (*this);
73  }
74  //------------------- jump to the right adress -------------------
75  template<typename T>
76  void adjust_pos(T & t, size_type & pos) {
77  if(!map_.contains(adress_type(&t))) {
78  map_[adress_type(&t)] = current_free_;
79  current_free_ += sizeof(t) + 1; //+1 for checksum
80  ASSERT(current_free_ < EEPROM_size);
81  pos = map_[adress_type(&t)];
82  }
83  else
84  pos = map_[adress_type(&t)];
85  }
86  //------------------- delete all eeprom -------------------
87  void clear(uint8_t const & arg = 0xFF) {
88  oes_.pos() = 0;
89  for(adress_type i = 0; i < EEPROM_size; ++i) {
90  oes_ & arg;
91  }
92  oes_.pos() = 0;
93  }
94  private:
98  adress_type current_free_;
99  uint8_t corrupted_;
100  };
101 
102  EEPROMV2_class<EEPROMClass, 1024> eeprom(EEPROM);
103 }//end namespace com
104 #endif //__EEPROM_HEADER
size_type & pos()
Definition: serializer.hpp:203
void clear(uint8_t const &arg=0xFF)
Definition: eeprom.hpp:87
EEPROMV2_class< EEPROMClass, 1024 > eeprom(EEPROM)
size_type & pos()
Definition: serializer.hpp:149
EEPROMV2_class & operator&(T &t)
Definition: eeprom.hpp:37
EEPROMV2_class & operator<<(T &t)
Definition: eeprom.hpp:61
Definition: adv_eeprom.hpp:44
#define DEBUG_MSG(x)
Definition: ard_assert.hpp:32
uint16_t size_type
Definition: serializer.hpp:113
EEPROMV2_class & operator>>(T &t)
Definition: eeprom.hpp:69
void reset()
Definition: reset.hpp:15
bool contains(K const &key) const
Definition: unordered_map.hpp:64
void reset()
Definition: eeprom.hpp:33
V & at(K const &key)
Definition: unordered_map.hpp:53
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
#define ASSERT(exp)
Definition: ard_assert.hpp:33
EEPROMV2_class(EEPROM_concept &eeprom)
Definition: eeprom.hpp:30
uint8_t read()
Definition: serializer.hpp:199