Sfoglia il codice sorgente

Fixed Storage save/load

master
Robin Thoni 4 anni fa
parent
commit
4f4622a9e7
Firmato da: Robin THONI <robin@rthoni.com> ID Chiave GPG: 4E09DEF46B99E61E
1 ha cambiato i file con 30 aggiunte e 6 eliminazioni
  1. 30
    6
      Storage.cpp

+ 30
- 6
Storage.cpp Vedi File

@@ -1,19 +1,43 @@
1 1
 #include "Storage.h"
2 2
 #include <EEPROM.h>
3
+#include <Arduino.h>
4
+
5
+template <class T> int EEPROM_writeAnything(int ee, const T& value)
6
+{
7
+    const byte* p = (const byte*)(const void*)&value;
8
+    unsigned int i;
9
+    for (i = 0; i < sizeof(value); i++)
10
+        EEPROM.write(ee++, *p++);
11
+    return i;
12
+}
13
+
14
+template <class T> int EEPROM_readAnything(int ee, T& value)
15
+{
16
+    byte* p = (byte*)(void*)&value;
17
+    unsigned int i;
18
+    for (i = 0; i < sizeof(value); i++)
19
+        *p++ = EEPROM.read(ee++);
20
+    return i;
21
+}
3 22
 
4 23
 void Storage::load(AppCoreState& data)
5 24
 {
6
-    auto version = EEPROM.read(0);
25
+    uint8_t version;
26
+    uint32_t index = 0;
27
+    index += EEPROM_readAnything(index, version);
7 28
     if (version == 1)
8 29
     {
9
-        data.appState.water.setting = EEPROM.read(1);
10
-        data.appState.heater.setting = EEPROM.read(2);
30
+        index += EEPROM_readAnything(index, data.appState.water.setting);
31
+        index += EEPROM_readAnything(index, data.appState.heater.setting);
32
+        index += EEPROM_readAnything(index, data.appState.tempTrigger);
11 33
     }
12 34
 }
13 35
 
14 36
 void Storage::save(const AppCoreState& data)
15 37
 {
16
-    EEPROM.write(0, 1);
17
-    EEPROM.write(1, data.appState.water.setting);
18
-    EEPROM.write(2, data.appState.heater.setting);
38
+    uint32_t index = 0;
39
+    index += EEPROM_writeAnything(index, (uint8_t)1);
40
+    index += EEPROM_writeAnything(index, data.appState.water.setting);
41
+    index += EEPROM_writeAnything(index, data.appState.heater.setting);
42
+    index += EEPROM_writeAnything(index, data.appState.tempTrigger);
19 43
 }

Loading…
Annulla
Salva