Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 13 commits into from
Feb 9, 2023
Merged

Conversation

raphaelcoeffic
Copy link
Member

@raphaelcoeffic raphaelcoeffic commented Jan 6, 2023

Port of #2986 to main, so that it can be tested separately.

Summary of changes:

  • removed duplicated light C functions (introduced by eLua LTR patches, but already integrated in Lua 5.2)
  • removed read-only table implementation introduced in OpenTx 2.3.0 (see here)
  • added read-only table implementation from NodeMCU (see here) including lookup cache (512 bytes)
  • changed LUA_NUMBER default type from double to float

After some tweaks, it seems that changing from double to float has a nice side effect: it removes some huge floating point functions and uses the smaller float version. This allows to regain around 8 KB of Flash memory!

Please note: Changing from double to float has been postponed as it introduces issue with LcdFlags using the full 32bits, whereby a float can only represent 24 bits faithfully.

TODOs:

  • test / fix byte-code compatibility

@stronnag
Copy link
Contributor

stronnag commented Jan 6, 2023

TX16S, INAV Lua script.
On selecting any model that uses the INAV lua, the screen freezes, requiring hard shutdown (power button 10s).

tx16s-2994

Models not using the script load normally.

@gagarinlg
Copy link
Member

And if you delete the luac files and just leave the lua files on the SD card?

@stronnag
Copy link
Contributor

stronnag commented Jan 6, 2023

And if you delete the luac files and just leave the lua files on the SD card?

Same. Freeze

@raphaelcoeffic
Copy link
Member Author

raphaelcoeffic commented Jan 6, 2023

@stronnag it would be highly beneficial if you could plug a on-board debugger ;-) That would at least give us a hint on what is happening.

When you say "model that use the INAV script", you mean that have a widget with that script, correct?

@stronnag
Copy link
Contributor

stronnag commented Jan 6, 2023

I don't have a on-board debugger, alas.
The INAV lua script comprises WIDGET & SCRIPTS/TELEMETRY

In companion29, having deleted every luac, the default model happens to use the INAV script.

0.02s: luaDumpState(/WIDGETS/GPS/main.luac): Saved bytecode to file.
0.02s: luaLoadWidgetCallback()
0.02s: register widget GPS
0.02s: Loaded Lua widget GPS
0.02s: 	not found
0.02s: f_stat(/home/jrh/Projects/EdgeTX28/sdcard/WIDGETS/iNav) = OK
0.02s: f_stat(/home/jrh/Projects/EdgeTX28/sdcard/WIDGETS/iNav/main.lua) = OK
0.02s: luaLoadFile(/WIDGETS/iNav/main.lua)
0.02s: 	not found
0.02s: f_stat(/home/jrh/Projects/EdgeTX28/sdcard/WIDGETS/iNav/main.luac) = error 2 (No such file or directory)
0.02s: f_stat(/home/jrh/Projects/EdgeTX28/sdcard/WIDGETS/iNav/main.lua) = OK
0.02s: luaLoadScriptFileToState(/WIDGETS/iNav/main.lua, T): loading /WIDGETS/iNav/main.lua
0.02s: f_open(/home/jrh/Projects/EdgeTX28/sdcard/WIDGETS/iNav/main.lua, 1) = 0x7f02f0002f30 (FIL 0x7f02fb7fcf38)
0.02s: f_close(0x7f02f0002f30) (FIL:0x7f02fb7fcf38)
0.02s: 	not found
0.02s: f_open(/home/jrh/Projects/EdgeTX28/sdcard/WIDGETS/iNav/main.luac, a) = 0x7f02f0002f30 (FIL 0x7f02fb7fd620)
0.02s: f_close(0x7f02f0002f30) (FIL:0x7f02fb7fd620)
0.02s: f_utime(/home/jrh/Projects/EdgeTX28/sdcard/WIDGETS/iNav/main.luac) set mtime = Tue Jan 25 09:07:26 2022

0.02s: luaDumpState(/WIDGETS/iNav/main.luac): Saved bytecode to file.
0.02s: 	not found
0.02s: f_stat(/home/jrh/Projects/EdgeTX28/sdcard/SCRIPTS/TELEMETRY/iNav.luac) = error 2 (No such file or directory)
0.02s: f_stat(/home/jrh/Projects/EdgeTX28/sdcard/SCRIPTS/TELEMETRY/iNav.lua) = OK
0.02s: luaLoadScriptFileToState(/SCRIPTS/TELEMETRY/iNav, bt): loading /SCRIPTS/TELEMETRY/iNav.lua
0.02s: f_open(/home/jrh/Projects/EdgeTX28/sdcard/SCRIPTS/TELEMETRY/iNav.lua, 1) = 0x7f02f0002f30 (FIL 0x7f02fb7fcc78)

Freeze. In the splash screen.

@raphaelcoeffic
Copy link
Member Author

@stronnag it should work better now: I had a nasty bug left from clean-up of some strange OpenTx change to computesizes(). It seems I had not fully reverted the code.

@stronnag
Copy link
Contributor

stronnag commented Jan 6, 2023

For the record and as reported on Discord:

  • 3b04042 et al fix the freezes
  • There are undesirable visual artefacts that are NOT present in contemporary main

Screenshot from 2023-01-06 13-09-29

@raphaelcoeffic
Copy link
Member Author

@stronnag regarding the visual artefacts: the issue is with the flags....

Lua uses in v5.2 only floating point numbers for storing any number. Now that we move to single precision floating point numbers, we have only 24 significant bits, which is not enough to encode the flags in full.

Using just RIGHT (8) is ok, as it makes a number that can be represented this way.

When, however, a color like WHITE is added to the mix, the flags become 0xFFFF0008, which cannot be represented with a 32 bit float...

So what happens is that the addition overflows, and this is what we see in the C code:
Screenshot 2023-01-06 at 15 42 15

There are different possible solution to this issue:

  • make flags a custom type (using "light user data" and a meta-table overloading the necessary operations like +),
  • jump the gun and move to Lua 5.3, which has support for 32 integers, which would be exactly what we need.

@raphaelcoeffic
Copy link
Member Author

@stronnag I reverted the change to float. It should behave normally now.

@stronnag
Copy link
Contributor

stronnag commented Jan 7, 2023

@stronnag I reverted the change to float. It should behave normally now.

OK again in the simulator and radio (TX16S).

@pfeerick pfeerick added the lua label Jan 8, 2023
@pfeerick pfeerick added this to the 2.9 milestone Jan 19, 2023
@pfeerick pfeerick self-assigned this Jan 26, 2023
@pfeerick
Copy link
Member

pfeerick commented Feb 3, 2023

When jumping between v2.8.0 and this PR build, most Lua widgets and scripts I tried seem to 'just work' fine... (i.e. most of the standard widgets, ExpressLRS lua, inav, few other random ones on the radio)... the exception being whatever build of Yappu telemetry I have on the TX16S (and also latest 2.0.0 dev build) ... works fine on 2.8.0 but with this PR I get one of those lovely cryptic ERROR in create(): ?:0: attempt to index field '?' (a table value errors... for both the yappu widget and also standalone lua config tool... I'm guessing something has changed? 😭

@raphaelcoeffic
Copy link
Member Author

@pfeerick This errors almost looks like some binary loading error. Did you try for the fun to remove the .luac files and see if that compiles fine from source on radio? If this is not the case, we have a real API change, which was not intended.

@pfeerick
Copy link
Member

pfeerick commented Feb 6, 2023

Nope. I'll give that a try and report back.

@raphaelcoeffic
Copy link
Member Author

raphaelcoeffic commented Feb 8, 2023

@pfeerick at least on Boxer and Color simu I can load ELRS LUA seamlessly regardless of using main, 2.8 or this PR. So as of now I cannot reproduce this. I had this issue earlier (basically before applying 670a515).

@raphaelcoeffic
Copy link
Member Author

I just found something that might affect lua_typename(). This could have affected this part of Yaapu script: https://github.com/yaapu/FrskyTelemetryScript/blob/master/OTX_ETX/c480x272/SD/WIDGETS/yaapu/main.lua#L654

-----------------------------
-- clears the loaded table
-- and recovers memory
-----------------------------
utils.clearTable = function(t)
  if type(t)=="table" then
    for i,v in pairs(t) do
      if type(v) == "table" then
        utils.clearTable(v)
      end
      t[i] = nil
    end
  end
  t = nil
  collectgarbage()
  collectgarbage()
end

The type() function uses lua_typename() underneath, and I believe it could have caused some troubles with the exact type returned being different (shift by 2 in the string table....).

This has been fixed with d7bc59e.

@pfeerick
Copy link
Member

pfeerick commented Feb 8, 2023

As mentioned above, yappu telemetry was the exception, and I've not gotten back to this PR yet (or anything EdgeTX today yet 😭 ) but will try deleting the .luac shortly, and then with updated PR. As well as checking out the latest state of the module-api PR wrt EL18 and XJT :)

@raphaelcoeffic
Copy link
Member Author

@pfeerick actually it should work without deleting the .luac files. That was just a suggestion to be able to better check the issue.

@raphaelcoeffic
Copy link
Member Author

To enable tests on Companion simulation, I started these 2 manual builds:

@pfeerick
Copy link
Member

pfeerick commented Feb 8, 2023

I've just retried this with the latest commit, and TX16S now starts yappu without any issue :)

This change was part of the eLua LTR patches (based on 5.1), whereby this feature was already integrated in Lua 5.2.
# Conflicts:
#	radio/src/lua/CMakeLists.txt
#	radio/src/lua/api_general.cpp
@pfeerick pfeerick merged commit be3f8d7 into main Feb 9, 2023
@pfeerick pfeerick deleted the lua-rotables branch February 9, 2023 10:30
philmoz pushed a commit to philmoz/edgetx that referenced this pull request Feb 13, 2023
* main: (30 commits)
  feat(cpn): Radiomaster Boxer support (EdgeTX#2910)
  chore: Move __global_locale to FLASH (frees some RAM!) (EdgeTX#3169)
  chore: Remove obsolete language defines, single source for telemetry sensor names (EdgeTX#3119)
  feat(lua): Port of NodeMCU Lua53 read-only tables (EdgeTX#2994)
  chore: New module/serial API (EdgeTX#3055)
  chore: Updated SE translations 🇸🇪 (EdgeTX#3148)
  Update README.md
  fix(color): Duplicate selected theme, update color list (EdgeTX#3122)
  fix: Regenerate yaml, carryTrim => trimSource cleanup (EdgeTX#3121)
  chore(color): LVGLify custom mixer scripts page (EdgeTX#3071)
  fix(translation): String casting for CZ/IT/FR Fixes EdgeTX#3145
  fix(cpn): Save "set main view" number for special/global function (EdgeTX#3132)
  chore(color): LVGLify statistics and debug screens (EdgeTX#3072)
  chore(color): LVGLify and enhance model -> telemetry page (EdgeTX#3070)
  chore: Updates to Danish translations 🇩🇰 (EdgeTX#3128)
  chore: CN/TW translations for theme save and delete strings (EdgeTX#3129)
  chore: fix STM32 HAL headers
  fix(color): Overlap in logical switch monitor footer (EdgeTX#3137)
  fix(doc): LUA doc link to units.md
  feat(color): Change global variables layout to better match other pages (EdgeTX#3116)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants