Lua marix API compiles. does not work
This commit is contained in:
parent
fda422524f
commit
3e9e8eb2b9
4 changed files with 75 additions and 58 deletions
|
@ -1,6 +1,7 @@
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "lua.hpp"
|
#include "lua.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include "luamatrix.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
@ -95,24 +96,6 @@ static void l_memstats() {
|
||||||
return ll_realloc(ptr, nsize);
|
return ll_realloc(ptr, nsize);
|
||||||
}
|
}
|
||||||
|
|
||||||
// CREATE ARRAY TYPE AND EXPOSE led_data TO LUA.
|
|
||||||
// metatable method for handling "array[index]"
|
|
||||||
/* static int led_data_index (lua_State* L) {
|
|
||||||
CRGB** parray = (CRGB **) luaL_checkudata(L, 1, "led_data");
|
|
||||||
int index = luaL_checkint(L, 2);
|
|
||||||
lua_pushnumber(L, (*parray)[index-1]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// metatable method for handle "array[index] = value"
|
|
||||||
static int led_data_newindex (lua_State* L) {
|
|
||||||
CRGB** parray = (CRGB **) luaL_checkudata(L, 1, "led_data");
|
|
||||||
int index = luaL_checkint(L, 2);
|
|
||||||
int value = luaL_checkint(L, 3);
|
|
||||||
(*parray)[index-1] = value;
|
|
||||||
return 0;
|
|
||||||
} */
|
|
||||||
|
|
||||||
void l_yield(lua_State *L, lua_Debug *ar) {
|
void l_yield(lua_State *L, lua_Debug *ar) {
|
||||||
lua_yield(L, 0);
|
lua_yield(L, 0);
|
||||||
}
|
}
|
||||||
|
@ -153,43 +136,6 @@ int full_run(lua_State *L, lua_State *t) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* static const struct luaL_Reg led_data_metamethods[] = {
|
|
||||||
{ "__index", led_data_index },
|
|
||||||
{ "__newindex", led_data_newindex },
|
|
||||||
{NULL, NULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// create a metatable for our array type
|
|
||||||
static void create_array_type(lua_State* L) {
|
|
||||||
luaL_newmetatable(L, "led_data");
|
|
||||||
luaL_setfuncs(L, led_data_metamethods, 0);
|
|
||||||
//lua_setglobal(L, "led_data");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// expose an array to lua, by storing it in a userdata with the array metatable
|
|
||||||
static int expose_array(lua_State* L, CRGB array[]) {
|
|
||||||
CRGB** parray = (CRGB **) lua_newuserdata(L, sizeof(CRGB**));
|
|
||||||
*parray = array;
|
|
||||||
luaL_getmetatable(L, "led_data");
|
|
||||||
lua_setmetatable(L, -2);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// test routine which exposes our test array to Lua
|
|
||||||
static int getarray (lua_State* L) {
|
|
||||||
return expose_array( L, led_data );
|
|
||||||
}
|
|
||||||
|
|
||||||
int luaopen_array (lua_State* L) {
|
|
||||||
create_array_type(L);
|
|
||||||
|
|
||||||
// make our test routine available to Lua
|
|
||||||
lua_register(L, "led_data", getarray);
|
|
||||||
return 0;
|
|
||||||
} */
|
|
||||||
|
|
||||||
// This code blatantly stolen and borrowed from lbaselib.c luaB_print
|
// This code blatantly stolen and borrowed from lbaselib.c luaB_print
|
||||||
static int l_print (lua_State *L) {
|
static int l_print (lua_State *L) {
|
||||||
int n = lua_gettop(L); /* number of arguments */
|
int n = lua_gettop(L); /* number of arguments */
|
||||||
|
@ -262,7 +208,7 @@ void l_start(const char *prgm) {
|
||||||
debug_log("Importing libraries");
|
debug_log("Importing libraries");
|
||||||
|
|
||||||
l_openlibs(L);
|
l_openlibs(L);
|
||||||
// luaopen_array(L);
|
luaopen_matrixpanel(L);
|
||||||
lua_pushcfunction(L, l_print);
|
lua_pushcfunction(L, l_print);
|
||||||
lua_setglobal(L, "print");
|
lua_setglobal(L, "print");
|
||||||
|
|
||||||
|
|
58
teensy/luamatrix.cpp
Normal file
58
teensy/luamatrix.cpp
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
#include "luamatrix.h"
|
||||||
|
#include "matrix.h"
|
||||||
|
#include "lua.hpp"
|
||||||
|
|
||||||
|
// CREATE ARRAY TYPE AND EXPOSE matrixpanel TO LUA.
|
||||||
|
// metatable method for handling "array[index]"
|
||||||
|
/* static int matrixpanel_index (lua_State* L) {
|
||||||
|
CRGB** parray = (CRGB **) luaL_checkudata(L, 1, "matrixpanel");
|
||||||
|
int index = luaL_checkint(L, 2);
|
||||||
|
lua_pushnumber(L, (*parray)[index-1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// metatable method for handle "array[index] = value"
|
||||||
|
static int matrixpanel_newindex (lua_State* L) {
|
||||||
|
CRGB** parray = (CRGB **) luaL_checkudata(L, 1, "matrixpanel");
|
||||||
|
int index = luaL_checkint(L, 2);
|
||||||
|
int value = luaL_checkint(L, 3);
|
||||||
|
(*parray)[index-1] = value;
|
||||||
|
return 0;
|
||||||
|
} */
|
||||||
|
|
||||||
|
static const struct luaL_Reg matrixpanel_metamethods[] = {
|
||||||
|
// { "__index", matrixpanel_index },
|
||||||
|
// { "__newindex", matrixpanel_newindex },
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// create a metatable for our array type
|
||||||
|
static void create_matrixpanel_type(lua_State* L) {
|
||||||
|
luaL_newmetatable(L, "matrixpanel");
|
||||||
|
luaL_setfuncs(L, matrixpanel_metamethods, 0);
|
||||||
|
//lua_setglobal(L, "matrixpanel");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// expose an array to lua, by storing it in a userdata with the array metatable
|
||||||
|
static int expose_array(lua_State* L) {
|
||||||
|
RGBmatrixPanel **pmatrix = (RGBmatrixPanel **) lua_newuserdata(L, sizeof(RGBmatrixPanel **));
|
||||||
|
*pmatrix = &matrixpanel;
|
||||||
|
luaL_getmetatable(L, "matrixpanel");
|
||||||
|
lua_setmetatable(L, -2);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// test routine which exposes our test array to Lua
|
||||||
|
static int getmatrixpanel (lua_State* L) {
|
||||||
|
return expose_array( L );
|
||||||
|
}
|
||||||
|
|
||||||
|
int luaopen_matrixpanel (lua_State* L) {
|
||||||
|
create_matrixpanel_type(L);
|
||||||
|
|
||||||
|
// make our test routine available to Lua
|
||||||
|
lua_register(L, "matrixpanel", getmatrixpanel);
|
||||||
|
return 0;
|
||||||
|
}
|
13
teensy/luamatrix.h
Normal file
13
teensy/luamatrix.h
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#ifndef __LUALIGHTS_LUAMATRIX_H
|
||||||
|
#define __LUALIGHTS_LUAMATRIX_H
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include "lua.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "lua-alloc.h"
|
||||||
|
};
|
||||||
|
|
||||||
|
int luaopen_matrixpanel (lua_State* L);
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef __LUALIGHTS_LUA_ALLOC_H
|
#ifndef __LUALIGHTS_MATRIX_H
|
||||||
#define __LUALIGHTS_LUA_ALLOC_H
|
#define __LUALIGHTS_MATRIX_H
|
||||||
|
|
||||||
#include <Adafruit_GFX.h>
|
#include <Adafruit_GFX.h>
|
||||||
#include <RGBmatrixPanel.h>
|
#include <RGBmatrixPanel.h>
|
||||||
|
|
Loading…
Add table
Reference in a new issue