This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
StoreNameAndSurname.ino
106 lines (81 loc) · 3.36 KB
/
StoreNameAndSurname.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/******************************************************************************************************************************************
StoreNameAndSurname.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
******************************************************************************************************************************************/
// Demonstrate how to use FlashStorage_STM32 with an API that is similar to the EEPROM library to Store and retrieve structured data.
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include <FlashStorage_STM32.h>
const int WRITTEN_SIGNATURE = 0xBEEFDEED;
// Create a structure that is big enough to contain a name
// and a surname. The "valid" variable is set to "true" once
// the structure is filled with actual data for the first time.
typedef struct
{
char name[100];
char surname[100];
} Person;
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print(F("\nStart StoreNameAndSurname on "));
Serial.println(BOARD_NAME);
Serial.println(FLASH_STORAGE_STM32_VERSION);
Serial.print("EEPROM length: ");
Serial.println(EEPROM.length());
// Check signature at address 0
int signature;
// Create a "Person" variable and call it "owner"
uint16_t storedAddress = 0;
Person owner;
EEPROM.get(storedAddress, signature);
// If the EEPROM is empty then no WRITTEN_SIGNATURE
if (signature == WRITTEN_SIGNATURE)
{
EEPROM.get(storedAddress + sizeof(signature), owner);
// Say hello to the returning user!
Serial.print("Hi ");
Serial.print(owner.name);
Serial.print(" ");
Serial.print(owner.surname);
Serial.println(", nice to see you again :-)");
Serial.println("Clearing WRITTEN_SIGNATURE for next try");
EEPROM.put(0, 0);
Serial.println("Done clearing signature in emulated EEPROM. You can reset now");
}
else
{
Serial.println("EEPROM is empty, writing WRITTEN_SIGNATURE and some example data:");
EEPROM.put(storedAddress, WRITTEN_SIGNATURE);
// ...in this case we ask for user data.
Serial.setTimeout(30000);
Serial.print("Insert your name : ");
String name = Serial.readStringUntil('\n');
Serial.println(name);
Serial.print("Insert your surname : ");
String surname = Serial.readStringUntil('\n');
Serial.println(surname);
// Fill the "owner" structure with the data entered by the user...
name.toCharArray(owner.name, 100);
surname.toCharArray(owner.surname, 100);
// ...and finally save everything into emulated-EEPROM
EEPROM.put(storedAddress + sizeof(signature), owner);
// Print a confirmation of the data inserted.
Serial.print("<< Your name: ");
Serial.print(owner.name);
Serial.print(". Your surname: ");
Serial.print(owner.surname);
Serial.println(" >> have been saved. Thank you!");
}
}
void loop()
{
// Do nothing...
}