Created
June 10, 2020 10:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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