Skip to content

Add lua library imports and improve print() function #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 5, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve print() function
Basically the original, but with Serial
  • Loading branch information
Sasszem committed Jan 27, 2019
commit 14db8ba8364a142eb4c6b123ce86bf8015fa7338
24 changes: 20 additions & 4 deletions src/LuaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,26 @@ extern "C" {
delay(a);
}

static int lua_wrapper_print(lua_State *lua) {
String a = String(luaL_checkstring(lua, 1));
Serial.println(a);
}
static int lua_wrapper_print (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int i;
lua_getglobal(L, "tostring");
for (i=1; i<=n; i++) {
const char *s;
size_t l;
lua_pushvalue(L, -1); /* function to be called */
lua_pushvalue(L, i); /* value to print */
lua_call(L, 1, 1);
s = lua_tolstring(L, -1, &l); /* get result */
if (s == NULL)
return luaL_error(L, "'tostring' must return a string to 'print'");
if (i>1) Serial.write("\t");
Serial.write(s);
lua_pop(L, 1); /* pop result */
}
Serial.println();
return 0;
}

static int lua_wrapper_millis(lua_State *lua) {
lua_pushnumber(lua, (lua_Number) millis());
Expand Down