From 8e1a0a2487880898330f55694d2b3385da16f96d Mon Sep 17 00:00:00 2001 From: lunokIoT <797792+lunokjod@users.noreply.github.com> Date: Tue, 5 Mar 2024 16:32:01 +0100 Subject: [PATCH] more draw primitives exposed --- src/app/LuaLauncher.cpp | 37 +++++++++++++++++++++++++++++-- src/system/lua.cpp | 48 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/src/app/LuaLauncher.cpp b/src/app/LuaLauncher.cpp index e16d459..36c6bb2 100644 --- a/src/app/LuaLauncher.cpp +++ b/src/app/LuaLauncher.cpp @@ -26,18 +26,21 @@ using namespace LuI; const char * HelloworldLuaScript = "\ log('Hello world from Lua script!')\n\ +Debug()\n\ log('Fill screen with color')\n\ for i=1,3 do\n\ local RandomBGColor = random(0,0xffff)\n\ FillScreen(RandomBGColor)\n\ delay(500);\n\ end\n\ +Debug()\n\ log('Fill screen with color cycle')\n\ local color=0\n\ for x=0,240 do\n\ DrawLine(x,0,x,240,RGBTft(color,color,color))\n\ color=color+1\n\ end\n\ +Debug()\n\ log('Fill screen with lines random')\n\ for i=1,100 do\n\ local color = random(0,0xffff)\n\ @@ -47,15 +50,45 @@ for i=1,100 do\n\ local y1 = random(0,240)\n\ DrawLine(x0,y0,x1,y1,color)\n\ end\n\ +Debug()\n\ log('Fill screen with circles random')\n\ -for i=1,100 do\n\ +for i=1,50 do\n\ local color = random(0,0xffff)\n\ local x0 = random(0,240)\n\ local y0 = random(0,240)\n\ local r = random(0,50)\n\ - DrawCircle(x0,y0,r,color)\n\ + DrawCircle(x0,y0,r,color,false)\n\ +end\n\ +for i=1,50 do\n\ + local color = random(0,0xffff)\n\ + local x0 = random(0,240)\n\ + local y0 = random(0,240)\n\ + local r = random(0,50)\n\ + DrawCircle(x0,y0,r,color,true)\n\ +end\n\ +Debug()\n\ +log('Fill screen with rects random')\n\ +for i=1,50 do\n\ + local color = random(0,0xffff)\n\ + local x0 = random(0,240)\n\ + local y0 = random(0,240)\n\ + local x1 = random(0,240)\n\ + local y1 = random(0,240)\n\ + DrawRect(x0,y0,x1,y1,color,false)\n\ +end\n\ +for i=1,50 do\n\ + local color = random(0,0xffff)\n\ + local x0 = random(0,240)\n\ + local y0 = random(0,240)\n\ + local x1 = random(0,240)\n\ + local y1 = random(0,240)\n\ + DrawRect(x0,y0,x1,y1,color,true)\n\ end\n\ +log('Little delay...')\n\ +delay(1000)\n\ +Debug()\n\ log('See you!')\n\ +LaunchWatchface()\n\ "; LuaLauncher::LuaLauncher(const char * script) { diff --git a/src/system/lua.cpp b/src/system/lua.cpp index 0f01644..459216b 100644 --- a/src/system/lua.cpp +++ b/src/system/lua.cpp @@ -46,6 +46,11 @@ extern "C" { */ + static int lua_wrapper_Debug(lua_State *lua_state) { + lRawLog("[LUA] Free System Heap: %d\n",esp_get_free_heap_size()); + lRawLog("[LUA] Free Task Heap: %d\n",uxTaskGetStackHighWaterMark(NULL)); + return 0; + } static int lua_wrapper_Restart(lua_State *lua_state) { LaunchApplication(new ShutdownApplication(true)); return 0; @@ -69,7 +74,7 @@ extern "C" { lua_pushnumber(lua_state, (lua_Number) millis()); return 1; } - static int lua_wrapper_Random(lua_State *lua_state) { + static int lua_wrapper_random(lua_State *lua_state) { long floorVal = luaL_optnumber(lua_state, 1,0.0); long maxVal = luaL_optnumber(lua_state, 2,1.0); lua_pushnumber(lua_state, (lua_Number) random(floorVal,maxVal)); @@ -93,11 +98,40 @@ extern "C" { int32_t y0 = luaL_checkinteger(lua_state, 2); int32_t r = luaL_checkinteger(lua_state, 3); uint32_t color = luaL_checkinteger(lua_state, 4); + luaL_checktype(lua_state, 5, LUA_TBOOLEAN); + bool fill = lua_toboolean(lua_state, 5); xSemaphoreTake( UISemaphore, LUNOKIOT_EVENT_MANDATORY_TIME_TICKS); LunokIoTApplication * current = currentApplication; if ( nullptr != currentApplication ) { - currentApplication->canvas->drawCircle(x0,y0,r,color); + if ( fill ) { + currentApplication->canvas->fillCircle(x0,y0,r,color); + } else { + currentApplication->canvas->drawCircle(x0,y0,r,color); + } + currentApplication->dirty=true; + } + xSemaphoreGive( UISemaphore ); + return 0; + } + + static int lua_wrapper_tft_drawRect(lua_State *lua_state) { + int32_t x0 = luaL_checkinteger(lua_state, 1); + int32_t y0 = luaL_checkinteger(lua_state, 2); + int32_t x1 = luaL_checkinteger(lua_state, 3); + int32_t y1 = luaL_checkinteger(lua_state, 4); + uint32_t color = luaL_checkinteger(lua_state, 5); + luaL_checktype(lua_state, 6, LUA_TBOOLEAN); + bool fill = lua_toboolean(lua_state, 6); + + xSemaphoreTake( UISemaphore, LUNOKIOT_EVENT_MANDATORY_TIME_TICKS); + LunokIoTApplication * current = currentApplication; + if ( nullptr != currentApplication ) { + if ( fill ) { + currentApplication->canvas->fillRect(x0,y0,x1,y1,color); + } else { + currentApplication->canvas->drawRect(x0,y0,x1,y1,color); + } currentApplication->dirty=true; } xSemaphoreGive( UISemaphore ); @@ -172,20 +206,24 @@ void LuaInit() { // arduino general lua.Lua_register("millis", &lua_wrapper_millis); lua.Lua_register("delay", &lua_wrapper_delay); - lua.Lua_register("random", &lua_wrapper_Random); + lua.Lua_register("random", &lua_wrapper_random); - // lwatch calls + // system (lwatch) calls lua.Lua_register("LaunchWatchface", &lua_wrapper_LaunchWatchface); lua.Lua_register("ScreenSleep", &lua_wrapper_ScreenSleep); lua.Lua_register("ScreenWake", &lua_wrapper_ScreenWake); lua.Lua_register("DoSleep", &lua_wrapper_DoSleep); lua.Lua_register("Shutdown", &lua_wrapper_Shutdown); lua.Lua_register("Restart", &lua_wrapper_Restart); - // UI calls + lua.Lua_register("Debug", &lua_wrapper_Debug); + // draw primitives lua.Lua_register("FillScreen", &lua_wrapper_tft_fillScreen); lua.Lua_register("DrawLine", &lua_wrapper_tft_drawLine); lua.Lua_register("DrawCircle", &lua_wrapper_tft_drawCircle); + lua.Lua_register("DrawRect", &lua_wrapper_tft_drawRect); lua.Lua_register("RGBTft", &lua_wrapper_tft_rgbTFTColor); + // @TODO UI calls + // @TODO GUI calls } void LuaRunTask(void *data) {