Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ESPFlash: Open a file with a filename of “/floatArrayExample” and get the last 5 float elements stored in the file with error checking.
/*
Open a file with a filename of “/floatArrayExample” and get the last 5
float elements stored in the file with error checking.
*/
float testGet[5];
ESPFlash<float> floatExample("/floatArrayExample");
bool success = floatExample.getBackElements(testGet, sizeof(testGet));
/* The above code replaces the following SPIFFS implementation */
float testGet[5];
File file;
uint32_t fileSizeInBytes = 0;
uint32_t fileSizeInElements = 0;
uint32_t firstElementIndex = 0;
uint32_t bytesRead = 0;
uint32_t getArraySizeInElements = sizeof(testGet);
uint32_t getArraySizeInBytes = sizeof(float)*sizeof(testGet);
bool success = false;
SPIFFS.begin();
file = SPIFFS.open("/floatArrayExample", "r");
if(file)
{
fileSizeInBytes = file.size();
fileSizeInElements = fileSizeInBytes/sizeof(float);
if(getArraySizeInElements <= fileSizeInElements)
{
firstElementIndex = fileSizeInBytes - getArraySizeInBytes;
file.seek(firstElementIndex, SeekSet);
bytesRead = file.read((uint8_t*)testGet, getArraySizeInBytes);
file.close();
if(bytesRead == getArraySizeInBytes)
{
success = true;
}
}
}
file.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment