Skip to content

Commit

Permalink
run lua script on main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongyihui committed Sep 18, 2015
1 parent 0adcbf8 commit 41ee9ce
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 95 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# GCC_BIN = ../LinkIt-1.1-1.60/hardware/tools/gcc-arm-none-eabi-4.8.3-2014q1/bin/
PROJECT = lua
OBJECTS = main.o syscalls_mtk.o console.o lua/lua.o lua/linenoise.o
OBJECTS += lua/lapi.o lua/lcode.o lua/ldebug.o lua/ldo.o lua/ldump.o lua/lfunc.o lua/lgc.o lua/llex.o lua/lmem.o \
OBJECTS = main.o syscalls_mtk.o console.o shell.o
OBJECTS += lua/linenoise.o lua/lapi.o lua/lcode.o lua/ldebug.o lua/ldo.o lua/ldump.o lua/lfunc.o lua/lgc.o lua/llex.o lua/lmem.o \
lua/lobject.o lua/lopcodes.o lua/lparser.o lua/lstate.o lua/lstring.o lua/ltable.o lua/ltm.o \
lua/lundump.o lua/lvm.o lua/lzio.o lua/lrotable.o \
lua/lauxlib.o lua/lbaselib.o lua/ldblib.o lua/liolib.o lua/lmathlib.o lua/loslib.o lua/ltablib.o \
Expand Down
8 changes: 5 additions & 3 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ void __console_irq_handler(void* parameter, VM_DCL_EVENT event, VM_DCL_HANDLE de
char data[SERIAL_BUFFER_SIZE];
int i;
VM_DCL_STATUS status;
VM_DCL_BUFF_LEN returned_len;
VM_DCL_BUFF_LEN returned_len = 0;

status = vm_dcl_read(device_handle,(VM_DCL_BUFF*)data,SERIAL_BUFFER_SIZE,&returned_len,vm_dcl_get_ownerid());
if(status<VM_DCL_STATUS_OK)
{
vm_log_info((char*)"read failed");
}
else
else if (returned_len)
{
vm_signal_post(console_rx_signal_id);
if (console_rx_buffer_head == console_rx_buffer_tail) {
vm_signal_post(console_rx_signal_id);
}

for (i = 0; i < returned_len; i++)
{
Expand Down
11 changes: 6 additions & 5 deletions lua/linit.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ int luaopen_dummy(lua_State *L)

static const luaL_Reg lualibs[] = {
{"", luaopen_base},
#ifdef LUA_PLATFORM_LIBS_REG
LUA_PLATFORM_LIBS_REG,
#endif
{LUA_LOADLIBNAME, luaopen_package},
{LUA_TABLIBNAME, luaopen_table},
{LUA_IOLIBNAME, luaopen_io},
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
#if defined(LUA_PLATFORM_LIBS_ROM)
LUA_PLATFORM_LIBS_ROM
#endif
Expand All @@ -132,7 +134,7 @@ static const luaL_Reg lualibs[] = {
{NULL, NULL}
};

const luaR_table lua_rotable[] =
const luaR_table lua_rotable[] =
{
#if defined(LUA_PLATFORM_LIBS_ROM) && LUA_OPTIMIZE_MEMORY == 2
#undef _ROM
Expand All @@ -151,4 +153,3 @@ LUALIB_API void luaL_openlibs (lua_State *L) {
lua_call(L, 1, 0);
}
}

12 changes: 7 additions & 5 deletions lua/luaconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#define LUA_CROSS_COMPILER
#define LUA_USE_LINENOISE

#define LUA_PATH_DEFAULT "?.lua;?.lc;C:\\?.lua;C:\\?.lc;"

/*
@@ LUA_ANSI controls the use of non-ansi features.
** CHANGE it (define it) if you want Lua to avoid the use of any
Expand Down Expand Up @@ -292,7 +294,7 @@
** CHANGE it if you need longer lines.
*/
#define LUA_MAXINPUT 128


/*
@@ lua_readline defines how to show a prompt and then read a line from
Expand Down Expand Up @@ -560,10 +562,10 @@

/* Define LUA_NUMBER_INTEGRAL to produce a system that uses no
floating point operations by changing the type of Lua numbers from
double to long. It implements division and modulus so that
double to long. It implements division and modulus so that
x == (x / y) * y + x % y.
x == (x / y) * y + x % y.
The exponentiation function returns zero for negative exponents.
Defining LUA_NUMBER_INTEGRAL also removes the difftime function,
and the math module should not be used. The string.format function
Expand Down Expand Up @@ -883,7 +885,7 @@ typedef long int32_t;
metatables for tables/userdata/types (but the VM might run slower)
*/
#if (LUA_OPTIMIZE_MEMORY == 2) && !defined(LUA_CROSS_COMPILER)
#define LUA_META_ROTABLES
#define LUA_META_ROTABLES
#endif

#if LUA_OPTIMIZE_MEMORY == 2 && defined(LUA_USE_POPEN)
Expand Down
112 changes: 35 additions & 77 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,38 @@
#include "lualib.h"
#include "lauxlib.h"


#include "shell.h"


lua_State *L = NULL;
vm_call_listener_func g_call_status_callback = NULL;

void vm_thread_change_priority(VM_THREAD_HANDLE thread_handle, VMUINT32 new_priority);
void dotty (lua_State *L);

void __handle_sysevt(VMINT message, VMINT param)
static int msleep_c(lua_State *L)
{
switch (message)
{
case VM_MSG_CREATE:
break;

case VM_MSG_QUIT:
break;
}
long ms = lua_tointeger(L, -1);
vm_thread_sleep(ms);
return 0;
}

void __call_listener_func(vm_call_listener_data* data)
void setup_lua()
{
if(g_call_status_callback)
{
g_call_status_callback(data);
}
}
VM_THREAD_HANDLE handle;

VMINT32 __main_thread(VM_THREAD_HANDLE thread_handle, void* user_data)
{
if (1)
{
const char *script = "print('Lua on Linkit\n')";
int error;
error = luaL_loadstring(L, script);
printf("luaL_loadstring(): %d\n", error);
if (!error) {
lua_pcall(L,0,0,0);
}
}
L = lua_open();
lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
luaL_openlibs(L); /* open libraries */

lua_register(L, "msleep", msleep_c);

dotty(L);
lua_gc(L, LUA_GCRESTART, 0);

for (;;)
{
// console_puts("hello, linkit\n");
luaL_dofile(L, "init.lua");

handle = vm_thread_create(shell_thread, L, 0);
vm_thread_change_priority(handle, 245);

console_putc(console_getc());
}
}


VMUINT32 blink_thread(VM_THREAD_HANDLE thread_handle, void* user_data)
{
VM_DCL_HANDLE gpio_handle;
Expand All @@ -83,6 +62,23 @@ VMUINT32 blink_thread(VM_THREAD_HANDLE thread_handle, void* user_data)
}
}

void __handle_sysevt(VMINT message, VMINT param)
{
switch (message)
{
case VM_MSG_CREATE:
setup_lua();
break;

case SHELL_MESSAGE_ID:
shell_docall(L);
break;

case VM_MSG_QUIT:
break;
}
}

/*
* \brief Main entry point of the application
*/
Expand All @@ -99,46 +95,8 @@ void vm_main( void )
}
console_puts("system is running\n");

printf("heap size: %d\n", vm_get_total_heap_size());

L = lua_open();
lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */

luaL_openlibs(L); /* open libraries */

lua_pushcfunction(L, luaopen_io);
lua_pushstring(L, "io");
lua_call(L, 1, 0);

lua_pushcfunction(L, luaopen_table);
lua_pushstring(L, "table");
lua_call(L, 1, 0);

lua_pushcfunction(L, luaopen_string);
lua_pushstring(L, "string");
lua_call(L, 1, 0);

lua_gc(L, LUA_GCRESTART, 0);

if (1)
{
char buf[64];
FILE *fd = fopen("init.lua", "r");
if (fd != NULL)
{
fread(buf, 1, sizeof(buf), fd);
console_puts(buf);
fclose(fd);
} else {
printf("failed to open file.txt\n");
}
}
vm_reg_sysevt_callback(__handle_sysevt);

luaL_dofile(L, "init.lua");

vm_reg_sysevt_callback(__handle_sysevt);
vm_call_reg_listener(__call_listener_func);
handle = vm_thread_create(__main_thread, NULL, 0);
vm_thread_change_priority(handle, 245);
vm_thread_create(blink_thread, NULL, 0);
}
Loading

0 comments on commit 41ee9ce

Please sign in to comment.