diff --git a/app/include/user_config.h b/app/include/user_config.h index c812f2fa..8b605dbc 100644 --- a/app/include/user_config.h +++ b/app/include/user_config.h @@ -65,6 +65,7 @@ #define LUA_USE_MODULES_OW #define LUA_USE_MODULES_BIT #define LUA_USE_MODULES_MQTT +#define LUA_USE_MODULES_U8G #endif /* LUA_USE_MODULES */ // #define LUA_NUMBER_INTEGRAL diff --git a/app/modules/Makefile b/app/modules/Makefile index fb170792..0198ae9c 100644 --- a/app/modules/Makefile +++ b/app/modules/Makefile @@ -40,6 +40,7 @@ INCLUDES := $(INCLUDES) -I $(PDIR)include INCLUDES += -I ./ INCLUDES += -I ../libc INCLUDES += -I ../mqtt +INCLUDES += -I ../u8g INCLUDES += -I ../lua INCLUDES += -I ../platform INCLUDES += -I ../wofs diff --git a/app/modules/auxmods.h b/app/modules/auxmods.h index 6ab6ef46..1823d300 100644 --- a/app/modules/auxmods.h +++ b/app/modules/auxmods.h @@ -64,6 +64,9 @@ LUALIB_API int ( luaopen_wifi )( lua_State *L ); #define AUXLIB_MQTT "mqtt" LUALIB_API int ( luaopen_mqtt )( lua_State *L ); +#define AUXLIB_U8G "u8g" +LUALIB_API int ( luaopen_u8g )( lua_State *L ); + #define AUXLIB_NODE "node" LUALIB_API int ( luaopen_node )( lua_State *L ); diff --git a/app/modules/modules.h b/app/modules/modules.h index f28775d5..f4ad6b43 100644 --- a/app/modules/modules.h +++ b/app/modules/modules.h @@ -45,6 +45,14 @@ #define ROM_MODULES_MQTT #endif +#if defined(LUA_USE_MODULES_U8G) +#define MODULES_U8G "u8g" +#define ROM_MODULES_U8G \ + _ROM(MODULES_U8G, luaopen_u8g, u8g_map) +#else +#define ROM_MODULES_MQTT +#endif + #if defined(LUA_USE_MODULES_I2C) #define MODULES_I2C "i2c" #define ROM_MODULES_I2C \ @@ -121,7 +129,8 @@ ROM_MODULES_GPIO \ ROM_MODULES_PWM \ ROM_MODULES_WIFI \ - ROM_MODULES_MQTT \ + ROM_MODULES_MQTT \ + ROM_MODULES_U8G \ ROM_MODULES_I2C \ ROM_MODULES_SPI \ ROM_MODULES_TMR \ diff --git a/app/modules/u8g.c b/app/modules/u8g.c new file mode 100644 index 00000000..686d3b2c --- /dev/null +++ b/app/modules/u8g.c @@ -0,0 +1,99 @@ +// Module for U8glib + +//#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" +#include "platform.h" +#include "auxmods.h" +#include "lrotable.h" + +#include "c_string.h" +#include "c_stdlib.h" + +#include "c_types.h" +#include "mem.h" +#include "espconn.h" + +typedef struct lu8g_userdata +{ + int some_data; +} lu8g_userdata_t; + + +static int lu8g_new( lua_State *L ) +{ + lu8g_userdata_t *userdata = (lu8g_userdata_t *) lua_newuserdata( L, sizeof( lu8g_userdata_t ) ); + + userdata->some_data = 100; + + // set its metatable + luaL_getmetatable(L, "u8g.display"); + lua_setmetatable(L, -2); + + return 1; +} + +// Lua: u8g.setup( self, id ) +static int lu8g_setup( lua_State *L ) +{ + unsigned id = luaL_checkinteger( L, 2 ); + + //MOD_CHECK_ID( u8g, id ); + + if (id == 0) + return luaL_error( L, "ID 0 not supported!" ); + + return 0; +} + + +// Module function map +#define MIN_OPT_LEVEL 2 +#include "lrodefs.h" + +static const LUA_REG_TYPE u8g_display_map[] = +{ + { LSTRKEY( "setup" ), LFUNCVAL( lu8g_setup ) }, +#if LUA_OPTIMIZE_MEMORY > 0 + { LSTRKEY( "__index" ), LROVAL ( u8g_display_map ) }, +#endif + { LNILKEY, LNILVAL } +}; + +const LUA_REG_TYPE u8g_map[] = +{ + { LSTRKEY( "new" ), LFUNCVAL ( lu8g_new ) }, +#if LUA_OPTIMIZE_MEMORY > 0 + { LSTRKEY( "__metatable" ), LROVAL( u8g_map ) }, +#endif + { LNILKEY, LNILVAL } +}; + +LUALIB_API int ICACHE_FLASH_ATTR luaopen_u8g( lua_State *L ) +{ +#if LUA_OPTIMIZE_MEMORY > 0 + luaL_rometatable(L, "u8g.display", (void *)u8g_display_map); // create metatable + return 0; +#else // #if LUA_OPTIMIZE_MEMORY > 0 + int n; + luaL_register( L, AUXLIB_U8G, u8g_map ); + + // Set it as its own metatable + lua_pushvalue( L, -1 ); + lua_setmetatable( L, -2 ); + + // Module constants + // MOD_REG_NUMBER( L, "TCP", TCP ); + + // create metatable + luaL_newmetatable(L, "u8g.display"); + // metatable.__index = metatable + lua_pushliteral(L, "__index"); + lua_pushvalue(L,-2); + lua_rawset(L,-3); + // Setup the methods inside metatable + luaL_register( L, NULL, u8g_display_map ); + + return 1; +#endif // #if LUA_OPTIMIZE_MEMORY > 0 +}