Kaynağa Gözat

add:使异步eink可用

Dozingfiretruck 3 yıl önce
ebeveyn
işleme
64f9ed4bd0

+ 62 - 49
components/eink/luat_lib_eink.c

@@ -69,7 +69,8 @@ static int l_eink_init(lua_State* L) {
         econf.port = LUAT_EINK_SPI_DEVICE;
     }
     if (econf.async){
-      econf.idp = luat_pushcwait(L);
+      luat_rtos_task_create(&econf.eink_task_handle, 1024, 80, "eink", EPD_Task, NULL, 0);
+      luat_rtos_queue_create(&econf.eink_queue_handle, 5, sizeof(uint8_t));
     }
     EPD_Model(luaL_checkinteger(L, 1));
 
@@ -162,17 +163,11 @@ static int l_eink_init(lua_State* L) {
     u8g2_SetFont(&(econf.luat_eink_u8g2), u8g2_font_opposansm8);
     u8g2_SetFontMode(&(econf.luat_eink_u8g2), 0);
     u8g2_SetFontDirection(&(econf.luat_eink_u8g2), 0);
-    if (!econf.async){
-      lua_pushboolean(L, 1);
-    }
+    lua_pushboolean(L, 1);
     return 1;
 
 end:
-    if (econf.async){
-      luat_pushcwait_error(L,1);
-    }else{
-      lua_pushboolean(L, 0);
-    }
+    lua_pushboolean(L, 0);
     return 1;
 }
 
@@ -220,9 +215,7 @@ static int l_eink_setup(lua_State *L) {
     size_t epd_w = 0;
     size_t epd_h = 0;
     size_t colors = 0;
-    if (econf.async){
-      econf.idp = luat_pushcwait(L);
-    }
+
     if(status == 0)
     {
         if(econf.full_mode)
@@ -262,9 +255,8 @@ static int l_eink_setup(lua_State *L) {
     u8g2_SetFontDirection(&(econf.luat_eink_u8g2), 0);
     //paint.inited = 1;
     //LLOGD("epd init complete");
-    if (!econf.async){
-      lua_pushboolean(L, 1);
-    }
+    lua_pushboolean(L, 1);
+
     return 1;
 }
 
@@ -274,14 +266,8 @@ static int l_eink_setup(lua_State *L) {
 */
 static int l_eink_sleep(lua_State *L)
 {
-    if (econf.async){
-      econf.idp = luat_pushcwait(L);
-    }
     EPD_Sleep();
     lua_pushboolean(L, 1);
-    if (!econf.async){
-      lua_pushboolean(L, 1);
-    }
     return 1;
 }
 
@@ -295,15 +281,26 @@ static int l_eink_sleep(lua_State *L)
 static int l_eink_clear(lua_State *L)
 {
     int colored = luaL_optinteger(L, 1, 1);
-    if (check_init() == 0)
-        return 0;
+    int no_clear = lua_toboolean(L, 2);
     if (econf.async){
       econf.idp = luat_pushcwait(L);
-    }
-    Paint_Clear(&econf.ctxs[econf.ctx_index]->paint, colored);
-    if(lua_toboolean(L, 2))
-      EPD_Clear();
-    if (!econf.async){
+      if (check_init() == 0) {
+        lua_pushinteger(L,0);
+	      luat_pushcwait_error(L,1);
+        return 1;
+      }
+      Paint_Clear(&econf.ctxs[econf.ctx_index]->paint, colored);
+      if (!no_clear){
+        uint8_t event = EPD_CLEAR;
+        luat_rtos_queue_send(econf.eink_queue_handle, &event, sizeof(uint8_t), 0);
+      }
+    }else{
+      if (check_init() == 0) {
+        return 0;
+      }
+      Paint_Clear(&econf.ctxs[econf.ctx_index]->paint, colored);
+      if(!no_clear)
+        EPD_Clear();
       lua_pushboolean(L, 1);
     }
     return 1;
@@ -654,20 +651,28 @@ static int l_eink_show(lua_State *L)
     //EPD_SetFrameMemory(&epd, frame_buffer, x, y, Paint_GetWidth(&econf.ctxs[econf.ctx_index]->paint), Paint_GetHeight(&econf.ctxs[econf.ctx_index]->paint));
     //EPD_DisplayFrame(&epd);
 
-    if (check_init() == 0) {
-      return 0;
-    }
     if (econf.async){
       econf.idp = luat_pushcwait(L);
-    }
-    if(!no_clear)
-      EPD_Clear();
-    if (econf.ctxs[1] == NULL)
-      EPD_Display(econf.ctxs[0]->fb, NULL);
-    else
-      EPD_Display(econf.ctxs[0]->fb, econf.ctxs[1]->fb);
-    lua_pushboolean(L, 1);
-    if (!econf.async){
+      if (check_init() == 0) {
+        lua_pushinteger(L,0);
+	      luat_pushcwait_error(L,1);
+        return 1;
+      }
+      uint8_t event = EPD_SHOW;
+      if (!no_clear){
+        event |= EPD_CLEAR;
+      }
+      luat_rtos_queue_send(econf.eink_queue_handle, &event, sizeof(uint8_t), 0);
+    }else{
+      if (check_init() == 0) {
+        return 0;
+      }
+      if(!no_clear)
+        EPD_Clear();
+      if (econf.ctxs[1] == NULL)
+        EPD_Display(econf.ctxs[0]->fb, NULL);
+      else
+        EPD_Display(econf.ctxs[0]->fb, econf.ctxs[1]->fb);
       lua_pushboolean(L, 1);
     }
     return 1;
@@ -691,15 +696,23 @@ static int l_eink_draw(lua_State *L)
     }
     if (econf.async){
       econf.idp = luat_pushcwait(L);
-    }
-    if (check_init() == 0) {
-      return 0;
-    }
-    if(!no_clear)
-      EPD_Clear();
-    EPD_Display(buff->addr, buff2->addr);
-    lua_pushboolean(L, 1);
-    if (!econf.async){
+      if (check_init() == 0) {
+        lua_pushinteger(L,0);
+	      luat_pushcwait_error(L,1);
+        return 1;
+      }
+      uint8_t event = EPD_DRAW;
+      if (!no_clear){
+        event |= EPD_CLEAR;
+      }
+      luat_rtos_queue_send(econf.eink_queue_handle, &event, sizeof(uint8_t), 0);
+    }else{
+      if (check_init() == 0) {
+        return 0;
+      }
+      if(!no_clear)
+        EPD_Clear();
+      EPD_Display(buff->addr, buff2->addr);
       lua_pushboolean(L, 1);
     }
     return 1;

+ 17 - 63
components/epaper/DEV_Config.c

@@ -30,7 +30,7 @@
 #
 ******************************************************************************/
 #include "DEV_Config.h"
-#include "luat_rtos.h"
+
 
 void DEV_SPI_WriteByte(UBYTE value)
 {
@@ -49,75 +49,29 @@ int DEV_Digital_Write(int pin, int level){
     return luat_gpio_set(pin, level);
 }
 
-static int32_t l_eink_callback(lua_State *L, void* ptr){
-    rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
-    luat_release_rtos_timer(econf.readbusy_timer);
-    luat_cbcwait(L, econf.idp, 1);
-    return 0;
-}
-
-static LUAT_RT_RET_TYPE readbusy_timer_cb(LUAT_RT_CB_PARAM){
-    rtos_msg_t msg = {
-		.handler = l_eink_callback,
-	};
-    if (econf.timer_count++ > 300){//30s
-        luat_cbcwait_noarg(econf.idp);
-        luat_stop_rtos_timer(econf.readbusy_timer);
-        luat_msgbus_put(&msg, 0);
-        return;
-    }
-    eink_async_t* async_cmd = (eink_async_t *)param;
-    if (async_cmd->level){
-        if(DEV_Digital_Read(EPD_BUSY_PIN)) {
-            if (async_cmd->send_cmd){
+void EPD_Busy_WaitUntil(uint8_t level,uint8_t send_cmd){
+    uint16_t count = 300;//30s
+    while(1){
+        if (level){
+            if (send_cmd){
                 DEV_Digital_Write(EPD_DC_PIN, 0);
                 DEV_Digital_Write(EPD_CS_PIN, 0);
                 DEV_SPI_WriteByte(0x71);
                 DEV_Digital_Write(EPD_CS_PIN, 1);
             }
-            luat_cbcwait_noarg(econf.idp);
-            luat_stop_rtos_timer(econf.readbusy_timer);
-            luat_msgbus_put(&msg, 0);
-        }
-    }else{
-        if(DEV_Digital_Read(EPD_BUSY_PIN)==0) {
-            luat_cbcwait_noarg(econf.idp);
-            luat_stop_rtos_timer(econf.readbusy_timer);
-            luat_msgbus_put(&msg, 0);
+            if(DEV_Digital_Read(EPD_BUSY_PIN)) 
+                break;
+        }else{
+            if(DEV_Digital_Read(EPD_BUSY_PIN)==0) 
+                break;
         }
-    }
-}
-
-void EPD_Busy_WaitUntil(uint8_t level,uint8_t send_cmd){
-    uint16_t count = 200;//20s
-    econf.async_cmd.level = level;
-    econf.async_cmd.send_cmd = send_cmd;
-    if (econf.async){
-        econf.readbusy_timer = luat_create_rtos_timer(readbusy_timer_cb, &econf.async_cmd, NULL);
-        luat_start_rtos_timer(econf.readbusy_timer, 100, 1);
-    }else{
-        while(1){
-            if (level){
-                if (send_cmd){
-                    DEV_Digital_Write(EPD_DC_PIN, 0);
-                    DEV_Digital_Write(EPD_CS_PIN, 0);
-                    DEV_SPI_WriteByte(0x71);
-                    DEV_Digital_Write(EPD_CS_PIN, 1);
-                }
-                if(DEV_Digital_Read(EPD_BUSY_PIN)) 
-                    break;
-            }else{
-                if(DEV_Digital_Read(EPD_BUSY_PIN)==0) 
-                    break;
-            }
-            if(!(count--)){
-                Debug("error: e-Paper busy timeout!!!\r\n");
-                return;
-            }
-            else
-                DEV_Delay_ms(100);
+        if(!(count--)){
+            Debug("error: e-Paper busy timeout!!!\r\n");
+            return;
         }
-        DEV_Delay_ms(100);
+        else
+            DEV_Delay_ms(100);
     }
+    DEV_Delay_ms(100);
 }
 

+ 8 - 10
components/epaper/DEV_Config.h

@@ -52,9 +52,9 @@
 #include "luat_gpio.h"
 #include "luat_spi.h"
 #include "luat_timer.h"
+#include "luat_rtos.h"
 
 #include "u8g2.h"
-
 #include "Debug.h"
 
 /**
@@ -66,18 +66,17 @@
 
 #define LUAT_EINK_SPI_DEVICE 255
 
+#define EPD_SHOW        (1<<0)
+#define EPD_DRAW        (1<<1)
+#define EPD_CLEAR       (1<<2)
+
+
 #include "epdpaint.h"
 typedef struct eink_ctx{
     uint32_t str_color;
     Paint paint;
     uint8_t fb[];
 }eink_ctx_t;
-
-typedef struct eink_async{
-    uint8_t level;
-    uint8_t send_cmd;
-}eink_async_t;
-
 typedef struct eink_conf {
     uint8_t full_mode;
     uint8_t port;
@@ -86,15 +85,14 @@ typedef struct eink_conf {
     uint8_t pin_cs;
     uint8_t pin_busy;
     uint8_t async;
-    eink_async_t async_cmd;
     uint64_t idp;
     uint32_t ctx_index;
     eink_ctx_t *ctxs[2]; // 暂时只支持2种颜色, 有需要的话后续继续
     u8g2_t luat_eink_u8g2;
     luat_spi_device_t* eink_spi_device;
     int eink_spi_ref;
-    uint8_t timer_count;
-    void* readbusy_timer;
+    luat_rtos_queue_t eink_queue_handle;
+    luat_rtos_task_handle eink_task_handle;
     void* userdata;
 }eink_conf_t;
 

+ 24 - 0
components/epaper/epd.c

@@ -98,3 +98,27 @@ void EPD_Sleep(void) {
     eink_regs[cur_model_index].sleep();
 }
 
+void EPD_Task(void *param){
+    uint8_t event;
+    while (1) {
+        luat_rtos_queue_recv(econf.eink_queue_handle, &event, sizeof(uint8_t), LUAT_WAIT_FOREVER);
+        if (event){
+            if (event & EPD_CLEAR){
+                EPD_Clear();
+            }
+            if (event & EPD_SHOW){
+                if(event & EPD_CLEAR)
+                    EPD_Clear();
+                if (econf.ctxs[1] == NULL)
+                    EPD_Display(econf.ctxs[0]->fb, NULL);
+                else
+                    EPD_Display(econf.ctxs[0]->fb, econf.ctxs[1]->fb);
+            }
+            if (event & EPD_DRAW){//初始化时候已经创建缓冲区,zbuff好像意义不大了?
+
+            }
+            luat_cbcwait_noarg(econf.idp);
+        }
+	}
+}
+

+ 1 - 0
components/epaper/epd.h

@@ -10,6 +10,7 @@ int EPD_Init(UBYTE Mode, size_t *w, size_t *h, size_t* color_count);
 void EPD_Clear(void);
 void EPD_Display(UBYTE *Image, UBYTE *Image2);
 void EPD_Sleep(void);
+void EPD_Task(void *param);
 
 enum EPD_MODEL {
         MODEL_1in02d = 1,