AceUtils  0.6.0
Useful Arduino utilties which are too small as separate libraries, but complex enough to be shared among multiple projects, and often have external dependencies to other libraries.
BufferedEEPROMClass.h
1 /*
2  Copyright (c) 2021 Brian T. Park.
3 
4  BufferedEEPROMClass.h - Buffered EEPROM emulation for STM32duino Based on the
5  API from EEPROM.h from the ESP8266 Arduino Core.
6 */
7 
8 #ifndef BUFFERED_EEPROM_CLASS_H
9 #define BUFFERED_EEPROM_CLASS_H
10 
11 #if defined(ARDUINO_ARCH_STM32)
12 
13 #include <stdint.h>
14 #include <Arduino.h>
15 #include <EEPROM.h> // eeprom_buffered_*()
16 
46 class BufferedEEPROMClass {
47 public:
48  BufferedEEPROMClass() = default;
49 
50  void begin() {
51  eeprom_buffer_fill();
52  }
53 
54  uint8_t read(int const address) {
55  return eeprom_buffered_read_byte(address);
56  }
57 
58  void write(int const address, uint8_t const val) {
59  eeprom_buffered_write_byte(address, val);
60  }
61 
62  bool commit() { eeprom_buffer_flush(); return true; }
63 
64  void end() { commit(); }
65 
66  template <typename T>
67  T &get(int address, T &t) {
68  size_t dataSize = sizeof(T);
69  uint8_t* data = (uint8_t*) &t;
70  while (dataSize--) {
71  *data++ = read(address++);
72  }
73  return t;
74  }
75 
76  template <typename T>
77  const T &put(int address, const T &t) {
78  size_t dataSize = sizeof(T);
79  uint8_t* data = (uint8_t*) &t;
80  while (dataSize--) {
81  write(address++, *data++);
82  }
83  return t;
84  }
85 
94  size_t length() const {
95  return E2END + 1;
96  }
97 
98  // Not implemented because the buffer address is not exposed and it's
99  // too much trouble to work around this using a helper class.
100  #if 0
101  uint8_t * getDataPtr();
102  uint8_t const * getConstDataPtr() const;
103 
104  uint8_t& operator[](int const address) {
105  return getDataPtr()[address];
106  }
107  uint8_t const & operator[](int const address) const {
108  return getConstDataPtr()[address];
109  }
110  #endif
111 };
112 
113 #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
114 extern BufferedEEPROMClass BufferedEEPROM;
115 #endif
116 
117 #endif // defined(ARDUINO_ARCH_STM32)
118 #endif // BUFFERED_EEPROM_CLASS_H