mirror of
git://projects.qi-hardware.com/nn-usb-fpga.git
synced 2025-04-21 12:27:27 +03:00
Adding LUA tutorials: lua_calls_C1 - lua_calls_C5
Adding blinker demo: lua_blink_led
This commit is contained in:
22
lua/examples/lua_calls_C5/Makefile
Normal file
22
lua/examples/lua_calls_C5/Makefile
Normal file
@@ -0,0 +1,22 @@
|
||||
CC = mipsel-openwrt-linux-gcc
|
||||
CXX = mipsel-openwrt-linux-g++
|
||||
OPENWRT_BUILD_DIR = /home/cain/Embedded/ingenic/sakc/build/openwrt-xburst/staging_dir/target-mipsel_uClibc-0.9.30.1
|
||||
INCLUDE = -I. -I$(OPENWRT_BUILD_DIR)/usr/include/
|
||||
WARNINGS = -Wcast-align -Wpacked -Wpadded -Wall
|
||||
CCFLAGS = ${INCLUDE} ${DEBUG} ${WARNINGS} -std=c99 -fPIC
|
||||
LDFLAGS = -L$(OPENWRT_BUILD_DIR)/usr/lib -llua -ldl
|
||||
DEBUG = -O3 -g0
|
||||
NANO_PATH = root@192.168.254.101:
|
||||
TARGET = mylib
|
||||
|
||||
$(TARGET):
|
||||
$(CC) $(CCFLAGS) $(LDFLAGS) -shared liba.c liba_wrapper.c -o $(TARGET).so
|
||||
|
||||
.c.o:
|
||||
$(CC) -c $(CCFLAGS) -o $@
|
||||
|
||||
upload: $(TARGET)
|
||||
scp $(TARGET).so test.lua $(NANO_PATH)
|
||||
|
||||
clean:
|
||||
rm -f *.o $(TARGET).so
|
||||
40
lua/examples/lua_calls_C5/liba.c
Normal file
40
lua/examples/lua_calls_C5/liba.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "liba.h"
|
||||
|
||||
#define ENTER do{printf("in %s \n" ,__FUNCTION__);}while(0);
|
||||
|
||||
void lib_a_f_1()
|
||||
{
|
||||
ENTER
|
||||
}
|
||||
|
||||
int lib_a_f_2(int a , int b)
|
||||
{
|
||||
ENTER
|
||||
return a * b ;
|
||||
}
|
||||
|
||||
|
||||
int lib_a_f_3(const char *s)
|
||||
{
|
||||
ENTER
|
||||
return strlen(s);
|
||||
}
|
||||
|
||||
int lib_a_f_4(Point *in_t)
|
||||
{
|
||||
ENTER
|
||||
return in_t->a * in_t->b ;
|
||||
}
|
||||
|
||||
|
||||
int lib_a_f_5(Point *out_t)
|
||||
{
|
||||
ENTER
|
||||
|
||||
out_t->a = 20;
|
||||
out_t->b = 30;
|
||||
|
||||
return 0;
|
||||
}
|
||||
10
lua/examples/lua_calls_C5/liba.h
Normal file
10
lua/examples/lua_calls_C5/liba.h
Normal file
@@ -0,0 +1,10 @@
|
||||
typedef struct tagT{
|
||||
int a ;
|
||||
int b ;
|
||||
}Point;
|
||||
|
||||
void lib_a_f_1(void);
|
||||
int lib_a_f_2(int a, int b);
|
||||
int lib_a_f_3(const char *s);
|
||||
int lib_a_f_4(Point *in_t);
|
||||
int lib_a_f_5(Point *out_t);
|
||||
131
lua/examples/lua_calls_C5/liba_wrapper.c
Normal file
131
lua/examples/lua_calls_C5/liba_wrapper.c
Normal file
@@ -0,0 +1,131 @@
|
||||
#include "liba.h"
|
||||
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
|
||||
//glue the luaVm and the actually libary
|
||||
|
||||
static int lib_a_f_1_wrapper(lua_State * luaVM)
|
||||
{
|
||||
// no parameters , just call
|
||||
lib_a_f_1();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lib_a_f_2_wrapper(lua_State * L)
|
||||
{
|
||||
// no parameters , just call
|
||||
int n = lua_gettop(L);
|
||||
//error checking & return back to lua scripter
|
||||
int a = lua_tonumber(L,1);
|
||||
int b = lua_tonumber(L,2);
|
||||
|
||||
int r = lib_a_f_2(a ,b );
|
||||
|
||||
lua_pushnumber(L,r);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int lib_a_f_3_wrapper(lua_State * L)
|
||||
{
|
||||
// no parameters , just call
|
||||
int n = lua_gettop(L);
|
||||
|
||||
//error checking & return back to lua scripter
|
||||
const char *s = lua_tostring(L,1);
|
||||
|
||||
int r = lib_a_f_3(s);
|
||||
|
||||
lua_pushnumber(L,r);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *metaname = "mine.Point"; // associated with userdata of type Point*
|
||||
|
||||
static int lib_a_f_4_wrapper(lua_State *L) {
|
||||
Point *t = luaL_checkudata(L, 1, metaname); // check argument type
|
||||
lua_pushnumber(L, (lua_Number)lib_a_f_4(t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int point_new_wrapper(lua_State *L) { // get Lua to allocate an initialize a Point*
|
||||
int a = luaL_checkint(L, 1);
|
||||
int b = luaL_checkint(L, 2);
|
||||
//create user data and associate metable with it
|
||||
Point *t = lua_newuserdata(L, sizeof(*t));
|
||||
luaL_getmetatable(L, metaname);
|
||||
lua_setmetatable(L, -2);
|
||||
t->a = a;
|
||||
t->b = b;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int point_geta_wrapper(lua_State *L)
|
||||
{
|
||||
Point *t = luaL_checkudata(L, 1, metaname);
|
||||
lua_pushnumber(L,t->a);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int point_getb_wrapper(lua_State *L)
|
||||
{
|
||||
Point *t = luaL_checkudata(L, 1, metaname);
|
||||
lua_pushnumber(L,t->b);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int lib_a_f_5_wrapper(lua_State *L)
|
||||
{
|
||||
// Point *t = luaL_checkudata(L, 1, metaname); // check argument type
|
||||
// lua_pushnumber(L, (lua_Number)lib_a_f_4(t));
|
||||
|
||||
Point t ;
|
||||
lib_a_f_5(&t);
|
||||
Point *r = lua_newuserdata(L,sizeof(*r));
|
||||
luaL_getmetatable(L, metaname);
|
||||
//It will Pops a table from the stack and
|
||||
//sets it as the new metatable for the value at the given acceptable index.
|
||||
//so , after this function , it is the userdata at the top of the stack.
|
||||
lua_setmetatable(L, -2);
|
||||
r->a = t.a;
|
||||
r->b = t.b;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const struct luaL_reg functions[] = {
|
||||
{"lib_a_f_1", lib_a_f_1_wrapper},
|
||||
{"lib_a_f_2", lib_a_f_2_wrapper},
|
||||
{"lib_a_f_3", lib_a_f_3_wrapper},
|
||||
{"lib_a_f_4", lib_a_f_4_wrapper},
|
||||
{"lib_a_f_5", lib_a_f_5_wrapper},
|
||||
{"point_new", point_new_wrapper},
|
||||
{"point_geta", point_geta_wrapper},
|
||||
{"point_getb", point_getb_wrapper},
|
||||
{ NULL, NULL}
|
||||
};
|
||||
|
||||
//This is the init function that will be called when you require 'mylib'
|
||||
|
||||
int luaopen_mylib(lua_State *L) {
|
||||
|
||||
|
||||
luaL_newmetatable(L, metaname);
|
||||
//pop 1 elements from the statck .. why?? to pop the newmetatable that is useless.
|
||||
//
|
||||
//lua_pop(L, 1);
|
||||
//replace luaL_openlib
|
||||
{
|
||||
lua_pop(L, 1);
|
||||
luaL_newmetatable(L,metaname);
|
||||
return 1;
|
||||
}
|
||||
luaL_register(L, "mylib", functions);
|
||||
return 1;
|
||||
}
|
||||
16
lua/examples/lua_calls_C5/test.lua
Normal file
16
lua/examples/lua_calls_C5/test.lua
Normal file
@@ -0,0 +1,16 @@
|
||||
package.cpath = "./?.so"
|
||||
require "mylib"
|
||||
--test1
|
||||
mylib.lib_a_f_1()
|
||||
--test2
|
||||
assert(6==mylib.lib_a_f_2(2,3))
|
||||
--test3
|
||||
assert(5==mylib.lib_a_f_3("hello"))
|
||||
--test4 : use userdata to pass structure
|
||||
t=mylib.point_new(3,6)
|
||||
assert(18 == mylib.lib_a_f_4(t))
|
||||
--test5, return userdata
|
||||
t=mylib.lib_a_f_5()
|
||||
assert(600 == mylib.lib_a_f_4(t))
|
||||
assert(mylib.point_geta(t) == 20)
|
||||
assert(mylib.point_getb(t) == 30)
|
||||
Reference in New Issue
Block a user