Dozingfiretruck 1 an în urmă
părinte
comite
ead60b4098
4 a modificat fișierele cu 108 adăugiri și 64 ștergeri
  1. 75 35
      components/tp/luat_lib_tp.c
  2. 19 15
      components/tp/luat_tp.c
  3. 5 0
      components/tp/luat_tp.h
  4. 9 14
      components/tp/luat_tp_gt911.c

+ 75 - 35
components/tp/luat_lib_tp.c

@@ -1,5 +1,6 @@
 #include "luat_base.h"
 #include "luat_tp.h"
+#include "luat_msgbus.h"
 
 #define LUAT_LOG_TAG "tp"
 #include "luat_log.h"
@@ -11,44 +12,90 @@
 typedef struct tp_reg {
     const char *name;
     const luat_tp_config_t *luat_tp_config;
-  }tp_reg_t;
+}tp_reg_t;
 
 static const tp_reg_t tp_regs[] = {
     {"gt911",  &tp_config_gt911},
     {"", NULL}
-  };
+};
+
+static int l_tp_handler(lua_State* L, void* ptr) {
+    rtos_msg_t *msg = (rtos_msg_t *)lua_topointer(L, -1);
+    luat_tp_config_t* luat_tp_config = msg->ptr;
+    luat_tp_data_t* luat_tp_data = msg->arg1;
+
+    if (luat_tp_config->luat_cb) {
+        lua_geti(L, LUA_REGISTRYINDEX, luat_tp_config->luat_cb);
+        if (lua_isfunction(L, -1)) {
+            lua_pushlightuserdata(L, luat_tp_config);
+            lua_newtable(L);
+            lua_pushstring(L, "event");
+            lua_pushinteger(L, luat_tp_data->event);
+            lua_settable(L, -3);
+            lua_pushstring(L, "track_id");
+            lua_pushinteger(L, luat_tp_data->track_id);
+            lua_settable(L, -3);
+            lua_pushstring(L, "x");
+            lua_pushinteger(L, luat_tp_data->x_coordinate);
+            lua_settable(L, -3);
+            lua_pushstring(L, "y");
+            lua_pushinteger(L, luat_tp_data->y_coordinate);
+            lua_settable(L, -3);
+            lua_pushstring(L, "width");
+            lua_pushinteger(L, luat_tp_data->width);
+            lua_settable(L, -3);
+            lua_pushstring(L, "timestamp");
+            lua_pushinteger(L, luat_tp_data->timestamp);
+            lua_settable(L, -3);
+            lua_call(L, 2, 0);
+        }
+    }
+    return 0;
+}
+
+int l_tp_callback(luat_tp_config_t* luat_tp_config, luat_tp_data_t* luat_tp_data){
+    rtos_msg_t msg = {.handler = l_tp_handler, .ptr=luat_tp_config, .arg1=luat_tp_data};
+    luat_msgbus_put(&msg, 1);
+    return 0;
+}
 
 static int l_tp_init(lua_State* L){
     int ret;
     size_t len = 0;
-    luat_tp_config_t *luat_tp = (luat_tp_config_t *)lua_newuserdata(L, sizeof(luat_tp_config_t));
-    if (luat_tp == NULL) {
+    luat_tp_config_t *luat_tp_config = (luat_tp_config_t *)lua_newuserdata(L, sizeof(luat_tp_config_t));
+    if (luat_tp_config == NULL) {
         LLOGE("out of system memory!!!");
         return 0;
     }
-    memset(luat_tp, 0x00, sizeof(luat_tp_config_t));
-    const char* tp_name = luaL_checklstring(L, 1, &len);
+    memset(luat_tp_config, 0x00, sizeof(luat_tp_config_t));
+    luat_tp_config->callback = l_tp_callback;
 
+    const char* tp_name = luaL_checklstring(L, 1, &len);
     for(int i = 0; i < 100; i++){
         if (strlen(tp_regs[i].name) == 0)
           break;
         if(strcmp(tp_regs[i].name,tp_name) == 0){
-            luat_tp->opts = tp_regs[i].luat_tp_config;
+            luat_tp_config->opts = tp_regs[i].luat_tp_config;
             break;
         }
     }
-
-    if (luat_tp->opts == NULL){
+    if (luat_tp_config->opts == NULL){
         LLOGE("tp_name:%s not found!!!", tp_name);
         return 0;
     }
+
+	if (lua_isfunction(L, 3)) {
+		lua_pushvalue(L, 3);
+		luat_tp_config->luat_cb = luaL_ref(L, LUA_REGISTRYINDEX);
+	}
+
     lua_settop(L, 2);
     lua_pushstring(L, "port");
     int port = lua_gettable(L, 2);
     if (LUA_TNUMBER == port) {
-        luat_tp->i2c_id = luaL_checkinteger(L, -1);
+        luat_tp_config->i2c_id = luaL_checkinteger(L, -1);
     }else if(LUA_TUSERDATA == port){
-        luat_tp->soft_i2c = (luat_ei2c_t*)lua_touserdata(L, -1);
+        luat_tp_config->soft_i2c = (luat_ei2c_t*)lua_touserdata(L, -1);
     }else{
         LLOGE("port type error!!!");
         return 0;
@@ -57,58 +104,51 @@ static int l_tp_init(lua_State* L){
 
     lua_pushstring(L, "pin_rst");
     if (LUA_TNUMBER == lua_gettable(L, 2)) {
-        luat_tp->pin_rst = luaL_checkinteger(L, -1);
+        luat_tp_config->pin_rst = luaL_checkinteger(L, -1);
     }
     lua_pop(L, 1);
     lua_pushstring(L, "pin_int");
     if (LUA_TNUMBER == lua_gettable(L, 2)) {
-        luat_tp->pin_int = luaL_checkinteger(L, -1);
+        luat_tp_config->pin_int = luaL_checkinteger(L, -1);
     }
     lua_pop(L, 1);
     lua_pushstring(L, "w");
     if (LUA_TNUMBER == lua_gettable(L, 2)) {
-        luat_tp->w = luaL_checkinteger(L, -1);
+        luat_tp_config->w = luaL_checkinteger(L, -1);
     }
     lua_pop(L, 1);
     lua_pushstring(L, "h");
     if (LUA_TNUMBER == lua_gettable(L, 2)) {
-        luat_tp->h = luaL_checkinteger(L, -1);
+        luat_tp_config->h = luaL_checkinteger(L, -1);
     }
     lua_pop(L, 1);
     lua_pushstring(L, "refresh_rate");
     if (LUA_TNUMBER == lua_gettable(L, 2)) {
-        luat_tp->refresh_rate = luaL_checkinteger(L, -1);
+        luat_tp_config->refresh_rate = luaL_checkinteger(L, -1);
     }
     lua_pop(L, 1);
     lua_pushstring(L, "int_type");
     if (LUA_TNUMBER == lua_gettable(L, 2)) {
-        luat_tp->int_type = luaL_checkinteger(L, -1);
+        luat_tp_config->int_type = luaL_checkinteger(L, -1);
     }
     lua_pop(L, 1);
     lua_pushstring(L, "tp_num");
     if (LUA_TNUMBER == lua_gettable(L, 2)) {
-        luat_tp->tp_num = luaL_checkinteger(L, -1);
+        luat_tp_config->tp_num = luaL_checkinteger(L, -1);
     }
     lua_pop(L, 1);
 
+    // luat_tp_config->luat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
 
-    // luat_tp->i2c_id = 0,
-    // luat_tp->pin_rst = gpio_rst,
-    // luat_tp->pin_int = gpio_int,
-    // luat_tp->soft_i2c = &soft_i2c,
-    // luat_tp->w = 320,
-    // luat_tp->h = 480,
-    // luat_tp->refresh_rate = 20,
-    // luat_tp->int_type = TP_INT_TYPE_FALLING_EDGE,
-    // luat_tp->tp_num = 1,
-    // luat_tp->opts = &tp_config_gt911;
-
-    // LLOGD("l_tp_init luat_tp:%p ",luat_tp);
-
-
-    ret = luat_tp_init(luat_tp);
-    lua_pushboolean(L, ret == 0 ? 1 : 0);
-    return 1;
+    ret = luat_tp_init(luat_tp_config);
+    if (ret){
+        // luat_tp_deinit(luat_tp_config);
+        // luaL_unref(L, LUA_REGISTRYINDEX, luat_tp_config->luat_ref);
+        return 0;
+    }else{
+        lua_pushlightuserdata(L, luat_tp_config);
+        return 1;
+    }
 }
 
 

+ 19 - 15
components/tp/luat_tp.c

@@ -1,6 +1,7 @@
 #include "luat_base.h"
 #include "luat_tp.h"
 #include "luat_mem.h"
+#include "luat_gpio.h"
 
 #define LUAT_LOG_TAG "tp"
 #include "luat_log.h"
@@ -10,31 +11,34 @@ luat_rtos_task_handle tp_task_handle = NULL;
 
 void luat_tp_task_entry(void* param){
     uint32_t message_id = 0;
-    luat_tp_config_t *luat_tp_config;
-    luat_tp_data_t tp_data[10];
+    luat_tp_config_t *luat_tp_config = NULL;
     while (1){
         luat_rtos_message_recv(tp_task_handle, &message_id, &luat_tp_config, LUAT_WAIT_FOREVER);
+        luat_tp_data_t* tp_data = luat_tp_config->tp_data;
         uint8_t touch_num = luat_tp_config->opts->read(luat_tp_config,tp_data);
-        for (uint8_t i=0; i<10; i++){
-            if ((TP_EVENT_TYPE_DOWN == tp_data[i].event) || (TP_EVENT_TYPE_UP == tp_data[i].event) || (TP_EVENT_TYPE_MOVE == tp_data[i].event)){
-                LLOGD("event=%d, track_id=%d, x=%d, y=%d, s=%d, timestamp=%u.\r\n", 
-                            tp_data[i].event,
-                            tp_data[i].track_id,
-                            tp_data[i].x_coordinate,
-                            tp_data[i].y_coordinate,
-                            tp_data[i].width,
-                            tp_data[i].timestamp);
-            }
-            if (luat_tp_config->callback){
-                luat_tp_config->callback(luat_tp_config,&tp_data[i]);
+        if (touch_num){
+            for (uint8_t i=0; i<LUAT_TP_TOUCH_MAX; i++){
+                if ((TP_EVENT_TYPE_DOWN == tp_data[i].event) || (TP_EVENT_TYPE_UP == tp_data[i].event) || (TP_EVENT_TYPE_MOVE == tp_data[i].event)){
+                //     LLOGD("event=%d, track_id=%d, x=%d, y=%d, s=%d, timestamp=%u.\r\n", 
+                //                 tp_data[i].event,
+                //                 tp_data[i].track_id,
+                //                 tp_data[i].x_coordinate,
+                //                 tp_data[i].y_coordinate,
+                //                 tp_data[i].width,
+                //                 tp_data[i].timestamp);
+                    if (luat_tp_config->callback){
+                        luat_tp_config->callback(luat_tp_config,&tp_data[i]);
+                    }
+                }
             }
         }
+        luat_gpio_irq_enable(luat_tp_config->pin_int, 1);
     }
 }
 
 int luat_tp_init(luat_tp_config_t* luat_tp_config){
     if (tp_task_handle == 0){
-        luat_rtos_task_create(&tp_task_handle, 2048, 50, "tp", luat_tp_task_entry, NULL, 10);
+        luat_rtos_task_create(&tp_task_handle, 4096, 50, "tp", luat_tp_task_entry, NULL, 10);
     }
     tp_config_gt911.init(luat_tp_config);
 

+ 5 - 0
components/tp/luat_tp.h

@@ -5,6 +5,8 @@
 #include "luat_rtos.h"
 #include "luat_i2c.h"
 
+#define LUAT_TP_TOUCH_MAX 10
+
 extern luat_rtos_task_handle tp_task_handle;
 
 typedef struct luat_tp_config luat_tp_config_t;
@@ -49,8 +51,11 @@ typedef struct luat_tp_config{
     uint8_t int_type;
     int16_t w;
     int16_t h;
+    int luat_ref;
+    void* luat_cb;
     luat_tp_opts_t* opts;
     int (*callback)(luat_tp_config_t* luat_tp_config, luat_tp_data_t* luat_tp_data);
+    luat_tp_data_t tp_data[LUAT_TP_TOUCH_MAX];
 } luat_tp_config_t;
 
 typedef struct luat_tp_opts {

+ 9 - 14
components/tp/luat_tp_gt911.c

@@ -161,7 +161,7 @@ int tp_gt911_read_status(luat_tp_config_t* luat_tp_config, uint8_t *status){
 		LLOGE("read status reg fail!\r\n");
 		return -1;
 	}
-	LLOGD("status=0x%02X\r\n", *status); // 调试需要看!!!
+	// LLOGD("status=0x%02X\r\n", *status); // 调试需要看!!!
 	return 0;
 }
 
@@ -369,8 +369,7 @@ void gt911_touch_down(void *buf, int8_t id, int16_t x, int16_t y, int16_t w){
 	pre_w[id] = w;
 }
 
-void gt911_read_point(uint8_t *input_buff, void *buf, uint8_t num){
-	uint8_t touch_num = 0;
+void gt911_read_point(uint8_t *input_buff, void *buf, uint8_t touch_num){
 	uint8_t *read_buf = input_buff;
 	uint8_t read_index;
 	int8_t read_id = 0;
@@ -381,8 +380,6 @@ void gt911_read_point(uint8_t *input_buff, void *buf, uint8_t num){
 	static uint8_t pre_touch = 0;
 	static int8_t pre_id[GT911_TOUCH_NUMBER_MAX] = {0};
 
-	touch_num = num;
-
 	if (pre_touch > touch_num){                                       /* point up */
 		for (read_index = 0; read_index < pre_touch; read_index++){
 			uint8_t j;
@@ -440,19 +437,18 @@ void gt911_read_point(uint8_t *input_buff, void *buf, uint8_t num){
 
 static int tp_gt911_read(luat_tp_config_t* luat_tp_config, luat_tp_data_t *luat_tp_data){
     static uint8_t pre_touch = 0;
-    uint8_t read_num = 0, touch_num=0, point_status=0;
-    luat_tp_info_t luat_touch_info = {0};
-    tp_gt911_get_info(luat_tp_config, &luat_touch_info);
-    read_num = luat_touch_info.touch_num;
-    LLOGD("tp_gt911_read read_num:%d",read_num);
+    uint8_t touch_num=0, point_status=0;
+
+    // luat_tp_info_t luat_touch_info = {0};
+    // tp_gt911_get_info(luat_tp_config, &luat_touch_info);
+    // uint8_t read_num = luat_touch_info.touch_num;
+    // LLOGD("tp_gt911_read read_num:%d",read_num);
 
     tp_gt911_read_status(luat_tp_config, &point_status);
     if (point_status == 0){           /* no data */
-        read_num = 0;
         goto exit_;
     }
     if ((point_status & 0x80) == 0){  /* data is not ready */
-        read_num = 0;
         goto exit_;
     }
     touch_num = point_status & 0x0F;  /* get point num */
@@ -461,7 +457,7 @@ static int tp_gt911_read(luat_tp_config_t* luat_tp_config, luat_tp_data_t *luat_
         goto exit_;
     }
     
-    LLOGD("tp_gt911_read touch_num:%d",touch_num);
+    // LLOGD("tp_gt911_read touch_num:%d",touch_num);
 
     if (touch_num){
         memset(read_buff, 0x00, sizeof(read_buff));
@@ -474,7 +470,6 @@ static int tp_gt911_read(luat_tp_config_t* luat_tp_config, luat_tp_data_t *luat_
     
 exit_:
     tp_gt911_clear_status(luat_tp_config);
-    luat_gpio_irq_enable(luat_tp_config->pin_int, 1);
     return touch_num;
 }