From 7a2c0b3b88d103f6a1c0bfb3df216f23413939a9 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Sat, 21 May 2016 11:08:01 +0200 Subject: [PATCH] [Horus] Widget::update added to the Lua interface (called when options are modified) --- radio/sdcard/horus/WIDGETS/Counter/main.lua | 6 ++++- radio/src/lua/interface.cpp | 29 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/radio/sdcard/horus/WIDGETS/Counter/main.lua b/radio/sdcard/horus/WIDGETS/Counter/main.lua index 5d9687929..6ee3deea6 100644 --- a/radio/sdcard/horus/WIDGETS/Counter/main.lua +++ b/radio/sdcard/horus/WIDGETS/Counter/main.lua @@ -10,9 +10,13 @@ local function create(zone, options) return pie end +local function update(pie, options) + pie.options = options +end + function refresh(pie) pie.counter = pie.counter + 1 lcd.drawNumber(pie.zone.x, pie.zone.y, pie.counter, LEFT + DBLSIZE + TEXT_COLOR); end -return { name="Counter", options=options, create=create, refresh=refresh } +return { name="Counter", options=options, create=create, update=update, refresh=refresh } diff --git a/radio/src/lua/interface.cpp b/radio/src/lua/interface.cpp index 5ca93650e..83c2b61ce 100644 --- a/radio/src/lua/interface.cpp +++ b/radio/src/lua/interface.cpp @@ -909,6 +909,8 @@ class LuaWidget: public Widget luaL_unref(L, LUA_REGISTRYINDEX, widgetData); } + virtual void update() const; + virtual void refresh(); virtual void background(); @@ -933,6 +935,7 @@ class LuaWidgetFactory: public WidgetFactory LuaWidgetFactory(const char * name, int widgetOptions, int createFunction): WidgetFactory(name, createOptionsArray(widgetOptions)), createFunction(createFunction), + updateFunction(0), refreshFunction(0), backgroundFunction(0) { @@ -969,10 +972,29 @@ class LuaWidgetFactory: public WidgetFactory protected: int createFunction; + int updateFunction; int refreshFunction; int backgroundFunction; }; +void LuaWidget::update() const +{ + SET_LUA_INSTRUCTIONS_COUNT(PERMANENT_SCRIPTS_MAX_INSTRUCTIONS); + LuaWidgetFactory * factory = (LuaWidgetFactory *)this->factory; + lua_rawgeti(L, LUA_REGISTRYINDEX, factory->updateFunction); + lua_rawgeti(L, LUA_REGISTRYINDEX, widgetData); + + lua_newtable(L); + int i = 0; + for (const ZoneOption * option = getOptions(); option->name; option++, i++) { + l_pushtableint(option->name, persistentData->options[i].signedValue); + } + + if (lua_pcall(L, 2, 0, 0) != 0) { + TRACE("Error in widget %s: %s", factory->getName(), lua_tostring(L, -1)); + } +} + void LuaWidget::refresh() { SET_LUA_INSTRUCTIONS_COUNT(PERMANENT_SCRIPTS_MAX_INSTRUCTIONS); @@ -998,7 +1020,7 @@ void LuaWidget::background() void luaLoadWidgetCallback() { const char * name=NULL; - int widgetOptions=0, createFunction=0, refreshFunction=0, backgroundFunction=0; + int widgetOptions=0, createFunction=0, updateFunction=0, refreshFunction=0, backgroundFunction=0; luaL_checktype(L, -1, LUA_TTABLE); @@ -1015,6 +1037,10 @@ void luaLoadWidgetCallback() createFunction = luaL_ref(L, LUA_REGISTRYINDEX); lua_pushnil(L); } + else if (!strcmp(key, "update")) { + updateFunction = luaL_ref(L, LUA_REGISTRYINDEX); + lua_pushnil(L); + } else if (!strcmp(key, "refresh")) { refreshFunction = luaL_ref(L, LUA_REGISTRYINDEX); lua_pushnil(L); @@ -1027,6 +1053,7 @@ void luaLoadWidgetCallback() if (name && createFunction) { LuaWidgetFactory * factory = new LuaWidgetFactory(name, widgetOptions, createFunction); + factory->updateFunction = updateFunction; factory->refreshFunction = refreshFunction; factory->backgroundFunction = backgroundFunction; }