You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.8 KiB

3 years ago
  1. #include "who_lcd.h"
  2. #include "esp_camera.h"
  3. #include <string.h>
  4. #include "logo_en_240x240_lcd.h"
  5. static const char *TAG = "who_lcd";
  6. static scr_driver_t g_lcd;
  7. static scr_info_t g_lcd_info;
  8. static QueueHandle_t xQueueFrameI = NULL;
  9. static QueueHandle_t xQueueFrameO = NULL;
  10. static bool gReturnFB = true;
  11. static void task_process_handler(void *arg)
  12. {
  13. camera_fb_t *frame = NULL;
  14. while (true)
  15. {
  16. if (xQueueReceive(xQueueFrameI, &frame, portMAX_DELAY))
  17. {
  18. g_lcd.draw_bitmap(0, 0, frame->width, frame->height, (uint16_t *)frame->buf);
  19. if (xQueueFrameO)
  20. {
  21. xQueueSend(xQueueFrameO, &frame, portMAX_DELAY);
  22. }
  23. else if (gReturnFB)
  24. {
  25. esp_camera_fb_return(frame);
  26. }
  27. else
  28. {
  29. free(frame);
  30. }
  31. }
  32. }
  33. }
  34. esp_err_t register_lcd(const QueueHandle_t frame_i, const QueueHandle_t frame_o, const bool return_fb)
  35. {
  36. spi_config_t bus_conf = {
  37. .miso_io_num = BOARD_LCD_MISO,
  38. .mosi_io_num = BOARD_LCD_MOSI,
  39. .sclk_io_num = BOARD_LCD_SCK,
  40. .max_transfer_sz = 2 * 240 * 240 + 10,
  41. };
  42. spi_bus_handle_t spi_bus = spi_bus_create(SPI2_HOST, &bus_conf);
  43. scr_interface_spi_config_t spi_lcd_cfg = {
  44. .spi_bus = spi_bus,
  45. .pin_num_cs = BOARD_LCD_CS,
  46. .pin_num_dc = BOARD_LCD_DC,
  47. .clk_freq = 40 * 1000000,
  48. .swap_data = 0,
  49. };
  50. scr_interface_driver_t *iface_drv;
  51. scr_interface_create(SCREEN_IFACE_SPI, &spi_lcd_cfg, &iface_drv);
  52. esp_err_t ret = scr_find_driver(SCREEN_CONTROLLER_ST7789, &g_lcd);
  53. if (ESP_OK != ret)
  54. {
  55. return ret;
  56. ESP_LOGE(TAG, "screen find failed");
  57. }
  58. scr_controller_config_t lcd_cfg = {
  59. .interface_drv = iface_drv,
  60. .pin_num_rst = BOARD_LCD_RST,
  61. .pin_num_bckl = BOARD_LCD_BL,
  62. .rst_active_level = 0,
  63. .bckl_active_level = 0,
  64. .offset_hor = 0,
  65. .offset_ver = 0,
  66. .width = 240,
  67. .height = 240,
  68. .rotate = 0,
  69. };
  70. ret = g_lcd.init(&lcd_cfg);
  71. if (ESP_OK != ret)
  72. {
  73. return ESP_FAIL;
  74. ESP_LOGE(TAG, "screen initialize failed");
  75. }
  76. g_lcd.get_info(&g_lcd_info);
  77. ESP_LOGI(TAG, "Screen name:%s | width:%d | height:%d", g_lcd_info.name, g_lcd_info.width, g_lcd_info.height);
  78. app_lcd_set_color(0x000000);
  79. vTaskDelay(pdMS_TO_TICKS(200));
  80. app_lcd_draw_wallpaper();
  81. vTaskDelay(pdMS_TO_TICKS(200));
  82. xQueueFrameI = frame_i;
  83. xQueueFrameO = frame_o;
  84. gReturnFB = return_fb;
  85. xTaskCreatePinnedToCore(task_process_handler, TAG, 4 * 1024, NULL, 5, NULL, 0);
  86. return ESP_OK;
  87. }
  88. void app_lcd_draw_wallpaper()
  89. {
  90. scr_info_t lcd_info;
  91. g_lcd.get_info(&lcd_info);
  92. uint16_t *pixels = (uint16_t *)heap_caps_malloc((logo_en_240x240_lcd_width * logo_en_240x240_lcd_height) * sizeof(uint16_t), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  93. if (NULL == pixels)
  94. {
  95. ESP_LOGE(TAG, "Memory for bitmap is not enough");
  96. return;
  97. }
  98. memcpy(pixels, logo_en_240x240_lcd, (logo_en_240x240_lcd_width * logo_en_240x240_lcd_height) * sizeof(uint16_t));
  99. g_lcd.draw_bitmap(0, 0, logo_en_240x240_lcd_width, logo_en_240x240_lcd_height, (uint16_t *)pixels);
  100. heap_caps_free(pixels);
  101. }
  102. void app_lcd_set_color(int color)
  103. {
  104. scr_info_t lcd_info;
  105. g_lcd.get_info(&lcd_info);
  106. uint16_t *buffer = (uint16_t *)malloc(lcd_info.width * sizeof(uint16_t));
  107. if (NULL == buffer)
  108. {
  109. ESP_LOGE(TAG, "Memory for bitmap is not enough");
  110. }
  111. else
  112. {
  113. for (size_t i = 0; i < lcd_info.width; i++)
  114. {
  115. buffer[i] = color;
  116. }
  117. for (int y = 0; y < lcd_info.height; y++)
  118. {
  119. g_lcd.draw_bitmap(0, y, lcd_info.width, 1, buffer);
  120. }
  121. free(buffer);
  122. }
  123. }