Thread Tools
This thread is privately moderated by l shems, who may elect to delete unwanted replies.
Sep 06, 2018, 12:11 PM
Have Fun and Just Fly!
l shems's Avatar
Thread OP
Mini-HowTo

Easy way to save and load a table in OpenTX lua scripts


With read and write access in LUA on opentx, it is tempting to use a settings file to persist certain user choices for some scripts. However, though writing a settings table to a file is easy, retrieving it by reading the file content string per string is tricky. Formatting, nested structures, etc.

The solution to this is to write the table to a settings file in a format that is lua interpretable, and will return a table on using the lua loadfile() command.

In this way, you use the lua interpreter to parse the structure from the save table.

The following script, to be saved under the name "saveTable.lua", can be used to dump a table to a file, that is then retrievable using loadfile().

Code:
local table,fileName=...

local indent = ""

local function outputTable (tbl)
  if not (type(tbl)=="table") then 
    return tbl
  end
  indent=indent .. "   "
  local res= "\n" .. indent .. "{\n" 
  for k, v in pairs(tbl) do
    if type(k) == "number" then
      k="[" .. k .. "]"
    end
    if type(v) == "table" then
      res=res .. indent .. k .. " = " .. outputTable(v) .. ",\n" 
    elseif type(v) == "function" then
    elseif type(v) == "string" then
      res=res .. indent .. k .. " = '" .. v .. "',\n" 
    elseif type(v) == "boolean" then
      if v then res=res .. indent .. k .. " = true,\n"
      else res=res .. indent .. k .. " = false,\n"
      end
    else
      res=res .. indent .. k .. " = " .. v .. ",\n" 
    end
  end
  res = res ..   indent .. "}"
  indent=string.sub(indent,4) 
  return res 
end  

if io and (type(table) == "table") then
   
  local f = io.open(fileName, "w")


  io.write(f,"return " .. outputTable(table))
  io.close(f)
    
  f=nil

end
The table is saved by calling
Code:
loadfile("saveTable.lua")(tableToSave,"SavedTable")
the table can be loaded into a variable by:
Code:
table=loadfile("SavedTable")()
The following demofile must be run from the sdcard browser. it shows a counter that is increased by pressing enter. After stopping and restarting the script, it will continue on the last value:

Code:
local script = {}

local data = {test=1}

script.init = function()
  if loadfile("savedData") then
    data = loadfile("savedData")()
  end
end

script.background = function() end  
  
script.run = function (event)
  lcd.clear()
  lcd.drawText(1,1,data.test)
  if event == EVT_ENTER_BREAK then
    data.test = data.test + 1
    loadfile("saveTable.lua")(data,"savedData")
  end
  return 0
end

return script
Good luck!

P.S. the attached files need to be placed in the sdcard root folder.
Last edited by l shems; Sep 07, 2018 at 10:18 AM.
Sign up now
to remove ads between posts
Dec 26, 2018, 11:35 AM
Registered User
Because my data table was so large, it takes too much memory to store two tables, and two much CPU time to save the whole thing in 30 mS. Therefore, I save each key of my settings as a separate file. Additionally, they are specific to each mode.
If a setting isn't found on the SD card, the default settings are used and a new SD card file is created.
Here's the code that accomplishes it.

Quote:
local sName = "DATA_" .. model.getInfo().name .. "_"
for k,v in pairs(mySettings) do
local settingFileName = sName .. k

if loadfile("/myFolder/" .. settingFileName) then --load setting from sd, overwrite default
mySettings[k] = loadfile("/myFolder/" .. settingFileName)
print(k .. "overwritten")
else --create SD setting file from default
loadfile("/myFolder/saveTable.lua")(mySettings[k],"/myFolder/" .. settingFileName)
end

end
This method is also nice because an adjustment in the menu of one setting does not require writing the entire table to the SD card.

The one downside of this method is all the files end up in the same folder. If I could create folders in the SD card for each model using Lua, that would be great!
Last edited by Rfriedman; Dec 26, 2018 at 11:43 AM.
Dec 26, 2018, 03:38 PM
Have Fun and Just Fly!
l shems's Avatar
Thread OP
Quote:
Originally Posted by Rfriedman
Because my data table was so large, it takes too much memory to store two tables, and two much CPU time to save the whole thing in 30 mS. Therefore, I save each key of my settings as a separate file. Additionally, they are specific to each mode.
If a setting isn't found on the SD card, the default settings are used and a new SD card file is created.
Here's the code that accomplishes it.



This method is also nice because an adjustment in the menu of one setting does not require writing the entire table to the SD card.

The one downside of this method is all the files end up in the same folder. If I could create folders in the SD card for each model using Lua, that would be great!
Just wait for the new OpenTX version. Seems to support all that.

If not, well, ???
Dec 28, 2018, 10:12 AM
Registered User
Quote:
Originally Posted by l shems
just wait for the new opentx version. Seems to support all that.

If not, well, ???
new opentx version = 2.3?
Dec 28, 2018, 12:55 PM
Have Fun and Just Fly!
l shems's Avatar
Thread OP
Quote:
Originally Posted by Rfriedman
new opentx version = 2.3?
Yep. I think I saw some pull requests passing. Max Paperno was busy with it.


Quick Reply
Message:
Thread Tools

Similar Threads
Category Thread Thread Starter Forum Replies Last Post
Question OpenTx Horus LUA Scripts sten1860 Multirotor Drone Electronics 1 Nov 05, 2017 08:13 AM
Question OpenTx Horus LUA Scripts sten1860 Radios 0 Oct 31, 2017 04:11 AM
New Product Taranis Q X7 OpenTX Battery Capacity Remaining Lua Script RcRav Radios 0 Mar 31, 2017 03:17 PM
Discussion OpenTx Lua Script Lap Timer RcRav Radios 0 Jan 10, 2017 02:08 PM
Discussion L/D and Sink rate LUA script for OpenTx FabFlight Sailplane Talk 2 Mar 23, 2016 07:28 PM