This code work fine with normal font. But when I use Freetype to load and render font, it crashes when lv_timer_handler() in loop() is called. The cause is from LittleFS read from the font file. Somehow the font is loaded aleady.
Here’s the
#include <LovyanGFX.hpp>
#include "finalFrontier.h"
#include "ft2build.h"
#include <LittleFS.h>
#include "lfs.h"
#include <Ticker.h>
#include "esp_log.h"
#include <stdio.h>
#include "esp_err.h"
#include FT_FREETYPE_H
#define LV_USE_FREETYPE 1
#define LV_USE_LOG 1
#define LV_LOG_LEVEL LV_LOG_LEVEL_WARN
#include <lvgl.h>
#include <ft2build.h>
#include "esp_log.h"
#include "esp_littlefs.h"
#include FT_FREETYPE_H
static LGFX lcd;
static lv_draw_buf_t *draw_buf; // Changed from lv_disp_draw_buf_t
static lv_color_t *lv_disp_buf;
static lv_disp_t *disp;
static lv_color_t *buf1;
static lv_color_t *buf2;
LGFX gfx;
// ========== LVGL Display Callbacks (LVGL 9.3 style) ==========
void my_flush_cb(lv_display_t *disp, const lv_area_t *area, uint8_t *px_map) {
gfx.startWrite();
gfx.setAddrWindow(area->x1, area->y1, area->x2 - area->x1 + 1, area->y2 - area->y1 + 1);
gfx.pushImageDMA(area->x1, area->y1, area->x2 - area->x1 + 1, area->y2 - area->y1 + 1, (uint16_t *)px_map);
gfx.endWrite();
lv_display_flush_ready(disp);
}
Ticker lvgl_tick;
static uint32_t my_tick(void)
{
return millis();
}
// ========== Setup ==========
void setup() {
Serial.begin(115200);
esp_vfs_littlefs_conf_t conf = {
.base_path = "/littlefs",
.partition_label = "spiffs",
.format_if_mount_failed = true,
.dont_mount = false
};
esp_err_t ret = esp_vfs_littlefs_register(&conf);
if (ret != ESP_OK) {
ESP_LOGE("TAG", "Failed to mount LittleFS");
return;
}
gfx.init();
lv_init();
lv_freetype_init(256);
lv_font_t * font = lv_freetype_font_create("/littlefs/Final-Frontier.ttf",
LV_FREETYPE_FONT_RENDER_MODE_BITMAP,
12,
LV_FREETYPE_FONT_STYLE_NORMAL);
if(!font) {
LV_LOG_ERROR("freetype font create failed.");
return;
}
static lv_color_t *buf1 = (lv_color_t *)heap_caps_malloc(240 * 10 * sizeof(lv_color_t), MALLOC_CAP_DMA);
lv_display_t *disp = lv_display_create(240, 320);
lv_display_set_flush_cb(disp, my_flush_cb);
lv_display_set_buffers(disp, buf1, NULL, 240 * 10, LV_DISPLAY_RENDER_MODE_PARTIAL);
lv_tick_set_cb(my_tick);
static lv_style_t style;
lv_style_init(&style);
lv_style_set_text_font(&style, font);
lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);
lv_obj_t *label = lv_label_create(lv_screen_active());
lv_label_set_text(label, "Hello");
lv_obj_center(label);
}
void loop() {
lv_timer_handler(); // Process GUI events
delay(100);
}
My lv_conf.h
`#ifndef LV_CONF_H
#define LV_CONF_H
/*====================
LVGL CONFIGURATION
====================/
/* Enable selected parts of LVGL */
#define LV_USE_DRAW_BUF 1
#define LV_USE_LABEL 1
#define LV_USE_FREETYPE 1
//#define LV_USE_FS_LITTLEFS 1
//#define LV_FS_LITTLEFS_LETTER ‘C’
#define LV_USE_FS_ARDUINO_ESP_LITTLEFS 1
#define LV_FS_ARDUINO_ESP_LITTLEFS_LETTER ‘C’
/* Enable logging (optional for debugging) */
#define LV_USE_LOG 1
#define LV_LOG_LEVEL LV_LOG_LEVEL_WARN
/* FreeType font settings /
#define LV_FREETYPE_CACHE_SIZE (32 * 1024)
//#define LV_FREETYPE_CACHE_SIZE 256 / Set to 0 to disable cache /
#define LV_USE_OS LV_OS_NONE / For ESP32, we disable OS support */
#define LV_FONT_MONTSERRAT_16 1 /* You can disable if using only FreeType */
/* Memory settings (tweak as needed) /
#define LV_MEM_SIZE (48 * 1024) / 48KB of memory for LVGL /
#define LV_TXT_ENC LV_TXT_ENC_UTF8
/ Tick period (ms) - depends on your system timer */
#define LV_TICK_PERIOD_MS 1
/* Default DPI for fonts (used by FreeType if applicable) */
#define LV_DPI_DEF 130
#endif /LV_CONF_H/``
type or paste code here