Browse Source

fix: 修复lcd在pc模拟器时异常显示和不响应

zengeshuai 6 months ago
parent
commit
b932d828bb

+ 13 - 0
components/lcd/luat_lcd.c

@@ -172,6 +172,10 @@ int luat_lcd_init_default(luat_lcd_conf_t* conf) {
     // 发送初始化命令
     if (conf->opts->init){
         conf->opts->init(conf);
+        // 在SDL2仿真模式下,不再继续发送硬件命令,直接完成初始化
+        if (conf->opts && conf->opts->name && strcmp(conf->opts->name, "sdl2") == 0) {
+            goto INIT_DONE;
+        }
     }else{
         luat_lcd_execute_cmds(conf);
         if(strcmp(conf->opts->name,"custom") == 0){
@@ -240,6 +244,9 @@ int luat_lcd_close(luat_lcd_conf_t* conf) {
 }
 
 int luat_lcd_display_off(luat_lcd_conf_t* conf) {
+    if (conf && conf->opts && conf->opts->name && strcmp(conf->opts->name, "sdl2") == 0) {
+        return 0;
+    }
     if (conf->pin_pwr != LUAT_GPIO_NONE) {
         luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
     }
@@ -250,6 +257,9 @@ int luat_lcd_display_off(luat_lcd_conf_t* conf) {
 }
 
 int luat_lcd_display_on(luat_lcd_conf_t* conf) {
+    if (conf && conf->opts && conf->opts->name && strcmp(conf->opts->name, "sdl2") == 0) {
+        return 0;
+    }
     if (conf->pin_pwr != LUAT_GPIO_NONE) {
         luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
     }
@@ -272,6 +282,9 @@ int luat_lcd_sleep(luat_lcd_conf_t* conf) {
 }
 
 int luat_lcd_wakeup(luat_lcd_conf_t* conf) {
+    if (conf && conf->opts && conf->opts->name && strcmp(conf->opts->name, "sdl2") == 0) {
+        return 0;
+    }
     if (conf->pin_pwr != LUAT_GPIO_NONE)
         luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
     luat_rtos_task_sleep(5);

+ 4 - 1
components/lcd/luat_lib_lcd.c

@@ -78,7 +78,10 @@ luat_lcd_conf_t *lcd_dft_conf = NULL;
 
 // 所有绘图相关的函数都应该调用本函数
 void lcd_auto_flush(luat_lcd_conf_t *conf) {
-#ifndef LUAT_USE_LCD_SDL2
+#ifdef LUAT_USE_LCD_SDL2
+  if (conf == NULL || conf->auto_flush == 0)
+    return;
+#else
   if (conf == NULL || conf->buff == NULL || conf->auto_flush == 0)
     return;
 #endif

+ 2 - 4
components/ui/sdl2/luat_lcd_sdl2.c

@@ -71,14 +71,12 @@ int luat_lcd_draw(luat_lcd_conf_t* conf, int16_t x1, int16_t y1, int16_t x2, int
     size_t rh = y2 - y1 + 1;
 
     uint32_t *tmp = fb;
-    lv_color_t c;
     for (size_t i = 0; i < rh; i++)
     {
         for (size_t j = 0; j < rw; j++)
         {
-            // *tmp = luat_color_565to8888(*color_p);
-            c.full = *color_p;
-            *tmp = lv_color_to32(c);
+            // 输入为 RGB565,SDL 纹理为 ARGB8888,这里做明确转换
+            *tmp = luat_color_565to8888(*color_p);
             tmp ++;
             color_p ++;
         }

+ 14 - 1
components/ui/sdl2/luat_sdl2.c

@@ -13,6 +13,17 @@ static SDL_Texture *framebuffer = NULL;
 static uint32_t* fb;
 static luat_sdl2_conf_t sdl_conf;
 
+static void luat_sdl2_pump_events(void) {
+    SDL_Event e;
+    while (SDL_PollEvent(&e)) {
+        if (e.type == SDL_QUIT) {
+            // Graceful exit on window close
+            exit(0);
+        }
+        // Other events are ignored; important is to pump to keep window responsive
+    }
+}
+
 int luat_sdl2_init(luat_sdl2_conf_t *conf) {
     if (framebuffer != NULL) {
         LLOGD("SDL2 aready inited");
@@ -28,13 +39,14 @@ int luat_sdl2_init(luat_sdl2_conf_t *conf) {
                               SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                               conf->width, conf->height, 0);
 
-    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
+    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
     framebuffer = SDL_CreateTexture(renderer,
                                     SDL_PIXELFORMAT_ARGB8888,
                                     SDL_TEXTUREACCESS_STREAMING,
                                     conf->width,
                                     conf->height);
     // fb = luat_heap_malloc(sizeof(uint32_t) * conf->width * conf->height);
+    luat_sdl2_pump_events();
     return 0;
 }
 
@@ -67,4 +79,5 @@ void luat_sdl2_flush(void) {
         SDL_RenderCopy(renderer, framebuffer, NULL, NULL);
         SDL_RenderPresent(renderer);
     }
+    luat_sdl2_pump_events();
 }