summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
author[email protected] <[email protected]>2012-11-13 07:43:55 +0000
committer[email protected] <[email protected]>2012-11-13 07:43:55 +0000
commit6c8080043f35a4d68851c18d36ac5e51d46ef25b (patch)
tree90c7b4782abaa3263cce88d477f252461f43201d /components
parent0f111d6e470b7fdcec71967f456eaa7b1e0e142b (diff)
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
Diffstat (limited to 'components')
-rw-r--r--components/external/lua/exlibs/larduinolib.c92
-rw-r--r--components/external/lua/exlibs/lexamplelib.c75
-rw-r--r--components/external/lua/exlibs/lexlibs.h15
-rw-r--r--components/external/lua/lua/linit.c1
4 files changed, 89 insertions, 94 deletions
diff --git a/components/external/lua/exlibs/larduinolib.c b/components/external/lua/exlibs/larduinolib.c
deleted file mode 100644
index e444f55d7f..0000000000
--- a/components/external/lua/exlibs/larduinolib.c
+++ /dev/null
@@ -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
-}
-
-
diff --git a/components/external/lua/exlibs/lexamplelib.c b/components/external/lua/exlibs/lexamplelib.c
new file mode 100644
index 0000000000..f160ae35d8
--- /dev/null
+++ b/components/external/lua/exlibs/lexamplelib.c
@@ -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
+}
+
+
diff --git a/components/external/lua/exlibs/lexlibs.h b/components/external/lua/exlibs/lexlibs.h
index 069a535139..ca179e0f67 100644
--- a/components/external/lua/exlibs/lexlibs.h
+++ b/components/external/lua/exlibs/lexlibs.h
@@ -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
diff --git a/components/external/lua/lua/linit.c b/components/external/lua/lua/linit.c
index 6f0f265f57..6a295683f0 100644
--- a/components/external/lua/lua/linit.c
+++ b/components/external/lua/lua/linit.c
@@ -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