examples: import_lib: simplify with fatfs partition generator

Instead of embedding the file and copying it to the FAT partition at
run time, generate the FAT partition with the file in it.
This commit is contained in:
Ivan Grokhotkov 2022-06-29 20:20:46 +02:00
parent faa349b93f
commit 94230777d9
No known key found for this signature in database
GPG Key ID: 1E050E141B280628
5 changed files with 54 additions and 75 deletions

View File

@ -1,4 +1,7 @@
idf_component_register(SRCS "main.cpp"
idf_component_register(SRCS "import_lib_example_main.cpp"
INCLUDE_DIRS "."
EMBED_TXTFILES sample.xml
PRIV_REQUIRES tinyxml2 fatfs)
# Create a FAT filesystem image from the contents of data/ subdirectory,
# The image will be flashed into the 'storage' partition when 'idf.py flash' is used.
fatfs_create_spiflash_image(storage data FLASH_IN_PROJECT)

View File

@ -0,0 +1,49 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include "esp_err.h"
#include "esp_log.h"
#include "esp_vfs_fat.h"
#include "tinyxml2.h"
static const char *TAG = "example";
extern "C" void app_main(void)
{
ESP_LOGI(TAG, "Initializing the filesystem");
esp_vfs_fat_mount_config_t mount_config = {};
mount_config.max_files = 1;
wl_handle_t wl_handle = WL_INVALID_HANDLE;
esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", "storage", &mount_config, &wl_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
return;
}
// Load the XML file from the filesystem and parse it using tinyxml2
ESP_LOGI(TAG, "Reading XML file");
tinyxml2::XMLDocument data;
data.LoadFile("/spiflash/sample.xml");
tinyxml2::XMLPrinter printer;
data.Print(&printer);
ESP_LOGI(TAG, "Read XML data:\n%s", printer.CStr());
const char* to_data = data.FirstChildElement("note")->FirstChildElement("to")->GetText();
const char* from_data = data.FirstChildElement("note")->FirstChildElement("from")->GetText();
const char* heading_data = data.FirstChildElement("note")->FirstChildElement("heading")->GetText();
const char* body_data = data.FirstChildElement("note")->FirstChildElement("body")->GetText();
ESP_LOGI(TAG, "Parsed XML data:\n\nTo: %s\nFrom: %s\nHeading: %s\nBody: %s",
to_data, from_data, heading_data, body_data);
// Clean up
esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", wl_handle);
ESP_LOGI(TAG, "Example end");
}

View File

@ -1,72 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_vfs_fat.h"
#include "tinyxml2.h"
using namespace tinyxml2;
static const char *TAG = "example";
// Handle of the wear levelling library instance
static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
// Mount path for the partition
const char *base_path = "/spiflash";
extern "C" void app_main(void)
{
// Do example setup
ESP_LOGI(TAG, "Setting up...");
esp_vfs_fat_mount_config_t mount_config;
mount_config.max_files = 4;
mount_config.format_if_mount_failed = true;
mount_config.allocation_unit_size = CONFIG_WL_SECTOR_SIZE;
esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(base_path, "storage", &mount_config, &s_wl_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
return;
}
// The sample XML is embedded binary data. Create a file first containing the embedded
// so it can be accessed.
ESP_LOGI(TAG, "Copying sample XML to filesystem...");
extern const char data_start[] asm("_binary_sample_xml_start");
extern const char data_end[] asm("_binary_sample_xml_end");
FILE *f = fopen("/spiflash/sample.xml", "wb");
if (f == NULL) {
ESP_LOGE(TAG, "Failed to open file for writing");
return;
}
fwrite(data_start, sizeof(char), data_end - data_start + 1, f);
fclose(f);
// Now that the file is created, load it using tinyxml2 and parse
ESP_LOGI(TAG, "Reading XML file");
XMLDocument data;
data.LoadFile("/spiflash/sample.xml");
XMLPrinter printer;
data.Print(&printer);
ESP_LOGI(TAG, "Read XML data:\n%s", printer.CStr());
const char* to_data = data.FirstChildElement("note")->FirstChildElement("to")->GetText();
const char* from_data = data.FirstChildElement("note")->FirstChildElement("from")->GetText();
const char* heading_data = data.FirstChildElement("note")->FirstChildElement("heading")->GetText();
const char* body_data = data.FirstChildElement("note")->FirstChildElement("body")->GetText();
ESP_LOGI(TAG, "Parsed XML data:\n\nTo: %s\nFrom: %s\nHeading: %s\nBody: %s",
to_data, from_data, heading_data, body_data);
ESP_LOGI(TAG, "Example end");
}

View File

@ -1733,7 +1733,6 @@ examples/bluetooth/nimble/bleprph_wifi_coex/main/bleprph.h
examples/bluetooth/nimble/bleprph_wifi_coex/main/gatt_svr.c
examples/bluetooth/nimble/bleprph_wifi_coex/main/main.c
examples/build_system/cmake/component_manager/main/component_manager.c
examples/build_system/cmake/import_lib/main/main.cpp
examples/build_system/cmake/import_prebuilt/main/main.c
examples/build_system/cmake/import_prebuilt/prebuilt/components/prebuilt/prebuilt.c
examples/build_system/cmake/import_prebuilt/prebuilt/components/prebuilt/prebuilt.h