summaryrefslogtreecommitdiff
path: root/components/external/lua/exlibs
diff options
context:
space:
mode:
Diffstat (limited to 'components/external/lua/exlibs')
-rw-r--r--components/external/lua/exlibs/SConscript11
-rw-r--r--components/external/lua/exlibs/larduinolib.c92
-rw-r--r--components/external/lua/exlibs/lexlibs.h15
3 files changed, 118 insertions, 0 deletions
diff --git a/components/external/lua/exlibs/SConscript b/components/external/lua/exlibs/SConscript
new file mode 100644
index 0000000000..c891d565be
--- /dev/null
+++ b/components/external/lua/exlibs/SConscript
@@ -0,0 +1,11 @@
+
+import os
+from building import *
+
+cwd = GetCurrentDir()
+src = Glob('*.c')
+CPPPATH = [cwd, str(Dir('#'))]
+
+group = DefineGroup('lua', src, depend = ['RT_USING_LUA'], CPPPATH = CPPPATH)
+
+Return('group')
diff --git a/components/external/lua/exlibs/larduinolib.c b/components/external/lua/exlibs/larduinolib.c
new file mode 100644
index 0000000000..e444f55d7f
--- /dev/null
+++ b/components/external/lua/exlibs/larduinolib.c
@@ -0,0 +1,92 @@
+/**
+ * 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/lexlibs.h b/components/external/lua/exlibs/lexlibs.h
new file mode 100644
index 0000000000..069a535139
--- /dev/null
+++ b/components/external/lua/exlibs/lexlibs.h
@@ -0,0 +1,15 @@
+/**
+ * External library
+ */
+
+#ifndef __LEXLIBS_H__
+#define __LEXLIBS_H__
+
+#define EXLIB_ARDUINO "arduino"
+
+#define LUA_EXLIBS_ROM\
+ _ROM(EXLIB_ARDUINO, luaopen_arduino, arduino_map )
+
+
+#endif
+