Skip to content

Commit

Permalink
feat(lua): Port of NodeMCU Lua53 read-only tables (#2994)
Browse files Browse the repository at this point in the history
* chore(lua): revert useless c light function change

This change was part of the eLua LTR patches (based on 5.1), whereby this feature was already integrated in Lua 5.2.

* feat(lua): port of NodeMCU Lua53 read-only tables

# Conflicts:
#	radio/src/lua/CMakeLists.txt
#	radio/src/lua/api_general.cpp

* fix(nv14): missing SDRAM definition

* fix: l_mathop

* fix: use float functions in drawPower()

* fix: ltable resize

* fix: correct name is "bit32"

* fix: first Lua context must be closed last

As the lookup cache is attached to that one

* Revert to Lua using double

* Ensure byte-code compatibility

* Fix possible compatibility issue with `lua_typename`/`luaL_typename`

* Fix Bitmap symbol table

* Re-add table lib for COLORLCD
  • Loading branch information
raphaelcoeffic authored Feb 9, 2023
1 parent 72193ed commit be3f8d7
Show file tree
Hide file tree
Showing 43 changed files with 1,389 additions and 2,213 deletions.
74 changes: 1 addition & 73 deletions radio/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ endif()

add_subdirectory(bitmaps)
add_subdirectory(fonts)
add_subdirectory(lua)

if(NOT PCB STREQUAL 9XRPRO)
option(DBLKEYS "Double Keys" ON)
Expand Down Expand Up @@ -177,78 +176,7 @@ if(RTC_BACKUP_RAM)
endif()

if(LUA)
add_definitions(-DLUA)
if(LUA_COMPILER)
add_definitions(-DLUA_COMPILER)
endif()
if(LUA_ALLOCATOR_TRACER AND DEBUG)
add_definitions(-DLUA_ALLOCATOR_TRACER)
endif()
if(NOT "${LUA_SCRIPT_LOAD_MODE}" STREQUAL "")
add_definitions(-DLUA_SCRIPT_LOAD_MODE="${LUA_SCRIPT_LOAD_MODE}")
endif()
include_directories(${LUA_DIR})
set(RADIO_DEPENDENCIES ${RADIO_DEPENDENCIES} ${LUA_EXPORT})
if(LUA_MIXER)
add_definitions(-DLUA_MODEL_SCRIPTS)
endif()

# TODO: LUA CMAKE
set(SRC ${SRC}
lua/interface.cpp
lua/api_general.cpp
lua/api_model.cpp
lua/api_filesystem.cpp
lua/lua_event.cpp
)

if(GUI_DIR STREQUAL colorlcd)
set(SRC ${SRC} lua/api_colorlcd.cpp lua/widgets.cpp)
else()
set(SRC ${SRC} lua/api_stdlcd.cpp)
endif()

set(LUA_SRC
lapi.c
lcode.c
lctype.c
ldebug.c
ldo.c
ldump.c
lfunc.c
lgc.c
llex.c
lmem.c
lobject.c
lopcodes.c
lparser.c
lstate.c
lstring.c
ltable.c
lrotable.c
ltm.c
lundump.c
lvm.c
lzio.c
linit.c
lbaselib.c
lmathlib.c
lbitlib.c
loadlib.c
lauxlib.c
ltablib.c
lcorolib.c
liolib.c
lstrlib.c
)

foreach(FILE ${LUA_SRC})
set(SRC ${SRC} ${LUA_DIR}/${FILE})
endforeach()
if(MSVC)
# silence lots of warnings regarding "insecure" use of functions (sprintf, strcpy, etc) from Lua code
set(WARNING_FLAGS "${WARNING_FLAGS} /D_CRT_SECURE_NO_WARNINGS")
endif()
include(lua/CMakeLists.txt)
endif()

if (LUA_MIXER AND NOT LUA)
Expand Down
6 changes: 3 additions & 3 deletions radio/src/gui/common/stdlcd/draw_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,18 @@ bool isSwitchAvailableInCustomFunctions(int swtch)

void drawPower(coord_t x, coord_t y, int8_t dBm, LcdFlags att)
{
float power_W_PREC1 = pow(10.0, (dBm - 30.0) / 10.0) * 10;
float power_W_PREC1 = powf(10.0, (dBm - 30.0) / 10.0) * 10;
if (dBm >= 30) {
lcdDrawNumber(x, y, power_W_PREC1, PREC1 | att);
lcdDrawText(lcdNextPos, y, "W", att);
}
else if (dBm < 10) {
uint16_t power_MW_PREC1 = round(power_W_PREC1 * 1000);
uint16_t power_MW_PREC1 = roundf(power_W_PREC1 * 1000);
lcdDrawNumber(x, y, power_MW_PREC1, PREC1 | att);
lcdDrawText(lcdNextPos, y, "mW", att);
}
else {
uint16_t power_MW = round(power_W_PREC1 * 100);
uint16_t power_MW = roundf(power_W_PREC1 * 100);
if (power_MW >= 50) {
power_MW = (power_MW / 5) * 5;
lcdDrawNumber(x, y, power_MW, att);
Expand Down
73 changes: 73 additions & 0 deletions radio/src/lua/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,76 @@ if(PYTHONINTERP_FOUND)
add_lua_export_target(t16 ${LUA_INCLUDES} -DPCBHORUS -DPCBX10 -DRADIO_T16)
add_lua_export_target(nv14 ${LUA_INCLUDES} -I${RADIO_SRC_DIR}/targets/nv14 -DPCBNV14)
endif()

add_definitions(-DLUA)

if(LUA_COMPILER)
add_definitions(-DLUA_COMPILER)
endif()

if(LUA_ALLOCATOR_TRACER AND DEBUG)
add_definitions(-DLUA_ALLOCATOR_TRACER)
endif()

if(NOT "${LUA_SCRIPT_LOAD_MODE}" STREQUAL "")
add_definitions(-DLUA_SCRIPT_LOAD_MODE="${LUA_SCRIPT_LOAD_MODE}")
endif()

include_directories(${LUA_DIR})

set(RADIO_DEPENDENCIES ${RADIO_DEPENDENCIES} ${LUA_EXPORT})

if(LUA_MIXER)
add_definitions(-DLUA_MODEL_SCRIPTS)
endif()

set(SRC ${SRC}
lua/interface.cpp
lua/api_general.cpp
lua/api_model.cpp
lua/api_filesystem.cpp
lua/lua_event.cpp
)

if(GUI_DIR STREQUAL colorlcd)
set(SRC ${SRC} lua/api_colorlcd.cpp lua/widgets.cpp)
else()
set(SRC ${SRC} lua/api_stdlcd.cpp)
endif()

set(LUA_SRC
lapi.c
lcode.c
lctype.c
ldebug.c
ldo.c
ldump.c
lfunc.c
lgc.c
llex.c
lmem.c
lobject.c
lopcodes.c
lparser.c
lstate.c
lstring.c
ltable.c
ltm.c
lundump.c
lvm.c
lzio.c
linit.c
lbaselib.c
lmathlib.c
lbitlib.c
loadlib.c
lauxlib.c
ltablib.c
lcorolib.c
liolib.c
lstrlib.c
)

foreach(FILE ${LUA_SRC})
set(SRC ${SRC} ${LUA_DIR}/${FILE})
endforeach()
128 changes: 70 additions & 58 deletions radio/src/lua/api_colorlcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@
* GNU General Public License for more details.
*/

#define LUA_LIB

#include <cctype>
#include <cstdio>

#include "opentx.h"
#include "libopenui.h"
#include "widget.h"

#include "lua_api.h"
#include "api_colorlcd.h"

#define BITMAP_METATABLE "BITMAP*"

constexpr coord_t INVERT_BOX_MARGIN = 2;
constexpr int8_t text_horizontal_offset[7] {-2,-1,-2,-2,-2,-2,-2};
constexpr int8_t text_vertical_offset[7] {0,0,0,0,0,-1,7};

BitmapBuffer* luaLcdBuffer = nullptr;
Widget* runningFS = nullptr;

Expand Down Expand Up @@ -499,8 +510,6 @@ static int luaLcdDrawSource(lua_State *L)
return 0;
}

#define LUA_BITMAPHANDLE "BITMAP*"

/*luadoc
@function Bitmap.open(name)
Expand Down Expand Up @@ -547,15 +556,15 @@ static int luaOpenBitmap(lua_State *L)
TRACE("luaOpenBitmap: %p (%u)", *b, size);
}

luaL_getmetatable(L, LUA_BITMAPHANDLE);
luaL_getmetatable(L, BITMAP_METATABLE);
lua_setmetatable(L, -2);

return 1;
}

static BitmapBuffer * checkBitmap(lua_State * L, int index)
{
BitmapBuffer ** b = (BitmapBuffer **)luaL_checkudata(L, index, LUA_BITMAPHANDLE);
BitmapBuffer ** b = (BitmapBuffer **)luaL_checkudata(L, index, BITMAP_METATABLE);
return *b;
}

Expand Down Expand Up @@ -633,7 +642,7 @@ static int luaBitmapResize(lua_State * L)
TRACE("luaResizeBitmap: %p (%u)", *n, size);
}

luaL_getmetatable(L, LUA_BITMAPHANDLE);
luaL_getmetatable(L, BITMAP_METATABLE);
lua_setmetatable(L, -2);

return 1;
Expand Down Expand Up @@ -683,23 +692,6 @@ static int luaDestroyBitmap(lua_State * L)
return 0;
}

const luaL_Reg bitmapFuncs[] = {
{ "open", luaOpenBitmap },
{ "getSize", luaGetBitmapSize },
{ "resize", luaBitmapResize },
{ "toMask", luaBitmapTo8bitMask },
{ "__gc", luaDestroyBitmap },
{ NULL, NULL }
};

void registerBitmapClass(lua_State * L)
{
luaL_newmetatable(L, LUA_BITMAPHANDLE);
luaL_setfuncs(L, bitmapFuncs, 0);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_setglobal(L, "Bitmap");
}

/*luadoc
@function lcd.drawBitmap(bitmap, x, y [, scale])
Expand Down Expand Up @@ -1427,39 +1419,59 @@ static int luaLcdExitFullScreen(lua_State *L)
return 0;
}

const luaL_Reg lcdLib[] = {
{ "refresh", luaLcdRefresh },
{ "clear", luaLcdClear },
{ "resetBacklightTimeout", luaLcdResetBacklightTimeout },
{ "drawPoint", luaLcdDrawPoint },
{ "drawLine", luaLcdDrawLine },
{ "drawRectangle", luaLcdDrawRectangle },
{ "drawFilledRectangle", luaLcdDrawFilledRectangle },
{ "invertRect", luaLcdInvertRect },
{ "drawText", luaLcdDrawText },
{ "drawTextLines", luaLcdDrawTextLines },
{ "sizeText", luaLcdSizeText },
{ "drawTimer", luaLcdDrawTimer },
{ "drawNumber", luaLcdDrawNumber },
{ "drawChannel", luaLcdDrawChannel },
{ "drawSwitch", luaLcdDrawSwitch },
{ "drawSource", luaLcdDrawSource },
{ "drawGauge", luaLcdDrawGauge },
{ "drawBitmap", luaLcdDrawBitmap },
{ "drawBitmapPattern", luaLcdDrawBitmapPattern },
{ "drawBitmapPatternPie", luaLcdDrawBitmapPatternPie },
{ "setColor", luaLcdSetColor },
{ "getColor", luaLcdGetColor },
{ "RGB", luaRGB },
{ "drawCircle", luaLcdDrawCircle },
{ "drawFilledCircle", luaLcdDrawFilledCircle },
{ "drawTriangle", luaLcdDrawTriangle },
{ "drawFilledTriangle", luaLcdDrawFilledTriangle },
{ "drawArc", luaLcdDrawArc },
{ "drawPie", luaLcdDrawPie },
{ "drawAnnulus", luaLcdDrawAnnulus },
{ "drawLineWithClipping", luaLcdDrawLineWithClipping },
{ "drawHudRectangle", luaLcdDrawHudRectangle },
{ "exitFullScreen", luaLcdExitFullScreen },
{ NULL, NULL } /* sentinel */
};
LROT_BEGIN(lcdlib, NULL, 0)
LROT_FUNCENTRY( refresh, luaLcdRefresh )
LROT_FUNCENTRY( clear, luaLcdClear )
LROT_FUNCENTRY( resetBacklightTimeout, luaLcdResetBacklightTimeout )
LROT_FUNCENTRY( drawPoint, luaLcdDrawPoint )
LROT_FUNCENTRY( drawLine, luaLcdDrawLine )
LROT_FUNCENTRY( drawRectangle, luaLcdDrawRectangle )
LROT_FUNCENTRY( drawFilledRectangle, luaLcdDrawFilledRectangle )
LROT_FUNCENTRY( invertRect, luaLcdInvertRect )
LROT_FUNCENTRY( drawText, luaLcdDrawText )
LROT_FUNCENTRY( drawTextLines, luaLcdDrawTextLines )
LROT_FUNCENTRY( sizeText, luaLcdSizeText )
LROT_FUNCENTRY( drawTimer, luaLcdDrawTimer )
LROT_FUNCENTRY( drawNumber, luaLcdDrawNumber )
LROT_FUNCENTRY( drawChannel, luaLcdDrawChannel )
LROT_FUNCENTRY( drawSwitch, luaLcdDrawSwitch )
LROT_FUNCENTRY( drawSource, luaLcdDrawSource )
LROT_FUNCENTRY( drawGauge, luaLcdDrawGauge )
LROT_FUNCENTRY( drawBitmap, luaLcdDrawBitmap )
LROT_FUNCENTRY( drawBitmapPattern, luaLcdDrawBitmapPattern )
LROT_FUNCENTRY( drawBitmapPatternPie, luaLcdDrawBitmapPatternPie )
LROT_FUNCENTRY( setColor, luaLcdSetColor )
LROT_FUNCENTRY( getColor, luaLcdGetColor )
LROT_FUNCENTRY( RGB, luaRGB )
LROT_FUNCENTRY( drawCircle, luaLcdDrawCircle )
LROT_FUNCENTRY( drawFilledCircle, luaLcdDrawFilledCircle )
LROT_FUNCENTRY( drawTriangle, luaLcdDrawTriangle )
LROT_FUNCENTRY( drawFilledTriangle, luaLcdDrawFilledTriangle )
LROT_FUNCENTRY( drawArc, luaLcdDrawArc )
LROT_FUNCENTRY( drawPie, luaLcdDrawPie )
LROT_FUNCENTRY( drawAnnulus, luaLcdDrawAnnulus )
LROT_FUNCENTRY( drawLineWithClipping, luaLcdDrawLineWithClipping )
LROT_FUNCENTRY( drawHudRectangle, luaLcdDrawHudRectangle )
LROT_FUNCENTRY( exitFullScreen, luaLcdExitFullScreen )
LROT_END(lcdlib, NULL, 0)

LROT_BEGIN(bitmap_mt, NULL, LROT_MASK_GC)
LROT_FUNCENTRY( __gc, luaDestroyBitmap )
LROT_FUNCENTRY( getSize, luaGetBitmapSize )
LROT_FUNCENTRY( resize, luaBitmapResize )
LROT_FUNCENTRY( toMask, luaBitmapTo8bitMask )
LROT_END(bitmap_mt, NULL, LROT_MASK_GC)

LROT_BEGIN(bitmaplib, NULL, 0)
LROT_FUNCENTRY( open, luaOpenBitmap )
LROT_FUNCENTRY( getSize, luaGetBitmapSize )
LROT_FUNCENTRY( resize, luaBitmapResize )
LROT_FUNCENTRY( toMask, luaBitmapTo8bitMask )
LROT_END(bitmaplib, NULL, 0)

extern "C" {
LUALIB_API int luaopen_bitmap(lua_State * L) {
luaL_rometatable( L, BITMAP_METATABLE, LROT_TABLEREF(bitmap_mt));
return 0;
}
}
25 changes: 2 additions & 23 deletions radio/src/lua/api_colorlcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,6 @@
* GNU General Public License for more details.
*/

//
// Obsoleted definitions:
// -> please check against libopenui_defines.h for conflicts
// -> here we use the 4 most significant bits for our flags (32 bit unsigned)
//
// INVERS & BLINK are used in most scripts, let's offer a compatibility mode.
//
#undef INVERS
#undef BLINK
#include "definitions.h"

#define INVERS 0x01u
#define BLINK 0x1000u
#define RGB_FLAG 0x8000u

constexpr coord_t INVERT_BOX_MARGIN = 2;

constexpr int8_t text_horizontal_offset[7] {-2,-1,-2,-2,-2,-2,-2};
constexpr int8_t text_vertical_offset[7] {0,0,0,0,0,-1,7};

extern bool luaLcdAllowed;
extern BitmapBuffer * luaLcdBuffer;
extern Widget * runningFS;

LcdFlags flagsRGB(LcdFlags flags);
EXTERN_C(LUALIB_API int luaopen_bitmap(lua_State * L));
Loading

0 comments on commit be3f8d7

Please sign in to comment.