move larduinolib.c into ART's repo, add lua external library example

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2405 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
xiongyihui3@gmail.com 2012-11-13 07:43:55 +00:00
parent 0f111d6e47
commit 6c8080043f
4 changed files with 89 additions and 94 deletions

View File

@ -1,92 +0,0 @@
/**
* Arduino library for lua
*/
#include "lua.h"
#include "lauxlib.h"
#include "lexlibs.h"
#include "libarduino.h"
int arduino_pinMode(lua_State *L)
{
pinMode(luaL_checkint(L, 1), luaL_checkint(L, 2));
return 0;
}
int arduino_digitalWrite(lua_State *L)
{
digitalWrite(luaL_checkint(L, 1), luaL_checkint(L, 2));
return 0;
}
int arduino_digitalRead(lua_State *L)
{
lua_pushinteger(L, digitalRead(luaL_checkint(L, 1)));
return 1;
}
int arduino_analogWrite(lua_State *L)
{
analogWrite(luaL_checkint(L, 1), luaL_checkint(L, 2));
return 0;
}
/* the minimum optimization level at which we use rotables */
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
#if 0
/* standard table */
static const luaL_Reg arduino_map[] =
{
{"pinMode", arduino_pinMode},
{"digitalRead", arduino_digitalRead},
{"digitalWrite", arduino_digitalWrite},
{"analogWrite", arduino_analogWrite},
{NULL, NULL}
}
#else
const LUA_REG_TYPE arduino_map[] =
{
{LSTRKEY("pinMode"), LFUNCVAL(arduino_pinMode)},
{LSTRKEY("digitalRead"), LFUNCVAL(arduino_digitalRead)},
{LSTRKEY("digitalWrite"), LFUNCVAL(arduino_digitalWrite)},
{LSTRKEY("analogWrite"), LFUNCVAL(arduino_analogWrite)},
#if LUA_OPTIMIZE_MEMORY > 0
{LSTRKEY("HIGH"), LFUNCVAL(HIGH)},
{LSTRKEY("LOW"), LFUNCVAL(LOW)},
{LSTRKEY("INPUT"), LFUNCVAL(INPUT)},
{LSTRKEY("OUTPUT"), LFUNCVAL(OUTPUT)},
{LSTRKEY("INPUT_PULLUP"), LFUNCVAL(INPUT_PULLUP)},
#endif /* LUA_OPTIMIZE_MEMORY > 0 */
{LNILKEY, LNILVAL}
};
#endif /* 0 */
/**
* Open arduino library
*/
LUALIB_API int luaopen_arduino(lua_State *L)
{
#if LUA_OPTIMIZE_MEMORY > 0
return 0;
#else
luaL_register(L, EXLIB_ARDUINO, arduino_map);
lua_pushnumber(L, HIGH);
lua_setfield(L, -2, "HIGH");
lua_pushnumber(L, LOW);
lua_setfield(L, -2, "LOW");
lua_pushnumber(L, INPUT);
lua_setfield(L, -2, "INPUT");
lua_pushnumber(L, OUTPUT);
lua_setfield(L, -2, "OUTPUT");
lua_pushnumber(L, INPUT_PULLUP);
lua_setfield(L, -2, "INPUT_PULLUP");
return 1;
#endif
}

View File

@ -0,0 +1,75 @@
/**
* example of adding lua external library
*/
#include "lua.h"
#include "lauxlib.h"
#include "lexlibs.h"
#define VERSION 1
int example_hello(lua_State *L)
{
rt_kprintf("Hello, Lua on RT-Thead!\n");
return 0;
}
int example_print(lua_State *L)
{
int n = lua_gettop(L);
int i;
for (i=1; i<=n; i++)
{
if (i>1)
rt_kprintf("\t");
if (lua_isstring(L,i))
rt_kprintf("%s",lua_tostring(L,i));
else if (lua_isnumber(L, i))
rt_kprintf("%d",lua_tointeger(L,i));
else if (lua_isnil(L,i))
rt_kprintf("%s","nil");
else if (lua_isboolean(L,i))
rt_kprintf("%s",lua_toboolean(L,i) ? "true" : "false");
else
rt_kprintf("%s:%p",luaL_typename(L,i),lua_topointer(L,i));
}
rt_kprintf("\n");
return 0;
}
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
const LUA_REG_TYPE example_map[] =
{
{LSTRKEY("hello"), LFUNCVAL(example_hello)},
{LSTRKEY("print"), LFUNCVAL(example_print)},
#if LUA_OPTIMIZE_MEMORY > 0
{LSTRKEY("version"), LNUMVAL(VERSION)},
#endif
{LNILKEY, LNILVAL}
};
/**
* Open exmaple library
*/
LUALIB_API int luaopen_example(lua_State *L)
{
#if LUA_OPTIMIZE_MEMORY > 0
return 0;
#else
luaL_register(L, EXLIB_EXAMPLE, example_map);
lua_pushnumber(L, VERSION);
lua_setfield(L, -2, "version");
return 1;
#endif
}

View File

@ -5,11 +5,22 @@
#ifndef __LEXLIBS_H__
#define __LEXLIBS_H__
/* Arduino library source - larduinolib.c is placed in ART's directories */
#if defined(RT_LUA_USE_ARDUINOLIB)
#define EXLIB_ARDUINO "arduino"
#define ROM_EXLIB_ARDUINO \
_ROM(EXLIB_ARDUINO, luaopen_arduino, arduino_map)
#else
#define ROM_EXLIB_ARDUINO
#endif
#define LUA_EXLIBS_ROM\
_ROM(EXLIB_ARDUINO, luaopen_arduino, arduino_map )
#define EXLIB_EXAMPLE "example"
#define ROM_EXLIB_EXAMPLE \
_ROM(EXLIB_EXAMPLE, luaopen_example, example_map)
#define LUA_EXLIBS_ROM \
ROM_EXLIB_EXAMPLE \
ROM_EXLIB_ARDUINO
#endif

View File

@ -36,6 +36,7 @@ static const luaL_Reg lualibs[] = {
{LUA_DBLIBNAME, luaopen_debug},
#endif
#if defined(LUA_EXLIBS_ROM)
#undef _ROM
#define _ROM( name, openf, table ) { name, openf },
LUA_EXLIBS_ROM
#endif