| 
									
										
										
										
											2025-06-27 00:32:57 +08:00
										 |  |  |  | #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; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-05 19:47:28 +08:00
										 |  |  |  | // <20><><EFBFBD><EFBFBD>lua<75>ű<EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2025-06-27 00:32:57 +08:00
										 |  |  |  | int mylua_run(const char *str) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |   int status, result; | 
					
						
							|  |  |  |  |   lua_State *L; | 
					
						
							| 
									
										
										
										
											2025-07-05 19:47:28 +08:00
										 |  |  |  |   L = luaL_newstate(); /* <20><><EFBFBD><EFBFBD>Lua<75><61><EFBFBD>л<EFBFBD><D0BB><EFBFBD> */ | 
					
						
							| 
									
										
										
										
											2025-06-27 00:32:57 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   luaL_openlibs(L);   | 
					
						
							|  |  |  |  |   mylib_init(L); | 
					
						
							| 
									
										
										
										
											2025-07-05 19:47:28 +08:00
										 |  |  |  |   luaL_dostring(L, str); /* <20><><EFBFBD><EFBFBD>Lua<75>ű<EFBFBD> */ | 
					
						
							| 
									
										
										
										
											2025-06-27 00:32:57 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   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; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-05 19:47:28 +08:00
										 |  |  |  | // <20>ű<F2BFAABD><C5B1>ļ<EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2025-06-27 00:32:57 +08:00
										 |  |  |  | int myLua_run_flie(const char *filename) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |   int status, result; | 
					
						
							|  |  |  |  |   lua_State *L; | 
					
						
							| 
									
										
										
										
											2025-07-05 19:47:28 +08:00
										 |  |  |  |   L = luaL_newstate(); /* <20><><EFBFBD><EFBFBD>Lua<75><61><EFBFBD>л<EFBFBD><D0BB><EFBFBD> */ | 
					
						
							| 
									
										
										
										
											2025-06-27 00:32:57 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   luaL_openlibs(L);   | 
					
						
							|  |  |  |  |   mylib_init(L); | 
					
						
							| 
									
										
										
										
											2025-07-05 19:47:28 +08:00
										 |  |  |  |   luaL_dofile(L, filename); /* <20><><EFBFBD><EFBFBD>Lua<75>ű<EFBFBD> */ | 
					
						
							| 
									
										
										
										
											2025-06-27 00:32:57 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   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"; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-05 19:47:28 +08:00
										 |  |  |  | // <20><><EFBFBD><EFBFBD>һ<EFBFBD>β<EFBFBD><CEB2>Խű<D4BD>
 | 
					
						
							| 
									
										
										
										
											2025-06-27 00:32:57 +08:00
										 |  |  |  | void mylua_test(void) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | //  mylua_run(g_test_str);
 | 
					
						
							|  |  |  |  |   char *argv[]={ | 
					
						
							|  |  |  |  |     "lua", | 
					
						
							|  |  |  |  |     "0:/Lua/test.lua", | 
					
						
							|  |  |  |  |   }; | 
					
						
							|  |  |  |  |   lua_main(2,argv); | 
					
						
							|  |  |  |  |   lua_assert(0); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 |