diff --git a/.github/workflows/build-examples-gh-pages-on-push.yml b/.github/workflows/build-examples-gh-pages-on-push.yml index 340804d35..fd5b4bf39 100644 --- a/.github/workflows/build-examples-gh-pages-on-push.yml +++ b/.github/workflows/build-examples-gh-pages-on-push.yml @@ -3,7 +3,8 @@ name: "ESP-IDF build examples to github pages (push)" on: push: branches: - - master + # - master + - feat/update_image_convert jobs: diff --git a/examples/display/CMakeLists.txt b/examples/display/CMakeLists.txt index db3ca766f..535c5dde1 100644 --- a/examples/display/CMakeLists.txt +++ b/examples/display/CMakeLists.txt @@ -4,6 +4,6 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -set(COMPONENTS main) # "Trim" the build. Include the minimal set of components; main and anything it depends on. +# set(COMPONENTS main) # "Trim" the build. Include the minimal set of components; main and anything it depends on. include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(display) diff --git a/examples/display/components/esp_lv_fs/CHANGELOG.md b/examples/display/components/esp_lv_fs/CHANGELOG.md new file mode 100644 index 000000000..8f1986cba --- /dev/null +++ b/examples/display/components/esp_lv_fs/CHANGELOG.md @@ -0,0 +1,6 @@ +# ChangeLog + +## v0.1.0 Initial Version (2024-07-29) + +* Dependence on esp_mmap_assets to build filesystem for LVGL. +* Supported functions: fopen, fclose, fread, ftell, fseek. diff --git a/examples/display/components/esp_lv_fs/CMakeLists.txt b/examples/display/components/esp_lv_fs/CMakeLists.txt new file mode 100644 index 000000000..45a6ae09e --- /dev/null +++ b/examples/display/components/esp_lv_fs/CMakeLists.txt @@ -0,0 +1,7 @@ +idf_component_register( + SRCS "esp_lv_fs.c" + INCLUDE_DIRS "include" +) + +include(package_manager) +cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR}) diff --git a/examples/display/components/esp_lv_fs/README.md b/examples/display/components/esp_lv_fs/README.md new file mode 100644 index 000000000..5f03201fb --- /dev/null +++ b/examples/display/components/esp_lv_fs/README.md @@ -0,0 +1,53 @@ +[![Component Registry](https://components.espressif.com/components/espressif/esp_lv_fs/badge.svg)](https://components.espressif.com/components/espressif/esp_lv_fs) + +## Instructions and Details + +`esp_lv_fs` allows LVGL to use filesystems for accessing assets. This component integrates with `esp_mmap_assets` to efficiently manage file operations and supports multiple partitions. + +### Features + - Integrates with `esp_mmap_assets` for filesystem creation. + + - Supports standard file operations: fopen, fclose, fread, ftell, and fseek. + + - Uses the `esp_partition_read` API for efficient file access. + + - Supports multiple partitions. + +## Add to project + +Packages from this repository are uploaded to [Espressif's component service](https://components.espressif.com/). +You can add them to your project via `idf.py add-dependancy`, e.g. +``` + idf.py add-dependency esp_lv_fs +``` + +## Usage + +### Dependencies +The [esp_mmap_assets](https://components.espressif.com/components/espressif/esp_mmap_assets) component is required. It provides file offset index relationship. + +### Initialization +```c + #include "esp_lv_fs.h" + #include "esp_mmap_assets.h" + + esp_lv_fs_handle_t fs_drive_a_handle; + mmap_assets_handle_t mmap_drive_a_handle; + + const mmap_assets_config_t asset_cfg = { + .partition_label = "assets_A", + .max_files = MMAP_DRIVE_A_FILES, + .checksum = MMAP_DRIVE_A_CHECKSUM, + .flags = { + .mmap_enable = true, + } + }; + mmap_assets_new(&asset_cfg, &mmap_drive_a_handle); + + const fs_cfg_t fs_drive_a_cfg = { + .fs_letter = 'A', + .fs_assets = mmap_drive_a_handle, + .fs_nums = MMAP_DRIVE_A_FILES + }; + esp_lv_fs_desc_init(&fs_drive_a_cfg, &fs_drive_a_handle); //Initialize this after lvgl starts +``` diff --git a/examples/display/components/esp_lv_fs/esp_lv_fs.c b/examples/display/components/esp_lv_fs/esp_lv_fs.c new file mode 100644 index 000000000..5ff9ff8b5 --- /dev/null +++ b/examples/display/components/esp_lv_fs/esp_lv_fs.c @@ -0,0 +1,272 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "esp_log.h" +#include "esp_check.h" +#include "esp_lv_fs.h" + +#include "lvgl.h" +#include + +static char *TAG = "lv_fs"; + +typedef struct { + const char *name; + const uint8_t *data; // asset_mem + size_t size; // asset_size +} file_descriptor_t; + +typedef struct { + int file_count; + file_descriptor_t **desc; + mmap_assets_handle_t fs_assets; + lv_fs_drv_t *fs_drv; +} file_system_t; + +typedef struct { + int fd; + size_t pos; + bool is_open; // Moved flag to indicate if the file is open +} FILE_t; + +static void *fs_open(lv_fs_drv_t *drv, const char *path, lv_fs_mode_t mode) +{ + LV_UNUSED(drv); + file_system_t *fs = drv->user_data; + + for (int i = 0; i < fs->file_count; i++) { + if (strcmp(fs->desc[i]->name, path) == 0) { + FILE_t *fp = (FILE_t *)malloc(sizeof(FILE_t)); + if (!fp) { + return NULL; + } + fp->is_open = true; + fp->fd = i; + fp->pos = 0; + return (void *)fp; + } + } + + return NULL; // file not found +} + +static lv_fs_res_t fs_close(lv_fs_drv_t *drv, void *file_p) +{ + LV_UNUSED(drv); + file_system_t *fs = drv->user_data; + + FILE_t *fp = (FILE_t *)file_p; + if (!fp || fp->fd < 0 || fp->fd >= fs->file_count) { + return LV_FS_RES_FS_ERR; + } + + fp->is_open = false; + free(fp); + return LV_FS_RES_OK; +} + +static lv_fs_res_t fs_read(lv_fs_drv_t *drv, void *file_p, void *buf, uint32_t btr, uint32_t *br) +{ + LV_UNUSED(drv); + file_system_t *fs = drv->user_data; + + FILE_t *fp = (FILE_t *)file_p; + if (!fp || fp->fd < 0 || fp->fd >= fs->file_count || !fp->is_open) { + return LV_FS_RES_FS_ERR; + } + + file_descriptor_t *file = fs->desc[fp->fd]; + if (fp->pos + btr > file->size) { + btr = file->size - fp->pos; + } + + mmap_assets_copy_mem(fs->fs_assets, (size_t)(file->data + fp->pos), buf, btr); + fp->pos += btr; + *br = btr; + return LV_FS_RES_OK; +} + +static lv_fs_res_t fs_write(lv_fs_drv_t *drv, void *file_p, const void *buf, uint32_t btw, uint32_t *bw) +{ + LV_UNUSED(drv); + file_system_t *fs = drv->user_data; + + FILE_t *fp = (FILE_t *)file_p; + if (!fp || fp->fd < 0 || fp->fd >= fs->file_count || !fp->is_open) { + return LV_FS_RES_FS_ERR; + } + + return LV_FS_RES_DENIED; +} + +static lv_fs_res_t fs_seek(lv_fs_drv_t *drv, void *file_p, uint32_t pos, lv_fs_whence_t whence) +{ + LV_UNUSED(drv); + file_system_t *fs = drv->user_data; + + FILE_t *fp = (FILE_t *)file_p; + if (!fp || fp->fd < 0 || fp->fd >= fs->file_count || !fp->is_open) { + return LV_FS_RES_FS_ERR; + } + + file_descriptor_t *file = fs->desc[fp->fd]; + size_t new_pos; + switch (whence) { + case LV_FS_SEEK_SET: + new_pos = pos; + break; + case LV_FS_SEEK_CUR: + new_pos = fp->pos + pos; + break; + case LV_FS_SEEK_END: + new_pos = file->size + pos; + break; + default: + return LV_FS_RES_INV_PARAM; + } + + if (new_pos > file->size) { + new_pos = file->size; + } + fp->pos = new_pos; + return LV_FS_RES_OK; +} + +static lv_fs_res_t fs_tell(lv_fs_drv_t *drv, void *file_p, uint32_t *pos_p) +{ + LV_UNUSED(drv); + file_system_t *fs = drv->user_data; + + FILE_t *fp = (FILE_t *)file_p; + if (!fp || fp->fd < 0 || fp->fd >= fs->file_count || !fp->is_open) { + return LV_FS_RES_FS_ERR; + } + + *pos_p = fp->pos; + return LV_FS_RES_OK; +} + +static void *fs_dir_open(lv_fs_drv_t *drv, const char *path) +{ + LV_UNUSED(drv); + // Return an error indicating that write operations are not supported + return NULL; +} + +static lv_fs_res_t fs_dir_read(lv_fs_drv_t *drv, void *dir_p, char *fn, uint32_t fn_len) +// static lv_fs_res_t fs_dir_read(lv_fs_drv_t *drv, void *dir_p, char *fn) +{ + LV_UNUSED(drv); + // Return an error indicating that write operations are not supported + return LV_FS_RES_DENIED; +} + +static lv_fs_res_t fs_dir_close(lv_fs_drv_t *drv, void *dir_p) +{ + LV_UNUSED(drv); + // Return an error indicating that write operations are not supported + return LV_FS_RES_DENIED; +} + +/** + * Register a driver for the File system interface + */ +static void lv_fs_flash_init(char fs_letter, file_system_t *handle) +{ + /*--------------------------------------------------- + * Register the file system interface in LVGL + *--------------------------------------------------*/ + + /*Add a simple drive to open images*/ + lv_fs_drv_init(handle->fs_drv); + + /*Set up fields...*/ + handle->fs_drv->letter = fs_letter; + + handle->fs_drv->open_cb = fs_open; + handle->fs_drv->close_cb = fs_close; + handle->fs_drv->read_cb = fs_read; + handle->fs_drv->write_cb = fs_write; + handle->fs_drv->seek_cb = fs_seek; + handle->fs_drv->tell_cb = fs_tell; + + handle->fs_drv->dir_open_cb = fs_dir_open; + handle->fs_drv->dir_read_cb = fs_dir_read; + handle->fs_drv->dir_close_cb = fs_dir_close; + +#if LVGL_VERSION_MAJOR >= 9 || LV_USE_USER_DATA + handle->fs_drv->user_data = handle; +#else +#error "LV_USE_USER_DATA is disabled. Please enable it in lv_conf.h" +#endif + lv_fs_drv_register(handle->fs_drv); +} + +esp_err_t esp_lv_fs_desc_deinit(esp_lv_fs_handle_t handle) +{ + ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "invalid argument"); + + file_system_t *fs = (file_system_t *)handle; + + if (fs->fs_drv) {//fs_drv can't be deleted, you can just delete them all + free(fs->fs_drv); + } + + for (int i = 0; i < fs->file_count; i++) { + free(fs->desc[i]); + } + if (fs->desc) { + free(fs->desc); + } + + free(fs); + + return ESP_OK; +} + +esp_err_t esp_lv_fs_desc_init(const fs_cfg_t *cfg, esp_lv_fs_handle_t *ret_handle) +{ + esp_err_t ret = ESP_OK; + + ESP_RETURN_ON_FALSE(cfg && ret_handle && cfg->fs_nums && cfg->fs_assets, ESP_ERR_INVALID_ARG, TAG, "invalid argument"); + + file_system_t *fs = (file_system_t *)calloc(1, sizeof(file_system_t)); + ESP_GOTO_ON_FALSE(fs, ESP_ERR_NO_MEM, err, TAG, "no mem for fs handle"); + + fs->desc = (file_descriptor_t **)calloc(1, cfg->fs_nums * sizeof(file_descriptor_t *)); + ESP_GOTO_ON_FALSE(fs, ESP_ERR_NO_MEM, err, TAG, "no mem for desc list"); + + fs->fs_assets = cfg->fs_assets; + fs->file_count = 0; + + for (int i = 0; i < cfg->fs_nums; i++) { + fs->desc[i] = (file_descriptor_t *)calloc(1, sizeof(file_descriptor_t)); + ESP_GOTO_ON_FALSE(fs, ESP_ERR_NO_MEM, err, TAG, "no mem for file descriptor"); + + fs->desc[i]->name = mmap_assets_get_name(fs->fs_assets, i); + fs->desc[i]->data = mmap_assets_get_mem(fs->fs_assets, i); + fs->desc[i]->size = mmap_assets_get_size(fs->fs_assets, i); + + fs->file_count++; + } + + fs->fs_drv = (lv_fs_drv_t *)calloc(1, sizeof(lv_fs_drv_t)); + ESP_GOTO_ON_FALSE(fs->fs_drv, ESP_ERR_NO_MEM, err, TAG, "no mem for fs_drv"); + + lv_fs_flash_init(cfg->fs_letter, fs); + + *ret_handle = (esp_lv_fs_handle_t)fs; + ESP_LOGD(TAG, "new fs handle:@%p", fs); + + ESP_LOGI(TAG, "Drive '%c' successfully created, version: %d.%d.%d", + cfg->fs_letter, ESP_LV_FS_VER_MAJOR, ESP_LV_FS_VER_MINOR, ESP_LV_FS_VER_PATCH); + + return ret; + +err: + esp_lv_fs_desc_deinit((esp_lv_fs_handle_t)fs); + return ret; +} diff --git a/examples/display/components/esp_lv_fs/idf_component.yml b/examples/display/components/esp_lv_fs/idf_component.yml new file mode 100644 index 000000000..f4ce804c8 --- /dev/null +++ b/examples/display/components/esp_lv_fs/idf_component.yml @@ -0,0 +1,24 @@ +version: "0.1.0" +targets: + - esp32 + - esp32c2 + - esp32c3 + - esp32c6 + - esp32h2 + - esp32s2 + - esp32s3 +description: File system for LVGL, supports reading files directly from flash. +url: https://github.com/espressif/esp-iot-solution/tree/master/components/display/tools/esp_lv_fs +issues: https://github.com/espressif/esp-iot-solution/issues +repository: https://github.com/espressif/esp-iot-solution.git +documentation: https://docs.espressif.com/projects/esp-iot-solution/en/latest/display/tools/esp_lv_fs.html +dependencies: + idf: ">=4.4" + lvgl/lvgl: + version: ">=8,<10" + esp_mmap_assets: + version: ">=1.2" + override_path: "../esp_mmap_assets" + cmake_utilities: "0.*" +examples: + - path: ../../../../examples/hmi/perf_benchmark diff --git a/examples/display/components/esp_lv_fs/include/esp_lv_fs.h b/examples/display/components/esp_lv_fs/include/esp_lv_fs.h new file mode 100644 index 000000000..495b411c1 --- /dev/null +++ b/examples/display/components/esp_lv_fs/include/esp_lv_fs.h @@ -0,0 +1,63 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "esp_err.h" +#include "esp_mmap_assets.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Configuration structure for the filesystem + */ +typedef struct { + char fs_letter; /*!< Filesystem letter identifier */ + int fs_nums; /*!< Number of filesystem instances */ + mmap_assets_handle_t fs_assets; /*!< Handle to memory-mapped assets */ +} fs_cfg_t; + +/** + * @brief Filesystem handle type definition + */ +typedef void *esp_lv_fs_handle_t; + +/** + * @brief Initialize file descriptors for the filesystem. + * + * This function sets up the filesystem by initializing file descriptors + * based on the provided configuration. It allocates necessary memory and + * populates the file descriptors with information about the assets. + * + * @param[in] cfg Pointer to the filesystem configuration structure. + * @param[out] ret_handle Pointer to the handle that will hold the initialized filesystem instance. + * + * @return + * - ESP_OK: Success + * - ESP_ERR_INVALID_ARG: Invalid argument + * - ESP_ERR_NO_MEM: Memory allocation failed + */ +esp_err_t esp_lv_fs_desc_init(const fs_cfg_t *cfg, esp_lv_fs_handle_t *ret_handle); + +/** + * @brief Deinitialize the filesystem and clean up resources. + * + * This function cleans up the filesystem by freeing allocated memory + * for file descriptors and other resources associated with the provided handle. + * + * @param[in] handle Handle to the filesystem instance to be deinitialized. + * + * @return + * - ESP_OK: Success + * - ESP_ERR_INVALID_ARG: Invalid argument + */ +esp_err_t esp_lv_fs_desc_deinit(esp_lv_fs_handle_t handle); + +#ifdef __cplusplus +} /* extern "C" */ +#endif diff --git a/examples/display/components/esp_lv_fs/license.txt b/examples/display/components/esp_lv_fs/license.txt new file mode 100644 index 000000000..d64569567 --- /dev/null +++ b/examples/display/components/esp_lv_fs/license.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/examples/display/components/esp_lv_fs/test_apps/CMakeLists.txt b/examples/display/components/esp_lv_fs/test_apps/CMakeLists.txt new file mode 100644 index 000000000..cba0964e3 --- /dev/null +++ b/examples/display/components/esp_lv_fs/test_apps/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) +set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components") +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(test_esp_map_assets) diff --git a/examples/display/components/esp_lv_fs/test_apps/Drive_A/color_A_jpg.jpg b/examples/display/components/esp_lv_fs/test_apps/Drive_A/color_A_jpg.jpg new file mode 100644 index 000000000..4c5ede6e1 Binary files /dev/null and b/examples/display/components/esp_lv_fs/test_apps/Drive_A/color_A_jpg.jpg differ diff --git a/examples/display/components/esp_lv_fs/test_apps/Drive_B/color_B_jpg.jpg b/examples/display/components/esp_lv_fs/test_apps/Drive_B/color_B_jpg.jpg new file mode 100644 index 000000000..4c5ede6e1 Binary files /dev/null and b/examples/display/components/esp_lv_fs/test_apps/Drive_B/color_B_jpg.jpg differ diff --git a/examples/display/components/esp_lv_fs/test_apps/main/CMakeLists.txt b/examples/display/components/esp_lv_fs/test_apps/main/CMakeLists.txt new file mode 100644 index 000000000..ad4a3dd5b --- /dev/null +++ b/examples/display/components/esp_lv_fs/test_apps/main/CMakeLists.txt @@ -0,0 +1,17 @@ + +idf_component_register( + SRC_DIRS "." + INCLUDE_DIRS ".") + +spiffs_create_partition_assets( + assets_A + ../Drive_A + FLASH_IN_PROJECT + MMAP_FILE_SUPPORT_FORMAT ".jpg" +) +spiffs_create_partition_assets( + assets_B + ../Drive_B + FLASH_IN_PROJECT + MMAP_FILE_SUPPORT_FORMAT ".jpg" +) diff --git a/examples/display/components/esp_lv_fs/test_apps/main/idf_component.yml b/examples/display/components/esp_lv_fs/test_apps/main/idf_component.yml new file mode 100644 index 000000000..56c4fc35c --- /dev/null +++ b/examples/display/components/esp_lv_fs/test_apps/main/idf_component.yml @@ -0,0 +1,6 @@ +## IDF Component Manager Manifest File +dependencies: + idf: ">=5.0" + esp_lv_fs: + version: "*" + override_path: "../../../esp_lv_fs" diff --git a/examples/display/components/esp_lv_fs/test_apps/main/mmap_generate_Drive_A.h b/examples/display/components/esp_lv_fs/test_apps/main/mmap_generate_Drive_A.h new file mode 100644 index 000000000..dd5acf546 --- /dev/null +++ b/examples/display/components/esp_lv_fs/test_apps/main/mmap_generate_Drive_A.h @@ -0,0 +1,21 @@ +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief This file was generated by esp_mmap_assets, don't modify it + */ + +#pragma once + +#include "esp_mmap_assets.h" + +#define MMAP_DRIVE_A_FILES 1 +#define MMAP_DRIVE_A_CHECKSUM 0xD012 + +enum MMAP_DRIVE_A_LISTS { + MMAP_DRIVE_A_COLOR_A_JPG_JPG = 0, /*!< color_A_jpg.jpg */ +}; diff --git a/examples/display/components/esp_lv_fs/test_apps/main/mmap_generate_Drive_B.h b/examples/display/components/esp_lv_fs/test_apps/main/mmap_generate_Drive_B.h new file mode 100644 index 000000000..79d0e3a92 --- /dev/null +++ b/examples/display/components/esp_lv_fs/test_apps/main/mmap_generate_Drive_B.h @@ -0,0 +1,21 @@ +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief This file was generated by esp_mmap_assets, don't modify it + */ + +#pragma once + +#include "esp_mmap_assets.h" + +#define MMAP_DRIVE_B_FILES 1 +#define MMAP_DRIVE_B_CHECKSUM 0xD013 + +enum MMAP_DRIVE_B_LISTS { + MMAP_DRIVE_B_COLOR_B_JPG_JPG = 0, /*!< color_B_jpg.jpg */ +}; diff --git a/examples/display/components/esp_lv_fs/test_apps/main/test_esp_lv_fs.c b/examples/display/components/esp_lv_fs/test_apps/main/test_esp_lv_fs.c new file mode 100644 index 000000000..78a300466 --- /dev/null +++ b/examples/display/components/esp_lv_fs/test_apps/main/test_esp_lv_fs.c @@ -0,0 +1,181 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "esp_err.h" +#include "esp_log.h" +#include "esp_check.h" + +#include "unity.h" +#include "unity_test_runner.h" +#include "unity_test_utils_memory.h" + +#include "esp_lv_fs.h" +#include "lvgl.h" + +#include "mmap_generate_Drive_A.h" +#include "mmap_generate_Drive_B.h" + +static const char *TAG = "FS test"; + +#define TEST_LCD_H_RES 100 +#define TEST_LCD_V_RES 100 + +static SemaphoreHandle_t flush_sem; + +static mmap_assets_handle_t mmap_drive_a_handle; +static mmap_assets_handle_t mmap_drive_b_handle; + +static esp_lv_fs_handle_t fs_drive_a_handle; +static esp_lv_fs_handle_t fs_drive_b_handle; + +void test_filesystem_init(void) +{ + const mmap_assets_config_t asset_cfg = { + .partition_label = "assets_A", + .max_files = MMAP_DRIVE_A_FILES, + .checksum = MMAP_DRIVE_A_CHECKSUM, + .flags = { + .mmap_enable = true, + } + }; + TEST_ESP_OK(mmap_assets_new(&asset_cfg, &mmap_drive_a_handle)); + + const mmap_assets_config_t spiffs_cfg = { + .partition_label = "assets_B", + .max_files = MMAP_DRIVE_B_FILES, + .checksum = MMAP_DRIVE_B_CHECKSUM, + .flags = { + .mmap_enable = false, + } + }; + TEST_ESP_OK(mmap_assets_new(&spiffs_cfg, &mmap_drive_b_handle)); + + const fs_cfg_t fs_drive_a_cfg = { + .fs_letter = 'A', + .fs_assets = mmap_drive_a_handle, + .fs_nums = MMAP_DRIVE_A_FILES + }; + TEST_ESP_OK(esp_lv_fs_desc_init(&fs_drive_a_cfg, &fs_drive_a_handle)); + + const fs_cfg_t fs_drive_b_cfg = { + .fs_letter = 'B', + .fs_assets = mmap_drive_b_handle, + .fs_nums = MMAP_DRIVE_B_FILES + }; + TEST_ESP_OK(esp_lv_fs_desc_init(&fs_drive_b_cfg, &fs_drive_b_handle)); +} + +static void test_flush_callback(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map) +{ + ESP_LOGI(TAG, "flush_cb, 0x%04X, [%d,%d,%d,%d]", color_map[0].full, area->x1, area->y1, area->x2, area->y2); + if (color_map[0].full == 0xF800) { + xSemaphoreGive(flush_sem); + ESP_LOGI(TAG, "decoder finished"); + } + + lv_disp_flush_ready(drv); +} + +static void test_lvgl_init(lv_disp_drv_t **disp_drv, lv_disp_draw_buf_t **disp_buf) +{ + /* LVGL init */ + lv_init(); + + test_filesystem_init(); + + *disp_buf = heap_caps_malloc(sizeof(lv_disp_draw_buf_t), MALLOC_CAP_DEFAULT); + TEST_ASSERT_NOT_NULL(*disp_buf); + + /* Initialize LVGL draw buffers */ + uint32_t buffer_size = TEST_LCD_H_RES * TEST_LCD_V_RES; + lv_color_t *buf1 = heap_caps_malloc(buffer_size * sizeof(lv_color_t), MALLOC_CAP_DEFAULT); + TEST_ASSERT_NOT_NULL(buf1); + lv_disp_draw_buf_init(*disp_buf, buf1, NULL, buffer_size); + + *disp_drv = heap_caps_malloc(sizeof(lv_disp_drv_t), MALLOC_CAP_DEFAULT); + TEST_ASSERT_NOT_NULL(*disp_drv); + /* Descriptor of a display driver */ + lv_disp_drv_init(*disp_drv); + (*disp_drv)->hor_res = TEST_LCD_H_RES; + (*disp_drv)->ver_res = TEST_LCD_V_RES; + (*disp_drv)->flush_cb = test_flush_callback; + (*disp_drv)->draw_buf = *disp_buf; + + /* Finally register the driver */ + lv_disp_drv_register(*disp_drv); + + // Initialize the semaphores + flush_sem = xSemaphoreCreateBinary(); + TEST_ASSERT_NOT_NULL(flush_sem); +} + +static void test_lvgl_deinit(lv_disp_drv_t *disp_drv, lv_disp_draw_buf_t *disp_buf) +{ + free(disp_drv->draw_buf->buf1); + free(disp_drv->draw_buf); + free(disp_drv); + lv_deinit(); + + // Delete the semaphores + vSemaphoreDelete(flush_sem); +} + +TEST_CASE("Validate filesystem functionality in LVGL", "[filesystem][file read]") +{ + lv_disp_drv_t *disp_drv = NULL; + lv_disp_draw_buf_t *disp_buf = NULL; + + /* Initialize LVGL */ + test_lvgl_init(&disp_drv, &disp_buf); + + lv_obj_t *img_decoder = lv_img_create(lv_scr_act()); + lv_obj_set_align(img_decoder, LV_ALIGN_TOP_LEFT); + + lv_img_set_src(img_decoder, "A:color_A_jpg.jpg"); + lv_refr_now(NULL); + TEST_ASSERT_TRUE(xSemaphoreTake(flush_sem, pdMS_TO_TICKS(3 * 1000))); + + lv_img_set_src(img_decoder, "B:color_B_jpg.jpg"); + lv_refr_now(NULL); + TEST_ASSERT_TRUE(xSemaphoreTake(flush_sem, pdMS_TO_TICKS(3 * 1000))); + + test_lvgl_deinit(disp_drv, disp_buf); + + esp_lv_fs_desc_deinit(fs_drive_a_handle); + esp_lv_fs_desc_deinit(fs_drive_b_handle); + + mmap_assets_del(mmap_drive_a_handle); + mmap_assets_del(mmap_drive_b_handle); +} + +// Some resources are lazy allocated in the LCD driver, the threadhold is left for that case +#define TEST_MEMORY_LEAK_THRESHOLD (500) + +static size_t before_free_8bit; +static size_t before_free_32bit; + +void setUp(void) +{ + before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); +} + +void tearDown(void) +{ + size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); + unity_utils_check_leak(before_free_8bit, after_free_8bit, "8BIT", TEST_MEMORY_LEAK_THRESHOLD); + unity_utils_check_leak(before_free_32bit, after_free_32bit, "32BIT", TEST_MEMORY_LEAK_THRESHOLD); +} + +void app_main(void) +{ + printf("ESP FileSystem TEST \n"); + unity_run_menu(); +} diff --git a/examples/display/components/esp_lv_fs/test_apps/partitions.csv b/examples/display/components/esp_lv_fs/test_apps/partitions.csv new file mode 100644 index 000000000..4fa5f9f45 --- /dev/null +++ b/examples/display/components/esp_lv_fs/test_apps/partitions.csv @@ -0,0 +1,7 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild +nvs, data, nvs, , 0x6000, +phy_init, data, phy, , 0x1000, +factory, app, factory, , 1000K, +assets_A, data, spiffs, , 500K, +assets_B, data, spiffs, , 500K, diff --git a/examples/display/components/esp_lv_fs/test_apps/pytest_esp_lv_fs.py b/examples/display/components/esp_lv_fs/test_apps/pytest_esp_lv_fs.py new file mode 100644 index 000000000..7ccdae757 --- /dev/null +++ b/examples/display/components/esp_lv_fs/test_apps/pytest_esp_lv_fs.py @@ -0,0 +1,17 @@ +# SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 +import pytest +from pytest_embedded import Dut + +@pytest.mark.target('esp32') +@pytest.mark.target('esp32c3') +@pytest.mark.target('esp32s3') +@pytest.mark.env('generic') +@pytest.mark.parametrize( + 'config', + [ + 'defaults', + ], +) +def test_esp_lv_fs(dut: Dut)-> None: + dut.run_all_single_board_cases() diff --git a/examples/display/components/esp_lv_fs/test_apps/sdkconfig.defaults b/examples/display/components/esp_lv_fs/test_apps/sdkconfig.defaults new file mode 100644 index 000000000..7042b0e56 --- /dev/null +++ b/examples/display/components/esp_lv_fs/test_apps/sdkconfig.defaults @@ -0,0 +1,6 @@ +# For IDF 5.0 +CONFIG_ESP_TASK_WDT_EN=n + +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_LV_USE_SJPG=y diff --git a/examples/display/components/esp_mmap_assets/CHANGELOG.md b/examples/display/components/esp_mmap_assets/CHANGELOG.md new file mode 100644 index 000000000..63939bb72 --- /dev/null +++ b/examples/display/components/esp_mmap_assets/CHANGELOG.md @@ -0,0 +1,22 @@ +# ChangeLog + +## v1.3.0 (2024-10-18) + +* CMake Enhancements: Transitioned from Kconfig options to CMake arguments, supporting independent parameters for each partition. +* JPG/PNG to QOI Conversion: Enabled conversion of JPG and PNG to QOI for optimized image handling. +* LVGL Image Conversion: Added support for converting images to LVGL-compatible binary formats. + +## v1.2.0 (2024-07-31) + +* Added mmap_enable flag. +* Added mmap_assets_copy_mem for reading files from flash to memory. +* Configurable filename length. +* Support multiple partitions. + +## v1.1.0 (2024-06-27) + +* Added support for split height setting. + +## v1.0.0 Initial Version + +* Added support for file packaging during build. diff --git a/examples/display/components/esp_mmap_assets/CMakeLists.txt b/examples/display/components/esp_mmap_assets/CMakeLists.txt new file mode 100644 index 000000000..314d555be --- /dev/null +++ b/examples/display/components/esp_mmap_assets/CMakeLists.txt @@ -0,0 +1,9 @@ +idf_component_register( + SRCS "esp_mmap_assets.c" + INCLUDE_DIRS "include" + REQUIRES esp_partition + PRIV_REQUIRES spi_flash +) + +include(package_manager) +cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR}) diff --git a/examples/display/components/esp_mmap_assets/Kconfig b/examples/display/components/esp_mmap_assets/Kconfig new file mode 100644 index 000000000..8934606e9 --- /dev/null +++ b/examples/display/components/esp_mmap_assets/Kconfig @@ -0,0 +1,9 @@ +# Kconfig file for MMAP + +menu "mmap file support format" + + config MMAP_FILE_NAME_LENGTH + int "Max file name length" + default 16 + range 1 32 +endmenu diff --git a/examples/display/components/esp_mmap_assets/README.md b/examples/display/components/esp_mmap_assets/README.md new file mode 100644 index 000000000..53601504b --- /dev/null +++ b/examples/display/components/esp_mmap_assets/README.md @@ -0,0 +1,97 @@ +[![Component Registry](https://components.espressif.com/components/espressif/esp_mmap_assets/badge.svg)](https://components.espressif.com/components/espressif/esp_mmap_assets) + +## Instructions and Details + +This module is primarily used for packaging assets (such as images, fonts, etc.) and directly mapping them for user access. + +### Features + +1. **Separation of Code and Assets**: + - Assets are kept separate from the application code, reducing the size of the application binary and improving performance compared to using SPIFFS. + +2. **Efficient Resource Management**: + - Simplifies assets management by using automatically generated enums to access resource information. + +3. **Memory-Efficient Image Decoding**: + - Includes an image splitting script to reduce the memory required for image decoding. + +4. **Support for Image Conversion (JPG/PNG to QOI)**: + - Supports image conversion from JPG and PNG formats to QOI (Quite OK Image), enabling faster decoding times for resource-limited systems. + +5. **LVGL-Specific Binary Format Support**: + - Supports image conversion to binary files required by LVGL, ensuring compatibility with the LVGL graphics library and optimizing performance for display rendering. + + +## Add to project + +Packages from this repository are uploaded to [Espressif's component service](https://components.espressif.com/). +You can add them to your project via `idf.py add-dependancy`, e.g. +``` + idf.py add-dependency esp_mmap_assets +``` + +Alternatively, you can create `idf_component.yml`. More is in [Espressif's documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html). + +## Usage + +### CMake +Optionally, users can opt to have the image automatically flashed together with the app binaries, partition tables, etc. on idf.py flash by specifying FLASH_IN_PROJECT. For example: +```c + spiffs_create_partition_assets( + my_spiffs_partition + my_folder + FLASH_IN_PROJECT + MMAP_FILE_SUPPORT_FORMAT ".png" + MMAP_SUPPORT_QOI + MMAP_SUPPORT_SQOI + MMAP_SPLIT_HEIGHT 16) +``` +Additionally, the following options are supported. These options allow you to configure various aspects of image handling, such as enabling support for different formats (SJPG, SPNG, QOI, SQOI, RAW), and controlling how RAW images are processed. +```c + set(options FLASH_IN_PROJECT, // Defines storage type (flash in project) + MMAP_SUPPORT_SJPG, // Enable support for SJPG format + MMAP_SUPPORT_SPNG, // Enable support for SPNG format + MMAP_SUPPORT_QOI, // Enable support for QOI format + MMAP_SUPPORT_SQOI, // Enable support for SQOI format + MMAP_SUPPORT_RAW, // Enable support for RAW format (LVGL conversion only) + MMAP_RAW_DITHER, // Enable dithering for RAW images (LVGL conversion only) + MMAP_RAW_BGR_MODE) // Enable BGR mode for RAW images (LVGL conversion only) + + set(one_value_args MMAP_FILE_SUPPORT_FORMAT, // Specify supported file format (e.g., .png, .jpg) + MMAP_SPLIT_HEIGHT, // Define the height for image splitting + MMAP_RAW_FILE_FORMAT) // Specify the file format for RAW images (LVGL conversion only) + +``` + +### Initialization +```c + mmap_assets_handle_t asset_handle; + + /* partitions.csv + * -------------------------------------------------------- + * | Name | Type | SubType | Offset | Size | Flags | + * -------------------------------------------------------- + * | my_spiffs_partition | data | spiffs | | 6000K | | + * -------------------------------------------------------- + */ + const mmap_assets_config_t config = { + .partition_label = "my_spiffs_partition", + .max_files = MMAP_MY_FOLDER_FILES, //Get it from the compiled .h + .checksum = MMAP_MY_FOLDER_CHECKSUM, //Get it from the compiled .h + .flags = { + .mmap_enable = true, + .app_bin_check = true, + }, + }; + + ESP_ERROR_CHECK(mmap_assets_new(&config, &asset_handle)); + + const char *name = mmap_assets_get_name(asset_handle, 0); + const void *mem = mmap_assets_get_mem(asset_handle, 0); + int size = mmap_assets_get_size(asset_handle, 0); + int width = mmap_assets_get_width(asset_handle, 0); + int height = mmap_assets_get_height(asset_handle, 0); + + ESP_LOGI(TAG, "Asset - Name:[%s], Memory:[%p], Size:[%d bytes], Width:[%d px], Height:[%d px]", name, mem, size, width, height); + +``` diff --git a/examples/display/components/esp_mmap_assets/config_template.json.in b/examples/display/components/esp_mmap_assets/config_template.json.in new file mode 100644 index 000000000..70918f213 --- /dev/null +++ b/examples/display/components/esp_mmap_assets/config_template.json.in @@ -0,0 +1,20 @@ +{ + "project_dir": "@PROJECT_DIR@", + "main_path": "@CMAKE_CURRENT_LIST_DIR@", + "assets_path": "@base_dir_full_path@", + "image_file": "@image_file@", + "lvgl_ver": "@lvgl_ver@", + "assets_size": "@size@", + "support_format": "@arg_MMAP_FILE_SUPPORT_FORMAT@", + "name_length": "@CONFIG_MMAP_FILE_NAME_LENGTH@", + "split_height": "@arg_MMAP_SPLIT_HEIGHT@", + "support_spng": @support_spng@, + "support_sjpg": @support_sjpg@, + "support_qoi": @support_qoi@, + "support_sqoi": @support_sqoi@, + "support_raw": @support_raw@, + "support_raw_dither": @support_raw_dither@, + "support_raw_bgr": @support_raw_bgr@, + "support_raw_ff": "@arg_MMAP_RAW_FILE_FORMAT@", + "support_raw_cf": "@arg_MMAP_RAW_COLOR_FORMAT@" +} diff --git a/examples/display/components/esp_mmap_assets/esp_mmap_assets.c b/examples/display/components/esp_mmap_assets/esp_mmap_assets.c new file mode 100644 index 000000000..7309ba2a1 --- /dev/null +++ b/examples/display/components/esp_mmap_assets/esp_mmap_assets.c @@ -0,0 +1,328 @@ +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include "esp_err.h" +#include "esp_log.h" +#include "esp_check.h" +#include +#include +#include +#include "esp_mmap_assets.h" + +static const char *TAG = "mmap_assets"; + +#define ASSETS_FILE_NUM_OFFSET 0 +#define ASSETS_CHECKSUM_OFFSET 4 +#define ASSETS_TABLE_LEN 8 +#define ASSETS_TABLE_OFFSET 12 + +#define ASSETS_FILE_MAGIC_HEAD 0x5A5A +#define ASSETS_FILE_MAGIC_LEN 2 + +/** + * @brief Asset table structure, contains detailed information for each asset. + */ +#pragma pack(1) +typedef struct { + char asset_name[CONFIG_MMAP_FILE_NAME_LENGTH]; /*!< Name of the asset */ + uint32_t asset_size; /*!< Size of the asset */ + uint32_t asset_offset; /*!< Offset of the asset */ + uint16_t asset_width; /*!< Width of the asset */ + uint16_t asset_height; /*!< Height of the asset */ +} mmap_assets_table_t; +#pragma pack() + +typedef struct { + const char *asset_mem; + const mmap_assets_table_t *table; +} mmap_assets_item_t; + +typedef struct { + esp_partition_mmap_handle_t *mmap_handle; + const esp_partition_t *partition; + mmap_assets_item_t *item; + int max_asset; + struct { + unsigned int mmap_enable: 1; /*!< Flag to indicate if memory-mapped I/O is enabled */ + unsigned int reserved: 31; /*!< Reserved for future use */ + } flags; + int stored_files; +} mmap_assets_t; + +static uint32_t compute_checksum(const uint8_t *data, uint32_t length) +{ + uint32_t checksum = 0; + for (uint32_t i = 0; i < length; i++) { + checksum += data[i]; + } + return checksum & 0xFFFF; +} + +esp_err_t mmap_assets_new(const mmap_assets_config_t *config, mmap_assets_handle_t *ret_item) +{ + esp_err_t ret = ESP_OK; + const void *root = NULL; + mmap_assets_item_t *item = NULL; + mmap_assets_t *map_asset = NULL; + esp_partition_mmap_handle_t *mmap_handle = NULL; + + ESP_GOTO_ON_FALSE(config && ret_item, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument"); + + map_asset = (mmap_assets_t *)calloc(1, sizeof(mmap_assets_t)); + ESP_GOTO_ON_FALSE(map_asset, ESP_ERR_NO_MEM, err, TAG, "no mem for map_asset handle"); + + map_asset->flags.mmap_enable = config->flags.mmap_enable; + + const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, config->partition_label); + ESP_GOTO_ON_FALSE(partition, ESP_ERR_NOT_FOUND, err, TAG, "Can not find \"%s\" in partition table", config->partition_label); + map_asset->partition = partition; + + int stored_files = 0; + int stored_len = 0; + uint32_t stored_chksum = 0; + uint32_t calculated_checksum = 0; + + if (map_asset->flags.mmap_enable) { + int free_pages = spi_flash_mmap_get_free_pages(ESP_PARTITION_MMAP_DATA); + uint32_t storage_size = free_pages * 64 * 1024; + ESP_LOGD(TAG, "The storage free size is %ld KB", storage_size / 1024); + ESP_LOGD(TAG, "The partition size is %ld KB", partition->size / 1024); + ESP_GOTO_ON_FALSE((storage_size > partition->size), ESP_ERR_INVALID_SIZE, err, TAG, "The free size is less than %s partition required", partition->label); + + mmap_handle = (esp_partition_mmap_handle_t *)malloc(sizeof(esp_partition_mmap_handle_t)); + ESP_GOTO_ON_ERROR(esp_partition_mmap(partition, 0, partition->size, ESP_PARTITION_MMAP_DATA, &root, mmap_handle), err, TAG, "esp_partition_mmap failed"); + + stored_files = *(int *)(root + ASSETS_FILE_NUM_OFFSET); + stored_chksum = *(uint32_t *)(root + ASSETS_CHECKSUM_OFFSET); + stored_len = *(uint32_t *)(root + ASSETS_TABLE_LEN); + + if (config->flags.full_check) { + calculated_checksum = compute_checksum((uint8_t *)(root + ASSETS_TABLE_OFFSET), stored_len); + } + } else { + esp_partition_read(partition, ASSETS_FILE_NUM_OFFSET, &stored_files, sizeof(stored_files)); + esp_partition_read(partition, ASSETS_CHECKSUM_OFFSET, &stored_chksum, sizeof(stored_chksum)); + esp_partition_read(partition, ASSETS_TABLE_LEN, &stored_len, sizeof(stored_len)); + + if (config->flags.full_check) { + uint32_t read_offset = ASSETS_TABLE_OFFSET; + uint32_t bytes_left = stored_len; + uint8_t buffer[1024]; + + while (bytes_left > 0) { + uint32_t read_size = (bytes_left > sizeof(buffer)) ? sizeof(buffer) : bytes_left; + ESP_GOTO_ON_ERROR(esp_partition_read(partition, read_offset, buffer, read_size), err, TAG, "esp_partition_read failed"); + + calculated_checksum += compute_checksum(buffer, read_size); + read_offset += read_size; + bytes_left -= read_size; + } + calculated_checksum &= 0xFFFF; + } + } + + if (config->flags.full_check) { + ESP_GOTO_ON_FALSE(calculated_checksum == stored_chksum, ESP_ERR_INVALID_CRC, err, TAG, "bad full checksum"); + } + + if (config->flags.app_bin_check) { + if (config->max_files != stored_files) { + ret = ESP_ERR_INVALID_SIZE; + ESP_LOGE(TAG, "bad input file num: Input %d, Stored %d", config->max_files, stored_files); + } + + if (config->checksum != stored_chksum) { + ret = ESP_ERR_INVALID_CRC; + ESP_LOGE(TAG, "bad input chksum: Input 0x%x, Stored 0x%x", (unsigned int)config->checksum, (unsigned int)stored_chksum); + } + } + + map_asset->stored_files = stored_files; + + item = (mmap_assets_item_t *)malloc(sizeof(mmap_assets_item_t) * config->max_files); + ESP_GOTO_ON_FALSE(item, ESP_ERR_NO_MEM, err, TAG, "no mem for asset item"); + + if (map_asset->flags.mmap_enable) { + mmap_assets_table_t *table = (mmap_assets_table_t *)(root + ASSETS_TABLE_OFFSET); + for (int i = 0; i < config->max_files; i++) { + (item + i)->table = (table + i); + (item + i)->asset_mem = (void *)(root + ASSETS_TABLE_OFFSET + config->max_files * sizeof(mmap_assets_table_t) + table[i].asset_offset); + } + } else { + mmap_assets_table_t *table = malloc(sizeof(mmap_assets_table_t) * config->max_files); + for (int i = 0; i < config->max_files; i++) { + esp_partition_read(partition, ASSETS_TABLE_OFFSET + i * sizeof(mmap_assets_table_t), (table + i), sizeof(mmap_assets_table_t)); + (item + i)->table = (table + i); + (item + i)->asset_mem = (char *)(ASSETS_TABLE_OFFSET + config->max_files * sizeof(mmap_assets_table_t) + table[i].asset_offset); + } + } + + for (int i = 0; i < config->max_files; i++) { + ESP_LOGD(TAG, "[%d], offset:[%" PRIu32 "], size:[%" PRIu32 "], name:%s, %p", + i, + (item + i)->table->asset_offset, + (item + i)->table->asset_size, + (item + i)->table->asset_name, + (item + i)->asset_mem); + + if (config->flags.metadata_check) { + uint16_t magic_data, *magic_ptr = NULL; + if (map_asset->flags.mmap_enable) { + magic_ptr = (uint16_t *)(item + i)->asset_mem; + } else { + esp_partition_read(map_asset->partition, (int)(item + i)->asset_mem, &magic_data, ASSETS_FILE_MAGIC_LEN); + magic_ptr = &magic_data; + } + ESP_GOTO_ON_FALSE(*magic_ptr == ASSETS_FILE_MAGIC_HEAD, ESP_ERR_INVALID_CRC, err, TAG, + "(%s) bad file magic header", (item + i)->table->asset_name); + } + } + + map_asset->mmap_handle = mmap_handle; + map_asset->item = item; + map_asset->max_asset = config->max_files; + *ret_item = (mmap_assets_handle_t)map_asset; + + ESP_LOGD(TAG, "new asset handle:@%p", map_asset); + + ESP_LOGI(TAG, "Partition '%s' successfully created, version: %d.%d.%d", + config->partition_label, ESP_MMAP_ASSETS_VER_MAJOR, ESP_MMAP_ASSETS_VER_MINOR, ESP_MMAP_ASSETS_VER_PATCH); + + return ret; + +err: + if (item) { + if (false == map_asset->flags.mmap_enable) { + free((void *)(item + 0)->table); + } + free(item); + } + + if (mmap_handle) { + esp_partition_munmap(*mmap_handle); + free(mmap_handle); + } + + if (map_asset) { + free(map_asset); + } + + return ret; +} + +esp_err_t mmap_assets_del(mmap_assets_handle_t handle) +{ + ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "handle is invalid"); + mmap_assets_t *map_asset = (mmap_assets_t *)(handle); + + if (map_asset->mmap_handle) { + esp_partition_munmap(*(map_asset->mmap_handle)); + free(map_asset->mmap_handle); + } + + if (map_asset->item) { + if (false == map_asset->flags.mmap_enable) { + free((void *)(map_asset->item + 0)->table); + } + free(map_asset->item); + } + + if (map_asset) { + free(map_asset); + } + + return ESP_OK; +} + +int mmap_assets_get_stored_files(mmap_assets_handle_t handle) +{ + assert(handle && "handle is invalid"); + mmap_assets_t *map_asset = (mmap_assets_t *)(handle); + + return map_asset->stored_files; +} + +size_t mmap_assets_copy_mem(mmap_assets_handle_t handle, size_t offset, void *dest_buffer, size_t size) +{ + assert(handle && "handle is invalid"); + mmap_assets_t *map_asset = (mmap_assets_t *)(handle); + + if (true == map_asset->flags.mmap_enable) { + memcpy(dest_buffer, (void *)offset, size); + return size; + } else if (offset) { + esp_partition_read(map_asset->partition, offset, dest_buffer, size); + return size; + } else { + ESP_LOGE(TAG, "Invalid offset: %zu.", offset); + return 0; + } +} + +const uint8_t *mmap_assets_get_mem(mmap_assets_handle_t handle, int index) +{ + assert(handle && "handle is invalid"); + mmap_assets_t *map_asset = (mmap_assets_t *)(handle); + + if (map_asset->max_asset > index) { + return (const uint8_t *)((map_asset->item + index)->asset_mem + ASSETS_FILE_MAGIC_LEN); + } else { + ESP_LOGE(TAG, "Invalid index: %d. Maximum index is %d.", index, map_asset->max_asset); + return NULL; + } +} + +const char *mmap_assets_get_name(mmap_assets_handle_t handle, int index) +{ + assert(handle && "handle is invalid"); + mmap_assets_t *map_asset = (mmap_assets_t *)(handle); + + if (map_asset->max_asset > index) { + return (map_asset->item + index)->table->asset_name; + } else { + ESP_LOGE(TAG, "Invalid index: %d. Maximum index is %d.", index, map_asset->max_asset); + return NULL; + } +} + +int mmap_assets_get_size(mmap_assets_handle_t handle, int index) +{ + assert(handle && "handle is invalid"); + mmap_assets_t *map_asset = (mmap_assets_t *)(handle); + + if (map_asset->max_asset > index) { + return (map_asset->item + index)->table->asset_size; + } else { + ESP_LOGE(TAG, "Invalid index: %d. Maximum index is %d.", index, map_asset->max_asset); + return -1; + } +} + +int mmap_assets_get_width(mmap_assets_handle_t handle, int index) +{ + assert(handle && "handle is invalid"); + mmap_assets_t *map_asset = (mmap_assets_t *)(handle); + + if (map_asset->max_asset > index) { + return (map_asset->item + index)->table->asset_width; + } else { + ESP_LOGE(TAG, "Invalid index: %d. Maximum index is %d.", index, map_asset->max_asset); + return -1; + } +} + +int mmap_assets_get_height(mmap_assets_handle_t handle, int index) +{ + assert(handle && "handle is invalid"); + mmap_assets_t *map_asset = (mmap_assets_t *)(handle); + + if (map_asset->max_asset > index) { + return (map_asset->item + index)->table->asset_height; + } else { + ESP_LOGE(TAG, "Invalid index: %d. Maximum index is %d.", index, map_asset->max_asset); + return -1; + } +} diff --git a/examples/display/components/esp_mmap_assets/idf_component.yml b/examples/display/components/esp_mmap_assets/idf_component.yml new file mode 100644 index 000000000..2f38dd9bb --- /dev/null +++ b/examples/display/components/esp_mmap_assets/idf_component.yml @@ -0,0 +1,22 @@ +version: 1.3.0 +targets: + - esp32 + - esp32c2 + - esp32c3 + - esp32c6 + - esp32h2 + - esp32s2 + - esp32s3 + - esp32p4 +dependencies: + cmake_utilities: + version: 0.* + idf: + version: '>=5.0' +description: assets mmap tool +issues: https://github.com/espressif/esp-iot-solution/issues +repository: git://github.com/espressif/esp-iot-solution.git +url: https://github.com/espressif/esp-iot-solution/tree/master/components/display/tools/esp_mmap_assets +documentation: https://docs.espressif.com/projects/esp-iot-solution/en/latest/display/tools/esp_mmap_assets.html +examples: + - path: ../../../../examples/hmi/perf_benchmark diff --git a/examples/display/components/esp_mmap_assets/include/esp_mmap_assets.h b/examples/display/components/esp_mmap_assets/include/esp_mmap_assets.h new file mode 100644 index 000000000..a6655d73e --- /dev/null +++ b/examples/display/components/esp_mmap_assets/include/esp_mmap_assets.h @@ -0,0 +1,142 @@ +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "esp_err.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Asset configuration structure, contains the asset table and other configuration information. + */ +typedef struct { + const char *partition_label; /*!< Label of the partition containing the assets */ + int max_files; /*!< Maximum number of assets supported */ + uint32_t checksum; /*!< Checksum of the asset table for integrity verification */ + struct { + unsigned int mmap_enable: 1; /*!< Flag to indicate if memory-mapped I/O is enabled */ + unsigned int app_bin_check: 1; /*!< Flag to enable app header and bin file consistency check */ + unsigned int full_check: 1; /*!< Flag to enable self-consistency check */ + unsigned int metadata_check: 1; /*!< Flag to enable metadata verification */ + unsigned int reserved: 28; /*!< Reserved for future use */ + } flags; /*!< Configuration flags */ +} mmap_assets_config_t; + +/** + * @brief Asset handle type, points to the asset. + */ +typedef struct mmap_assets_t *mmap_assets_handle_t; /*!< Type of asset handle */ + +/** + * @brief Create a new asset instance. + * + * @param[in] config Pointer to the asset configuration structure. + * @param[out] ret_item Pointer to the handle of the newly created asset instance. + * + * @return + * - ESP_OK: Success + * - ESP_ERR_NO_MEM: Insufficient memory + * - ESP_ERR_NOT_FOUND: Can't find partition + * - ESP_ERR_INVALID_SIZE: File num mismatch + * - ESP_ERR_INVALID_CRC: Checksum mismatch + */ +esp_err_t mmap_assets_new(const mmap_assets_config_t *config, mmap_assets_handle_t *ret_item); + +/** + * @brief Delete an asset instance. + * + * @param[in] handle Asset instance handle. + * + * @return + * - ESP_OK: Success + * - ESP_ERR_INVALID_ARG: Invalid argument + */ +esp_err_t mmap_assets_del(mmap_assets_handle_t handle); + +/** + * @brief Get the memory of the asset at the specified index. + * + * @param[in] handle Asset instance handle. + * @param[in] index Index of the asset. + * + * @return Pointer to the asset memory, or NULL if index is invalid. + */ +const uint8_t *mmap_assets_get_mem(mmap_assets_handle_t handle, int index); + +/** + * @brief Copy a portion of an asset's memory to a destination buffer. + * + * @param[in] handle Asset instance handle. + * @param[in] offset Offset within the asset's memory from which to start copying. + * @param[out] dest_buffer Pointer to the destination buffer where the memory will be copied. + * @param[in] size Number of bytes to copy from the asset's memory to the destination buffer. + * + * @return The actual number of bytes copied, or 0 if the operation fails. + */ +size_t mmap_assets_copy_mem(mmap_assets_handle_t handle, size_t offset, void *dest_buffer, size_t size); + +/** + * @brief Get the name of the asset at the specified index. + * + * @param[in] handle Asset instance handle. + * @param[in] index Index of the asset. + * + * @return Pointer to the asset name, or NULL if index is invalid. + */ +const char *mmap_assets_get_name(mmap_assets_handle_t handle, int index); + +/** + * @brief Get the size of the asset at the specified index. + * + * @param[in] handle Asset instance handle. + * @param[in] index Index of the asset. + * + * @return Size of the asset, or -1 if index is invalid. + */ +int mmap_assets_get_size(mmap_assets_handle_t handle, int index); + +/** + * @brief Get the width of the asset at the specified index. + * + * @param[in] handle Asset instance handle. + * @param[in] index Index of the asset. + * + * @return Width of the asset, or -1 if index is invalid. + */ +int mmap_assets_get_width(mmap_assets_handle_t handle, int index); + +/** + * @brief Get the height of the asset at the specified index. + * + * @param[in] handle Asset instance handle. + * @param[in] index Index of the asset. + * + * @return Height of the asset, or -1 if index is invalid. + */ +int mmap_assets_get_height(mmap_assets_handle_t handle, int index); + +/** + * @brief Get the number of stored files in the memory-mapped asset. + * + * This function returns the total number of assets stored in the memory-mapped file + * associated with the given handle. This can be useful for iterating over all assets + * or validating the file contents. + * + * @param[in] handle Asset instance handle. + * This handle is used to identify the memory-mapped file instance + * containing the assets. + * + * @return The number of stored assets. + * Returns the total count of assets if the handle is valid, otherwise returns 0. + */ +int mmap_assets_get_stored_files(mmap_assets_handle_t handle); + +#ifdef __cplusplus +} +#endif diff --git a/examples/display/components/esp_mmap_assets/license.txt b/examples/display/components/esp_mmap_assets/license.txt new file mode 100644 index 000000000..d64569567 --- /dev/null +++ b/examples/display/components/esp_mmap_assets/license.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/examples/display/components/esp_mmap_assets/project_include.cmake b/examples/display/components/esp_mmap_assets/project_include.cmake new file mode 100644 index 000000000..5b3acbc49 --- /dev/null +++ b/examples/display/components/esp_mmap_assets/project_include.cmake @@ -0,0 +1,200 @@ +# spiffs_create_partition_assets +# +# Create a spiffs image of the specified directory on the host during build and optionally +# have the created image flashed using `idf.py flash` +function(spiffs_create_partition_assets partition base_dir) + # Define option flags (BOOL) + set(options FLASH_IN_PROJECT + MMAP_SUPPORT_SJPG + MMAP_SUPPORT_SPNG + MMAP_SUPPORT_QOI + MMAP_SUPPORT_SQOI + MMAP_SUPPORT_RAW + MMAP_RAW_DITHER + MMAP_RAW_BGR_MODE) + + # Define one-value arguments (STRING and INT) + set(one_value_args MMAP_FILE_SUPPORT_FORMAT + MMAP_SPLIT_HEIGHT + MMAP_RAW_FILE_FORMAT + MMAP_RAW_COLOR_FORMAT) + + # Define multi-value arguments + set(multi DEPENDS) + + # Parse the arguments passed to the function + cmake_parse_arguments(arg + "${options}" + "${one_value_args}" + "${multi}" + "${ARGN}") + + if(NOT DEFINED arg_MMAP_FILE_SUPPORT_FORMAT OR arg_MMAP_FILE_SUPPORT_FORMAT STREQUAL "") + message(FATAL_ERROR "MMAP_FILE_SUPPORT_FORMAT is empty. Please input the file suffixes you want (e.g .png, .jpg).") + endif() + + if(arg_MMAP_SUPPORT_QOI AND (arg_MMAP_SUPPORT_SJPG OR arg_MMAP_SUPPORT_SPNG)) + message(FATAL_ERROR "MMAP_SUPPORT_QOI depends on !MMAP_SUPPORT_SJPG && !MMAP_SUPPORT_SPNG.") + endif() + + if(arg_MMAP_SUPPORT_SQOI AND NOT arg_MMAP_SUPPORT_QOI) + message(FATAL_ERROR "MMAP_SUPPORT_SQOI depends on MMAP_SUPPORT_QOI.") + endif() + + if( (arg_MMAP_SUPPORT_SJPG OR arg_MMAP_SUPPORT_SPNG OR arg_MMAP_SUPPORT_SQOI) AND + (NOT DEFINED arg_MMAP_SPLIT_HEIGHT OR arg_MMAP_SPLIT_HEIGHT LESS 1) ) + message(FATAL_ERROR "MMAP_SPLIT_HEIGHT must be defined and its value >= 1 when MMAP_SUPPORT_SJPG, MMAP_SUPPORT_SPNG, or MMAP_SUPPORT_SQOI is enabled.") + endif() + + if(DEFINED arg_MMAP_SPLIT_HEIGHT) + if(NOT (arg_MMAP_SUPPORT_SJPG OR arg_MMAP_SUPPORT_SPNG OR arg_MMAP_SUPPORT_SQOI)) + message(FATAL_ERROR "MMAP_SPLIT_HEIGHT depends on MMAP_SUPPORT_SJPG || MMAP_SUPPORT_SPNG || MMAP_SUPPORT_SQOI.") + endif() + endif() + + if(arg_MMAP_SUPPORT_RAW AND (arg_MMAP_SUPPORT_SJPG OR arg_MMAP_SUPPORT_SPNG OR arg_MMAP_SUPPORT_QOI OR arg_MMAP_SUPPORT_SQOI)) + message(FATAL_ERROR "MMAP_SUPPORT_RAW and MMAP_SUPPORT_SJPG/MMAP_SUPPORT_SPNG/MMAP_SUPPORT_QOI/MMAP_SUPPORT_SQOI cannot be enabled at the same time.") + endif() + + # Try to install Pillow using pip + idf_build_get_property(python PYTHON) + execute_process( + COMMAND ${python} -c "import PIL" + RESULT_VARIABLE PIL_FOUND + OUTPUT_QUIET + ERROR_QUIET + ) + + if(NOT PIL_FOUND EQUAL 0) + message(STATUS "Pillow not found. Attempting to install it using pip...") + + execute_process( + COMMAND ${python} -m pip install -U Pillow + RESULT_VARIABLE result + OUTPUT_VARIABLE output + ERROR_VARIABLE error + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_STRIP_TRAILING_WHITESPACE + ) + + if(result) + message(FATAL_ERROR "Failed to install Pillow using pip. Please install it manually.\nError: ${error}") + else() + message(STATUS "Pillow successfully installed.") + endif() + endif() + + # Try to install qoi-conv using pip + execute_process( + COMMAND ${python} -c "import importlib; importlib.import_module('qoi-conv')" + RESULT_VARIABLE PIL_FOUND + OUTPUT_QUIET + ERROR_QUIET + ) + + if(NOT PIL_FOUND EQUAL 0) + message(STATUS "qoi-conv not found. Attempting to install it using pip...") + + execute_process( + COMMAND ${python} -m pip install -U qoi-conv + RESULT_VARIABLE result + OUTPUT_VARIABLE output + ERROR_VARIABLE error + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_STRIP_TRAILING_WHITESPACE + ) + + if(result) + message(FATAL_ERROR "Failed to install qoi-conv using pip. Please install it manually.\nError: ${error}") + else() + message(STATUS "qoi-conv successfully installed.") + endif() + endif() + + get_filename_component(base_dir_full_path ${base_dir} ABSOLUTE) + get_filename_component(base_dir_name "${base_dir_full_path}" NAME) + + partition_table_get_partition_info(size "--partition-name ${partition}" "size") + partition_table_get_partition_info(offset "--partition-name ${partition}" "offset") + + if("${size}" AND "${offset}") + + set(TARGET_COMPONENT "") + set(TARGET_COMPONENT_PATH "") + + idf_build_get_property(build_components BUILD_COMPONENTS) + foreach(COMPONENT ${build_components}) + if(COMPONENT MATCHES "esp_mmap_assets" OR COMPONENT MATCHES "espressif__esp_mmap_assets") + set(TARGET_COMPONENT ${COMPONENT}) + break() + endif() + endforeach() + + if(TARGET_COMPONENT STREQUAL "") + message(FATAL_ERROR "Component 'esp_mmap_assets' not found.") + else() + idf_component_get_property(TARGET_COMPONENT_PATH ${TARGET_COMPONENT} COMPONENT_DIR) + endif() + + set(image_file ${CMAKE_BINARY_DIR}/mmap_build/${base_dir_name}/${partition}.bin) + set(MVMODEL_EXE ${TARGET_COMPONENT_PATH}/spiffs_assets_gen.py) + + if(arg_MMAP_SUPPORT_RAW) + foreach(COMPONENT ${build_components}) + if(COMPONENT MATCHES "^lvgl$" OR COMPONENT MATCHES "^lvgl__lvgl$") + set(lvgl_name ${COMPONENT}) + if(COMPONENT STREQUAL "lvgl") + set(lvgl_ver $ENV{LVGL_VERSION}) + else() + idf_component_get_property(lvgl_ver ${lvgl_name} COMPONENT_VERSION) + endif() + break() + endif() + endforeach() + + if("${lvgl_ver}" STREQUAL "") + message("Could not determine LVGL version, assuming v8.x") + set(lvgl_ver "8.0.0") + endif() + message(STATUS "LVGL version: ${lvgl_ver}") + endif() + + if(NOT arg_MMAP_SPLIT_HEIGHT) + set(arg_MMAP_SPLIT_HEIGHT 0) # Default value + endif() + + string(TOLOWER "${arg_MMAP_SUPPORT_SJPG}" support_sjpg) + string(TOLOWER "${arg_MMAP_SUPPORT_SPNG}" support_spng) + string(TOLOWER "${arg_MMAP_SUPPORT_QOI}" support_qoi) + string(TOLOWER "${arg_MMAP_SUPPORT_SQOI}" support_sqoi) + string(TOLOWER "${arg_MMAP_SUPPORT_RAW}" support_raw) + string(TOLOWER "${arg_MMAP_RAW_DITHER}" support_raw_dither) + string(TOLOWER "${arg_MMAP_RAW_BGR_MODE}" support_raw_bgr) + + set(CONFIG_FILE_PATH "${CMAKE_BINARY_DIR}/mmap_build/${base_dir_name}/config.json") + configure_file( + "${TARGET_COMPONENT_PATH}/config_template.json.in" + "${CONFIG_FILE_PATH}" + @ONLY + ) + + add_custom_target(spiffs_${partition}_bin ALL + COMMENT "Move and Pack assets..." + COMMAND python ${MVMODEL_EXE} --config "${CONFIG_FILE_PATH}" + DEPENDS ${arg_DEPENDS} + VERBATIM) + + set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY + ADDITIONAL_CLEAN_FILES + ${image_file}) + + if(arg_FLASH_IN_PROJECT) + esptool_py_flash_to_partition(flash "${partition}" "${image_file}") + add_dependencies(flash spiffs_${partition}_bin) + endif() + else() + set(message "Failed to create assets bin for partition '${partition}'. " + "Check project configuration if using the correct partition table file.") + fail_at_build_time(spiffs_${partition}_bin "${message}") + endif() +endfunction() diff --git a/examples/display/components/esp_mmap_assets/spiffs_assets_gen.py b/examples/display/components/esp_mmap_assets/spiffs_assets_gen.py new file mode 100644 index 000000000..eb2eeed26 --- /dev/null +++ b/examples/display/components/esp_mmap_assets/spiffs_assets_gen.py @@ -0,0 +1,595 @@ +# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 +import io +import os +import argparse +import json +import shutil +import math +import sys +import time +import numpy as np +import importlib +import subprocess +import urllib.request + +from PIL import Image +from datetime import datetime +from dataclasses import dataclass +from typing import List +from pathlib import Path +from packaging import version + +sys.dont_write_bytecode = True + +@dataclass +class AssetCopyConfig: + assets_path: str + target_path: str + spng_enable: bool + sjpg_enable: bool + qoi_enable: bool + sqoi_enable: bool + row_enable: bool + support_format: List[str] + split_height: int + +@dataclass +class PackModelsConfig: + target_path: str + main_path: str + image_file: str + assets_path: str + name_length: int + +def generate_header_filename(path): + asset_name = os.path.basename(path) + + header_filename = f'mmap_generate_{asset_name}.h' + return header_filename + +def compute_checksum(data): + checksum = sum(data) & 0xFFFF + return checksum + +def sort_key(filename): + basename, extension = os.path.splitext(filename) + return extension, basename + +def download_v8_script(convert_path): + """ + Ensure that the lvgl_image_converter repository is present at the specified path. + If not, clone the repository. Then, checkout to a specific commit. + + Parameters: + - convert_path (str): The directory path where lvgl_image_converter should be located. + """ + + # Check if convert_path is not empty + if convert_path: + # If the directory does not exist, create it and clone the repository + if not os.path.exists(convert_path): + os.makedirs(convert_path, exist_ok=True) + try: + subprocess.run( + ['git', 'clone', 'https://github.com/W-Mai/lvgl_image_converter.git', convert_path], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + check=True + ) + except subprocess.CalledProcessError as e: + print(f'Git clone failed: {e}') + sys.exit(1) + + # Checkout to the specific commit + try: + subprocess.run( + ['git', 'checkout', '9174634e9dcc1b21a63668969406897aad650f35'], + cwd=convert_path, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + check=True + ) + except subprocess.CalledProcessError as e: + print(f'Failed to checkout to the specific commit: {e}') + sys.exit(1) + else: + print('Error: convert_path is NULL') + sys.exit(1) + +def download_v9_script(url: str, destination: str) -> None: + """ + Download a Python script from a URL to a local destination. + + Parameters: + - url (str): URL to download the script from. + - destination (str): Local path to save the downloaded script. + + Raises: + - Exception: If the download fails. + """ + file_path = Path(destination) + + # Check if the file already exists + if file_path.exists(): + if file_path.is_file(): + return + + try: + # Create the parent directories if they do not exist + file_path.parent.mkdir(parents=True, exist_ok=True) + + # Open the URL and retrieve the data + with urllib.request.urlopen(url) as response, open(file_path, 'wb') as out_file: + data = response.read() # Read the entire response + out_file.write(data) # Write data to the local file + + except urllib.error.HTTPError as e: + print(f'HTTP Error: {e.code} - {e.reason} when accessing {url}') + sys.exit(1) + except urllib.error.URLError as e: + print(f'URL Error: {e.reason} when accessing {url}') + sys.exit(1) + except Exception as e: + print(f'An unexpected error occurred: {e}') + sys.exit(1) + +def split_image(im, block_size, input_dir, ext, convert_to_qoi): + """Splits the image into blocks based on the block size.""" + width, height = im.size + + if block_size: + splits = math.ceil(height / block_size) + else: + splits = 1 + + for i in range(splits): + if i < splits - 1: + crop = im.crop((0, i * block_size, width, (i + 1) * block_size)) + else: + crop = im.crop((0, i * block_size, width, height)) + + output_path = os.path.join(input_dir, str(i) + ext) + crop.save(output_path, quality=100) + + qoi_module = importlib.import_module('qoi-conv.qoi') + Qoi = qoi_module.Qoi + replace_extension = qoi_module.replace_extension + + if convert_to_qoi: + with Image.open(output_path) as img: + if img.mode != 'RGBA': + img = img.convert('RGBA') + + img_data = np.asarray(img) + out_path = qoi_module.replace_extension(output_path, 'qoi') + new_image = qoi_module.Qoi().save(out_path, img_data) + os.remove(output_path) + + + return width, height, splits + +def create_header(width, height, splits, split_height, lenbuf, ext): + """Creates the header for the output file based on the format.""" + header = bytearray() + + if ext.lower() == '.jpg': + header += bytearray('_SJPG__'.encode('UTF-8')) + elif ext.lower() == '.png': + header += bytearray('_SPNG__'.encode('UTF-8')) + elif ext.lower() == '.qoi': + header += bytearray('_SQOI__'.encode('UTF-8')) + + # 6 BYTES VERSION + header += bytearray(('\x00V1.00\x00').encode('UTF-8')) + + # WIDTH 2 BYTES + header += width.to_bytes(2, byteorder='little') + + # HEIGHT 2 BYTES + header += height.to_bytes(2, byteorder='little') + + # NUMBER OF ITEMS 2 BYTES + header += splits.to_bytes(2, byteorder='little') + + # SPLIT HEIGHT 2 BYTES + header += split_height.to_bytes(2, byteorder='little') + + for item_len in lenbuf: + # LENGTH 2 BYTES + header += item_len.to_bytes(2, byteorder='little') + + return header + +def save_image(output_file_path, header, split_data): + """Saves the image with the constructed header and split data.""" + with open(output_file_path, 'wb') as f: + if header is not None: + f.write(header + split_data) + else: + f.write(split_data) + +def handle_lvgl_version_v9(input_file: str, input_dir: str, + input_filename: str, convert_path: str) -> None: + """ + Handle conversion for LVGL versions greater than 9.0. + + Parameters: + - input_file (str): Path to the input image file. + - input_dir (str): Directory of the input file. + - input_filename (str): Name of the input file. + - convert_path (str): Path for conversion scripts and outputs. + """ + + convert_file = os.path.join(convert_path, 'LVGLImage.py') + lvgl_image_url = 'https://raw.githubusercontent.com/lvgl/lvgl/refs/tags/v9.2.0/scripts/LVGLImage.py' + + download_v9_script(url=lvgl_image_url, destination=convert_file) + lvgl_script = Path(convert_file) + + cmd = [ + 'python', + str(lvgl_script), + '--ofmt', 'BIN', + '--cf', config_data['support_raw_cf'], + '--compress', 'NONE', + '--output', str(input_dir), + input_file + ] + + try: + result = subprocess.run( + cmd, + check=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True + ) + print(f'Completed {input_filename} -> BIN') + except subprocess.CalledProcessError as e: + print('An error occurred while executing LVGLImage.py:') + print(e.stderr) + sys.exit(e.returncode) + +def handle_lvgl_version_v8(input_file: str, input_dir: str, input_filename: str, convert_path: str) -> None: + """ + Handle conversion for supported LVGL versions (<= 9.0). + + Parameters: + - input_file (str): Path to the input image file. + - input_dir (str): Directory of the input file. + - input_filename (str): Name of the input file. + - convert_path (str): Path for conversion scripts and outputs. + """ + + download_v8_script(convert_path=convert_path) + + if convert_path not in sys.path: + sys.path.append(convert_path) + + try: + import lv_img_conv + except ImportError as e: + print(f"Failed to import 'lv_img_conv' from '{convert_path}': {e}") + sys.exit(1) + + try: + lv_img_conv.conv_one_file( + root=Path(input_dir), + filepath=Path(input_file), + f=config_data['support_raw_ff'], + cf=config_data['support_raw_cf'], + ff='BIN', + dither=config_data['support_raw_dither'], + bgr_mode=config_data['support_raw_bgr'], + ) + print(f'Completed {input_filename} -> BIN') + except KeyError as e: + print(f'Missing configuration key: {e}') + sys.exit(1) + except Exception as e: + print(f'An error occurred during conversion: {e}') + sys.exit(1) + +def process_image(input_file, height_str, output_extension, convert_to_qoi=False): + """Main function to process the image and save it as .sjpg, .spng, or .sqoi.""" + try: + SPLIT_HEIGHT = int(height_str) + if SPLIT_HEIGHT < 0: + raise ValueError('Height must be a positive integer') + except ValueError as e: + print('Error: Height must be a positive integer') + sys.exit(1) + + input_dir, input_filename = os.path.split(input_file) + base_filename, ext = os.path.splitext(input_filename) + OUTPUT_FILE_NAME = base_filename + + try: + im = Image.open(input_file) + except Exception as e: + print('Error:', e) + sys.exit(0) + + width, height, splits = split_image(im, SPLIT_HEIGHT, input_dir, ext, convert_to_qoi) + + split_data = bytearray() + lenbuf = [] + + if convert_to_qoi: + ext = '.qoi' + + for i in range(splits): + with open(os.path.join(input_dir, str(i) + ext), 'rb') as f: + a = f.read() + split_data += a + lenbuf.append(len(a)) + os.remove(os.path.join(input_dir, str(i) + ext)) + + header = None + if splits == 1 and convert_to_qoi: + output_file_path = os.path.join(input_dir, OUTPUT_FILE_NAME + ext) + else: + header = create_header(width, height, splits, SPLIT_HEIGHT, lenbuf, ext) + output_file_path = os.path.join(input_dir, OUTPUT_FILE_NAME + output_extension) + + save_image(output_file_path, header, split_data) + + print('Completed', input_filename, '->', os.path.basename(output_file_path)) + +def convert_image_to_qoi(input_file, height_str): + process_image(input_file, height_str, '.sqoi', convert_to_qoi=True) + +def convert_image_to_simg(input_file, height_str): + input_dir, input_filename = os.path.split(input_file) + _, ext = os.path.splitext(input_filename) + output_extension = '.sjpg' if ext.lower() == '.jpg' else '.spng' + process_image(input_file, height_str, output_extension, convert_to_qoi=False) + +def convert_image_to_raw(input_file: str) -> None: + """ + Convert an image to raw binary format compatible with LVGL. + + Parameters: + - input_file (str): Path to the input image file. + + Raises: + - FileNotFoundError: If required scripts are not found. + - subprocess.CalledProcessError: If the external conversion script fails. + - KeyError: If required keys are missing in config_data. + """ + input_dir, input_filename = os.path.split(input_file) + _, ext = os.path.splitext(input_filename) + convert_path = os.path.join(os.path.dirname(input_file), 'lvgl_image_converter') + lvgl_ver_str = config_data.get('lvgl_ver', '9.0.0') + + try: + lvgl_version = version.parse(lvgl_ver_str) + except version.InvalidVersion: + print(f'Invalid LVGL version format: {lvgl_ver_str}') + sys.exit(1) + + if lvgl_version >= version.parse('9.0.0'): + handle_lvgl_version_v9( + input_file=input_file, + input_dir=input_dir, + input_filename=input_filename, + convert_path=convert_path + ) + else: + handle_lvgl_version_v8( + input_file=input_file, + input_dir=input_dir, + input_filename=input_filename, + convert_path=convert_path + ) + +def pack_assets(config: PackModelsConfig): + """ + Pack models based on the provided configuration. + """ + + target_path = config.target_path + assets_c_path = config.main_path + out_file = config.image_file + assets_path = config.assets_path + max_name_len = config.name_length + + merged_data = bytearray() + file_info_list = [] + skip_files = ['config.json', 'lvgl_image_converter'] + + file_list = sorted(os.listdir(target_path), key=sort_key) + for filename in file_list: + if filename in skip_files: + continue + + file_path = os.path.join(target_path, filename) + file_name = os.path.basename(file_path) + file_size = os.path.getsize(file_path) + + try: + img = Image.open(file_path) + width, height = img.size + except Exception as e: + # print("Error:", e) + _, file_extension = os.path.splitext(file_path) + if file_extension.lower() in ['.sjpg', '.spng', '.sqoi']: + offset = 14 + with open(file_path, 'rb') as f: + f.seek(offset) + width_bytes = f.read(2) + height_bytes = f.read(2) + width = int.from_bytes(width_bytes, byteorder='little') + height = int.from_bytes(height_bytes, byteorder='little') + else: + width, height = 0, 0 + + file_info_list.append((file_name, len(merged_data), file_size, width, height)) + # Add 0x5A5A prefix to merged_data + merged_data.extend(b'\x5A' * 2) + + with open(file_path, 'rb') as bin_file: + bin_data = bin_file.read() + + merged_data.extend(bin_data) + + total_files = len(file_info_list) + + mmap_table = bytearray() + for file_name, offset, file_size, width, height in file_info_list: + if len(file_name) > int(max_name_len): + print(f'\033[1;33mWarn:\033[0m "{file_name}" exceeds {max_name_len} bytes and will be truncated.') + fixed_name = file_name.ljust(int(max_name_len), '\0')[:int(max_name_len)] + mmap_table.extend(fixed_name.encode('utf-8')) + mmap_table.extend(file_size.to_bytes(4, byteorder='little')) + mmap_table.extend(offset.to_bytes(4, byteorder='little')) + mmap_table.extend(width.to_bytes(2, byteorder='little')) + mmap_table.extend(height.to_bytes(2, byteorder='little')) + + combined_data = mmap_table + merged_data + combined_checksum = compute_checksum(combined_data) + combined_data_length = len(combined_data).to_bytes(4, byteorder='little') + header_data = total_files.to_bytes(4, byteorder='little') + combined_checksum.to_bytes(4, byteorder='little') + final_data = header_data + combined_data_length + combined_data + + with open(out_file, 'wb') as output_bin: + output_bin.write(final_data) + + os.makedirs(assets_c_path, exist_ok=True) + current_year = datetime.now().year + + asset_name = os.path.basename(assets_path) + file_path = os.path.join(assets_c_path, f'mmap_generate_{asset_name}.h') + with open(file_path, 'w') as output_header: + output_header.write('/*\n') + output_header.write(' * SPDX-FileCopyrightText: 2022-{} Espressif Systems (Shanghai) CO LTD\n'.format(current_year)) + output_header.write(' *\n') + output_header.write(' * SPDX-License-Identifier: Apache-2.0\n') + output_header.write(' */\n\n') + output_header.write('/**\n') + output_header.write(' * @file\n') + output_header.write(" * @brief This file was generated by esp_mmap_assets, don't modify it\n") + output_header.write(' */\n\n') + output_header.write('#pragma once\n\n') + output_header.write("#include \"esp_mmap_assets.h\"\n\n") + output_header.write(f'#define MMAP_{asset_name.upper()}_FILES {total_files}\n') + output_header.write(f'#define MMAP_{asset_name.upper()}_CHECKSUM 0x{combined_checksum:04X}\n\n') + output_header.write(f'enum MMAP_{asset_name.upper()}_LISTS {{\n') + + for i, (file_name, _, _, _, _) in enumerate(file_info_list): + enum_name = file_name.replace('.', '_') + output_header.write(f' MMAP_{asset_name.upper()}_{enum_name.upper()} = {i}, /*!< {file_name} */\n') + + output_header.write('};\n') + + print(f'All bin files have been merged into {out_file}') + +def copy_assets(config: AssetCopyConfig): + """ + Copy assets to target_path based on the provided configuration. + """ + format_tuple = tuple(config.support_format) + assets_path = config.assets_path + target_path = config.target_path + + for filename in os.listdir(assets_path): + if any(filename.endswith(suffix) for suffix in format_tuple): + source_file = os.path.join(assets_path, filename) + target_file = os.path.join(target_path, filename) + shutil.copyfile(source_file, target_file) + + conversion_map = { + '.jpg': [ + (config.sjpg_enable, convert_image_to_simg), + (config.qoi_enable, convert_image_to_qoi), + ], + '.png': [ + (config.spng_enable, convert_image_to_simg), + (config.qoi_enable, convert_image_to_qoi), + ], + } + + file_ext = os.path.splitext(filename)[1].lower() + conversions = conversion_map.get(file_ext, []) + converted = False + + for enable_flag, convert_func in conversions: + if enable_flag: + convert_func(target_file, config.split_height) + os.remove(target_file) + converted = True + break + + if not converted and config.row_enable: + convert_image_to_raw(target_file) + os.remove(target_file) + else: + print(f'No match found for file: {filename}, format_tuple: {format_tuple}') + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Move and Pack assets.') + parser.add_argument('--config', required=True, help='Path to the configuration file') + args = parser.parse_args() + + with open(args.config, 'r') as f: + config_data = json.load(f) + + assets_path = config_data['assets_path'] + image_file = config_data['image_file'] + target_path = os.path.dirname(image_file) + main_path = config_data['main_path'] + name_length = config_data['name_length'] + split_height = config_data['split_height'] + support_format = [fmt.strip() for fmt in config_data['support_format'].split(',')] + + copy_config = AssetCopyConfig( + assets_path=assets_path, + target_path=target_path, + spng_enable=config_data['support_spng'], + sjpg_enable=config_data['support_sjpg'], + qoi_enable=config_data['support_qoi'], + sqoi_enable=config_data['support_sqoi'], + row_enable=config_data['support_raw'], + support_format=support_format, + split_height=split_height + ) + + pack_config = PackModelsConfig( + target_path=target_path, + main_path=main_path, + image_file=image_file, + assets_path=assets_path, + name_length=name_length + ) + + print('--support_format:', support_format) + + if '.jpg' in support_format or '.png' in support_format: + print('--support_spng:', copy_config.spng_enable) + print('--support_sjpg:', copy_config.sjpg_enable) + print('--support_qoi:', copy_config.qoi_enable) + print('--support_raw:', copy_config.row_enable) + + if copy_config.sqoi_enable: + print('--support_sqoi:', copy_config.sqoi_enable) + if copy_config.spng_enable or copy_config.sjpg_enable or copy_config.sqoi_enable: + print('--split_height:', copy_config.split_height) + if copy_config.row_enable: + print('--lvgl_version:', config_data['lvgl_ver']) + + if os.path.isfile(image_file): + os.remove(image_file) + + copy_assets(copy_config) + pack_assets(pack_config) + + total_size = os.path.getsize(os.path.join(target_path, image_file)) + recommended_size = math.ceil(total_size / 1024) + partition_size = math.ceil(int(config_data['assets_size'], 16) / 1024) + + if int(config_data['assets_size'], 16) <= total_size: + print(f'Given assets partition size: {partition_size}K') + print(f'Recommended assets partition size: {recommended_size}K') + print('\033[1;31mError:\033[0m assets partition size is smaller than recommended.') + sys.exit(1) diff --git a/examples/display/components/esp_mmap_assets/test_apps/CMakeLists.txt b/examples/display/components/esp_mmap_assets/test_apps/CMakeLists.txt new file mode 100644 index 000000000..cba0964e3 --- /dev/null +++ b/examples/display/components/esp_mmap_assets/test_apps/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) +set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components") +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(test_esp_map_assets) diff --git a/examples/display/components/esp_mmap_assets/test_apps/main/CMakeLists.txt b/examples/display/components/esp_mmap_assets/test_apps/main/CMakeLists.txt new file mode 100644 index 000000000..fb8c5d3ea --- /dev/null +++ b/examples/display/components/esp_mmap_assets/test_apps/main/CMakeLists.txt @@ -0,0 +1,12 @@ + +idf_component_register( + SRC_DIRS "." + INCLUDE_DIRS "." +) + +spiffs_create_partition_assets( + assets + ../spiffs_assets + FLASH_IN_PROJECT + MMAP_FILE_SUPPORT_FORMAT ".jpg,.png,.ttf" +) diff --git a/examples/display/components/esp_mmap_assets/test_apps/main/idf_component.yml b/examples/display/components/esp_mmap_assets/test_apps/main/idf_component.yml new file mode 100644 index 000000000..1b30c9b52 --- /dev/null +++ b/examples/display/components/esp_mmap_assets/test_apps/main/idf_component.yml @@ -0,0 +1,6 @@ +## IDF Component Manager Manifest File +dependencies: + idf: ">=5.0" + esp_mmap_assets: + version: "*" + override_path: "../../../esp_mmap_assets" diff --git a/examples/display/components/esp_mmap_assets/test_apps/main/mmap_generate_spiffs_assets.h b/examples/display/components/esp_mmap_assets/test_apps/main/mmap_generate_spiffs_assets.h new file mode 100644 index 000000000..af5f58f38 --- /dev/null +++ b/examples/display/components/esp_mmap_assets/test_apps/main/mmap_generate_spiffs_assets.h @@ -0,0 +1,22 @@ +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief This file was generated by esp_mmap_assets, don't modify it + */ + +#pragma once + +#include "esp_mmap_assets.h" + +#define MMAP_SPIFFS_ASSETS_FILES 2 +#define MMAP_SPIFFS_ASSETS_CHECKSUM 0xB1AD + +enum MMAP_SPIFFS_ASSETS_LISTS { + MMAP_SPIFFS_ASSETS_JPG_JPG = 0, /*!< jpg.jpg */ + MMAP_SPIFFS_ASSETS_PNG_PNG = 1, /*!< png.png */ +}; diff --git a/examples/display/components/esp_mmap_assets/test_apps/main/test_esp_mmap_assets.c b/examples/display/components/esp_mmap_assets/test_apps/main/test_esp_mmap_assets.c new file mode 100644 index 000000000..3fa892d9c --- /dev/null +++ b/examples/display/components/esp_mmap_assets/test_apps/main/test_esp_mmap_assets.c @@ -0,0 +1,147 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_err.h" +#include "esp_log.h" +#include "esp_check.h" +#include "string.h" + +#include "unity.h" +#include "unity_test_runner.h" +#include "unity_test_utils_memory.h" + +#include "mmap_generate_spiffs_assets.h" + +static const char *TAG = "assets_test"; + +static int is_png(const uint8_t *raw_data, size_t len) +{ + const uint8_t png_signature[] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a}; + if (len < sizeof(png_signature)) { + return false; + } + return memcmp(png_signature, raw_data, sizeof(png_signature)) == 0; +} + +static int is_jpg(const uint8_t *raw_data, size_t len) +{ + const uint8_t jpg_signature[] = {0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46}; + if (len < sizeof(jpg_signature)) { + return false; + } + return memcmp(jpg_signature, raw_data, sizeof(jpg_signature)) == 0; +} + +TEST_CASE("test assets mmap table", "[mmap_assets][mmap_enable]") +{ + mmap_assets_handle_t asset_handle; + + const mmap_assets_config_t config = { + .partition_label = "assets", + .max_files = MMAP_SPIFFS_ASSETS_FILES, + .checksum = MMAP_SPIFFS_ASSETS_CHECKSUM, + .flags = { + .mmap_enable = true, + .app_bin_check = false, + .full_check = true, + .metadata_check = true, + }, + }; + + TEST_ESP_OK(mmap_assets_new(&config, &asset_handle)); + + int stored_files = mmap_assets_get_stored_files(asset_handle); + ESP_LOGI(TAG, "stored_files:%d", stored_files); + + for (int i = 0; i < MMAP_SPIFFS_ASSETS_FILES; i++) { + const char *name = mmap_assets_get_name(asset_handle, i); + const uint8_t *mem = mmap_assets_get_mem(asset_handle, i); + int size = mmap_assets_get_size(asset_handle, i); + int width = mmap_assets_get_width(asset_handle, i); + int height = mmap_assets_get_height(asset_handle, i); + + ESP_LOGI(TAG, "name:[%s], mem:[%p], size:[%d bytes], w:[%d], h:[%d]", name, mem, size, width, height); + + if (strstr(name, ".png")) { + TEST_ASSERT_TRUE(is_png(mem, size)); + } else if (strstr(name, ".jpg")) { + TEST_ASSERT_TRUE(is_jpg(mem, size)); + } + } + + mmap_assets_del(asset_handle); +} + +TEST_CASE("test assets mmap table", "[mmap_assets][mmap_disable]") +{ + mmap_assets_handle_t asset_handle; + + const mmap_assets_config_t config = { + .partition_label = "assets", + .max_files = MMAP_SPIFFS_ASSETS_FILES, + .checksum = MMAP_SPIFFS_ASSETS_CHECKSUM, + .flags = { + .mmap_enable = false, + .app_bin_check = false, + .full_check = true, + .metadata_check = true, + }, + }; + + TEST_ESP_OK(mmap_assets_new(&config, &asset_handle)); + + int stored_files = mmap_assets_get_stored_files(asset_handle); + ESP_LOGI(TAG, "stored_files:%d", stored_files); + + for (int i = 0; i < MMAP_SPIFFS_ASSETS_FILES; i++) { + const char *name = mmap_assets_get_name(asset_handle, i); + const uint8_t *mem = mmap_assets_get_mem(asset_handle, i); + int size = mmap_assets_get_size(asset_handle, i); + int width = mmap_assets_get_width(asset_handle, i); + int height = mmap_assets_get_height(asset_handle, i); + + ESP_LOGI(TAG, "name:[%s], mem:[%p], size:[%d bytes], w:[%d], h:[%d]", name, mem, size, width, height); + + uint8_t load_data[20]; + mmap_assets_copy_mem(asset_handle, (size_t)mem, load_data, sizeof(load_data)); + + if (strstr(name, ".png")) { + TEST_ASSERT_TRUE(is_png((const uint8_t *)load_data, size)); + } else if (strstr(name, ".jpg")) { + TEST_ASSERT_TRUE(is_jpg((const uint8_t *)load_data, size)); + } + } + + mmap_assets_del(asset_handle); +} + +// Some resources are lazy allocated in the LCD driver, the threadhold is left for that case +#define TEST_MEMORY_LEAK_THRESHOLD (500) + +static size_t before_free_8bit; +static size_t before_free_32bit; + +void setUp(void) +{ + before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); +} + +void tearDown(void) +{ + size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); + unity_utils_check_leak(before_free_8bit, after_free_8bit, "8BIT", TEST_MEMORY_LEAK_THRESHOLD); + unity_utils_check_leak(before_free_32bit, after_free_32bit, "32BIT", TEST_MEMORY_LEAK_THRESHOLD); +} + +void app_main(void) +{ + printf("mmap assets TEST \n"); + unity_run_menu(); +} diff --git a/examples/display/components/esp_mmap_assets/test_apps/partitions.csv b/examples/display/components/esp_mmap_assets/test_apps/partitions.csv new file mode 100644 index 000000000..f065bd27d --- /dev/null +++ b/examples/display/components/esp_mmap_assets/test_apps/partitions.csv @@ -0,0 +1,6 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild +nvs, data, nvs, , 0x6000, +phy_init, data, phy, , 0x1000, +factory, app, factory, , 500K, +assets, data, spiffs, , 1M, diff --git a/examples/display/components/esp_mmap_assets/test_apps/pytest_esp_mmap_assets.py b/examples/display/components/esp_mmap_assets/test_apps/pytest_esp_mmap_assets.py new file mode 100644 index 000000000..7404eca7f --- /dev/null +++ b/examples/display/components/esp_mmap_assets/test_apps/pytest_esp_mmap_assets.py @@ -0,0 +1,18 @@ +# SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 +import pytest +from pytest_embedded import Dut + +@pytest.mark.target('esp32') +@pytest.mark.target('esp32c3') +@pytest.mark.target('esp32s3') +@pytest.mark.target('esp32p4') +@pytest.mark.env('generic') +@pytest.mark.parametrize( + 'config', + [ + 'defaults', + ], +) +def test_esp_mmap_assets(dut: Dut)-> None: + dut.run_all_single_board_cases() diff --git a/examples/display/components/esp_mmap_assets/test_apps/sdkconfig.defaults b/examples/display/components/esp_mmap_assets/test_apps/sdkconfig.defaults new file mode 100644 index 000000000..b3940ca33 --- /dev/null +++ b/examples/display/components/esp_mmap_assets/test_apps/sdkconfig.defaults @@ -0,0 +1,5 @@ +# For IDF 5.0 +CONFIG_ESP_TASK_WDT_EN=n + +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +CONFIG_PARTITION_TABLE_CUSTOM=y diff --git a/examples/display/components/esp_mmap_assets/test_apps/spiffs_assets/jpg.jpg b/examples/display/components/esp_mmap_assets/test_apps/spiffs_assets/jpg.jpg new file mode 100644 index 000000000..c1132a308 Binary files /dev/null and b/examples/display/components/esp_mmap_assets/test_apps/spiffs_assets/jpg.jpg differ diff --git a/examples/display/components/esp_mmap_assets/test_apps/spiffs_assets/png.png b/examples/display/components/esp_mmap_assets/test_apps/spiffs_assets/png.png new file mode 100644 index 000000000..889cec9b6 Binary files /dev/null and b/examples/display/components/esp_mmap_assets/test_apps/spiffs_assets/png.png differ diff --git a/examples/display/main/CMakeLists.txt b/examples/display/main/CMakeLists.txt index 838a18b1d..e20fe9db9 100644 --- a/examples/display/main/CMakeLists.txt +++ b/examples/display/main/CMakeLists.txt @@ -4,3 +4,12 @@ idf_component_register(SRCS "display_main.c" "lvgl_demo_ui.c" lvgl_port_create_c_image("images/esp_logo.png" "images/gen/" "ARGB8888" "NONE") lvgl_port_create_c_image("images/esp_text.png" "images/gen/" "ARGB8888" "NONE") lvgl_port_add_images(${COMPONENT_LIB} "images/gen/") + +spiffs_create_partition_assets( + storage + "./images" + FLASH_IN_PROJECT + MMAP_FILE_SUPPORT_FORMAT ".png" + MMAP_SUPPORT_RAW + MMAP_RAW_COLOR_FORMAT "ARGB8888" +) diff --git a/examples/display/main/display_main.c b/examples/display/main/display_main.c index a7115d6b0..a1b8ac63e 100644 --- a/examples/display/main/display_main.c +++ b/examples/display/main/display_main.c @@ -9,11 +9,50 @@ #include "lvgl.h" #include "esp_log.h" +#include "esp_lv_fs.h" +#include "mmap_generate_images.h" + extern void example_lvgl_demo_ui(lv_obj_t *scr); +static const char *TAG = "display"; + +static mmap_assets_handle_t mmap_drive_handle; +static esp_lv_fs_handle_t fs_drive_handle; + +esp_err_t mount_mmap_filesystem(void) +{ + esp_err_t ret; + + const mmap_assets_config_t asset_cfg = { + .partition_label = "storage", + .max_files = MMAP_IMAGES_FILES, + .checksum = MMAP_IMAGES_CHECKSUM, + .flags = {.mmap_enable = true} + }; + ret = mmap_assets_new(&asset_cfg, &mmap_drive_handle); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize %s", "storage"); + return ret; + } + + fs_cfg_t fs_cfg; + + fs_cfg.fs_letter = 'A'; + fs_cfg.fs_assets = mmap_drive_handle; + fs_cfg.fs_nums = MMAP_IMAGES_FILES; + + ret = esp_lv_fs_desc_init(&fs_cfg, &fs_drive_handle); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize FS for %s", "storage"); + return ret; + } + + return ESP_OK; +} void app_main(void) { bsp_display_start(); + mount_mmap_filesystem(); ESP_LOGI("example", "Display LVGL animation"); bsp_display_lock(0); diff --git a/examples/display/main/idf_component.yml b/examples/display/main/idf_component.yml index 772f667d7..a30d15a17 100644 --- a/examples/display/main/idf_component.yml +++ b/examples/display/main/idf_component.yml @@ -1,6 +1,6 @@ description: BSP Display example dependencies: - esp_wrover_kit: + esp-box: version: "*" - override_path: "../../../bsp/esp_wrover_kit" + override_path: "../../../bsp/esp-box" diff --git a/examples/display/main/lvgl_demo_ui.c b/examples/display/main/lvgl_demo_ui.c index 9eb6da102..837462955 100644 --- a/examples/display/main/lvgl_demo_ui.c +++ b/examples/display/main/lvgl_demo_ui.c @@ -54,7 +54,8 @@ static void anim_timer_cb(lv_timer_t *timer) // Create new image and make it transparent img_text = lv_img_create(scr); - lv_img_set_src(img_text, &esp_text); + lv_img_set_src(img_text, "A:esp_text.bin"); + lv_obj_set_style_img_opa(img_text, 0, 0); } @@ -78,7 +79,7 @@ void example_lvgl_demo_ui(lv_obj_t *scr) { // Create image img_logo = lv_img_create(scr); - lv_img_set_src(img_logo, &esp_logo); + lv_img_set_src(img_logo, "A:esp_logo.bin"); lv_obj_center(img_logo); // Create arcs diff --git a/examples/display/main/mmap_generate_images.h b/examples/display/main/mmap_generate_images.h new file mode 100644 index 000000000..c3be86aaa --- /dev/null +++ b/examples/display/main/mmap_generate_images.h @@ -0,0 +1,22 @@ +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief This file was generated by esp_mmap_assets, don't modify it + */ + +#pragma once + +#include "esp_mmap_assets.h" + +#define MMAP_IMAGES_FILES 2 +#define MMAP_IMAGES_CHECKSUM 0xC382 + +enum MMAP_IMAGES_LISTS { + MMAP_IMAGES_ESP_LOGO_BIN = 0, /*!< esp_logo.bin */ + MMAP_IMAGES_ESP_TEXT_BIN = 1, /*!< esp_text.bin */ +}; diff --git a/examples/display/partitions.csv b/examples/display/partitions.csv new file mode 100644 index 000000000..b895d62ea --- /dev/null +++ b/examples/display/partitions.csv @@ -0,0 +1,6 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild +nvs, data, nvs, 0x9000, 0x6000, +phy_init, data, phy, 0xf000, 0x1000, +factory, app, factory, 0x10000, 1M, +storage, data, spiffs, 0x110000,0x2f0000,