Quellcode durchsuchen

add: 添加gpio/msgbus/sys/timer的占位文件

Wendal Chen vor 6 Jahren
Ursprung
Commit
f7beba3dbc

+ 36 - 0
luat/inculde/luat_gpio.h

@@ -0,0 +1,36 @@
+
+#ifndef LUAT_SYS
+#define LUAT_SYS
+
+
+#include "lua.h"
+#include "lualib.h"
+#include "lauxlib.h"
+#include "stdint.h"
+
+
+#define LUAT_GPIO_MODE_INPUT 0
+#define LUAT_GPIO_MODE_OUTPUT 1
+#define LUAT_GPIO_MODE_INT 2
+
+#define LUAT_GPIO_PULL_UP 0
+#define LUAT_GPIO_PULL_DOWN 1
+
+#define LUAT_GPIO_INT_UP 1
+#define LUAT_GPIO_INT_DOWN 2
+#define LUAT_GPIO_INT_BOTH 3
+
+typedef struct luat_gpio_t
+{
+    int pin;
+    int mode;
+    int pull;
+    void* callback;
+} luat_gpio_t;
+
+
+int luat_gpio_setup(luat_gpio_t gpio);
+int luat_gpio_set(luat_gpio_t gpio, int level);
+int luat_gpio_get(luat_gpio_t gpio);
+
+#endif

+ 4 - 7
luat/inculde/luat_msgbus.h

@@ -4,16 +4,13 @@
 
 #include "luat_base.h"
 
+typedef int (*luat_msg_handler) (lua_State *L, const void *ptr);
+
 typedef struct{
-    uint32_t id;
-    void* data;
+    luat_msg_handler handler;
+    void* ptr;
 }rtos_msg;
 
-// 定义msgtype
-#define LUAT_MSG_TIMER       (1)
-#define LUAT_MSG_GPIO        (2)
-#define LUAT_MSG_UART_RX     (3)
-#define LUAT_MSG_UART_TXDONE (4)
 
 // 定义接口方法
 void luat_msgbus_init(void);

+ 13 - 0
luat/inculde/luat_sys.h

@@ -0,0 +1,13 @@
+
+#ifndef LUAT_SYS
+#define LUAT_SYS
+
+
+#include "lua.h"
+#include "lualib.h"
+#include "lauxlib.h"
+#include "stdint.h"
+
+int luat_sys_mdelay(size_t ms);
+
+#endif

+ 34 - 0
luat/modules/luat_lib_gpio.c

@@ -0,0 +1,34 @@
+
+#include "luat_base.h"
+#include "luat_log.h"
+#include "luat_gpio.h"
+
+static int l_gpio_handler(lua_State *L, const void *ptr) {
+
+}
+
+static int l_gpio_setup(lua_State *L) {
+    
+    return 0;
+}
+
+static int l_gpio_set(lua_State *L) {
+    return 0;
+}
+
+static int l_gpio_get(lua_State *L) {
+    return 0;
+}
+
+static const luaL_Reg reg_gpio[] =
+{
+    { "setup" , l_gpio_setup },
+    { "set" , l_gpio_set },
+    { "get" , l_gpio_get },
+	{ NULL, NULL }
+};
+
+LUAMOD_API int luaopen_gpio( lua_State *L ) {
+    luaL_newlib(L, reg_gpio);
+    return 1;
+}

+ 22 - 0
luat/modules/luat_lib_msgbus.c

@@ -0,0 +1,22 @@
+
+#include "luat_base.h"
+#include "luat_log.h"
+#include "luat_sys.h"
+#include "luat_msgbus.h"
+
+static int l_msgbus_version(lua_State *L) {
+    lua_pushstring(L, LUAT_VERSION);
+    return 1;
+}
+
+static const luaL_Reg reg_msgbus[] =
+{
+    { "version" , l_msgbus_version },
+	{ NULL, NULL }
+};
+
+LUAMOD_API int luaopen_msgbus( lua_State *L ) {
+    luat_msgbus_init();
+    luaL_newlib(L, reg_msgbus);
+    return 1;
+}

+ 21 - 0
luat/modules/luat_lib_sys.c

@@ -1,14 +1,35 @@
 
 #include "luat_base.h"
 #include "luat_log.h"
+#include "luat_sys.h"
+#include "luat_msgbus.h"
 
 static int l_sys_run(lua_State *L) {
+    rtos_msg msg;
+    int re;
+    while (1) {
+        re = luat_msgbus_get(&msg, 0);
+        if (re == 0) {
+            msg.handler(L, msg.ptr);
+        }
+        else {
+            luat_sys_mdelay(1); // 暂缓1ms
+        }
+    }
+    return 0;
+}
+
+static int l_sys_mdelay(lua_State *L) {
+    size_t ms = luaL_checkinteger(L, 1);
+    if (ms > 0)
+        luat_sys_mdelay(ms);
     return 0;
 }
 
 static const luaL_Reg reg_sys[] =
 {
     { "run" , l_sys_run },
+    { "mdelay" , l_sys_mdelay },
 	{ NULL, NULL }
 };
 

+ 14 - 0
luat/modules/luat_lib_timer.c

@@ -2,7 +2,21 @@
 #include "luat_base.h"
 #include "luat_log.h"
 
+struct luat_lib_timer_t
+{
+    void* osTimer;
+    int type;
+    size_t timeout;
+    size_t repeat;
+};
+
+
+static int l_timer_handler(lua_State *L, const void *ptr) {
+
+}
+
 static int l_timer_start(lua_State *L) {
+    
     return 0;
 }
 

+ 15 - 3
luat/modules/luat_main.c

@@ -5,6 +5,20 @@
 //#include "luat_fs.h"
 #include "luat_log.h"
 
+static void luat_openlibs(lua_State *L) {
+    luaL_requiref(L, "msgbus", luaopen_msgbus, 1);
+    lua_pop(L, 1);
+
+    luaL_requiref(L, "sys", luaopen_sys, 1);
+    lua_pop(L, 1);
+    
+    luaL_requiref(L, "timer", luaopen_timer, 1);
+    lua_pop(L, 1);
+    
+    luaL_requiref(L, "gpio", luaopen_gpio, 1);
+    lua_pop(L, 1);
+}
+
 static int luat_app_main(lua_State *L) {
         int re = 0;
     luat_print("luat_pmain!!!\n");
@@ -12,9 +26,7 @@ static int luat_app_main(lua_State *L) {
     luaL_openlibs(L);
 
     // 加载本地库
-    //luaL_requiref(L, "rtos", luaopen_rtos, 1);
-    luaL_requiref(L, "sys", luaopen_sys, 1);
-    luaL_requiref(L, "timer", luaopen_timer, 1);
+    luat_openlibs(L);
 
     // 打印个提示
     luat_print("luat_boot_complete\n");

+ 7 - 4
luat/rtt/luat_msgbus_rtt.c

@@ -20,10 +20,12 @@ void luat_msgbus_init(void) {
                         sizeof(rtos_msg),        /* 每个消息的大小是 8 字节 */
                         sizeof(msg_pool),        /* 内存池的大小是 msg_pool 的大小 */
                         RT_IPC_FLAG_FIFO);       /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
-};
+}
+
 uint32_t luat_msgbus_put(rtos_msg* msg, size_t timeout) {
     return rt_mq_send(&mq, msg, timeout);
-};
+}
+
 uint32_t luat_msgbus_get(rtos_msg* msg, size_t timeout) {
     rt_err_t result;
     result = rt_mq_recv(&mq, msg, sizeof(rtos_msg), timeout);
@@ -33,7 +35,8 @@ uint32_t luat_msgbus_get(rtos_msg* msg, size_t timeout) {
     else {
         return result;
     }
-};
+}
+
 uint32_t luat_msgbus_freesize(void) {
-    return 1024;
+    return 1;
 }

+ 11 - 0
luat/rtt/luat_sys_rtt.c

@@ -0,0 +1,11 @@
+
+#include "luat_base.h"
+#include "luat_sys.h"
+#include "rtthread.h"
+
+int luat_sys_mdelay(size_t ms) {
+    if (ms > 0)
+        rt_thread_mdelay(ms);
+    return 0;
+}
+