AceUtils  0.5.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> // eeprom_buffered_*()
15 
43 class BufferedEEPROMClass {
44 public:
45  BufferedEEPROMClass() = default;
46 
47  void begin(size_t size) {
48  (void) size;
49  eeprom_buffer_fill();
50  }
51 
52  uint8_t read(int const address) {
53  return eeprom_buffered_read_byte(address);
54  }
55 
56  void write(int const address, uint8_t const val) {
57  eeprom_buffered_write_byte(address, val);
58  }
59 
60  bool commit() { eeprom_buffer_flush(); return true; }
61 
62  void end() { commit(); }
63 
64  template<typename T>
65  T &get(int address, T &t) {
66  size_t dataSize = sizeof(T);
67  uint8_t* data = (uint8_t*) &t;
68  while (dataSize--) {
69  *data++ = read(address++);
70  }
71  return t;
72  }
73 
74  template<typename T>
75  const T &put(int address, const T &t) {
76  size_t dataSize = sizeof(T);
77  uint8_t* data = (uint8_t*) &t;
78  while (dataSize--) {
79  write(address++, *data++);
80  }
81  return t;
82  }
83 
84  size_t length() { return FLASH_PAGE_SIZE; }
85 
86  // Not implemented because the buffer address is not exposed and it's
87  // too much trouble to work around this using a helper class.
88  #if 0
89  uint8_t * getDataPtr();
90  uint8_t const * getConstDataPtr() const;
91 
92  uint8_t& operator[](int const address) {
93  return getDataPtr()[address];
94  }
95  uint8_t const & operator[](int const address) const {
96  return getConstDataPtr()[address];
97  }
98  #endif
99 };
100 
101 #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
102 extern BufferedEEPROMClass BufferedEEPROM;
103 #endif
104 
105 #endif // defined(ARDUINO_ARCH_STM32)
106 #endif // BUFFERED_EEPROM_CLASS_H