1
0
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:
Carlos Camargo
2010-09-06 20:24:53 -05:00
parent acf516e22d
commit 7d9b7b803c
29 changed files with 1049 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
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 =
DEBUG = -O3 -g0
NANO_PATH = root@192.168.254.101:
TARGET = luapassing
$(TARGET): $(TARGET)
$(CC) $(CCFLAGS) -o $(TARGET).so -shared $(TARGET).c
.c.o:
$(CC) -c $(CCFLAGS) -o $@
upload: $(TARGET)
scp $(TARGET).so top2.lua $(NANO_PATH)
clean:
rm -f *.o $(TARGET).so

View File

@@ -0,0 +1,29 @@
Examples from http://www.wellho.net/mouth/1844_Calling-functions-in-C-from-your-Lua-script-a-first-HowTo.html
The code is very similar, but you'll notice six additions - in the Lua:
a) We have added a parameter to the call to dothis in the Lua
b) We have added an assignment to collect a return value from the C
summat = cstuff.dothis(value)
c) We have printed out these values
print ("Values in Lua now",value, summat)
and in the C:
d) We have used lua_tonumber to collect a numeric value that was passed from the Lua to the C; you'll note that all such numbers are treated as doubles. The parameter "1" indicates 1 down - i.e. top of - the state stack.
double trouble = lua_tonumber(L, 1);
e) We have performed a calculation (representative of something being done in the C code) and pushed the result of this back onto the Lua state stack so that it can be picked up once we have returned from the C to the Lua
lua_pushnumber(L, 16.0 - trouble);
f) We have changed return 0 into return 1 to indicate that one result is being returned.
return 1; }

View File

@@ -0,0 +1,19 @@
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include <stdio.h>
static int myCfunc ( lua_State *L) {
printf ("lua SIE's test\n");
double trouble = lua_tonumber(L, 1);
lua_pushnumber(L, 16.0 - trouble);
return 1; }
int luaopen_luapassing ( lua_State *L) {
static const luaL_reg Map [] = {
{"dothis", myCfunc},
{NULL,NULL} } ;
luaL_register(L, "cstuff", Map);
return 1; }

View File

@@ -0,0 +1,7 @@
package.cpath = "./?.so"
require "luapassing"
print("hello")
value = 10
summat = cstuff.dothis(value)
print ("It's run da C code!")
print ("Values in Lua now",value, summat)