forked from khoih-prog/FlashStorage_STM32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EEPROM_get.ino
92 lines (68 loc) · 2.81 KB
/
EEPROM_get.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/******************************************************************************************************************************************
EEPROM_CRC.ino
For STM32 using Flash emulated-EEPROM
The FlashStorage_STM32 library aims to provide a convenient way to store and retrieve user's data using the non-volatile flash memory
of STM32F/L/H/G/WB/MP1. It's using the buffered read and write to minimize the access to Flash.
It now supports writing and reading the whole object, not just byte-and-byte.
Inspired by Cristian Maglie's FlashStorage (https://github.com/cmaglie/FlashStorage)
Built by Khoi Hoang https://github.com/khoih-prog/FlashStorage_STM32
Licensed under MIT license
******************************************************************************************************************************************/
/***
eeprom_get example.
This shows how to use the EEPROM.get() method.
To pre-set the EEPROM data, run the example sketch eeprom_put. This sketch will run without it.
However, the values shown will be shown from what ever is already on the EEPROM.
This may cause the serial object to print out a large string of garbage if there is no null character
inside one of the strings loaded.
Written by Christopher Andrews 2015
Released under MIT licence.
***/
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include <FlashStorage_STM32.h>
struct MyObject
{
float field1;
byte field2;
char name[10];
};
void secondTest()
{
int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
MyObject customVar; //Variable to store custom object read from EEPROM.
EEPROM.get(eeAddress, customVar);
Serial.println("Read custom object from EEPROM: ");
Serial.println(customVar.field1, 5);
Serial.println(customVar.field2);
Serial.println(customVar.name);
}
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print(F("\nStart EEPROM_get on "));
Serial.println(BOARD_NAME);
Serial.println(FLASH_STORAGE_STM32_VERSION);
Serial.print("EEPROM length: ");
Serial.println(EEPROM.length());
float f = 0.00f; //Variable to store data read from EEPROM.
int eeAddress = 0; //EEPROM address to start reading from
Serial.print("Read float from EEPROM: ");
//Get the float data from the EEPROM at position 'eeAddress'
EEPROM.get(eeAddress, f);
Serial.println(f, 3); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
/***
As get also returns a reference to 'f', you can use it inline.
E.g: Serial.print( EEPROM.get( eeAddress, f ) );
***/
/***
Get can be used with custom structures too.
I have separated this into an extra function.
***/
secondTest(); //Run the next test.
}
void loop()
{
/* Empty loop */
}