Parcourir la source

Merge branch 'master' of https://gitee.com/openLuat/LuatOS

Dozingfiretruck il y a 4 ans
Parent
commit
7a13e85c7f

+ 12 - 0
bsp/sysp/CMakeLists.txt

@@ -20,12 +20,17 @@ include_directories(${TOPROOT}/lua/include
                     ${TOPROOT}/luat/include
                     ${TOPROOT}/components/lcd
                     ${TOPROOT}/luat/packages/u8g2
+                    ${TOPROOT}/luat/packages/qrcode
                     )
 
 aux_source_directory(./port PORT_SRCS)
 aux_source_directory(${TOPROOT}/lua/src LUA_SRCS)
 aux_source_directory(${TOPROOT}/luat/packages/lua-cjson CJSON_SRCS)
 
+aux_source_directory(${TOPROOT}/components/lcd LCD_SRCS)
+aux_source_directory(${TOPROOT}/luat/packages/u8g2 U8G2_SRCS)
+aux_source_directory(${TOPROOT}/luat/packages/qrcode QRCODE_SRCS)
+
 add_library(lua ${LUA_SRCS})
 add_library(luatos_port ${PORT_SRCS})
 add_library(cjson ${CJSON_SRCS})
@@ -42,6 +47,10 @@ add_library(luat ${TOPROOT}/luat/modules/luat_main.c
                  ${TOPROOT}/luat/modules/luat_lib_mqttcore.c
                  ${TOPROOT}/luat/modules/luat_lib_libcoap.c
                  ${TOPROOT}/luat/modules/luat_lib_crypto.c
+                 ${TOPROOT}/luat/modules/luat_lib_gpio.c
+                 ${TOPROOT}/luat/modules/luat_lib_i2c.c
+                 ${TOPROOT}/luat/modules/luat_lib_spi.c
+                 ${TOPROOT}/luat/modules/luat_lib_adc.c
                  ${TOPROOT}/luat/modules/crc.c
                  ${TOPROOT}/luat/vfs/luat_vfs.c
                  ${TOPROOT}/luat/vfs/luat_fs_luadb.c
@@ -49,6 +58,9 @@ add_library(luat ${TOPROOT}/luat/modules/luat_main.c
                  ${TOPROOT}/luat/vfs/luat_luadb_inline.c
                  ${TOPROOT}/luat/vfs/luat_inline_sys.c
                  ${TOPROOT}/luat/vfs/luat_fs_onefile.c
+                 ${LCD_SRCS}
+                 ${U8G2_SRCS}
+                 ${QRCODE_SRCS}
             )
 
 #-----------------------

+ 40 - 0
bsp/sysp/include/luat_conf_bsp.h

@@ -9,6 +9,46 @@
 
 // #define LUAT_MAIN_DEMO 1
 
+#define USE_U8G2_OPPOSANSM12_CHINESE
+
+
+//---------------------
+// UI
+// LCD  是彩屏, 若使用LVGL就必须启用LCD
+#define LUAT_USE_LCD
+// EINK 是墨水屏
+// #define LUAT_USE_EINK
+
+//---------------------
+// U8G2
+// 单色屏, 支持i2c/spi
+// #define LUAT_USE_DISP
+// #define LUAT_USE_U8G2
+// #define U8G2_USE_SH1106
+// #define U8G2_USE_ST7567
+
+/**************FONT*****************/
+// Luat Fonts
+// #define LUAT_USE_FONTS
+// #define LUAT_FONTS_BASE_8P
+// #define LUAT_FONTS_BASE_12P
+// #define LUAT_FONTS_BASE_16P
+// #define LUAT_FONTS_BASE_20P
+// #define LUAT_FONTS_BASE_24P
+// #define LUAT_FONTS_BASE_28P
+/**********U8G2&LCD FONT*************/
+// #define USE_U8G2_UNIFONT_SYMBOLS
+#define USE_U8G2_OPPOSANSM12_CHINESE
+// #define USE_U8G2_OPPOSANSM16_CHINESE
+// #define USE_U8G2_OPPOSANSM24_CHINESE
+// #define USE_U8G2_OPPOSANSM32_CHINESE
+
+
+
+
+
+
+
 #define LV_TICK_CUSTOM     1
 #define LV_TICK_CUSTOM_INCLUDE "stdio.h"
 unsigned int get_timestamp(void);

+ 6 - 4
bsp/sysp/port/luat_base_sysp.c

@@ -9,10 +9,6 @@
 #include <emscripten/html5.h>
 #endif
 
-LUAMOD_API int luaopen_win32( lua_State *L );
-int luaopen_lfs(lua_State * L);
-int luaopen_rs232_core(lua_State * L);
-
 static const luaL_Reg loadedlibs[] = {
   {"_G", luaopen_base}, // _G
   {LUA_LOADLIBNAME, luaopen_package}, // require
@@ -37,6 +33,12 @@ static const luaL_Reg loadedlibs[] = {
   {"libcoap", luaopen_libcoap},        // 
   {"crypto", luaopen_crypto},
   {"lvgl", luaopen_lvgl},
+//------------------------------------------------
+// 这部分是外设, 只能模拟
+  {"gpio", luaopen_gpio},
+  {"spi",  luaopen_spi},
+  {"lcd",  luaopen_lcd},
+//-------------------------------------------------
   {NULL, NULL}
 };
 

+ 18 - 0
bsp/sysp/port/luat_gpio_sysp.c

@@ -0,0 +1,18 @@
+#include "luat_base.h"
+#include "luat_gpio.h"
+#include "luat_msgbus.h"
+
+int luat_gpio_setup(luat_gpio_t* gpio) {
+    return 0;
+}
+
+int luat_gpio_set(int pin, int level) {
+    return 0;
+}
+
+int luat_gpio_get(int pin) {
+    return 0;
+}
+
+void luat_gpio_close(int pin) {
+}

+ 31 - 0
bsp/sysp/port/luat_spi_device_sysp.c

@@ -0,0 +1,31 @@
+#include "luat_base.h"
+#include "luat_gpio.h"
+#include "luat_spi.h"
+
+// luat_spi_device_t* 在lua层看到的是一个userdata
+int luat_spi_device_setup(luat_spi_device_t* spi_dev) {
+    return 0;
+}
+
+//关闭SPI设备,成功返回0
+int luat_spi_device_close(luat_spi_device_t* spi_dev) {
+    return 0;
+}
+
+//收发SPI数据,返回接收字节数
+int luat_spi_device_transfer(luat_spi_device_t* spi_dev, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length) {
+    memset(recv_buf, 0, recv_length);
+    return recv_length;
+}
+
+//收SPI数据,返回接收字节数
+int luat_spi_device_recv(luat_spi_device_t* spi_dev, char* recv_buf, size_t length) {
+    memset(recv_buf, 0, length);
+    return length;
+}
+
+//发SPI数据,返回发送字节数
+int luat_spi_device_send(luat_spi_device_t* spi_dev, const char* send_buf, size_t length) {
+    return length;
+}
+

+ 35 - 0
bsp/sysp/port/luat_spi_sysp.c

@@ -0,0 +1,35 @@
+
+#include "luat_base.h"
+#include "luat_gpio.h"
+#include "luat_msgbus.h"
+#include "luat_spi.h"
+
+int luat_spi_device_config(luat_spi_device_t* spi_dev){
+    return 0;
+}
+
+int luat_spi_bus_setup(luat_spi_device_t* spi_dev){
+    return 0;
+}
+
+int luat_spi_setup(luat_spi_t* spi) {
+    return 0;
+}
+//关闭SPI,成功返回0
+int luat_spi_close(int spi_id) {
+    return 0;
+}
+//收发SPI数据,返回接收字节数
+int luat_spi_transfer(int spi_id, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length) {
+    memset(recv_buf, 0, recv_length);
+    return recv_length;
+}
+//收SPI数据,返回接收字节数
+int luat_spi_recv(int spi_id, char* recv_buf, size_t length) {
+    memset(recv_buf, 0, length);
+    return length;
+}
+//发SPI数据,返回发送字节数
+int luat_spi_send(int spi_id, const char* send_buf, size_t length) {
+    return length;
+}

+ 47 - 117
components/lcd/luat_lcd.c

@@ -8,9 +8,9 @@
 #define LUAT_LOG_TAG "lcd"
 #include "luat_log.h"
 
-uint32_t BACK_COLOR = WHITE, FORE_COLOR = BLACK;
+luat_color_t BACK_COLOR = WHITE, FORE_COLOR = BLACK;
 
-#define LUAT_LCD_CONF_COUNT (2)
+#define LUAT_LCD_CONF_COUNT (1)
 static luat_lcd_conf_t* confs[LUAT_LCD_CONF_COUNT] = {0};
 
 void luat_lcd_execute_cmds(luat_lcd_conf_t* conf, uint32_t* cmds, uint32_t count) {
@@ -80,24 +80,6 @@ int lcd_write_data(luat_lcd_conf_t* conf, const uint8_t data){
     }
 }
 
-int lcd_write_half_word(luat_lcd_conf_t* conf, const uint32_t da){
-    size_t len = 0;
-    char data[2] = {0};
-    data[0] = da >> 8;
-    data[1] = da;
-    if (conf->port == LUAT_LCD_SPI_DEVICE){
-        len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device),  (const char*)data, 2);
-    }else{
-        len = luat_spi_send(conf->port,  (const char*)data, 2);
-    }
-    if (len != 2){
-        LLOGI("lcd_write_half_word error. %d", len);
-        return -1;
-    }else{
-        return 0;
-    }
-}
-
 luat_lcd_conf_t* luat_lcd_get_default(void) {
     for (size_t i = 0; i < LUAT_LCD_CONF_COUNT; i++){
         if (confs[i] != NULL) {
@@ -172,21 +154,21 @@ int luat_lcd_inv_on(luat_lcd_conf_t* conf) {
 }
 
 int luat_lcd_set_address(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
-        lcd_write_cmd(conf,0x2a);
-        lcd_write_data(conf,(x1+conf->xoffset)>>8);
-        lcd_write_data(conf,x1+conf->xoffset);
-        lcd_write_data(conf,(x2+conf->xoffset)>>8);
-        lcd_write_data(conf,x2+conf->xoffset);
-        lcd_write_cmd(conf,0x2b);
-        lcd_write_data(conf,(y1+conf->yoffset)>>8);
-        lcd_write_data(conf,y1+conf->yoffset);
-        lcd_write_data(conf,(y2+conf->yoffset)>>8);
-        lcd_write_data(conf,y2+conf->yoffset);
-        lcd_write_cmd(conf,0x2C);
+    lcd_write_cmd(conf,0x2a);
+    lcd_write_data(conf,(x1+conf->xoffset)>>8);
+    lcd_write_data(conf,x1+conf->xoffset);
+    lcd_write_data(conf,(x2+conf->xoffset)>>8);
+    lcd_write_data(conf,x2+conf->xoffset);
+    lcd_write_cmd(conf,0x2b);
+    lcd_write_data(conf,(y1+conf->yoffset)>>8);
+    lcd_write_data(conf,y1+conf->yoffset);
+    lcd_write_data(conf,(y2+conf->yoffset)>>8);
+    lcd_write_data(conf,y2+conf->yoffset);
+    lcd_write_cmd(conf,0x2C);
     return 0;
 }
 
-int luat_lcd_set_color(uint32_t back, uint32_t fore){
+int luat_lcd_set_color(luat_color_t back, luat_color_t fore){
     BACK_COLOR = back;
     FORE_COLOR = fore;
     return 0;
@@ -194,8 +176,8 @@ int luat_lcd_set_color(uint32_t back, uint32_t fore){
 
 int luat_lcd_draw(luat_lcd_conf_t* conf, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, luat_color_t* color) {
     uint32_t size = (x2 - x1 + 1) * (y2 - y1 + 1) * 2;
-    luat_lcd_set_address(conf,x1, y1, x2, y2);
-    luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
+    luat_lcd_set_address(conf, x1, y1, x2, y2);
+    luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH); // draw是data, 这一句应该不需要的
 	if (conf->port == LUAT_LCD_SPI_DEVICE){
 		luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)color, size);
 	}else{
@@ -204,104 +186,52 @@ int luat_lcd_draw(luat_lcd_conf_t* conf, uint16_t x1, uint16_t y1, uint16_t x2,
     return 0;
 }
 
-int luat_lcd_clear(luat_lcd_conf_t* conf,uint32_t color){
-    uint16_t i, j;
-    uint8_t data[2] = {0};
-    uint8_t *buf = NULL;
-    data[0] = color >> 8;
-    data[1] = color;
-    luat_lcd_set_address(conf,0, 0, conf->w - 1, conf->h - 1);
-    buf = luat_heap_malloc(conf->w*conf->h/10);
-    if (buf)
-    {
-        for (j = 0; j < conf->w*conf->h/10 / 2; j++)
-        {
-            buf[j * 2] =  data[0];
-            buf[j * 2 + 1] =  data[1];
-        }
-        luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
-        for (i = 0; i < 20; i++)
-        {
-            if (conf->port == LUAT_LCD_SPI_DEVICE){
-                luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device),  (const char*)buf, conf->w*conf->h/10);
-            }else{
-                luat_spi_send(conf->port,  (const char*)buf, conf->w*conf->h/10);
-            }
-        }
-        luat_heap_free(buf);
-    }
-    else
-    {
-        luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
-        for (i = 0; i < conf->w; i++)
-        {
-            for (j = 0; j < conf->h; j++)
-            {
-                if (conf->port == LUAT_LCD_SPI_DEVICE){
-                    luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device),  (const char*)data, 2);
-                }else{
-                    luat_spi_send(conf->port,  (const char*)data, 2);
-                }
-            }
-        }
-    }
+int luat_lcd_clear(luat_lcd_conf_t* conf, luat_color_t color){
+    luat_lcd_draw_fill(conf, 0, 0, conf->w, conf->h, color);
     return 0;
 }
 
-int luat_lcd_draw_fill(luat_lcd_conf_t* conf,uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint32_t color){          
-	uint16_t i,j; 
-	luat_lcd_set_address(conf,x1,y1,x2-1,y2-1);//设置显示范围
+int luat_lcd_draw_fill(luat_lcd_conf_t* conf,uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, luat_color_t color) {          
+	uint16_t i;
 	for(i=y1;i<y2;i++)
-	{													   	 	
-		for(j=x1;j<x2;j++)
-		{
-			lcd_write_half_word(conf,color);
-		}
+	{
+		luat_lcd_draw_line(conf, x1, i, x2, i, color);
 	}
     return 0;			  	    
 }
 
-int luat_lcd_draw_point(luat_lcd_conf_t* conf, uint16_t x, uint16_t y, uint32_t color) {
-    luat_lcd_set_address(conf,x, y, x, y);
-    lcd_write_half_word(conf,color);
-    return 0;
+int luat_lcd_draw_point(luat_lcd_conf_t* conf, uint16_t x, uint16_t y, luat_color_t color) {
+    return luat_lcd_draw(conf, x, y, x, y, &color);
 }
 
-int luat_lcd_draw_vline(luat_lcd_conf_t* conf, uint16_t x, uint16_t y,uint16_t h, uint32_t color) {
-    luat_lcd_set_address(conf,x, y, x, y + h - 1);
-    while ( h-- ) {lcd_write_half_word(conf,color);}
-    return 0;
+int luat_lcd_draw_vline(luat_lcd_conf_t* conf, uint16_t x, uint16_t y,uint16_t h, luat_color_t color) {
+    return luat_lcd_draw_line(conf, x, y, x, y + h, color);
 }
 
-int luat_lcd_draw_hline(luat_lcd_conf_t* conf, uint16_t x, uint16_t y,uint16_t w, uint32_t color) {
-    luat_lcd_set_address(conf,x, y, x + w - 1, y);
-    while ( w-- ) {lcd_write_half_word(conf,color);}
-    return 0;
+int luat_lcd_draw_hline(luat_lcd_conf_t* conf, uint16_t x, uint16_t y,uint16_t w, luat_color_t color) {
+    return luat_lcd_draw_line(conf, x, y, x+w, y, color);
 }
-int luat_lcd_draw_line(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint32_t color){
+
+int luat_lcd_draw_line(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,luat_color_t color) {
     uint16_t t;
     uint32_t i = 0;
     int xerr = 0, yerr = 0, delta_x, delta_y, distance;
     int incx, incy, row, col;
-    if (x1 == x2 || y1 == y2)
+    if (x1 == x2 || y1 == y2) // 直线
     {
         /* fast draw transverse line */
-        luat_lcd_set_address(conf,x1, y1, x2, y2);
+        // luat_lcd_set_address(conf,x1, y1, x2, y2);
         size_t dots = (x2 - x1 + 1) * (y2 - y1 + 1);//点数量
-        uint8_t* line_buf = (uint8_t*)luat_heap_malloc(dots * 2);
-        for (i = 0; i < dots; i++)
-        {
-            line_buf[2 * i] = color >> 8;
-            line_buf[2 * i + 1] = color;
-        }
-        luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
-        if (conf->port == LUAT_LCD_SPI_DEVICE){
-            luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device),  (const char*)line_buf, dots * 2);
-        }else{
-            luat_spi_send(conf->port,  (const char*)line_buf, dots * 2);
+        luat_color_t* line_buf = (luat_color_t*) luat_heap_malloc(dots * sizeof(luat_color_t));
+        if (line_buf) {
+            for (i = 0; i < dots; i++)
+            {
+                line_buf[i] = color;
+            }
+            luat_lcd_draw(conf, x1, y1, x2, y2, line_buf);
+            luat_heap_free(line_buf);
+            return 0;
         }
-        luat_heap_free(line_buf);
-        return 0;
     }
 
     delta_x = x2 - x1;
@@ -343,15 +273,15 @@ int luat_lcd_draw_line(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t
     return 0;
 }
 
-int luat_lcd_draw_rectangle(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint32_t color){
-    luat_lcd_draw_line(conf,x1, y1, x2, y1,color);
-    luat_lcd_draw_line(conf,x1, y1, x1, y2,color);
-    luat_lcd_draw_line(conf,x1, y2, x2, y2,color);
-    luat_lcd_draw_line(conf,x2, y1, x2, y2,color);
+int luat_lcd_draw_rectangle(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, luat_color_t color){
+    luat_lcd_draw_line(conf,x1, y1, x2, y1, color);
+    luat_lcd_draw_line(conf,x1, y1, x1, y2, color);
+    luat_lcd_draw_line(conf,x1, y2, x2, y2, color);
+    luat_lcd_draw_line(conf,x2, y1, x2, y2, color);
     return 0;
 }
 
-int luat_lcd_draw_circle(luat_lcd_conf_t* conf,uint16_t x0, uint16_t y0, uint8_t r,uint32_t color){
+int luat_lcd_draw_circle(luat_lcd_conf_t* conf,uint16_t x0, uint16_t y0, uint8_t r, luat_color_t color){
     int a, b;
     int di;
     a = 0;

+ 9 - 9
components/lcd/luat_lcd.h

@@ -68,7 +68,7 @@ typedef struct luat_lcd_opts {
 
 int lcd_write_cmd(luat_lcd_conf_t* conf,const uint8_t cmd);
 int lcd_write_data(luat_lcd_conf_t* conf,const uint8_t data);
-int lcd_write_half_word(luat_lcd_conf_t* conf,const uint32_t da);
+int lcd_write_half_word(luat_lcd_conf_t* conf,const luat_color_t da);
 
 luat_lcd_conf_t* luat_lcd_get_default(void);
 const char* luat_lcd_name(luat_lcd_conf_t* conf);
@@ -81,17 +81,17 @@ int luat_lcd_wakeup(luat_lcd_conf_t* conf);
 int luat_lcd_inv_off(luat_lcd_conf_t* conf);
 int luat_lcd_inv_on(luat_lcd_conf_t* conf);
 int luat_lcd_set_address(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
-int luat_lcd_set_color(uint32_t back, uint32_t fore);
+int luat_lcd_set_color(luat_color_t back, luat_color_t fore);
 int luat_lcd_draw(luat_lcd_conf_t* conf, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, luat_color_t* color);
 int luat_lcd_draw_no_block(luat_lcd_conf_t* conf, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, luat_color_t* color, uint8_t last_flush);
 int luat_lcd_clear(luat_lcd_conf_t* conf,uint32_t color);
-int luat_lcd_draw_fill(luat_lcd_conf_t* conf,uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint32_t color);
-int luat_lcd_draw_point(luat_lcd_conf_t* conf, uint16_t x, uint16_t y, uint32_t color);
-int luat_lcd_draw_line(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint32_t color);
-int luat_lcd_draw_vline(luat_lcd_conf_t* conf, uint16_t x, uint16_t y,uint16_t h, uint32_t color);
-int luat_lcd_draw_hline(luat_lcd_conf_t* conf, uint16_t x, uint16_t y,uint16_t h, uint32_t color);
-int luat_lcd_draw_rectangle(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint32_t color);
-int luat_lcd_draw_circle(luat_lcd_conf_t* conf,uint16_t x0, uint16_t y0, uint8_t r, uint32_t color);
+int luat_lcd_draw_fill(luat_lcd_conf_t* conf,uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,luat_color_t color);
+int luat_lcd_draw_point(luat_lcd_conf_t* conf, uint16_t x, uint16_t y, luat_color_t color);
+int luat_lcd_draw_line(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, luat_color_t color);
+int luat_lcd_draw_vline(luat_lcd_conf_t* conf, uint16_t x, uint16_t y,uint16_t h, luat_color_t color);
+int luat_lcd_draw_hline(luat_lcd_conf_t* conf, uint16_t x, uint16_t y,uint16_t h, luat_color_t color);
+int luat_lcd_draw_rectangle(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, luat_color_t color);
+int luat_lcd_draw_circle(luat_lcd_conf_t* conf,uint16_t x0, uint16_t y0, uint8_t r, luat_color_t color);
 
 void luat_lcd_execute_cmds(luat_lcd_conf_t* conf, uint32_t* cmds, uint32_t count);
 

+ 21 - 20
components/lcd/luat_lib_lcd.c

@@ -22,7 +22,7 @@
 int8_t u8g2_font_decode_get_signed_bits(u8g2_font_decode_t *f, uint8_t cnt);
 uint8_t u8g2_font_decode_get_unsigned_bits(u8g2_font_decode_t *f, uint8_t cnt);
 
-extern uint32_t BACK_COLOR , FORE_COLOR ;
+extern luat_color_t BACK_COLOR , FORE_COLOR ;
 
 extern const luat_lcd_opts_t lcd_opts_st7735;
 extern const luat_lcd_opts_t lcd_opts_st7735v;
@@ -59,7 +59,7 @@ static const lcd_reg_t lcd_regs[] = {
 static luat_lcd_conf_t *default_conf = NULL;
 static int dft_conf_lua_ref = 0;
 
-static uint32_t lcd_str_fg_color,lcd_str_bg_color;
+static luat_color_t lcd_str_fg_color,lcd_str_bg_color;
 /*
 lcd显示屏初始化
 @api lcd.init(tp, args)
@@ -369,9 +369,9 @@ lcd颜色设置
 lcd.setColor(0xFFFF,0x0000)
 */
 static int l_lcd_set_color(lua_State* L) {
-    uint32_t back,fore;
-    back = (uint32_t)luaL_checkinteger(L, 1);
-    fore = (uint32_t)luaL_checkinteger(L, 2);
+    luat_color_t back,fore;
+    back = (luat_color_t)luaL_checkinteger(L, 1);
+    fore = (luat_color_t)luaL_checkinteger(L, 2);
     int ret = luat_lcd_set_color(back, fore);
     lua_pushboolean(L, ret == 0 ? 1 : 0);
     return 1;
@@ -401,7 +401,7 @@ static int l_lcd_draw(lua_State* L) {
     y2 = luaL_checkinteger(L, 4);
     if (lua_isinteger(L, 5)) {
         // color = (luat_color_t *)luaL_checkstring(L, 5);
-        uint32_t color = (uint32_t)luaL_checkinteger(L, 1);
+        luat_color_t color = (uint32_t)luaL_checkinteger(L, 1);
         ret = luat_lcd_draw(default_conf, x1, y1, x2, y2, &color);
     }
     else if (lua_isuserdata(L, 5)) {
@@ -431,7 +431,7 @@ lcd.clear()
 */
 static int l_lcd_clear(lua_State* L) {
     //size_t len = 0;
-    uint32_t color = BACK_COLOR;
+    luat_color_t color = BACK_COLOR;
     if (lua_gettop(L) > 0)
         color = (uint32_t)luaL_checkinteger(L, 1);
     int ret = luat_lcd_clear(default_conf, color);
@@ -453,13 +453,13 @@ lcd.fill(20,30,220,30,0x0000)
 */
 static int l_lcd_draw_fill(lua_State* L) {
     uint16_t x1, y1, x2, y2;
-    uint32_t color = BACK_COLOR;
+    luat_color_t color = BACK_COLOR;
     x1 = luaL_checkinteger(L, 1);
     y1 = luaL_checkinteger(L, 2);
     x2 = luaL_checkinteger(L, 3);
     y2 = luaL_checkinteger(L, 4);
     if (lua_gettop(L) > 4)
-        color = (uint32_t)luaL_checkinteger(L, 5);
+        color = (luat_color_t)luaL_checkinteger(L, 5);
     int ret = luat_lcd_draw_fill(default_conf, x1,  y1,  x2,  y2, color);
     lua_pushboolean(L, ret == 0 ? 1 : 0);
     return 1;
@@ -476,11 +476,11 @@ lcd.drawPoint(20,30,0x001F)
 */
 static int l_lcd_draw_point(lua_State* L) {
     uint16_t x, y;
-    uint32_t color = FORE_COLOR;
+    luat_color_t color = FORE_COLOR;
     x = luaL_checkinteger(L, 1);
     y = luaL_checkinteger(L, 2);
     if (lua_gettop(L) > 2)
-        color = (uint32_t)luaL_checkinteger(L, 3);
+        color = (luat_color_t)luaL_checkinteger(L, 3);
     int ret = luat_lcd_draw_point(default_conf, x, y, color);
     lua_pushboolean(L, ret == 0 ? 1 : 0);
     return 1;
@@ -499,13 +499,13 @@ lcd.drawLine(20,30,220,30,0x001F)
 */
 static int l_lcd_draw_line(lua_State* L) {
     uint16_t x1, y1, x2, y2;
-    uint32_t color = FORE_COLOR;
+    luat_color_t color = FORE_COLOR;
     x1 = luaL_checkinteger(L, 1);
     y1 = luaL_checkinteger(L, 2);
     x2 = luaL_checkinteger(L, 3);
     y2 = luaL_checkinteger(L, 4);
     if (lua_gettop(L) > 4)
-        color = (uint32_t)luaL_checkinteger(L, 5);
+        color = (luat_color_t)luaL_checkinteger(L, 5);
     int ret = luat_lcd_draw_line(default_conf, x1,  y1,  x2,  y2, color);
     lua_pushboolean(L, ret == 0 ? 1 : 0);
     return 1;
@@ -524,13 +524,13 @@ lcd.drawRectangle(20,40,220,80,0x001F)
 */
 static int l_lcd_draw_rectangle(lua_State* L) {
     uint16_t x1, y1, x2, y2;
-    uint32_t color = FORE_COLOR;
+    luat_color_t color = FORE_COLOR;
     x1 = luaL_checkinteger(L, 1);
     y1 = luaL_checkinteger(L, 2);
     x2 = luaL_checkinteger(L, 3);
     y2 = luaL_checkinteger(L, 4);
     if (lua_gettop(L) > 4)
-        color = (uint32_t)luaL_checkinteger(L, 5);
+        color = (luat_color_t)luaL_checkinteger(L, 5);
     int ret = luat_lcd_draw_rectangle(default_conf, x1,  y1,  x2,  y2, color);
     lua_pushboolean(L, ret == 0 ? 1 : 0);
     return 1;
@@ -548,12 +548,12 @@ lcd.drawCircle(120,120,20,0x001F)
 */
 static int l_lcd_draw_circle(lua_State* L) {
     uint16_t x0, y0, r;
-    uint32_t color = FORE_COLOR;
+    luat_color_t color = FORE_COLOR;
     x0 = luaL_checkinteger(L, 1);
     y0 = luaL_checkinteger(L, 2);
     r = luaL_checkinteger(L, 3);
     if (lua_gettop(L) > 3)
-        color = (uint32_t)luaL_checkinteger(L, 4);
+        color = (luat_color_t)luaL_checkinteger(L, 4);
     int ret = luat_lcd_draw_circle(default_conf, x0,  y0,  r, color);
     lua_pushboolean(L, ret == 0 ? 1 : 0);
     return 1;
@@ -829,7 +829,7 @@ static int l_lcd_draw_str(lua_State* L) {
     x = luaL_checkinteger(L, 1);
     y = luaL_checkinteger(L, 2);
     data = (const uint8_t*)luaL_checklstring(L, 3, &sz);
-    lcd_str_fg_color = (uint32_t)luaL_optinteger(L, 4,FORE_COLOR);
+    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;
@@ -924,8 +924,8 @@ static int l_lcd_draw_gtfont_gb2312_gray(lua_State* L) {
 	int i = 0;
 	uint8_t strhigh,strlow ;
 	uint16_t str;
-    const char *fontCode = luaL_checklstring(L, 1,&len);
-    unsigned char size = luaL_checkinteger(L, 2);
+  const char *fontCode = luaL_checklstring(L, 1,&len);
+  unsigned char size = luaL_checkinteger(L, 2);
 	unsigned char font_g = luaL_checkinteger(L, 3);
 	int x = luaL_checkinteger(L, 4);
 	int y = luaL_checkinteger(L, 5);
@@ -1066,6 +1066,7 @@ static int l_lcd_get_size(lua_State *L) {
     if (conf) {
       lua_pushinteger(L, conf->w);
       lua_pushinteger(L, conf->h);
+      return 2;
     }
   }
   if (default_conf == NULL) {

+ 19 - 46
components/lvgl/sdl2/lv_sdl_drv_display.c

@@ -31,6 +31,7 @@
 
 static lv_disp_buf_t disp_buf;
 static lv_color_t *pixels;
+static uint32_t *fb;
 #ifdef NXDK
 #include <hal/video.h>
 void *framebuffer;
@@ -46,34 +47,30 @@ static void sdl_fb_flush(lv_disp_drv_t *disp_drv,
                          const lv_area_t *area,
                          lv_color_t *color_p)
 {
-
+    //printf("disp flush %d %d %d %d\n", area->x1, area->y1, area->x2, area->y2);
     if(area->x2 < 0 || area->y2 < 0 ||
        area->x1 > disp_drv->hor_res - 1 || area->y1 > disp_drv->ver_res - 1) {
         lv_disp_flush_ready(disp_drv);
         return;
     }
-    #ifdef NXDK
-    static uint8_t pitch = (LV_COLOR_DEPTH + 7) / 8;
-    for(uint32_t y = area->y1; y <= area->y2; y++)
-    {
-        uint32_t line_start = y * disp_drv->hor_res * pitch;
-        for(uint32_t x = area->x1; x <= area->x2; x++)
-        {
-            lv_color_t *pixel = framebuffer + line_start + x * pitch;
-            *pixel = *color_p;
-            color_p++;
-        }
-    }
-    #else
     SDL_Rect r;
     r.x = area->x1;
     r.y = area->y1;
     r.w = area->x2 - area->x1 + 1;
     r.h = area->y2 - area->y1 + 1;
 
-    SDL_UpdateTexture(framebuffer, &r, color_p, r.w * ((LV_COLOR_DEPTH + 7) / 8));
-    #endif
+    uint32_t *tmp = fb;
+    for (size_t i = 0; i < disp_drv->hor_res; i++)
+    {
+        for (size_t j = 0; j < disp_drv->ver_res; j++)
+        {
+            *tmp = lv_color_to32(*color_p);
+            tmp ++;
+            color_p ++;
+        }
+    }
 
+    SDL_UpdateTexture(framebuffer, &r, fb, r.w * 4);
     lv_disp_flush_ready(disp_drv);
 }
 
@@ -102,9 +99,11 @@ lv_disp_t *lv_sdl_init_display(const char *win_name, int width, int height)
         width = LV_HOR_RES_MAX;
     if (height > LV_VER_RES_MAX)
         height = LV_VER_RES_MAX;
-    pixels = malloc(sizeof(lv_color_t) * width * height);
+    size_t buff_line = 16;
+    pixels = malloc(sizeof(lv_color_t) * width * buff_line);
+    fb = malloc(sizeof(uint32_t) * width * height);
     //printf("pixels %p\n", pixels);
-    lv_disp_buf_init(&disp_buf, pixels, NULL, width * height);
+    lv_disp_buf_init(&disp_buf, pixels, NULL, width * buff_line);
     lv_disp_drv_t disp_drv;
     lv_disp_drv_init(&disp_drv);
     disp_drv.buffer = &disp_buf;
@@ -113,18 +112,6 @@ lv_disp_t *lv_sdl_init_display(const char *win_name, int width, int height)
     disp_drv.hor_res = width;
     disp_drv.ver_res = height;
 
-    #ifdef NXDK
-    #define PCRTC_START 0x00600800
-    framebuffer = MmAllocateContiguousMemoryEx(LV_HOR_RES_MAX * LV_VER_RES_MAX * ((LV_COLOR_DEPTH + 7) / 8),
-                                               0x00000000, 0x7FFFFFFF,
-                                               0x1000,
-                                               PAGE_READWRITE |
-                                               PAGE_WRITECOMBINE);
-    assert (framebuffer != NULL);
-    RtlZeroMemory(framebuffer, LV_HOR_RES_MAX * LV_VER_RES_MAX * ((LV_COLOR_DEPTH + 7) / 8));
-    XVideoFlushFB();
-    VIDEOREG(PCRTC_START) = (unsigned int)MmGetPhysicalAddress(framebuffer);
-    #else
     if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0)
         printf("SDL_InitSubSystem failed: %s\n", SDL_GetError());
 
@@ -132,28 +119,14 @@ lv_disp_t *lv_sdl_init_display(const char *win_name, int width, int height)
                               SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                               width, height, 0);
 
-    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
-
-#if (LV_COLOR_DEPTH == 16)
-    #if LV_COLOR_16_SWAP
-    printf("SDL_PIXELFORMAT_BGR565\n");
-    Uint32 color_depth = SDL_PIXELFORMAT_BGR565;
-    #else
-    printf("SDL_PIXELFORMAT_RGB565\n");
-    Uint32 color_depth = SDL_PIXELFORMAT_RGB565;
-    #endif
-#else
-    printf("SDL_PIXELFORMAT_ARGB8888\n");
-    Uint32 color_depth = SDL_PIXELFORMAT_ARGB8888;
-#endif
+    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
     framebuffer = SDL_CreateTexture(renderer,
-                                    color_depth,
+                                    SDL_PIXELFORMAT_ARGB8888,
                                     SDL_TEXTUREACCESS_STREAMING,
                                     width,
                                     height);
 
     sdl_present_task = lv_task_create(sdl_present, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_HIGHEST, NULL);
-    #endif
 
     return lv_disp_drv_register(&disp_drv);
 }

+ 6 - 5
demo/lvgl/win32/lvgl_demo/button_counter/main.lua

@@ -6,7 +6,7 @@
 @date    2021.07.12
 ]]
 
-local sys = require "sys"
+_G.sys = require "sys"
 
 log.info("sys", "from win32")
 
@@ -16,7 +16,7 @@ local counter = 0
 local function screen_plusevent_handler(obj, event)
 	if event == lvgl.EVENT_CLICKED then
 		counter = counter+1
-		lvgl.obj_set_style_local_text_font(ui.screen_counter, lvgl.LABEL_PART_MAIN, lvgl.STATE_DEFAULT, lvgl.font_get("simsun_48"))
+		lvgl.obj_set_style_local_text_font(ui.screen_counter, lvgl.LABEL_PART_MAIN, lvgl.STATE_DEFAULT, lvgl.font_get("opposans_m_16"))
 		lvgl.label_set_text(ui.screen_counter, string.format("%d",counter))
 	end
 end
@@ -24,7 +24,7 @@ end
 local function screen_minusevent_handler(obj, event)
 	if event == lvgl.EVENT_CLICKED then
 		if counter>0 then counter = counter-1 end
-		lvgl.obj_set_style_local_text_font(ui.screen_counter, lvgl.LABEL_PART_MAIN, lvgl.STATE_DEFAULT, lvgl.font_get("simsun_48"))
+		lvgl.obj_set_style_local_text_font(ui.screen_counter, lvgl.LABEL_PART_MAIN, lvgl.STATE_DEFAULT, lvgl.font_get("opposans_m_16"))
 		lvgl.label_set_text(ui.screen_counter, string.format("%d",counter))
 	end
 end
@@ -55,7 +55,7 @@ sys.taskInit(function ()
 	lvgl.style_set_bg_grad_dir(style_screen_counter_main, lvgl.STATE_DEFAULT, lvgl.GRAD_DIR_VER)
 	lvgl.style_set_bg_opa(style_screen_counter_main, lvgl.STATE_DEFAULT, 0)
 	lvgl.style_set_text_color(style_screen_counter_main, lvgl.STATE_DEFAULT, lvgl.color_make(0x00, 0x00, 0x00))
-	lvgl.style_set_text_font(style_screen_counter_main, lvgl.STATE_DEFAULT, lvgl.font_get("simsun_48"))
+	lvgl.style_set_text_font(style_screen_counter_main, lvgl.STATE_DEFAULT, lvgl.font_get("opposans_m_16"))
 	lvgl.style_set_text_letter_space(style_screen_counter_main, lvgl.STATE_DEFAULT, 2)
 	lvgl.style_set_pad_left(style_screen_counter_main, lvgl.STATE_DEFAULT, 0)
 	lvgl.style_set_pad_right(style_screen_counter_main, lvgl.STATE_DEFAULT, 0)
@@ -198,7 +198,8 @@ sys.taskInit(function ()
     lvgl.scr_load(ui.screen)
 
     while true do
-        sys.wait(500)
+        sys.wait(1000)
+		log.info("main", os.date())
     end
 end)
 

+ 8 - 0
script/libs/sys/package.json

@@ -0,0 +1,8 @@
+{
+    "type" : "lua",
+    "enterpoint" : "sys.lua",
+    "compatibility" : {
+        "soc" : true
+    },
+    "dependency" : []
+}

+ 18 - 1
bsp/win32/lib/sys.lua → script/libs/sys/sys.lua

@@ -1,5 +1,22 @@
 --- 模块功能:Luat协程调度框架
---module(..., package.seeall)
+--[[
+@module sys
+@summary LuaTask核心逻辑
+@version 1.0
+@date    2018.01.01
+@author  稀饭/wendal/晨旭
+@usage
+-- sys一般会内嵌到固件内, 不需要手工添加到脚本列表,除非你正在修改sys.lua
+-- 本文件修改后, 需要调用 update_inline_sys.lua/update_luadb_inline 对 vfs 中的C文件进行更新
+_G.sys = require("sys")
+
+sys.taskInit(function()
+    sys.wait(1000)
+    log.info("sys", "say hi")
+end)
+
+sys.run()
+]]
 
 local sys = {}