Skip to content

Instantly share code, notes, and snippets.

@DaleGia
Created June 10, 2020 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaleGia/00f7d31b21d8ca4b74abbaeebc0d894c to your computer and use it in GitHub Desktop.
Save DaleGia/00f7d31b21d8ca4b74abbaeebc0d894c to your computer and use it in GitHub Desktop.
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