forked from espressif/arduino-esp32
Add EEPROMClass and examples (#1034)
* enchanced EEPROMClass * Added eeprom examles and modified partition * added eeprom class and extra examples * No changes * No changes * added eeprom class and examples * fixed typo * length() returns user-defined sector size * updated and annotated example
This commit is contained in:
committed by
Me No Dev
parent
694c3a453f
commit
3310e2e31d
92
libraries/EEPROM/examples/eeprom_class/eeprom_class.ino
Normal file
92
libraries/EEPROM/examples/eeprom_class/eeprom_class.ino
Normal file
@ -0,0 +1,92 @@
|
||||
/*
|
||||
ESP32 eeprom_class example with EEPROM library
|
||||
|
||||
This simple example demonstrates using EEPROM library to store different data in
|
||||
ESP32 Flash memory in a multiple user-defined EEPROM partition (0x1000 or 4KB max size or less).
|
||||
|
||||
Install 'ESP32 Partiton Manager' ONCE from https://github.com/francis94c/ESP32Partitions
|
||||
And generate different partitions with 'partition_name'
|
||||
Usage: EEPROMClass ANY_OBJECT_NAME("partition_name", size);
|
||||
|
||||
Generated partition that would work perfectly with this example
|
||||
#Name, Type, SubType, Offset, Size, Flags
|
||||
nvs, data, nvs, 0x9000, 0x5000,
|
||||
otadata, data, ota, 0xe000, 0x2000,
|
||||
app0, app, ota_0, 0x10000, 0x140000,
|
||||
app1, app, ota_1, 0x150000, 0x140000,
|
||||
eeprom0, data, 0x99, 0x290000, 0x1000,
|
||||
eeprom1, data, 0x9a, 0x291000, 0x500,
|
||||
eeprom2, data, 0x9b, 0x292000, 0x100,
|
||||
spiffs, data, spiffs, 0x293000, 0x16d000,
|
||||
|
||||
Created for arduino-esp32 on 25 Dec, 2017
|
||||
by Elochukwu Ifediora (fedy0)
|
||||
*/
|
||||
|
||||
#include "EEPROM.h"
|
||||
|
||||
// Instantiate eeprom objects with parameter/argument names and size same as in the partition table
|
||||
EEPROMClass NAMES("eeprom0", 0x1000);
|
||||
EEPROMClass HEIGHT("eeprom1", 0x500);
|
||||
EEPROMClass AGE("eeprom2", 0x100);
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
Serial.println("Testing EEPROMClass\n");
|
||||
if (!NAMES.begin(NAMES.length())) {
|
||||
Serial.println("Failed to initialise NAMES");
|
||||
Serial.println("Restarting...");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
if (!HEIGHT.begin(HEIGHT.length())) {
|
||||
Serial.println("Failed to initialise HEIGHT");
|
||||
Serial.println("Restarting...");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
if (!AGE.begin(AGE.length())) {
|
||||
Serial.println("Failed to initialise AGE");
|
||||
Serial.println("Restarting...");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
char* name = "Teo Swee Ann";
|
||||
double height = 5.8;
|
||||
uint32_t age = 47;
|
||||
|
||||
// Write: Variables ---> EEPROM partitions
|
||||
NAMES.put(0, name);
|
||||
HEIGHT.put(0, height);
|
||||
AGE.put(0, age);
|
||||
Serial.print("name: "); Serial.println(name);
|
||||
Serial.print("height: "); Serial.println(height);
|
||||
Serial.print("age: "); Serial.println(age);
|
||||
Serial.println("------------------------------------\n");
|
||||
|
||||
// Clear variables
|
||||
name = '\0';
|
||||
height = 0;
|
||||
age = 0;
|
||||
Serial.print("name: "); Serial.println(name);
|
||||
Serial.print("height: "); Serial.println(height);
|
||||
Serial.print("age: "); Serial.println(age);
|
||||
Serial.println("------------------------------------\n");
|
||||
|
||||
// Read: Variables <--- EEPROM partitions
|
||||
NAMES.get(0, name);
|
||||
HEIGHT.get(0, height);
|
||||
AGE.get(0, age);
|
||||
Serial.print("name: "); Serial.println(name);
|
||||
Serial.print("height: "); Serial.println(height);
|
||||
Serial.print("age: "); Serial.println(age);
|
||||
|
||||
Serial.println("Done!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
}
|
90
libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino
Normal file
90
libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino
Normal file
@ -0,0 +1,90 @@
|
||||
/*
|
||||
ESP32 eeprom_extra example with EEPROM library
|
||||
|
||||
This simple example demonstrates using other EEPROM library resources
|
||||
|
||||
Created for arduino-esp32 on 25 Dec, 2017
|
||||
by Elochukwu Ifediora (fedy0)
|
||||
*/
|
||||
|
||||
#include "EEPROM.h"
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
Serial.println("\nTesting EEPROM Library\n");
|
||||
if (!EEPROM.begin(EEPROM.length())) {
|
||||
Serial.println("Failed to initialise EEPROM");
|
||||
Serial.println("Restarting...");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
int address = 0; // Same address is used through the example
|
||||
|
||||
EEPROM.writeByte(address, -128); // -2^7
|
||||
Serial.println(EEPROM.readByte(address));
|
||||
|
||||
EEPROM.writeChar(address, 'A'); // Same as writyByte and readByte
|
||||
Serial.println(char(EEPROM.readChar(address)));
|
||||
|
||||
EEPROM.writeUChar(address, 255); // 2^8 - 1
|
||||
Serial.println(EEPROM.readUChar(address));
|
||||
|
||||
EEPROM.writeShort(address, -32768); // -2^15
|
||||
Serial.println(EEPROM.readShort(address));
|
||||
|
||||
EEPROM.writeUShort(address, 65535); // 2^16 - 1
|
||||
Serial.println(EEPROM.readUShort(address));
|
||||
|
||||
EEPROM.writeInt(address, -2147483648); // -2^31
|
||||
Serial.println(EEPROM.readInt(address));
|
||||
|
||||
EEPROM.writeUInt(address, 4294967295); // 2^32 - 1
|
||||
Serial.println(EEPROM.readUInt(address));
|
||||
|
||||
EEPROM.writeLong(address, -2147483648); // Same as writeInt and readInt
|
||||
Serial.println(EEPROM.readLong(address));
|
||||
|
||||
EEPROM.writeULong(address, 4294967295); // Same as writeUInt and readUInt
|
||||
Serial.println(EEPROM.readULong(address));
|
||||
|
||||
int64_t value = -9223372036854775808; // -2^63
|
||||
EEPROM.writeLong64(address, value);
|
||||
value = 0; // Clear value
|
||||
value = EEPROM.readLong64(value);
|
||||
Serial.printf("0x%08X", (uint32_t)(value >> 32)); // Print High 4 bytes in HEX
|
||||
Serial.printf("%08X\n", (uint32_t)value); // Print Low 4 bytes in HEX
|
||||
|
||||
uint64_t Value = 18446744073709551615; // 2^64 - 1
|
||||
EEPROM.writeULong64(address, Value);
|
||||
Value = 0; // Clear Value
|
||||
Value = EEPROM.readULong64(Value);
|
||||
Serial.printf("0x%08X", (uint32_t)(Value >> 32)); // Print High 4 bytes in HEX
|
||||
Serial.printf("%08X\n", (uint32_t)Value); // Print Low 4 bytes in HEX
|
||||
|
||||
EEPROM.writeFloat(address, 1234.1234);
|
||||
Serial.println(EEPROM.readFloat(address), 4);
|
||||
|
||||
EEPROM.writeDouble(address, 123456789.123456789);
|
||||
Serial.println(EEPROM.readDouble(address), 8);
|
||||
|
||||
EEPROM.writeBool(address, true);
|
||||
Serial.println(EEPROM.readBool(address));
|
||||
|
||||
String sentence = "I love ESP32.";
|
||||
EEPROM.writeString(address, sentence);
|
||||
Serial.println(EEPROM.readString(address));
|
||||
|
||||
char gratitude[] = "Thank You Espressif!";
|
||||
EEPROM.writeString(address, gratitude);
|
||||
Serial.println(EEPROM.readString(address));
|
||||
|
||||
// See also the general purpose writeBytes() and readBytes() for BLOB in EEPROM library
|
||||
// To avoid data overwrite, next address should be chosen/offset by using "address =+ sizeof(previousData)"
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
}
|
Reference in New Issue
Block a user