160 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			160 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include "main.h"
 | |
| #include "mylua.h"
 | |
| #include "rtthread.h"
 | |
| 
 | |
| #include "lprefix.h"
 | |
| 
 | |
| 
 | |
| #include <stdio.h>
 | |
| #include <stdlib.h>
 | |
| #include <string.h>
 | |
| 
 | |
| #include <signal.h>
 | |
| 
 | |
| #include "lua.h"
 | |
| #include "llimits.h"
 | |
| #include "lauxlib.h"
 | |
| #include "lualib.h"
 | |
| 
 | |
| 
 | |
| 
 | |
| static int lua_led_on(lua_State *L)
 | |
| {
 | |
|   LED2_ON;
 | |
|   return 0;
 | |
| }
 | |
| static int lua_led_off(lua_State *L)
 | |
| {
 | |
|   LED2_OFF;
 | |
|   return 0;
 | |
| }
 | |
|  
 | |
| static int lua_delay(lua_State *L)
 | |
| {
 | |
|   int num;
 | |
|   num= lua_tointeger(L, 1);
 | |
|   rt_thread_delay(num);
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| static const struct luaL_Reg g_mylib[]=
 | |
| {
 | |
|   {"led_on",lua_led_on},
 | |
|   {"led_off",lua_led_off},
 | |
|   {"delay",lua_delay},
 | |
|   {NULL,NULL}
 | |
| };
 | |
| 
 | |
| 
 | |
| static int mylib_init_(lua_State *L)
 | |
| {
 | |
| 	luaL_newlib(L, g_mylib);
 | |
| 	return 1;
 | |
| }
 | |
| 
 | |
| 
 | |
| void mylib_init(lua_State *L)
 | |
| {
 | |
| 	luaL_requiref(L, "mylib", mylib_init_, 1);
 | |
| 	lua_pop(L, 1);  /* remove lib */
 | |
| }
 | |
| 
 | |
| 
 | |
| /*
 | |
| ** Prints an error message, adding the program name in front of it
 | |
| ** (if present)
 | |
| */
 | |
| static void l_message (const char *pname, const char *msg) {
 | |
|   if (pname) lua_writestringerror("%s: ", pname);
 | |
|   lua_writestringerror("%s\n", msg);
 | |
| }
 | |
| 
 | |
| /*
 | |
| ** Check whether 'status' is not OK and, if so, prints the error
 | |
| ** message on the top of the stack. It assumes that the error object
 | |
| ** is a string, as it was either generated by Lua or by 'msghandler'.
 | |
| */
 | |
| static int report (lua_State *L, int status) {
 | |
|   if (status != LUA_OK) {
 | |
|     const char *msg = lua_tostring(L, -1);
 | |
|     l_message("lua", msg);
 | |
|     lua_pop(L, 1);  /* remove message */
 | |
|   }
 | |
|   return status;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| // 运行lua脚本
 | |
| int mylua_run(const char *str)
 | |
| {
 | |
|   int status, result;
 | |
|   lua_State *L;
 | |
|   L = luaL_newstate(); /* 建立Lua运行环境 */
 | |
|   
 | |
|   luaL_openlibs(L);  
 | |
|   mylib_init(L);
 | |
|   luaL_dostring(L, str); /* 运行Lua脚本 */
 | |
|   
 | |
|   status = lua_pcall(L, 2, 1, 0);  /* do the call */
 | |
|   result = lua_toboolean(L, -1);  /* get result */
 | |
|   report(L, status);
 | |
|   return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
 | |
| }
 | |
| 
 | |
| // 打开脚本文件
 | |
| int myLua_run_flie(const char *filename)
 | |
| {
 | |
|   int status, result;
 | |
|   lua_State *L;
 | |
|   L = luaL_newstate(); /* 建立Lua运行环境 */
 | |
|   
 | |
|   luaL_openlibs(L);  
 | |
|   mylib_init(L);
 | |
|   luaL_dofile(L, filename); /* 运行Lua脚本 */
 | |
|   
 | |
|   status = lua_pcall(L, 2, 1, 0);  /* do the call */
 | |
|   result = lua_toboolean(L, -1);  /* get result */
 | |
|   report(L, status);
 | |
|   return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
 | |
| }
 | |
| 
 | |
| 
 | |
| char g_test_str[] ="  \
 | |
| off = 500     \
 | |
| on = 500       \
 | |
| while true do \
 | |
|  mylib.led_on() \
 | |
|  mylib.delay(off)    \
 | |
|  mylib.led_off()        \
 | |
|  mylib.delay(on)      \
 | |
|  print(\"Hello World!\") \
 | |
| end";
 | |
| 
 | |
| 
 | |
| // 运行一段测试脚本
 | |
| void mylua_test(void)
 | |
| {
 | |
| //  mylua_run(g_test_str);
 | |
|   char *argv[]={
 | |
|     "lua",
 | |
|     "0:/Lua/test.lua",
 | |
|   };
 | |
|   lua_main(2,argv);
 | |
|   lua_assert(0);
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 |