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 = candy
$(TARGET): $(TARGET)
$(CC) $(CCFLAGS) -o $(TARGET).so -shared $(TARGET).c
.c.o:
$(CC) -c $(CCFLAGS) -o $@
upload: $(TARGET)
scp $(TARGET).so top3.lua $(NANO_PATH)
clean:
rm -f *.o $(TARGET).so

View File

@@ -0,0 +1,22 @@
Examples from http://www.wellho.net/mouth/1844_Calling-functions-in-C-from-your-Lua-script-a-first-HowTo.html
If you may with to pass a table from your Lua script into a C function, so that the C function can make use of a value from the table code like this:
stuff = {hotel = 48, hq = 404, town = 1}
....
summat = extratasks.dothis(stuff)
This isn't as easy as just passing a value, since the size of a table can vary, and Lua's dynamic memory allocation doesn't sit well, in a simple interface, with C's static model. The "trick" is to tackle it within the C code by using function calls to get (and if necessary) reset values from the table - with the data required by those function calls being processed via the Lua stack.
/* Looking up based on the key */
/* Add key we're interested in to the stack*/
lua_pushstring(L,"hq");
/* Have Lua functions look up the keyed value */
lua_gettable(L, -2 );
/* Extract the result which is on the stack */
double workon = lua_tonumber(L,-1);
/* Tidy up the stack - get rid of the extra we added*/
lua_pop(L,1);
That code retrieves the "hq" value from the table that's been passed in (called stuff in my Lua example above) and stores it into a C variable called workon ... objective achieved!

View File

@@ -0,0 +1,67 @@
/* This is the third (of three) examples of calling from
a Lua script to a C library. In this example, we have
registered multiple C functions with the Lua, and we have
also passed in a table which is intrinically much harder
to handle as it doesn't map directly to a C type. */
#include "lua.h"
#include "lauxlib.h"
#include <stdio.h>
/* The peardrop function is called with a single
numeric parameter, and it returns a number */
static int peardrop ( lua_State *L) {
printf ("lua SIE's test\n");
double trouble = lua_tonumber(L, 1);
lua_pushnumber(L, 16.0 - trouble);
return 1;
}
/* The humbug function is called with a TABLE as
its parameter (or it should be!). It extracts the
element with the key "hq", and returns 123 more
than that number (the sum added is just done to
demonstrate the C doing something! */
static int humbug ( lua_State *L) {
printf ("Enter humbug function\n");
/* Is it a table? */
if (lua_istable(L,-1)) {
printf("Table passed to humbug\n");
/* Looking up based on the key */
/* Add key we're interested in to the stack*/
lua_pushstring(L,"hq");
/* Have Lua functions look up the keyed value */
lua_gettable(L, -2 );
/* Extract the result which is on the stack */
double workon = lua_tonumber(L,-1);
/* Tidy up the stack - get rid of the extra we added*/
lua_pop(L,1);
/* We can now use the result */
lua_pushnumber(L, workon + 123.0 );
} else {
printf("Not a Table passed to humbug\n");
lua_pushnumber(L, 12.0 );
}
return 1;
}
/* This registers "dothis" and "dothat" with Lua, so that when
they're called they run the humbug and peardrop functions respectively */
int luaopen_candy ( lua_State *L) {
static const luaL_reg Map [] = {
{"dothis", humbug},
{"dothat", peardrop},
{NULL,NULL}
} ;
/* dothis and dothat are put in the extratasks table */
luaL_register(L, "extratasks", Map);
return 1;
}

View File

@@ -0,0 +1,25 @@
--[[ This is a short (demonstration) piece of Lua
script which calls in some C functions from a file
called candy.c; the functions are accessed via a
table called extratasks (that's defines in the
C code) and called via the names dothis and dothat -
again defined in the candy.c file
]]
-- Setting up demo variables that will be passed to C
print("Lua code running ...")
stuff = {hotel = 48, hq = 404, town = 1}
-- Loading and calling the C
require "candy"
summat = extratasks.dothis(stuff)
somemore = extratasks.dothat(summat)
-- Lua code to show you the results
print ("... back into Lua code")
print ("Values in Lua now", summat, somemore)
print ("Table contains ...")
for k,v in pairs(stuff) do
print (k,v)
end