Exemple #1
0
void doWork(void *ignore) {

	char buff[5*1024];
	ESP_LOGD(tag, "Flash address is 0x%x", (int)flashAddress);
	if (espFsInit(flashAddress, 64*1024) != ESPFS_INIT_RESULT_OK) {
		ESP_LOGD(tag, "Failed to initialize espfs");
		return;
	}

	EspFsFile *fh = espFsOpen("files/test3.txt");;

	if (fh != NULL) {
		int sizeRead = 0;
		sizeRead = espFsRead(fh, buff, sizeof(buff));
		ESP_LOGD(tag, "Result: %.*s", sizeRead, buff);

		size_t fileSize;
		char *data;
		sizeRead = espFsAccess(fh, (void **)&data, &fileSize);
		ESP_LOGD(tag, "Result from access: %.*s", fileSize, data);

		espFsClose(fh);
		vTaskDelete(NULL);
	}
}
/*
 * Load a file using the ESPFS file system.  The file to be loaded is specified
 * by the "path" parameter.  If the file can be loaded, then the full data
 * of the file is returned and the "fileSize" size pointer is updated.
 * If the file can't be loaded, NULL is returned.
 *
 * If data is returned, it does NOT need to be cleaned up because the nature of
 * the ESPFS file system is that the data is found in flash and addressable.  As
 * such there is no RAM cost for getting the data of such a file.
 *
 * Prior to calling this function, the espFsInit() function should have been
 * previously called to initialize the ESPFS environment.
 */
char *esp32_loadFileESPFS(char *path, size_t *fileSize) {
	EspFsFile *fh = espFsOpen((char *)path);
	if (fh == NULL) {
		*fileSize = 0;
		LOGD("ESPFS: Failed to open file %s", path);
		return NULL;
	}
  char *data;
  espFsAccess(fh, (void **)&data, fileSize);
  espFsClose(fh);
  // Note ... because data is mapped in memory from flash ... it will be good
  // past the file close.
  LOGD("esp32_loadFileESPFS: Read file %s for size %d", path, *fileSize);
  return data;
} // esp32_loadFileESPFS