main.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/unistd.h>
  4. #include <sys/stat.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "esp_err.h"
  8. #include "esp_log.h"
  9. #include "esp_vfs_fat.h"
  10. #include "lib/tinyxml2/tinyxml2.h"
  11. using namespace tinyxml2;
  12. static const char *TAG = "example";
  13. // Handle of the wear levelling library instance
  14. static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
  15. // Mount path for the partition
  16. const char *base_path = "/spiflash";
  17. extern "C" void app_main(void)
  18. {
  19. // Do example setup
  20. ESP_LOGI(TAG, "Setting up...");
  21. esp_vfs_fat_mount_config_t mount_config;
  22. mount_config.max_files = 4;
  23. mount_config.format_if_mount_failed = true;
  24. mount_config.allocation_unit_size = CONFIG_WL_SECTOR_SIZE;
  25. esp_err_t err = esp_vfs_fat_spiflash_mount(base_path, "storage", &mount_config, &s_wl_handle);
  26. if (err != ESP_OK) {
  27. ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
  28. return;
  29. }
  30. // The sample XML is embedded binary data. Create a file first containing the embedded
  31. // so it can be accessed.
  32. ESP_LOGI(TAG, "Copying sample XML to filesystem...");
  33. extern const char data_start[] asm("_binary_sample_xml_start");
  34. extern const char data_end[] asm("_binary_sample_xml_end");
  35. FILE *f = fopen("/spiflash/sample.xml", "wb");
  36. if (f == NULL) {
  37. ESP_LOGE(TAG, "Failed to open file for writing");
  38. return;
  39. }
  40. fwrite(data_start, sizeof(char), data_end - data_start + 1, f);
  41. fclose(f);
  42. // Now that the file is created, load it using tinyxml2 and parse
  43. ESP_LOGI(TAG, "Reading XML file");
  44. XMLDocument data;
  45. data.LoadFile("/spiflash/sample.xml");
  46. XMLPrinter printer;
  47. data.Print(&printer);
  48. ESP_LOGI(TAG, "Read XML data:\n%s", printer.CStr());
  49. const char* to_data = data.FirstChildElement("note")->FirstChildElement("to")->GetText();
  50. const char* from_data = data.FirstChildElement("note")->FirstChildElement("from")->GetText();
  51. const char* heading_data = data.FirstChildElement("note")->FirstChildElement("heading")->GetText();
  52. const char* body_data = data.FirstChildElement("note")->FirstChildElement("body")->GetText();
  53. ESP_LOGI(TAG, "Parsed XML data:\n\nTo: %s\nFrom: %s\nHeading: %s\nBody: %s",
  54. to_data, from_data, heading_data, body_data);
  55. ESP_LOGI(TAG, "Example end");
  56. }