Files
spincoat-plater-firmware/main/display.c
2025-12-26 11:04:16 -05:00

258 lines
7.5 KiB
C

#include "display.h"
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "unity.h"
#include "unity_test_runner.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "hal/spi_types.h"
#include "esp_lcd_panel_ops.h"
#include "driver/spi_common.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_commands.h"
#include "esp_lcd_ili9341.h"
#include "esp_lcd_touch_xpt2046.h"
#include "lvgl.h"
#include "esp_lvgl_port.h"
SemaphoreHandle_t refresh_finish = NULL;
esp_lcd_panel_io_handle_t io_handle = NULL;
esp_lcd_panel_handle_t panel_handle = NULL;
esp_lcd_touch_handle_t tp = NULL;
static const char * TAG = "spincoat-plater-firmware/display";
/**
* Callback for the TFT LCD, notifying when the screen is ready for another chunk of data and
* releasing the drawing semaphore.
*/
IRAM_ATTR static bool notify_refresh_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
{
BaseType_t need_yield = pdFALSE;
xSemaphoreGiveFromISR(refresh_finish, &need_yield);
return (need_yield == pdTRUE);
}
/**
* Callback for LVGL pointer input device, it reads from the esp_lcd_touch driver and updates
* the LVGL internal state for event handling.
*/
void touch_driver_read(lv_indev_t * indev, lv_indev_data_t * data) {
ESP_ERROR_CHECK(esp_lcd_touch_read_data(tp));
uint16_t x[1];
uint16_t y[1];
uint16_t strength[1];
uint8_t count = 0;
bool touchpad_pressed = esp_lcd_touch_get_coordinates(tp, x, y, strength, &count, 1);
if(touchpad_pressed == true) {
//ESP_LOGI(TAG, "Touchpad pressed from LVGL..\n");
data->point.x = x[0];
data->point.y = y[0];
data->state = LV_INDEV_STATE_PRESSED;
} else {
data->state = LV_INDEV_STATE_RELEASED;
}
}
/**
* Callback for LVGL button events.
*/
static void btn_event_cb(lv_event_t * e) {
lv_event_code_t code = lv_event_get_code(e);
//ESP_LOGI(TAG, "Button callback fired.");
if(code == LV_EVENT_CLICKED) {
ESP_LOGI(TAG, "LVGL button pressed.");
}
}
/**
* Draws a test UI
*/
void lv_example_btn_1(void)
{
lv_obj_t * label;
lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
lv_obj_add_event_cb(btn1, btn_event_cb, LV_EVENT_ALL, NULL);
lv_obj_set_width(btn1, 120);
lv_obj_set_height(btn1, 100);
lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -40);
label = lv_label_create(btn1);
lv_label_set_text(label, "Button");
lv_obj_center(label);
lv_obj_t * btn2 = lv_btn_create(lv_scr_act());
lv_obj_add_event_cb(btn2, btn_event_cb, LV_EVENT_ALL, NULL);
lv_obj_align(btn2, LV_ALIGN_CENTER, 0, 40);
lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE);
lv_obj_set_height(btn2, LV_SIZE_CONTENT);
label = lv_label_create(btn2);
lv_label_set_text(label, "Toggle");
lv_obj_center(label);
}
/**
* Initializes the SPI bus and starts communication with xpt2046 touch controller.
*/
void init_touchscreen_xpt2046(void) {
ESP_LOGI(TAG, "Initialize touchscreen SPI bus");
const spi_bus_config_t ts_bus_config = {
.mosi_io_num = CONFIG_TOUCH_MOSI,
.miso_io_num = CONFIG_TOUCH_MISO,
.sclk_io_num = CONFIG_TOUCH_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4096,
};
ESP_ERROR_CHECK(spi_bus_initialize(TOUCH_SPI_HOST, &ts_bus_config, SPI_DMA_CH_AUTO));
esp_lcd_panel_io_handle_t tp_io_handle = NULL;
esp_lcd_panel_io_spi_config_t tp_io_config = ESP_LCD_TOUCH_IO_SPI_XPT2046_CONFIG(CONFIG_TOUCH_CS);
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(TOUCH_SPI_HOST, &tp_io_config, &tp_io_handle));
esp_lcd_touch_config_t tp_cfg = {
.x_max = TFT_HRES,
.y_max = TFT_VRES,
.rst_gpio_num = -1,
.int_gpio_num = -1,
.flags = {
.swap_xy = 1,
.mirror_x = 0,
.mirror_y = 0,
},
};
ESP_LOGI(TAG, "Initialize touch controller XPT2046");
ESP_ERROR_CHECK(esp_lcd_touch_new_spi_xpt2046(tp_io_handle, &tp_cfg, &tp));
ESP_LOGI(TAG, "Finished initialization of XPT2046 touch driver");
}
/**
* Initializes the SPI LCD in preparation for writing graphics to it.
*/
void init_spi_lcd(void) {
ESP_LOGI(TAG, "Turn on backlight");
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << CONFIG_TFT_BL),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
gpio_config(&io_conf);
gpio_set_level(CONFIG_TFT_BL, 1);
ESP_LOGI(TAG, "Initialize SPI bus");
const spi_bus_config_t bus_config = ILI9341_PANEL_BUS_SPI_CONFIG(CONFIG_TFT_SCKL,
CONFIG_TFT_MOSI, TFT_HRES * 80 * TFT_BPP / 8);
TEST_ESP_OK(spi_bus_initialize(LCD_SPI_HOST, &bus_config, SPI_DMA_CH_AUTO));
ESP_LOGI(TAG, "Install panel IO");
const esp_lcd_panel_io_spi_config_t io_config = ILI9341_PANEL_IO_SPI_CONFIG(CONFIG_TFT_CS, CONFIG_TFT_DC,
notify_refresh_ready, NULL);
TEST_ESP_OK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_SPI_HOST, &io_config, &io_handle));
ESP_LOGI(TAG, "Install ili9341 panel driver");
const esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = -1, // Shared with Touch reset
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
.color_space = ESP_LCD_COLOR_SPACE_BGR,
#elif ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(6, 0, 0)
.rgb_endian = LCD_RGB_ENDIAN_BGR,
#else
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR,
#endif
.bits_per_pixel = TFT_BPP,
};
TEST_ESP_OK(esp_lcd_new_panel_ili9341(io_handle, &panel_config, &panel_handle));
TEST_ESP_OK(esp_lcd_panel_reset(panel_handle));
TEST_ESP_OK(esp_lcd_panel_init(panel_handle));
TEST_ESP_OK(esp_lcd_panel_mirror(panel_handle, true, true));
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
TEST_ESP_OK(esp_lcd_panel_disp_off(panel_handle, false));
#else
TEST_ESP_OK(esp_lcd_panel_disp_on_off(panel_handle, true));
#endif
ESP_LOGI(TAG, "Finished init of spi LCD");
}
/**
* Initializes the LVGL display and sets everything up so that LVGL can draw to the TFT LCD.
*/
void init_lvgl_display(void) {
static lv_disp_t * disp_handle = NULL;
const lvgl_port_cfg_t lvgl_cfg = ESP_LVGL_PORT_INIT_CONFIG();
TEST_ESP_OK(lvgl_port_init(&lvgl_cfg));
const lvgl_port_display_cfg_t disp_cfg = {
.io_handle = io_handle,
.panel_handle = panel_handle,
.buffer_size = LVGL_BUF_SIZE,
.double_buffer = true,
.hres = TFT_HRES,
.vres = TFT_VRES,
.monochrome = false,
.color_format = LV_COLOR_FORMAT_RGB565,
.rotation = {
.swap_xy = true,
.mirror_x = false,
.mirror_y = false,
},
.flags = {
.buff_dma = false,
.swap_bytes = false,
}
};
disp_handle = lvgl_port_add_disp(&disp_cfg);
}
/**
* Initializes the LVGL input driver as a pointer type so that LVGL can read input and dispatch
* UI events.
*/
void init_input(void) {
lv_indev_t * indev = lv_indev_create();
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(indev, touch_driver_read);
}
/**
* Main function that completely initializes the TFT LCD, the touchscreen, the LVGL display,
* input device and draws the UI.
*/
void init_display(void) {
init_spi_lcd();
init_touchscreen_xpt2046();
init_lvgl_display();
init_input();
lv_example_btn_1();
}