Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // change below to your settings
- // screen dimensions
- #define LCD_WIDTH 320
- #define LCD_HEIGHT 240
- // screen connections
- #define LCD_CLK 18
- #define LCD_MOSI 23
- #define LCD_DC 2
- #define LCD_CS 15
- #define LCD_RST 4
- #define LCD_BL 32 // optional
- #include "driver/gpio.h"
- #include "driver/spi_master.h"
- #include "esp_lcd_panel_io.h"
- #include "esp_lcd_panel_ops.h"
- #include "esp_lcd_panel_vendor.h"
- // include your custom panel driver here
- ...
- // the size of our transfer buffer(s)
- static const constexpr size_t lcd_transfer_buffer_size = LCD_WIDTH*LCD_HEIGHT*2/10;
- static uint8_t* lcd_transfer_buffer1 = nullptr;
- // the second buffer
- static uint8_t* lcd_transfer_buffer2 = nullptr;
- ...
- // initialize the screen using the esp panel API
- static void lcd_init() {
- #ifdef LCD_BL
- #if LCD_BL > -1
- gpio_set_direction((gpio_num_t)LCD_BL,GPIO_MODE_OUTPUT);
- gpio_set_level((gpio_num_t)LCD_BL,0);
- #endif
- #endif
- lcd_transfer_buffer1 = (uint8_t*)heap_caps_malloc(lcd_transfer_buffer_size,MALLOC_CAP_DMA);
- #ifdef TWO_BUFFERS
- lcd_transfer_buffer2 = (uint8_t*)heap_caps_malloc(lcd_transfer_buffer_size,MALLOC_CAP_DMA);
- #endif
- if(lcd_transfer_buffer1==nullptr
- #ifdef TWO_BUFFERS
- ||lcd_transfer_buffer2==nullptr
- #endif
- ) {
- puts("Out of memory allocating transfer buffer");
- while(1) vTaskDelay(5);
- }
- spi_bus_config_t buscfg;
- memset(&buscfg, 0, sizeof(buscfg));
- buscfg.sclk_io_num = LCD_CLK;
- buscfg.mosi_io_num = LCD_MOSI;
- buscfg.miso_io_num = -1;
- buscfg.quadwp_io_num = -1;
- buscfg.quadhd_io_num = -1;
- buscfg.max_transfer_sz = lcd_transfer_buffer_size + 8;
- // Initialize the SPI bus on VSPI (SPI3)
- spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO);
- esp_lcd_panel_io_handle_t io_handle = NULL;
- esp_lcd_panel_io_spi_config_t io_config;
- memset(&io_config, 0, sizeof(io_config));
- io_config.dc_gpio_num = LCD_DC,
- io_config.cs_gpio_num = LCD_CS,
- io_config.pclk_hz = 40*1000*1000,
- io_config.lcd_cmd_bits = 8,
- io_config.lcd_param_bits = 8,
- io_config.spi_mode = 0,
- io_config.trans_queue_depth = 10,
- io_config.on_color_trans_done = lcd_flush_ready;
- // Attach the LCD to the SPI bus
- esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)SPI3_HOST, &io_config, &io_handle);
- esp_lcd_panel_dev_config_t lcd_config;
- memset(&lcd_config, 0, sizeof(lcd_config));
- lcd_config.reset_gpio_num = LCD_RST;
- #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
- lcd_config.rgb_endian = LCD_RGB_ENDIAN_RGB;
- #else
- lcd_config.color_space = ESP_LCD_COLOR_SPACE_RGB;
- #endif
- lcd_config.bits_per_pixel = 16;
- // Initialize the LCD configuration
- esp_lcd_panel_handle_t lcd_handle = nullptr;
- //
- // Change this from st7789 to your panel's display controller
- //
- esp_lcd_new_panel_st7789(io_handle, &lcd_config, &lcd_handle);
- // Reset the display
- esp_lcd_panel_reset(lcd_handle);
- // Initialize LCD panel
- esp_lcd_panel_init(lcd_handle);
- //
- // Configure the panel (Different LCD screens may need different options)
- //
- esp_lcd_panel_swap_xy(lcd_handle, false);
- esp_lcd_panel_set_gap(lcd_handle, 0, 35);
- esp_lcd_panel_swap_xy(lcd_handle,true);
- esp_lcd_panel_mirror(lcd_handle, false, true);
- esp_lcd_panel_invert_color(lcd_handle, true);
- // Turn on the screen
- #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
- esp_lcd_panel_disp_on_off(lcd_handle, true);
- #else
- esp_lcd_panel_disp_off(lcd_handle, false);
- #endif
- #ifdef LCD_BL
- #if LCD_BL > -1
- gpio_set_level((gpio_num_t)LCD_BL,1);
- #endif
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement