// Image gallery: raw W x H RGB332 images in fixed slots of the "gallery" // partition. Each slot is a 4 KB header sector followed by the pixels. #include #include "esp_log.h" #include "esp_partition.h" #include "app.h" #include "display.h" #define SLOT_SIZE 0x80000 #define HEADER_SIZE 0x1000 #define MAGIC 0x56474131 // "VGA1" _Static_assert(HEADER_SIZE + W * H <= SLOT_SIZE, "image doesn't fit in a gallery slot"); typedef struct { uint32_t magic; uint16_t w, h; char name[GALLERY_NAME_MAX + 1]; } header_t; static const char *TAG = "gallery"; static const esp_partition_t *part; void gallery_init(void) { part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 0x40, "gallery"); if (part) { ESP_LOGI(TAG, "%d slots of %dx%d", gallery_slots(), W, H); } else { ESP_LOGE(TAG, "no gallery partition"); } } int gallery_slots(void) { return part ? part->size / SLOT_SIZE : 0; } static bool read_header(int slot, header_t *hdr) { if (slot < 0 || slot >= gallery_slots()) { return false; } if (esp_partition_read(part, slot * SLOT_SIZE, hdr, sizeof(*hdr)) != ESP_OK) { return false; } // An image saved in the other video mode won't fit, so treat it as empty return hdr->magic == MAGIC && hdr->w == W && hdr->h == H; } bool gallery_name(int slot, char *name) { header_t hdr; if (!read_header(slot, &hdr)) { return false; } hdr.name[GALLERY_NAME_MAX] = 0; strcpy(name, hdr.name); return true; } bool gallery_read(int slot, uint8_t *dst) { header_t hdr; if (!read_header(slot, &hdr)) { return false; } return esp_partition_read(part, slot * SLOT_SIZE + HEADER_SIZE, dst, W * H) == ESP_OK; } bool gallery_write(int slot, const char *name, const uint8_t *data) { if (slot < 0 || slot >= gallery_slots()) { return false; } size_t base = slot * SLOT_SIZE; size_t used = (HEADER_SIZE + W * H + 0xfff) & ~0xfff; if (esp_partition_erase_range(part, base, used) != ESP_OK) { return false; } // Pixels first, header last, so a half-written slot reads as empty if (esp_partition_write(part, base + HEADER_SIZE, data, W * H) != ESP_OK) { return false; } header_t hdr = {.magic = MAGIC, .w = W, .h = H}; strlcpy(hdr.name, name, sizeof(hdr.name)); return esp_partition_write(part, base, &hdr, sizeof(hdr)) == ESP_OK; } bool gallery_delete(int slot) { if (slot < 0 || slot >= gallery_slots()) { return false; } return esp_partition_erase_range(part, slot * SLOT_SIZE, HEADER_SIZE) == ESP_OK; }