Browse Source

update: lcd的drawStr也改成ufont模式,但还需优化一下,非buff模式很慢

Wendal Chen 3 years ago
parent
commit
e8110d7223
1 changed files with 50 additions and 222 deletions
  1. 50 222
      components/lcd/luat_lib_lcd.c

+ 50 - 222
components/lcd/luat_lib_lcd.c

@@ -25,6 +25,10 @@ uint8_t u8g2_font_decode_get_unsigned_bits(u8g2_font_decode_t *f, uint8_t cnt);
 
 extern luat_color_t BACK_COLOR , FORE_COLOR ;
 
+#include "luat_ufont.h"
+extern const lv_font_t luat_font_sarasa_bold_16_lvgl;
+static lv_font_t* font_current;
+
 extern const luat_lcd_opts_t lcd_opts_st7735;
 extern const luat_lcd_opts_t lcd_opts_st7735v;
 extern const luat_lcd_opts_t lcd_opts_st7735s;
@@ -259,9 +263,10 @@ static int l_lcd_init(lua_State* L) {
         }
         // 初始化OK, 配置额外的参数
         default_conf = conf;
-        u8g2_SetFont(&(conf->luat_lcd_u8g2), u8g2_font_opposansm8);
-        u8g2_SetFontMode(&(conf->luat_lcd_u8g2), 0);
-        u8g2_SetFontDirection(&(conf->luat_lcd_u8g2), 0);
+        // u8g2_SetFont(&(conf->luat_lcd_u8g2), u8g2_font_opposansm8);
+        // u8g2_SetFontMode(&(conf->luat_lcd_u8g2), 0);
+        // u8g2_SetFontDirection(&(conf->luat_lcd_u8g2), 0);
+        font_current = &luat_font_sarasa_bold_16_lvgl;
         lua_pushboolean(L, 1);
         return 1;
     }
@@ -648,195 +653,6 @@ static int l_lcd_drawQrcode(lua_State *L)
     return 0;
 }
 
-static uint8_t utf8_state;
-static uint16_t encoding;
-static uint16_t utf8_next(uint8_t b)
-{
-  if ( b == 0 )  /* '\n' terminates the string to support the string list procedures */
-    return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
-  if ( utf8_state == 0 )
-  {
-    if ( b >= 0xfc )  /* 6 byte sequence */
-    {
-      utf8_state = 5;
-      b &= 1;
-    }
-    else if ( b >= 0xf8 )
-    {
-      utf8_state = 4;
-      b &= 3;
-    }
-    else if ( b >= 0xf0 )
-    {
-      utf8_state = 3;
-      b &= 7;
-    }
-    else if ( b >= 0xe0 )
-    {
-      utf8_state = 2;
-      b &= 15;
-    }
-    else if ( b >= 0xc0 )
-    {
-      utf8_state = 1;
-      b &= 0x01f;
-    }
-    else
-    {
-      /* do nothing, just use the value as encoding */
-      return b;
-    }
-    encoding = b;
-    return 0x0fffe;
-  }
-  else
-  {
-    utf8_state--;
-    /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
-    encoding<<=6;
-    b &= 0x03f;
-    encoding |= b;
-    if ( utf8_state != 0 )
-      return 0x0fffe; /* nothing to do yet */
-  }
-  return encoding;
-}
-
-static void u8g2_draw_hv_line(u8g2_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint16_t color){
-  switch(dir)
-  {
-    case 0:
-      luat_lcd_draw_hline(default_conf,x,y,len,color);
-      break;
-    case 1:
-      luat_lcd_draw_vline(default_conf,x,y,len,color);
-      break;
-    case 2:
-        luat_lcd_draw_hline(default_conf,x-len+1,y,len,color);
-      break;
-    case 3:
-      luat_lcd_draw_vline(default_conf,x,y-len+1,len,color);
-      break;
-  }
-}
-
-static void u8g2_font_decode_len(u8g2_t *u8g2, uint8_t len, uint8_t is_foreground){
-  uint8_t cnt;  /* total number of remaining pixels, which have to be drawn */
-  uint8_t rem;  /* remaining pixel to the right edge of the glyph */
-  uint8_t current;  /* number of pixels, which need to be drawn for the draw procedure */
-    /* current is either equal to cnt or equal to rem */
-  /* local coordinates of the glyph */
-  uint8_t lx,ly;
-  /* target position on the screen */
-  int16_t x, y;
-  u8g2_font_decode_t *decode = &(u8g2->font_decode);
-  cnt = len;
-  /* get the local position */
-  lx = decode->x;
-  ly = decode->y;
-  for(;;){
-    /* calculate the number of pixel to the right edge of the glyph */
-    rem = decode->glyph_width;
-    rem -= lx;
-    /* calculate how many pixel to draw. This is either to the right edge */
-    /* or lesser, if not enough pixel are left */
-    current = rem;
-    if ( cnt < rem )
-      current = cnt;
-    /* now draw the line, but apply the rotation around the glyph target position */
-    //u8g2_font_decode_draw_pixel(u8g2, lx,ly,current, is_foreground);
-    // printf("lx:%d,ly:%d,current:%d, is_foreground:%d \r\n",lx,ly,current, is_foreground);
-    /* get target position */
-    x = decode->target_x;
-    y = decode->target_y;
-    /* apply rotation */
-    x = u8g2_add_vector_x(x, lx, ly, decode->dir);
-    y = u8g2_add_vector_y(y, lx, ly, decode->dir);
-    /* draw foreground and background (if required) */
-    if ( current > 0 )		/* avoid drawing zero length lines, issue #4 */
-    {
-      if ( is_foreground )
-      {
-	    u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, lcd_str_fg_color);
-      }
-      // else if ( decode->is_transparent == 0 )
-      // {
-	    // u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, lcd_str_bg_color);
-      // }
-    }
-    /* check, whether the end of the run length code has been reached */
-    if ( cnt < rem )
-      break;
-    cnt -= rem;
-    lx = 0;
-    ly++;
-  }
-  lx += cnt;
-  decode->x = lx;
-  decode->y = ly;
-}
-static void u8g2_font_setup_decode(u8g2_t *u8g2, const uint8_t *glyph_data)
-{
-  u8g2_font_decode_t *decode = &(u8g2->font_decode);
-  decode->decode_ptr = glyph_data;
-  decode->decode_bit_pos = 0;
-
-  /* 8 Nov 2015, this is already done in the glyph data search procedure */
-  /*
-  decode->decode_ptr += 1;
-  decode->decode_ptr += 1;
-  */
-
-  decode->glyph_width = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_char_width);
-  decode->glyph_height = u8g2_font_decode_get_unsigned_bits(decode,u8g2->font_info.bits_per_char_height);
-
-}
-static int8_t u8g2_font_decode_glyph(u8g2_t *u8g2, const uint8_t *glyph_data){
-  uint8_t a, b;
-  int8_t x, y;
-  int8_t d;
-  int8_t h;
-  u8g2_font_decode_t *decode = &(u8g2->font_decode);
-  u8g2_font_setup_decode(u8g2, glyph_data);
-  h = u8g2->font_decode.glyph_height;
-  x = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_x);
-  y = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_y);
-  d = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_delta_x);
-
-  if ( decode->glyph_width > 0 )
-  {
-    decode->target_x = u8g2_add_vector_x(decode->target_x, x, -(h+y), decode->dir);
-    decode->target_y = u8g2_add_vector_y(decode->target_y, x, -(h+y), decode->dir);
-    //u8g2_add_vector(&(decode->target_x), &(decode->target_y), x, -(h+y), decode->dir);
-    /* reset local x/y position */
-    decode->x = 0;
-    decode->y = 0;
-    /* decode glyph */
-    for(;;){
-      a = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_0);
-      b = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_1);
-      do{
-        u8g2_font_decode_len(u8g2, a, 0);
-        u8g2_font_decode_len(u8g2, b, 1);
-      } while( u8g2_font_decode_get_unsigned_bits(decode, 1) != 0 );
-      if ( decode->y >= h )
-        break;
-    }
-  }
-  return d;
-}
-const uint8_t *u8g2_font_get_glyph_data(u8g2_t *u8g2, uint16_t encoding);
-static int16_t u8g2_font_draw_glyph(u8g2_t *u8g2, int16_t x, int16_t y, uint16_t encoding){
-  int16_t dx = 0;
-  u8g2->font_decode.target_x = x;
-  u8g2->font_decode.target_y = y;
-  const uint8_t *glyph_data = u8g2_font_get_glyph_data(u8g2, encoding);
-  if ( glyph_data != NULL ){
-    dx = u8g2_font_decode_glyph(u8g2, glyph_data);
-  }
-  return dx;
-}
-
 /*
 设置字体
 @api lcd.setFont(font)
@@ -864,11 +680,35 @@ static int l_lcd_set_font(lua_State *L) {
       LLOGE("only font pointer is allow");
       return 0;
     }
-    u8g2_SetFont(&(default_conf->luat_lcd_u8g2), ptr);
+    // u8g2_SetFont(&(default_conf->luat_lcd_u8g2), ptr);
+    font_current = (lv_font_t*)ptr;
     lua_pushboolean(L, 1);
     return 1;
 }
 
+
+static void (lcd_ui_draw_point)(void* userdata, int x, int y, uint32_t color) {
+    luat_lcd_draw_point((luat_lcd_conf_t*)userdata, x, y, (uint16_t)color);
+}
+static void (lcd_ui_draw_line)(void* userdata, int x1, int y1, int x2, int y2, uint32_t color) {
+    luat_lcd_draw_line((luat_lcd_conf_t*)userdata, x1, y1, x2, y2, (uint16_t)color);
+}
+static void (lcd_ui_draw_block)(void* userdata, int x, int y, int w, int h, size_t bitw, void* data) {
+    LLOGD("lcd_ui_draw_block NOT support yet");
+}
+static void (lcd_ui_draw_fill)(void* userdata, int x, int y, int w, int h, uint32_t color) {
+    luat_lcd_draw_fill((luat_lcd_conf_t*)userdata, x, y, w, h, (uint16_t)color);
+}
+static void (lcd_ui_draw_flush)(void* userdata) {};
+
+const ui_draw_opts_t lcd_ui_opts = {
+    .draw_point = lcd_ui_draw_point,
+    .draw_line  = lcd_ui_draw_line,
+    .draw_block = lcd_ui_draw_block,
+    .draw_fill  = lcd_ui_draw_fill,
+    .draw_flush = lcd_ui_draw_flush
+};
+
 /*
 显示字符串
 @api lcd.drawStr(x,y,str,fg_color)
@@ -887,42 +727,30 @@ lcd.drawStr(40,40,"drawStr测试")
 static int l_lcd_draw_str(lua_State* L) {
     int x, y;
     size_t sz;
-    const uint8_t* data;
+    const uint8_t* str;
     x = luaL_checkinteger(L, 1);
     y = luaL_checkinteger(L, 2);
-    data = (const uint8_t*)luaL_checklstring(L, 3, &sz);
+    str = (const uint8_t*)luaL_checklstring(L, 3, &sz);
     lcd_str_fg_color = (luat_color_t)luaL_optinteger(L, 4,FORE_COLOR);
     // lcd_str_bg_color = (uint32_t)luaL_optinteger(L, 5,BACK_COLOR);
     if (sz == 0)
         return 0;
-    uint16_t e;
-    int16_t delta;
-    utf8_state = 0;
-
-    for(;;){
-        e = utf8_next((uint8_t)*data);
-        if ( e == 0x0ffff )
-        break;
-        data++;
-        if ( e != 0x0fffe ){
-        delta = u8g2_font_draw_glyph(&(default_conf->luat_lcd_u8g2), x, y, e);
-        switch(default_conf->luat_lcd_u8g2.font_decode.dir){
-            case 0:
-            x += delta;
-            break;
-            case 1:
-            y += delta;
-            break;
-            case 2:
-            x -= delta;
-            break;
-            case 3:
-            y -= delta;
-            break;
-        }
-        }
-    }
+    ui_draw_str_ctx_t ctx = {
+        .font = font_current,
+        .bg_color = BACK_COLOR,
+        .front_color = lcd_str_fg_color,
+        .draw_mode = 0,
+        .opts = lcd_ui_opts,
+        .ui_h = default_conf->h,
+        .ui_w = default_conf->w,
+        .x = x,
+        .y = y,
+        .utf8_letters = str,
+        .userdata = default_conf,
+    };
+    int ret = luat_ufont_drawUTF8(&ctx);
     lcd_auto_flush(default_conf);
+    lua_pushinteger(L, ret);
     return 0;
 }