ESP MMAP ASSETS

[English]

该模块主要用于打包资源(如图像、字体等),并将其直接映射以供用户访问。

功能

添加导入文件类型
  • 支持多种文件格式,如 .bin.jpg.ttf 等。

启用分片 JPG
  • 需要使用 SJPG 来解析。参见 LVGL SJPG

启用分片 PNG
  • 需要使用 esp_lv_spng 来解析。参见组件 esp_lv_spng

设置分片高度
  • 设置分片高度,依赖于 MMAP_SUPPORT_SJPGMMAP_SUPPORT_SPNG

CMake

用户可以选择将图像与应用程序二进制文件、分区表等一起自动刷写到设备上,通过在 idf.py flash 时指定 FLASH_IN_PROJECT。例如:

/* partitions.csv
 * --------------------------------------------------------
 * | Name               | Type | SubType | Offset | Size  | Flags     |
 * --------------------------------------------------------
 * | my_spiffs_partition | data | spiffs  |        | 6000K |           |
 * --------------------------------------------------------
 */
 spiffs_create_partition_assets(my_spiffs_partition my_folder FLASH_IN_PROJECT)

应用示例

生成头文件 (assets_generate.h)

该头文件自动生成,包含内存映射资源的基本定义。

#include "esp_mmap_assets.h"

#define TOTAL_MMAP_FILES      2
#define MMAP_CHECKSUM         0xB043

enum MMAP_FILES {
   MMAP_JPG_JPG = 0,   /*!< jpg.jpg */
   MMAP_PNG_PNG = 1,   /*!< png.png */
};

创建资源句柄

资源初始化配置确保与 assets_generate.h 一致。它设置了 max_fileschecksum,用来验证头文件和内存映射的二进制文件是否匹配。

mmap_assets_handle_t asset_handle;

const mmap_assets_config_t config = {
   .partition_label = "my_spiffs_partition",
   .max_files = TOTAL_MMAP_FILES,
   .checksum = MMAP_CHECKSUM,
};

ESP_ERROR_CHECK(mmap_assets_new(&config, &asset_handle));

资源使用

可以使用 assets_generate.h 中定义的枚举来获取资源信息。

const char *name = mmap_assets_get_name(asset_handle, MMAP_JPG_JPG);
const void *mem = mmap_assets_get_mem(asset_handle, MMAP_JPG_JPG);
int size = mmap_assets_get_size(asset_handle, MMAP_JPG_JPG);
int width = mmap_assets_get_width(asset_handle, MMAP_JPG_JPG);
int height = mmap_assets_get_height(asset_handle, MMAP_JPG_JPG);

ESP_LOGI(TAG, "Name:[%s], Mem:[%p], Size:[%d bytes], Width:[%d px], Height:[%d px]", name, mem, size, width, height);

API 参考

Header File

Functions

esp_err_t mmap_assets_new(const mmap_assets_config_t *config, mmap_assets_handle_t *ret_item)

Create a new asset instance.

参数
  • config[in] Pointer to the asset configuration structure.

  • ret_item[out] Pointer to the handle of the newly created asset instance.

返回

  • 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_del(mmap_assets_handle_t handle)

Delete an asset instance.

参数

handle[in] Asset instance handle.

返回

  • ESP_OK: Success

  • ESP_ERR_INVALID_ARG: Invalid argument

const uint8_t *mmap_assets_get_mem(mmap_assets_handle_t handle, int index)

Get the memory of the asset at the specified index.

参数
  • handle[in] Asset instance handle.

  • index[in] Index of the asset.

返回

Pointer to the asset memory, or NULL if index is invalid.

const char *mmap_assets_get_name(mmap_assets_handle_t handle, int index)

Get the name of the asset at the specified index.

参数
  • handle[in] Asset instance handle.

  • index[in] Index of the asset.

返回

Pointer to the asset name, or NULL if index is invalid.

int mmap_assets_get_size(mmap_assets_handle_t handle, int index)

Get the size of the asset at the specified index.

参数
  • handle[in] Asset instance handle.

  • index[in] Index of the asset.

返回

Size of the asset, or -1 if index is invalid.

int mmap_assets_get_width(mmap_assets_handle_t handle, int index)

Get the width of the asset at the specified index.

参数
  • handle[in] Asset instance handle.

  • index[in] Index of the asset.

返回

Width of the asset, or -1 if index is invalid.

int mmap_assets_get_height(mmap_assets_handle_t handle, int index)

Get the height of the asset at the specified index.

参数
  • handle[in] Asset instance handle.

  • index[in] Index of the asset.

返回

Height of the asset, or -1 if index is invalid.

Structures

struct mmap_assets_config_t

Asset configuration structure, contains the asset table and other configuration information.

Public Members

const char *partition_label

Configuration partition_label

int max_files

Number of assets

uint32_t checksum

Checksum of table

Type Definitions

typedef struct mmap_assets_t *mmap_assets_handle_t

Asset handle type, points to the asset.

Type of asset handle