Advertisement
honey_the_codewitch

ESP LCD Panel API example

Apr 10th, 2025 (edited)
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. // change below to your settings
  2. // screen dimensions
  3. #define LCD_WIDTH 320
  4. #define LCD_HEIGHT 240
  5. // screen connections
  6. #define LCD_CLK 18
  7. #define LCD_MOSI 23
  8. #define LCD_DC 2
  9. #define LCD_CS 15
  10. #define LCD_RST 4
  11. #define LCD_BL 32 // optional
  12.  
  13. #include "driver/gpio.h"
  14. #include "driver/spi_master.h"
  15. #include "esp_lcd_panel_io.h"
  16. #include "esp_lcd_panel_ops.h"
  17. #include "esp_lcd_panel_vendor.h"
  18. // include your custom panel driver here
  19. ...
  20. // the size of our transfer buffer(s)
  21. static const constexpr size_t lcd_transfer_buffer_size = LCD_WIDTH*LCD_HEIGHT*2/10;
  22. static uint8_t* lcd_transfer_buffer1 = nullptr;
  23. // the second buffer
  24. static uint8_t* lcd_transfer_buffer2 = nullptr;
  25. ...
  26. // initialize the screen using the esp panel API
  27. static void lcd_init() {
  28. #ifdef LCD_BL
  29. #if LCD_BL > -1
  30.     gpio_set_direction((gpio_num_t)LCD_BL,GPIO_MODE_OUTPUT);
  31.     gpio_set_level((gpio_num_t)LCD_BL,0);
  32. #endif
  33. #endif
  34.     lcd_transfer_buffer1 = (uint8_t*)heap_caps_malloc(lcd_transfer_buffer_size,MALLOC_CAP_DMA);
  35. #ifdef TWO_BUFFERS
  36.     lcd_transfer_buffer2 = (uint8_t*)heap_caps_malloc(lcd_transfer_buffer_size,MALLOC_CAP_DMA);
  37. #endif
  38.     if(lcd_transfer_buffer1==nullptr
  39. #ifdef TWO_BUFFERS
  40.         ||lcd_transfer_buffer2==nullptr
  41. #endif
  42.     ) {
  43.         puts("Out of memory allocating transfer buffer");
  44.         while(1) vTaskDelay(5);
  45.     }
  46.     spi_bus_config_t buscfg;
  47.     memset(&buscfg, 0, sizeof(buscfg));
  48.     buscfg.sclk_io_num = LCD_CLK;
  49.     buscfg.mosi_io_num = LCD_MOSI;
  50.     buscfg.miso_io_num = -1;
  51.     buscfg.quadwp_io_num = -1;
  52.     buscfg.quadhd_io_num = -1;
  53.     buscfg.max_transfer_sz = lcd_transfer_buffer_size + 8;
  54.  
  55.     // Initialize the SPI bus on VSPI (SPI3)
  56.     spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO);
  57.  
  58.     esp_lcd_panel_io_handle_t io_handle = NULL;
  59.     esp_lcd_panel_io_spi_config_t io_config;
  60.     memset(&io_config, 0, sizeof(io_config));
  61.     io_config.dc_gpio_num = LCD_DC,
  62.     io_config.cs_gpio_num = LCD_CS,
  63.     io_config.pclk_hz = 40*1000*1000,
  64.     io_config.lcd_cmd_bits = 8,
  65.     io_config.lcd_param_bits = 8,
  66.     io_config.spi_mode = 0,
  67.     io_config.trans_queue_depth = 10,
  68.     io_config.on_color_trans_done = lcd_flush_ready;
  69.     // Attach the LCD to the SPI bus
  70.     esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)SPI3_HOST, &io_config, &io_handle);
  71.     esp_lcd_panel_dev_config_t lcd_config;
  72.     memset(&lcd_config, 0, sizeof(lcd_config));
  73.     lcd_config.reset_gpio_num = LCD_RST;
  74. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  75.     lcd_config.rgb_endian = LCD_RGB_ENDIAN_RGB;
  76. #else
  77.     lcd_config.color_space = ESP_LCD_COLOR_SPACE_RGB;
  78. #endif
  79.     lcd_config.bits_per_pixel = 16;
  80.  
  81.     // Initialize the LCD configuration
  82.     esp_lcd_panel_handle_t lcd_handle = nullptr;
  83.  
  84.     //
  85.     // Change this from st7789 to your panel's display controller
  86.     //
  87.     esp_lcd_new_panel_st7789(io_handle, &lcd_config, &lcd_handle);
  88.  
  89.     // Reset the display
  90.     esp_lcd_panel_reset(lcd_handle);
  91.  
  92.     // Initialize LCD panel
  93.     esp_lcd_panel_init(lcd_handle);
  94.     //
  95.     //  Configure the panel (Different LCD screens may need different options)
  96.     //
  97.     esp_lcd_panel_swap_xy(lcd_handle, false);
  98.     esp_lcd_panel_set_gap(lcd_handle, 0, 35);
  99.     esp_lcd_panel_swap_xy(lcd_handle,true);
  100.     esp_lcd_panel_mirror(lcd_handle, false, true);
  101.     esp_lcd_panel_invert_color(lcd_handle, true);
  102.     // Turn on the screen
  103. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  104.     esp_lcd_panel_disp_on_off(lcd_handle, true);
  105. #else
  106.     esp_lcd_panel_disp_off(lcd_handle, false);
  107. #endif
  108. #ifdef LCD_BL
  109. #if LCD_BL > -1
  110.     gpio_set_level((gpio_num_t)LCD_BL,1);
  111. #endif
  112. #endif
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement