Skip to content

Commit

Permalink
lua integration experimentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lunokjod committed Sep 18, 2024
1 parent ddb0ba1 commit 5505952
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 14 deletions.
34 changes: 31 additions & 3 deletions src/app/LuaLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ using namespace LuI;
const char * HelloworldLuaScript =
"\
log('Hello world from Lua script!')\n\
log('globals:')\n\
for n in pairs(_G) do log(n) end\n\
--[[\n\
canDo = Allow(\"net\")\n\
if canDo then\n\
log('USER CONSENT')\n\
else\n\
log('USER DENIES')\n\
end\n\
]]--\n\
Debug()\n\
log('Fill screen with color')\n\
for i=1,3 do\n\
Expand All @@ -39,7 +49,7 @@ for i=1,100 do\n\
local color = random(0,0xffff)\n\
local x0 = random(0,240)\n\
local y0 = random(0,240)\n\
local tsize = random(1,8)\n\
local tsize = random(1,6)\n\
SetTextColor(color)\n\
SetTextSize(tsize)\n\
DrawText(x0,y0,\"hello world!\")\n\
Expand Down Expand Up @@ -99,9 +109,27 @@ log('Little delay...')\n\
-- delay(1000)\n\
Debug()\n\
log('See you!')\n\
LaunchWatchface()\n\
-- LaunchWatchface()\n\
";

LuaLauncher::LuaLauncher(const char * script) {
LuaRun(script);
LuaRun(script,[](const char* response, void *payload) {
lLog("[LUA] Script end\n");
LuaLauncher * self = (LuaLauncher *)payload;
if ( nullptr != self->myDialog ) {
delete self->myDialog;
self->myDialog=nullptr;
}
LaunchWatchface();
}, this);
}
/*
bool LuaLauncher::Tick() {
if ( nullptr == myDialog ) {
bool res = TemplateLuIApplication::Tick();
return res;
}
myDialog->Tick();
return false;
}
*/
9 changes: 6 additions & 3 deletions src/app/LuaLauncher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
#ifndef __LUNOKIOT__APPLICATION__LUA__
#define __LUNOKIOT__APPLICATION__LUA__

#include "../system/Application.hpp"

//#include "../system/Application.hpp"
#include "../UI/AppLuITemplate.hpp"
#include "LuiDialog.hpp"
extern const char * HelloworldLuaScript;

class LuaLauncher : public LunokIoTApplication {
class LuaLauncher : public TemplateLuIApplication {
public:
LuaLauncher(const char * script);
const char *AppName() override { return "LUA script"; };
//bool Tick();
LuIDialogApplication * myDialog = nullptr;
};

#endif
88 changes: 81 additions & 7 deletions src/system/lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,30 @@
#include "../app/Shutdown.hpp"
#include <LilyGoWatch.h>
#include "Application.hpp"
#include "../app/LuaLauncher.hpp"

LuaWrapper lua;
const char* LuaPermissions[] = {
"net",
"disk",
"setting",
"energy",
"alarm",
"ble"
};

class MyOwnLuaWrapper: public LuaWrapper {
public:
bool netAllow = false;
bool diskAllow = false;
bool settingsAllow = false;
bool energyAllow = false;
bool alarmAllow = false;
bool bleAllow = false;
//lua_State *state;
MyOwnLuaWrapper() { LuaWrapper(); /*state = LuaWrapper::_state;*/ }
};

MyOwnLuaWrapper lua;
static uint32_t RunningLuaScripts=0;

typedef struct {
Expand All @@ -35,6 +57,7 @@ typedef struct {
String * program;
} LuaCallbackData;


extern "C" {
// get mandatory: uint16_t color = luaL_checkinteger(lua_state, 1);
// get optional: long floorVal = luaL_optnumber(lua_state, 1,0.0);
Expand All @@ -47,8 +70,8 @@ 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));
lLog("[LUA] Free System Heap: %d\n",esp_get_free_heap_size());
lLog("[LUA] Free Task Heap: %d\n",uxTaskGetStackHighWaterMark(NULL));
return 0;
}
static int lua_wrapper_Restart(lua_State *lua_state) {
Expand Down Expand Up @@ -138,18 +161,21 @@ extern "C" {
xSemaphoreGive( UISemaphore );
return 0;
}

static int lua_wrapper_tft_drawText(lua_State *lua_state) {
int32_t x0 = luaL_checkinteger(lua_state, 1);
int32_t y0 = luaL_checkinteger(lua_state, 2);
const char * what = luaL_checkstring(lua_state, 3);
xSemaphoreTake( UISemaphore, LUNOKIOT_EVENT_MANDATORY_TIME_TICKS);
//LunokIoTApplication * current = currentApplication;
if ( nullptr != currentApplication ) {
/* @TODO


// @TODO setFreeFont and setTextDatum must be exposed to lua
currentApplication->canvas->setFreeFont(&FreeMonoBold9pt7b);
currentApplication->canvas->setTextDatum(BC_DATUM);
*/


currentApplication->canvas->drawString(what,x0,y0);
currentApplication->dirty=true;
}
Expand All @@ -158,6 +184,44 @@ extern "C" {
return 0;
}

static int lua_wrapper_Allow(lua_State *lua_state) {
const char * what = luaL_checkstring(lua_state, 1);
size_t numElements = sizeof(LuaPermissions) / sizeof(LuaPermissions[0]);
for(size_t c=0;c<numElements;c++) {
if ( 0 == strcmp(LuaPermissions[c],what) ) {
lLog("\n\nWARNING!!! @TODO USER CONSENT APP MUST BE LAUNCHED HERE\n\n\n");
//lua.netAllow = true;
//lua_pushboolean(lua_state,lua.netAllow);
//return 1;
LuaLauncher * currScript = (LuaLauncher *)currentApplication;
if ( nullptr == currScript->myDialog ) {
xSemaphoreTake(UISemaphore, LUNOKIOT_EVENT_DONTCARE_TIME_TICKS);
currScript->myDialog = new LuIDialogApplication();
currScript->myDialog->dialogContents->SetText("Allow network?");
currScript->myDialog->yesButton->tapCallbackParam = xTaskGetCurrentTaskHandle();
currScript->myDialog->yesButton->tapCallback = [](void *payload){
TaskHandle_t mtask = (TaskHandle_t)payload;
vTaskResume(mtask);
};
currScript->myDialog->noButton->tapCallbackParam = currScript->myDialog->yesButton->tapCallbackParam;
currScript->myDialog->noButton->tapCallback = [](void *payload){
TaskHandle_t mtask = (TaskHandle_t)payload;
vTaskResume(mtask);
};
currScript->myDialog->screen->dirty=true;
xSemaphoreGive(UISemaphore);
vTaskSuspend(NULL); // wait until user responds...
xSemaphoreTake(UISemaphore, LUNOKIOT_EVENT_DONTCARE_TIME_TICKS);
delete currScript->myDialog;
currScript->myDialog=nullptr;
xSemaphoreGive(UISemaphore);
}
}
}
lua_pushboolean(lua_state,false);
return 1;
}

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);
Expand Down Expand Up @@ -209,7 +273,6 @@ extern "C" {
return 1;
}


static int lua_wrapper_lwatch_print(lua_State *L) {
// manual blocking of log to print in block
xSemaphoreTake( lLogAsBlockSemaphore, LUNOKIOT_EVENT_MANDATORY_TIME_TICKS);
Expand Down Expand Up @@ -243,7 +306,14 @@ void LuaEndDefaultCallback(const char* response, void *payload) {
lLog("LUA (%u) End callback: no action\n",RunningLuaScripts);
};

int LuaPanicHandler (lua_State *L) {
lLog("[LUA] PANIC!!!\n");
lua_wrapper_Debug(L);
return 0;
}

void LuaInit() {
//@TODO lua_atpanic(lua.state,&LuaPanicHandler);
// log and stdout functions
lua.Lua_register("print", &lua_wrapper_lwatch_print);
lua.Lua_register("log", &lua_wrapper_lwatch_print);
Expand All @@ -253,6 +323,8 @@ void LuaInit() {
lua.Lua_register("delay", &lua_wrapper_delay);
lua.Lua_register("random", &lua_wrapper_random);

// system check calls
lua.Lua_register("Allow", &lua_wrapper_Allow);
// system (lwatch) calls
lua.Lua_register("LaunchWatchface", &lua_wrapper_LaunchWatchface);
lua.Lua_register("ScreenSleep", &lua_wrapper_ScreenSleep);
Expand All @@ -270,6 +342,8 @@ void LuaInit() {
lua.Lua_register("SetTextSize", &lua_wrapper_tft_setTextSize);
lua.Lua_register("DrawText", &lua_wrapper_tft_drawText);
lua.Lua_register("RGBTft", &lua_wrapper_tft_rgbTFTColor);
// @TODO haptic calls
// @TODO sensors calls
// @TODO UI calls
// @TODO GUI calls

Expand Down
2 changes: 1 addition & 1 deletion tool/.buildCount.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26479
26480

0 comments on commit 5505952

Please sign in to comment.