Skip to content

Instantly share code, notes, and snippets.

@mzyy94
Created January 24, 2022 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzyy94/c3a82e9a613088b2be8da5138939bbc5 to your computer and use it in GitHub Desktop.
Save mzyy94/c3a82e9a613088b2be8da5138939bbc5 to your computer and use it in GitHub Desktop.
Write display data
#include <stdio.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_vfs_fat.h"
#include "driver/sdspi_host.h"
#include "driver/spi_common.h"
#include "sdmmc_cmd.h"
#include "M5Unified.h"
static const char *TAG = "main";
#define MOUNT_POINT "/sdcard"
#define PIN_NUM_CS 4
void write_data(void)
{
esp_err_t ret;
const char mount_point[] = MOUNT_POINT;
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = false,
.max_files = 5,
.allocation_unit_size = 16 * 1024,
};
sdmmc_card_t *card;
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.slot = VSPI_HOST;
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_cs = static_cast<gpio_num_t>(PIN_NUM_CS);
slot_config.host_id = static_cast<spi_host_device_t>(host.slot);
ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to initialize the card (%s). ", esp_err_to_name(ret));
return;
}
ESP_LOGI(TAG, "Filesystem mounted");
sdmmc_card_print_info(stdout, card);
FILE *f = fopen(MOUNT_POINT "/tmp1.bin", "wb");
if (f == NULL)
{
ESP_LOGE(TAG, "Failed to open file for writing");
return;
}
uint16_t *buf = reinterpret_cast<uint16_t *>(malloc(sizeof(uint16_t) * M5.Display.width()));
for (int16_t y = M5.Display.height() - 1; y >= 0; y--)
{
#if 1
M5.Display.readRect(0, y, M5.Display.width(), 1, buf);
#endif
fwrite(buf, sizeof(uint16_t), M5.Display.width(), f);
}
fclose(f);
free(buf);
ESP_LOGI(TAG, "Binary data written");
esp_vfs_fat_sdcard_unmount(mount_point, card);
ESP_LOGI(TAG, "Card unmounted");
}
extern "C"
{
void app_main()
{
M5.begin();
M5.Display.clear();
write_data();
while (true)
{
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment