Prvni ulozeni z chegewara githubu
This commit is contained in:
4
libraries/EEPROM/README.md
Normal file
4
libraries/EEPROM/README.md
Normal file
@ -0,0 +1,4 @@
|
||||
## EEPROM
|
||||
|
||||
EEPROM is deprecated. For new applications on ESP32, use Preferences. EEPROM is provided for backwards compatibility with existing Arduino applications.
|
||||
EEPROM is implemented using a single blob within NVS, so it is a container within a container. As such, it is not going to be a high performance storage method. Preferences will directly use nvs, and store each entry as a single object therein.
|
77
libraries/EEPROM/examples/eeprom_class/eeprom_class.ino
Normal file
77
libraries/EEPROM/examples/eeprom_class/eeprom_class.ino
Normal file
@ -0,0 +1,77 @@
|
||||
/*
|
||||
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 class objects.
|
||||
|
||||
Created for arduino-esp32 on 25 Dec, 2017
|
||||
by Elochukwu Ifediora (fedy0)
|
||||
converted to nvs by lbernstone - 06/22/2019
|
||||
*/
|
||||
|
||||
#include "EEPROM.h"
|
||||
|
||||
// Instantiate eeprom objects with parameter/argument names and sizes
|
||||
EEPROMClass NAMES("eeprom0");
|
||||
EEPROMClass HEIGHT("eeprom1");
|
||||
EEPROMClass AGE("eeprom2");
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
Serial.println("Testing EEPROMClass\n");
|
||||
if (!NAMES.begin(0x500)) {
|
||||
Serial.println("Failed to initialise NAMES");
|
||||
Serial.println("Restarting...");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
if (!HEIGHT.begin(0x200)) {
|
||||
Serial.println("Failed to initialise HEIGHT");
|
||||
Serial.println("Restarting...");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
if (!AGE.begin(0x100)) {
|
||||
Serial.println("Failed to initialise AGE");
|
||||
Serial.println("Restarting...");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
const char* name = "Teo Swee Ann";
|
||||
char rname[32];
|
||||
double height = 5.8;
|
||||
uint32_t age = 47;
|
||||
|
||||
// Write: Variables ---> EEPROM stores
|
||||
NAMES.writeString(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
|
||||
rname[0] = '\0';
|
||||
height = 0;
|
||||
age = 0;
|
||||
Serial.print("name: "); Serial.println(rname);
|
||||
Serial.print("height: "); Serial.println(height);
|
||||
Serial.print("age: "); Serial.println(age);
|
||||
Serial.println("------------------------------------\n");
|
||||
|
||||
// Read: Variables <--- EEPROM stores
|
||||
NAMES.get(0, rname);
|
||||
HEIGHT.get(0, height);
|
||||
AGE.get(0, age);
|
||||
Serial.print("name: "); Serial.println(rname);
|
||||
Serial.print("height: "); Serial.println(height);
|
||||
Serial.print("age: "); Serial.println(age);
|
||||
|
||||
Serial.println("Done!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
delay(0xFFFFFFFF);
|
||||
}
|
139
libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino
Normal file
139
libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino
Normal file
@ -0,0 +1,139 @@
|
||||
/*
|
||||
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(1000)) {
|
||||
Serial.println("Failed to initialise EEPROM");
|
||||
Serial.println("Restarting...");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
int address = 0;
|
||||
|
||||
EEPROM.writeByte(address, -128); // -2^7
|
||||
address += sizeof(byte);
|
||||
|
||||
EEPROM.writeChar(address, 'A'); // Same as writyByte and readByte
|
||||
address += sizeof(char);
|
||||
|
||||
EEPROM.writeUChar(address, 255); // 2^8 - 1
|
||||
address += sizeof(unsigned char);
|
||||
|
||||
EEPROM.writeShort(address, -32768); // -2^15
|
||||
address += sizeof(short);
|
||||
|
||||
EEPROM.writeUShort(address, 65535); // 2^16 - 1
|
||||
address += sizeof(unsigned short);
|
||||
|
||||
EEPROM.writeInt(address, -2147483648); // -2^31
|
||||
address += sizeof(int);
|
||||
|
||||
EEPROM.writeUInt(address, 4294967295); // 2^32 - 1
|
||||
address += sizeof(unsigned int);
|
||||
|
||||
EEPROM.writeLong(address, -2147483648); // Same as writeInt and readInt
|
||||
address += sizeof(long);
|
||||
|
||||
EEPROM.writeULong(address, 4294967295); // Same as writeUInt and readUInt
|
||||
address += sizeof(unsigned long);
|
||||
|
||||
int64_t value = -1223372036854775808LL; // -2^63
|
||||
EEPROM.writeLong64(address, value);
|
||||
address += sizeof(int64_t);
|
||||
|
||||
uint64_t Value = 18446744073709551615ULL; // 2^64 - 1
|
||||
EEPROM.writeULong64(address, Value);
|
||||
address += sizeof(uint64_t);
|
||||
|
||||
EEPROM.writeFloat(address, 1234.1234);
|
||||
address += sizeof(float);
|
||||
|
||||
EEPROM.writeDouble(address, 123456789.123456789);
|
||||
address += sizeof(double);
|
||||
|
||||
EEPROM.writeBool(address, true);
|
||||
address += sizeof(bool);
|
||||
|
||||
String sentence = "I love ESP32.";
|
||||
EEPROM.writeString(address, sentence);
|
||||
address += sentence.length() + 1;
|
||||
|
||||
char gratitude[21] = "Thank You Espressif!";
|
||||
EEPROM.writeString(address, gratitude);
|
||||
address += 21;
|
||||
|
||||
// See also the general purpose writeBytes() and readBytes() for BLOB in EEPROM library
|
||||
EEPROM.commit();
|
||||
address = 0;
|
||||
|
||||
Serial.println(EEPROM.readByte(address));
|
||||
address += sizeof(byte);
|
||||
|
||||
Serial.println((char)EEPROM.readChar(address));
|
||||
address += sizeof(char);
|
||||
|
||||
Serial.println(EEPROM.readUChar(address));
|
||||
address += sizeof(unsigned char);
|
||||
|
||||
Serial.println(EEPROM.readShort(address));
|
||||
address += sizeof(short);
|
||||
|
||||
Serial.println(EEPROM.readUShort(address));
|
||||
address += sizeof(unsigned short);
|
||||
|
||||
Serial.println(EEPROM.readInt(address));
|
||||
address += sizeof(int);
|
||||
|
||||
Serial.println(EEPROM.readUInt(address));
|
||||
address += sizeof(unsigned int);
|
||||
|
||||
Serial.println(EEPROM.readLong(address));
|
||||
address += sizeof(long);
|
||||
|
||||
Serial.println(EEPROM.readULong(address));
|
||||
address += sizeof(unsigned long);
|
||||
|
||||
value = 0;
|
||||
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
|
||||
address += sizeof(int64_t);
|
||||
|
||||
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
|
||||
address += sizeof(uint64_t);
|
||||
|
||||
Serial.println(EEPROM.readFloat(address), 4);
|
||||
address += sizeof(float);
|
||||
|
||||
Serial.println(EEPROM.readDouble(address), 8);
|
||||
address += sizeof(double);
|
||||
|
||||
Serial.println(EEPROM.readBool(address));
|
||||
address += sizeof(bool);
|
||||
|
||||
Serial.println(EEPROM.readString(address));
|
||||
address += sentence.length() + 1;
|
||||
|
||||
Serial.println(EEPROM.readString(address));
|
||||
address += 21;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
}
|
63
libraries/EEPROM/examples/eeprom_write/eeprom_write.ino
Normal file
63
libraries/EEPROM/examples/eeprom_write/eeprom_write.ino
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
EEPROM Write
|
||||
|
||||
Stores random values into the EEPROM.
|
||||
These values will stay in the EEPROM when the board is
|
||||
turned off and may be retrieved later by another sketch.
|
||||
*/
|
||||
|
||||
#include "EEPROM.h"
|
||||
|
||||
// the current address in the EEPROM (i.e. which byte
|
||||
// we're going to write to next)
|
||||
int addr = 0;
|
||||
#define EEPROM_SIZE 64
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("start...");
|
||||
if (!EEPROM.begin(EEPROM_SIZE))
|
||||
{
|
||||
Serial.println("failed to initialise EEPROM"); delay(1000000);
|
||||
}
|
||||
Serial.println(" bytes read from Flash . Values are:");
|
||||
for (int i = 0; i < EEPROM_SIZE; i++)
|
||||
{
|
||||
Serial.print(byte(EEPROM.read(i))); Serial.print(" ");
|
||||
}
|
||||
Serial.println();
|
||||
Serial.println("writing random n. in memory");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// need to divide by 4 because analog inputs range from
|
||||
// 0 to 1023 and each byte of the EEPROM can only hold a
|
||||
// value from 0 to 255.
|
||||
// int val = analogRead(10) / 4;
|
||||
int val = byte(random(10020));
|
||||
// write the value to the appropriate byte of the EEPROM.
|
||||
// these values will remain there when the board is
|
||||
// turned off.
|
||||
EEPROM.write(addr, val);
|
||||
Serial.print(val); Serial.print(" ");
|
||||
// advance to the next address. there are 512 bytes in
|
||||
// the EEPROM, so go back to 0 when we hit 512.
|
||||
// save all changes to the flash.
|
||||
addr = addr + 1;
|
||||
if (addr == EEPROM_SIZE)
|
||||
{
|
||||
Serial.println();
|
||||
addr = 0;
|
||||
EEPROM.commit();
|
||||
Serial.print(EEPROM_SIZE);
|
||||
Serial.println(" bytes written on Flash . Values are:");
|
||||
for (int i = 0; i < EEPROM_SIZE; i++)
|
||||
{
|
||||
Serial.print(byte(EEPROM.read(i))); Serial.print(" ");
|
||||
}
|
||||
Serial.println(); Serial.println("----------------------------------");
|
||||
}
|
||||
|
||||
delay(100);
|
||||
}
|
19
libraries/EEPROM/keywords.txt
Normal file
19
libraries/EEPROM/keywords.txt
Normal file
@ -0,0 +1,19 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For Ultrasound
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
EEPROM KEYWORD1
|
||||
EEPROMClass KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
9
libraries/EEPROM/library.properties
Normal file
9
libraries/EEPROM/library.properties
Normal file
@ -0,0 +1,9 @@
|
||||
name=EEPROM
|
||||
version=2.0.0
|
||||
author=Ivan Grokhotkov
|
||||
maintainer=Paolo Becchi <pbecchi@aerobusiness.it>
|
||||
sentence=Enables reading and writing data a sequential, addressable FLASH storage
|
||||
paragraph=
|
||||
category=Data Storage
|
||||
url=http://arduino.cc/en/Reference/EEPROM
|
||||
architectures=esp32
|
557
libraries/EEPROM/src/EEPROM.cpp
Normal file
557
libraries/EEPROM/src/EEPROM.cpp
Normal file
@ -0,0 +1,557 @@
|
||||
/*
|
||||
EEPROM.h -ported by Paolo Becchi to Esp32 from esp8266 EEPROM
|
||||
-Modified by Elochukwu Ifediora <ifedioraelochukwuc@gmail.com>
|
||||
-Converted to nvs lbernstone@gmail.com
|
||||
|
||||
Uses a nvs byte array to emulate EEPROM
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "EEPROM.h"
|
||||
#include <nvs.h>
|
||||
#include <esp_partition.h>
|
||||
#include <esp_log.h>
|
||||
|
||||
EEPROMClass::EEPROMClass(void)
|
||||
: _handle(0)
|
||||
, _data(0)
|
||||
, _size(0)
|
||||
, _dirty(false)
|
||||
, _name("eeprom")
|
||||
{
|
||||
}
|
||||
|
||||
EEPROMClass::EEPROMClass(uint32_t sector)
|
||||
// Only for compatiility, no sectors in nvs!
|
||||
: _handle(0)
|
||||
, _data(0)
|
||||
, _size(0)
|
||||
, _dirty(false)
|
||||
, _name("eeprom")
|
||||
{
|
||||
}
|
||||
|
||||
EEPROMClass::EEPROMClass(const char* name)
|
||||
: _handle(0)
|
||||
, _data(0)
|
||||
, _size(0)
|
||||
, _dirty(false)
|
||||
, _name(name)
|
||||
{
|
||||
}
|
||||
|
||||
EEPROMClass::~EEPROMClass() {
|
||||
end();
|
||||
}
|
||||
|
||||
bool EEPROMClass::begin(size_t size) {
|
||||
if (!size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
esp_err_t res = nvs_open(_name, NVS_READWRITE, &_handle);
|
||||
if (res != ESP_OK) {
|
||||
log_e("Unable to open NVS namespace: %d", res);
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t key_size = 0;
|
||||
res = nvs_get_blob(_handle, _name, NULL, &key_size);
|
||||
if(res != ESP_OK && res != ESP_ERR_NVS_NOT_FOUND) {
|
||||
log_e("Unable to read NVS key: %d", res);
|
||||
return false;
|
||||
}
|
||||
if (size < key_size) { // truncate
|
||||
log_w("truncating EEPROM from %d to %d", key_size, size);
|
||||
uint8_t* key_data = (uint8_t*) malloc(key_size);
|
||||
if(!key_data) {
|
||||
log_e("Not enough memory to truncate EEPROM!");
|
||||
return false;
|
||||
}
|
||||
nvs_get_blob(_handle, _name, key_data, &key_size);
|
||||
nvs_set_blob(_handle, _name, key_data, size);
|
||||
nvs_commit(_handle);
|
||||
free(key_data);
|
||||
}
|
||||
else if (size > key_size) { // expand or new
|
||||
size_t expand_size = size - key_size;
|
||||
uint8_t* expand_key = (uint8_t*) malloc(expand_size);
|
||||
if(!expand_key) {
|
||||
log_e("Not enough memory to expand EEPROM!");
|
||||
return false;
|
||||
}
|
||||
// check for adequate free space
|
||||
if(nvs_set_blob(_handle, "expand", expand_key, expand_size)) {
|
||||
log_e("Not enough space to expand EEPROM from %d to %d", key_size, size);
|
||||
free(expand_key);
|
||||
return false;
|
||||
}
|
||||
free(expand_key);
|
||||
nvs_erase_key(_handle, "expand");
|
||||
uint8_t* key_data = (uint8_t*) malloc(size);
|
||||
if(!key_data) {
|
||||
log_e("Not enough memory to expand EEPROM!");
|
||||
return false;
|
||||
}
|
||||
memset(key_data, 0xFF, size);
|
||||
if(key_size) {
|
||||
log_i("Expanding EEPROM from %d to %d", key_size, size);
|
||||
// hold data while key is deleted
|
||||
nvs_get_blob(_handle, _name, key_data, &key_size);
|
||||
nvs_erase_key(_handle, _name);
|
||||
} else {
|
||||
log_i("New EEPROM of %d bytes", size);
|
||||
}
|
||||
nvs_commit(_handle);
|
||||
nvs_set_blob(_handle, _name, key_data, size);
|
||||
free(key_data);
|
||||
nvs_commit(_handle);
|
||||
}
|
||||
|
||||
if (_data) {
|
||||
delete[] _data;
|
||||
}
|
||||
|
||||
_data = (uint8_t*) malloc(size);
|
||||
if(!_data) {
|
||||
log_e("Not enough memory for %d bytes in EEPROM", size);
|
||||
return false;
|
||||
}
|
||||
_size = size;
|
||||
nvs_get_blob(_handle, _name, _data, &_size);
|
||||
return true;
|
||||
}
|
||||
|
||||
void EEPROMClass::end() {
|
||||
if (!_size) {
|
||||
return;
|
||||
}
|
||||
|
||||
commit();
|
||||
if (_data) {
|
||||
delete[] _data;
|
||||
}
|
||||
_data = 0;
|
||||
_size = 0;
|
||||
|
||||
nvs_close(_handle);
|
||||
_handle = 0;
|
||||
}
|
||||
|
||||
uint8_t EEPROMClass::read(int address) {
|
||||
if (address < 0 || (size_t)address >= _size) {
|
||||
return 0;
|
||||
}
|
||||
if (!_data) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _data[address];
|
||||
}
|
||||
|
||||
void EEPROMClass::write(int address, uint8_t value) {
|
||||
if (address < 0 || (size_t)address >= _size)
|
||||
return;
|
||||
if (!_data)
|
||||
return;
|
||||
|
||||
// Optimise _dirty. Only flagged if data written is different.
|
||||
uint8_t* pData = &_data[address];
|
||||
if (*pData != value)
|
||||
{
|
||||
*pData = value;
|
||||
_dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool EEPROMClass::commit() {
|
||||
bool ret = false;
|
||||
if (!_size) {
|
||||
return false;
|
||||
}
|
||||
if (!_data) {
|
||||
return false;
|
||||
}
|
||||
if (!_dirty) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ESP_OK != nvs_set_blob(_handle, _name, _data, _size)) {
|
||||
log_e( "error in write");
|
||||
} else {
|
||||
_dirty = false;
|
||||
ret = true;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint8_t * EEPROMClass::getDataPtr() {
|
||||
_dirty = true;
|
||||
return &_data[0];
|
||||
}
|
||||
|
||||
/*
|
||||
Get EEPROM total size in byte defined by the user
|
||||
*/
|
||||
uint16_t EEPROMClass::length ()
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
|
||||
/*
|
||||
Convert EEPROM partition into nvs blob
|
||||
Call convert before you call begin
|
||||
*/
|
||||
uint16_t EEPROMClass::convert (bool clear, const char* EEPROMname, const char* nvsname)
|
||||
{
|
||||
uint16_t result = 0;
|
||||
const esp_partition_t* mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, EEPROMname);
|
||||
if (mypart == NULL) {
|
||||
log_i("EEPROM partition not found for conversion");
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t size = mypart->size;
|
||||
uint8_t* data = (uint8_t*) malloc(size);
|
||||
if (!data) {
|
||||
log_e("Not enough memory to convert EEPROM!");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (esp_partition_read (mypart, 0, (void *) data, size) != ESP_OK) {
|
||||
log_e("Unable to read EEPROM partition");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
bool empty;
|
||||
empty = true;
|
||||
for (int x=0; x<size; x++) {
|
||||
if (data[x] != 0xFF) {
|
||||
empty = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (empty) {
|
||||
log_i("EEPROM partition is empty, will not convert");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
nvs_handle handle;
|
||||
if (nvs_open(nvsname, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
log_e("Unable to open NVS");
|
||||
goto exit;
|
||||
}
|
||||
esp_err_t err;
|
||||
err = nvs_set_blob(handle, nvsname, data, size);
|
||||
if (err != ESP_OK) {
|
||||
log_e("Unable to add EEPROM data to NVS: %s", esp_err_to_name(err));
|
||||
goto exit;
|
||||
}
|
||||
result = size;
|
||||
|
||||
if (clear) {
|
||||
if (esp_partition_erase_range (mypart, 0, size) != ESP_OK) {
|
||||
log_w("Unable to clear EEPROM partition");
|
||||
}
|
||||
}
|
||||
exit:
|
||||
free(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
Read 'value' from 'address'
|
||||
*/
|
||||
uint8_t EEPROMClass::readByte (int address)
|
||||
{
|
||||
uint8_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
int8_t EEPROMClass::readChar (int address)
|
||||
{
|
||||
int8_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
uint8_t EEPROMClass::readUChar (int address)
|
||||
{
|
||||
uint8_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
int16_t EEPROMClass::readShort (int address)
|
||||
{
|
||||
int16_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
uint16_t EEPROMClass::readUShort (int address)
|
||||
{
|
||||
uint16_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
int32_t EEPROMClass::readInt (int address)
|
||||
{
|
||||
int32_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
uint32_t EEPROMClass::readUInt (int address)
|
||||
{
|
||||
uint32_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
int32_t EEPROMClass::readLong (int address)
|
||||
{
|
||||
int32_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
uint32_t EEPROMClass::readULong (int address)
|
||||
{
|
||||
uint32_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
int64_t EEPROMClass::readLong64 (int address)
|
||||
{
|
||||
int64_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
uint64_t EEPROMClass::readULong64 (int address)
|
||||
{
|
||||
uint64_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
float_t EEPROMClass::readFloat (int address)
|
||||
{
|
||||
float_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
double_t EEPROMClass::readDouble (int address)
|
||||
{
|
||||
double_t value = 0;
|
||||
return EEPROMClass::readAll (address, value);
|
||||
}
|
||||
|
||||
bool EEPROMClass::readBool (int address)
|
||||
{
|
||||
int8_t value = 0;
|
||||
return EEPROMClass::readAll (address, value) ? 1 : 0;
|
||||
}
|
||||
|
||||
size_t EEPROMClass::readString (int address, char* value, size_t maxLen)
|
||||
{
|
||||
if (!value)
|
||||
return 0;
|
||||
|
||||
if (address < 0 || address + maxLen > _size)
|
||||
return 0;
|
||||
|
||||
uint16_t len;
|
||||
for (len = 0; len <= _size; len++)
|
||||
if (_data[address + len] == 0)
|
||||
break;
|
||||
|
||||
if (address + len > _size)
|
||||
return 0;
|
||||
|
||||
if (len > maxLen)
|
||||
return 0; //Maybe return part of the string instead?
|
||||
|
||||
memcpy((uint8_t*) value, _data + address, len);
|
||||
value[len] = 0;
|
||||
return len;
|
||||
}
|
||||
|
||||
String EEPROMClass::readString (int address)
|
||||
{
|
||||
if (address < 0 || address > _size)
|
||||
return String();
|
||||
|
||||
uint16_t len;
|
||||
for (len = 0; len <= _size; len++)
|
||||
if (_data[address + len] == 0)
|
||||
break;
|
||||
|
||||
if (address + len > _size)
|
||||
return String();
|
||||
|
||||
char value[len+1];
|
||||
memcpy((uint8_t*) value, _data + address, len);
|
||||
value[len] = 0;
|
||||
return String(value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::readBytes (int address, void* value, size_t maxLen)
|
||||
{
|
||||
if (!value || !maxLen)
|
||||
return 0;
|
||||
|
||||
if (address < 0 || address + maxLen > _size)
|
||||
return 0;
|
||||
|
||||
memcpy((void*) value, _data + address, maxLen);
|
||||
return maxLen;
|
||||
}
|
||||
|
||||
template <class T> T EEPROMClass::readAll (int address, T &value)
|
||||
{
|
||||
if (address < 0 || address + sizeof(T) > _size)
|
||||
return value;
|
||||
|
||||
memcpy((uint8_t*) &value, _data + address, sizeof(T));
|
||||
return value;
|
||||
}
|
||||
|
||||
/*
|
||||
Write 'value' to 'address'
|
||||
*/
|
||||
size_t EEPROMClass::writeByte (int address, uint8_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeChar (int address, int8_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeUChar (int address, uint8_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeShort (int address, int16_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeUShort (int address, uint16_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeInt (int address, int32_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeUInt (int address, uint32_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeLong (int address, int32_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeULong (int address, uint32_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeLong64 (int address, int64_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeULong64 (int address, uint64_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeFloat (int address, float_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeDouble (int address, double_t value)
|
||||
{
|
||||
return EEPROMClass::writeAll (address, value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeBool (int address, bool value)
|
||||
{
|
||||
int8_t Bool;
|
||||
value ? Bool = 1 : Bool = 0;
|
||||
return EEPROMClass::writeAll (address, Bool);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeString (int address, const char* value)
|
||||
{
|
||||
if (!value)
|
||||
return 0;
|
||||
|
||||
if (address < 0 || address > _size)
|
||||
return 0;
|
||||
|
||||
uint16_t len;
|
||||
for (len = 0; len <= _size; len++)
|
||||
if (value[len] == 0)
|
||||
break;
|
||||
|
||||
if (address + len > _size)
|
||||
return 0;
|
||||
|
||||
memcpy(_data + address, (const uint8_t*) value, len + 1);
|
||||
_dirty = true;
|
||||
return strlen(value);
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeString (int address, String value)
|
||||
{
|
||||
return EEPROMClass::writeString (address, value.c_str());
|
||||
}
|
||||
|
||||
size_t EEPROMClass::writeBytes (int address, const void* value, size_t len)
|
||||
{
|
||||
if (!value || !len)
|
||||
return 0;
|
||||
|
||||
if (address < 0 || address + len > _size)
|
||||
return 0;
|
||||
|
||||
memcpy(_data + address, (const void*) value, len);
|
||||
_dirty = true;
|
||||
return len;
|
||||
}
|
||||
|
||||
template <class T> T EEPROMClass::writeAll (int address, const T &value)
|
||||
{
|
||||
if (address < 0 || address + sizeof(T) > _size)
|
||||
return value;
|
||||
|
||||
memcpy(_data + address, (const uint8_t*) &value, sizeof(T));
|
||||
_dirty = true;
|
||||
|
||||
return sizeof (value);
|
||||
}
|
||||
|
||||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
|
||||
EEPROMClass EEPROM;
|
||||
#endif
|
121
libraries/EEPROM/src/EEPROM.h
Normal file
121
libraries/EEPROM/src/EEPROM.h
Normal file
@ -0,0 +1,121 @@
|
||||
/*
|
||||
EEPROM.h -ported by Paolo Becchi to Esp32 from esp8266 EEPROM
|
||||
-Modified by Elochukwu Ifediora <ifedioraelochukwuc@gmail.com>
|
||||
-Converted to nvs lbernstone@gmail.com
|
||||
|
||||
Uses a nvs byte array to emulate EEPROM
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef EEPROM_h
|
||||
#define EEPROM_h
|
||||
#ifndef EEPROM_FLASH_PARTITION_NAME
|
||||
#define EEPROM_FLASH_PARTITION_NAME "eeprom"
|
||||
#endif
|
||||
#include <Arduino.h>
|
||||
|
||||
typedef uint32_t nvs_handle;
|
||||
|
||||
class EEPROMClass {
|
||||
public:
|
||||
EEPROMClass(uint32_t sector);
|
||||
EEPROMClass(const char* name);
|
||||
EEPROMClass(void);
|
||||
~EEPROMClass(void);
|
||||
|
||||
bool begin(size_t size);
|
||||
uint8_t read(int address);
|
||||
void write(int address, uint8_t val);
|
||||
uint16_t length();
|
||||
bool commit();
|
||||
void end();
|
||||
|
||||
uint8_t * getDataPtr();
|
||||
uint16_t convert(bool clear, const char* EEPROMname = "eeprom", const char* nvsname = "eeprom");
|
||||
|
||||
template<typename T>
|
||||
T &get(int address, T &t) {
|
||||
if (address < 0 || address + sizeof(T) > _size)
|
||||
return t;
|
||||
|
||||
memcpy((uint8_t*) &t, _data + address, sizeof(T));
|
||||
return t;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
const T &put(int address, const T &t) {
|
||||
if (address < 0 || address + sizeof(T) > _size)
|
||||
return t;
|
||||
|
||||
memcpy(_data + address, (const uint8_t*) &t, sizeof(T));
|
||||
_dirty = true;
|
||||
return t;
|
||||
}
|
||||
|
||||
uint8_t readByte(int address);
|
||||
int8_t readChar(int address);
|
||||
uint8_t readUChar(int address);
|
||||
int16_t readShort(int address);
|
||||
uint16_t readUShort(int address);
|
||||
int32_t readInt(int address);
|
||||
uint32_t readUInt(int address);
|
||||
int32_t readLong(int address);
|
||||
uint32_t readULong(int address);
|
||||
int64_t readLong64(int address);
|
||||
uint64_t readULong64(int address);
|
||||
float_t readFloat(int address);
|
||||
double_t readDouble(int address);
|
||||
bool readBool(int address);
|
||||
size_t readString(int address, char* value, size_t maxLen);
|
||||
String readString(int address);
|
||||
size_t readBytes(int address, void * value, size_t maxLen);
|
||||
template <class T> T readAll (int address, T &);
|
||||
|
||||
size_t writeByte(int address, uint8_t value);
|
||||
size_t writeChar(int address, int8_t value);
|
||||
size_t writeUChar(int address, uint8_t value);
|
||||
size_t writeShort(int address, int16_t value);
|
||||
size_t writeUShort(int address, uint16_t value);
|
||||
size_t writeInt(int address, int32_t value);
|
||||
size_t writeUInt(int address, uint32_t value);
|
||||
size_t writeLong(int address, int32_t value);
|
||||
size_t writeULong(int address, uint32_t value);
|
||||
size_t writeLong64(int address, int64_t value);
|
||||
size_t writeULong64(int address, uint64_t value);
|
||||
size_t writeFloat(int address, float_t value);
|
||||
size_t writeDouble(int address, double_t value);
|
||||
size_t writeBool(int address, bool value);
|
||||
size_t writeString(int address, const char* value);
|
||||
size_t writeString(int address, String value);
|
||||
size_t writeBytes(int address, const void* value, size_t len);
|
||||
template <class T> T writeAll (int address, const T &);
|
||||
|
||||
protected:
|
||||
nvs_handle _handle;
|
||||
uint8_t* _data;
|
||||
size_t _size;
|
||||
bool _dirty;
|
||||
const char* _name;
|
||||
};
|
||||
|
||||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
|
||||
extern EEPROMClass EEPROM;
|
||||
#endif
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user