Просмотр исходного кода

add:更改字体设置方式,贴近原生API

Dozingfiretruck 5 лет назад
Родитель
Сommit
f42c067329
3 измененных файлов с 92 добавлено и 36 удалено
  1. 14 14
      bsp/air640w/demo/61.u8g2/main.lua
  2. 1 1
      luat/include/luat_base.h
  3. 77 21
      luat/modules/luat_lib_u8g2.c

+ 14 - 14
bsp/air640w/demo/61.u8g2/main.lua

@@ -5,7 +5,7 @@
 
 local sys = require("sys")
 
---[[ 注意:如需使用u8g2的全中文字库需将 luat_base.h中26行#define USE_U8G2_WQY12_T_GB2312 打开]]
+--[[ 注意:如需使用u8g2的全中文字库需将 luat_base.h中#define USE_U8G2_WQY12_T_GB2312 打开]]
 
 -- 项目信息,预留
 VERSION = "1.0.0"
@@ -17,14 +17,14 @@ local last_temp_data = "0"
 
 -- 初始化显示屏
 log.info(TAG, "init ssd1306")
---u8g2.begin({mode="i2c_sw", pin0=18, pin1=19})
-u8g2.begin({mode="i2c_hw",i2c_id=0})
+u8g2.begin({mode="i2c_sw", pin0=18, pin1=19})
+--u8g2.begin({mode="i2c_hw",i2c_id=0})
 --u8g2.begin({mode="spi_hw_4pin",spi_id=1,OLED_SPI_PIN_RES=20,OLED_SPI_PIN_DC=28,OLED_SPI_PIN_CS=29})
 u8g2.SetFontMode(1)
 u8g2.ClearBuffer()
-u8g2.SetFont("u8g2_font_ncenB08_tr")
+u8g2.SetFont(u8g2.font_ncenB08_tr)
 u8g2.DrawUTF8("U8g2+LuatOS", 32, 22)
-u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
+u8g2.SetFont(u8g2.font_wqy12_t_gb2312)
 u8g2.DrawUTF8("中文测试", 40, 38)
 u8g2.SendBuffer()
 
@@ -32,7 +32,7 @@ u8g2.SendBuffer()
 sys.taskInit(function()
     sys.wait(2000)
     u8g2.ClearBuffer()
-    u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
+    u8g2.SetFont(u8g2.font_wqy12_t_gb2312)
     u8g2.DrawUTF8("屏幕宽度", 20, 24)
     u8g2.DrawUTF8("屏幕高度", 20, 42)
     u8g2.SetFont("u8g2_font_ncenB08_tr")
@@ -42,7 +42,7 @@ sys.taskInit(function()
 
     sys.wait(2000)
     u8g2.ClearBuffer()
-    u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
+    u8g2.SetFont(u8g2.font_wqy12_t_gb2312)
     u8g2.DrawUTF8("画线测试:", 30, 24)
     for i = 0, 128, 8 do
         u8g2.DrawLine(0,40,i,40)
@@ -52,7 +52,7 @@ sys.taskInit(function()
 
     sys.wait(1000)
     u8g2.ClearBuffer()
-    u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
+    u8g2.SetFont(u8g2.font_wqy12_t_gb2312)
     u8g2.DrawUTF8("画圆测试:", 30, 24)
     u8g2.DrawCircle(30,50,10,15)
     u8g2.DrawDisc(90,50,10,15)
@@ -60,7 +60,7 @@ sys.taskInit(function()
 
     sys.wait(1000)
     u8g2.ClearBuffer()
-    u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
+    u8g2.SetFont(u8g2.font_wqy12_t_gb2312)
     u8g2.DrawUTF8("椭圆测试:", 30, 24)
     u8g2.DrawEllipse(30,50,6,10,15)
     u8g2.DrawFilledEllipse(90,50,6,10,15)
@@ -68,7 +68,7 @@ sys.taskInit(function()
 
     sys.wait(1000)
     u8g2.ClearBuffer()
-    u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
+    u8g2.SetFont(u8g2.font_wqy12_t_gb2312)
     u8g2.DrawUTF8("方框测试:", 30, 24)
     u8g2.DrawBox(30,40,30,24)
     u8g2.DrawFrame(90,40,30,24)
@@ -76,7 +76,7 @@ sys.taskInit(function()
 
     sys.wait(1000)
     u8g2.ClearBuffer()
-    u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
+    u8g2.SetFont(u8g2.font_wqy12_t_gb2312)
     u8g2.DrawUTF8("圆角方框:", 30, 24)
     u8g2.DrawRBox(30,40,30,24,8)
     u8g2.DrawRFrame(90,40,30,24,8)
@@ -84,16 +84,16 @@ sys.taskInit(function()
 
     sys.wait(1000)
     u8g2.ClearBuffer()
-    u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
+    u8g2.SetFont(u8g2.font_wqy12_t_gb2312)
     u8g2.DrawUTF8("符号测试:", 30, 24)
     u8g2.DrawUTF8("显示雪人", 30, 38)
-    u8g2.SetFont("u8g2_font_unifont_t_symbols")
+    u8g2.SetFont(u8g2.font_unifont_t_symbols)
     u8g2.DrawGlyph( 50, 60, 0x2603 )
     u8g2.SendBuffer()
 
     sys.wait(1000)
     u8g2.ClearBuffer()
-    u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
+    u8g2.SetFont(u8g2.font_wqy12_t_gb2312)
     u8g2.DrawUTF8("三角测试:", 30, 24)
     u8g2.DrawTriangle(30,60, 60,30, 90,60)
     u8g2.SendBuffer()

+ 1 - 1
luat/include/luat_base.h

@@ -21,10 +21,10 @@
 #include "lauxlib.h"
 #include "stdint.h"
 #include "string.h"
-//-------------------------------
 
 //以下是u8g2的字库,默认不开启,根据需要自行开启对应宏定义
 //#define USE_U8G2_WQY12_T_GB2312      //enable u8g2 chinese font
+//#define USE_U8G2_UNIFONT_SYMBOLS
 //#define USE_U8G2_ICONIC_WEATHER_6X
 
 //lua_State * luat_get_state();

+ 77 - 21
luat/modules/luat_lib_u8g2.c

@@ -12,12 +12,19 @@
 #include "luat_gpio.h"
 #include "luat_timer.h"
 #include "luat_i2c.h"
+#include "luat_spi.h"
 
 #include "u8g2.h"
 
 #define LUAT_LOG_TAG "luat.u8g2"
 #include "luat_log.h"
 
+
+#define font_ncenB08_tr                     0
+#define font_wqy12_t_gb2312                 1
+#define font_unifont_t_symbols              2
+#define font_open_iconic_weather_6x_t       3
+
 static u8g2_t* u8g2;
 static int u8g2_lua_ref;
 static uint8_t i2c_id;
@@ -279,37 +286,82 @@ static int l_u8g2_SetFontMode(lua_State *L){
 -- 设置为中文字体,对之后的drawStr有效,使用中文字体需在luat_base.h开启#define USE_U8G2_WQY12_T_GB2312
 u8g2.setFont("u8g2_font_wqy12_t_gb2312")
 */
+// static int l_u8g2_SetFont(lua_State *L) {
+//     if (u8g2 == NULL) {
+//         LLOGI("disp not init yet!!!");
+//         lua_pushboolean(L, 0);
+//         return 1;
+//     }
+//     size_t len;
+//     LLOGI("lua_type:%s",lua_type(L,1));
+
+//     const char* font = luaL_checklstring(L, 1, &len);
+//     if (strcmp("u8g2_font_ncenB08_tr", font) == 0) {
+//         u8g2_SetFont(u8g2, u8g2_font_ncenB08_tr);
+//         lua_pushboolean(L, 1);
+//         }
+// #if defined USE_U8G2_WQY12_T_GB2312
+//     else if (strcmp("u8g2_font_wqy12_t_gb2312", font) == 0) {
+//         u8g2_SetFont(u8g2, u8g2_font_wqy12_t_gb2312);
+//         lua_pushboolean(L, 1);
+//     }
+// #endif
+//     else if (strcmp("u8g2_font_unifont_t_symbols", font) == 0) {
+//         u8g2_SetFont(u8g2, u8g2_font_unifont_t_symbols);
+//         lua_pushboolean(L, 1);
+//     }
+// #if defined USE_U8G2_ICONIC_WEATHER_6X
+//     else if (strcmp("u8g2_font_open_iconic_weather_6x_t", font) == 0) {
+//         u8g2_SetFont(u8g2, u8g2_font_open_iconic_weather_6x_t);
+//         lua_pushboolean(L, 1);
+//     }
+// #endif
+//     else
+//         lua_pushboolean(L, 0);
+//     return 1;
+// }
+
 static int l_u8g2_SetFont(lua_State *L) {
     if (u8g2 == NULL) {
         LLOGI("disp not init yet!!!");
         lua_pushboolean(L, 0);
         return 1;
     }
-    size_t len;
-    const char* font = luaL_checklstring(L, 1, &len);
-    if (strcmp("u8g2_font_ncenB08_tr", font) == 0) {
-        u8g2_SetFont(u8g2, u8g2_font_ncenB08_tr);
-        lua_pushboolean(L, 1);
-        }
+    int font = luaL_checkinteger(L, 1);
+    switch (font)
+        {
+        case font_ncenB08_tr:
+            LLOGI("font_ncenB08_tr");
+            u8g2_SetFont(u8g2, u8g2_font_ncenB08_tr);
+            lua_pushboolean(L, 1);
+            break;
 #if defined USE_U8G2_WQY12_T_GB2312
-    else if (strcmp("u8g2_font_wqy12_t_gb2312", font) == 0) {
-        u8g2_SetFont(u8g2, u8g2_font_wqy12_t_gb2312);
-        lua_pushboolean(L, 1);
-    }
+        case font_wqy12_t_gb2312:
+            LLOGI("font_wqy12_t_gb2312");
+            u8g2_SetFont(u8g2, u8g2_font_wqy12_t_gb2312);
+            lua_pushboolean(L, 1);
+            break;
+#endif
+#if defined USE_U8G2_UNIFONT_SYMBOLS
+        case font_unifont_t_symbols:
+            LLOGI("font_wqy12_t_gb2312");
+            u8g2_SetFont(u8g2, u8g2_font_unifont_t_symbols);
+            lua_pushboolean(L, 1);
+            break;
 #endif
-    else if (strcmp("u8g2_font_unifont_t_symbols", font) == 0) {
-        u8g2_SetFont(u8g2, u8g2_font_unifont_t_symbols);
-        lua_pushboolean(L, 1);
-    }
 #if defined USE_U8G2_ICONIC_WEATHER_6X
-    else if (strcmp("u8g2_font_open_iconic_weather_6x_t", font) == 0) {
-        u8g2_SetFont(u8g2, u8g2_font_open_iconic_weather_6x_t);
-        lua_pushboolean(L, 1);
-    }
+        case font_open_iconic_weather_6x_t:
+            LLOGI("font_wqy12_t_gb2312");
+            u8g2_SetFont(u8g2, u8g2_font_open_iconic_weather_6x_t);
+            lua_pushboolean(L, 1);
+            break;
 #endif
-    else
-        lua_pushboolean(L, 0);
-    return 1;
+        default:
+            lua_pushboolean(L, 0);
+            LLOGI("default");
+            break;
+        }
+    return 0;
 }
 
 /*
@@ -577,6 +629,10 @@ static const rotable_Reg reg_u8g2[] =
     { "DrawGlyph",    l_u8g2_DrawGlyph,    0},
     { "DrawTriangle",    l_u8g2_DrawTriangle,    0},
     { "SetBitmapMode",    l_u8g2_SetBitmapMode,    0},
+    { "font_ncenB08_tr", NULL,       font_ncenB08_tr},
+    { "font_wqy12_t_gb2312", NULL,       font_wqy12_t_gb2312},
+    { "font_unifont_t_symbols", NULL,       font_unifont_t_symbols},
+    { "font_open_iconic_weather_6x_t", NULL,       font_open_iconic_weather_6x_t},
 	{ NULL, NULL, 0}
 };