Saving Memory
This chapter will show you some ways that large script projects can be fitted into the limited memory of our radios.
Regarding memory, the situation is a bit different for the radios with black/white or grey scale screens and telemetry scripts, and the radios with color screens and widget scripts. The telemetry script radios only have 128-192 KB RAM memory - that is very small! The widget script radios have 8 MB RAM memory. But the way that widgets are designed means that all widget scripts present on the SD card will be loaded into memory, whether or not they are actually used. Therefore, different strategies should be applied to save memory for the two different types of radios and scripts.

Telemetry Script Radios

Radios with black/white or grey scale screens and telemetry scripts such as e.g. FrSky Taranis, Xlite, Jumper T12 and Radiomaster TX12 have extremely small RAM memories, and therefore it may be necessary to divide up your script into smaller loadable modules.
The following simple example demonstrates how different screens can be loaded on demand, and how shared data can be stored in a table.
1
-- Main telemetry script
2
3
local shared = { }
4
shared.screens = {
5
"/SCRIPTS/Test/menu1.lua",
6
"/SCRIPTS/Test/menu2.lua",
7
"/SCRIPTS/Test/menu3.lua"
8
}
9
10
function shared.changeScreen(delta)
11
shared.current = shared.current + delta
12
if shared.current > #shared.screens then
13
shared.current = 1
14
elseif shared.current < 1 then
15
shared.current = #shared.screens
16
end
17
local chunk = loadScript(shared.screens[shared.current])
18
chunk(shared)
19
end
20
21
local function init()
22
shared.current = 1
23
shared.changeScreen(0)
24
end
25
26
local function run(event)
27
shared.run(event)
28
end
29
30
return { run = run, init = init }
Copied!
1
-- /SCRIPTS/Test/menu1.lua
2
3
local shared = ...
4
5
function shared.run(event)
6
lcd.clear()
7
lcd.drawText(20, 10, "Screen 1", MIDSIZE)
8
9
if event == EVT_VIRTUAL_NEXT then
10
shared.changeScreen(1)
11
elseif event == EVT_VIRTUAL_PREV then
12
shared.changeScreen(-1)
13
end
14
end
Copied!
1
-- /SCRIPTS/Test/menu2.lua
2
3
local shared = ...
4
5
function shared.run(event)
6
lcd.clear()
7
lcd.drawText(20, 10, "Screen 2", MIDSIZE)
8
9
if event == EVT_VIRTUAL_NEXT then
10
shared.changeScreen(1)
11
elseif event == EVT_VIRTUAL_PREV then
12
shared.changeScreen(-1)
13
end
14
end
Copied!
1
-- /SCRIPTS/Test/menu3.lua
2
3
local shared = ...
4
5
function shared.run(event)
6
lcd.clear()
7
lcd.drawText(20, 10, "Screen 3", MIDSIZE)
8
9
if event == EVT_VIRTUAL_NEXT then
10
shared.changeScreen(1)
11
elseif event == EVT_VIRTUAL_PREV then
12
shared.changeScreen(-1)
13
end
14
end
Copied!
The table shared contains data that is shared between the main telemetry script and the loadable screens. Notice that the functions shared.changeScreen and shared.run are also shared this way.
Code is loaded by shared.changeScreen with the loadScript function, which returns the loadable script as a chunk of code. The code is executed with shared as the argument, and the loadable script adds a new run function to the shared table. shared.run is called by run in the main script.

Widget Script Radios

Radios with color screens and widget scripts such as e.g. FrSky Horus, Jumper T16 and Radiomaster TX16S have fairly large RAM memories, but since all widget scripts present on the SD card are always loaded into memory, they could run out of memory if many big widget scripts are present on the card - even if they are not being used by the selected model. Therefore, large widget scripts should be divided into a small main script and a large loadable part. One way to accomplish this is the following.
1
-- main.lua
2
local name = "<widget name>"
3
4
local function create(zone, options)
5
-- Loadable code is called immediately and returns a widget table
6
return loadScript("/WIDGETS/" .. name .. "/loadable.lua")(zone, options)
7
end
8
9
local function refresh(widget, event, touchState)
10
widget.refresh(event, touchState)
11
end
12
13
local options = {
14
-- default options here
15
}
16
17
local function update(widget, options)
18
widget.update(options)
19
end
20
21
local function background(widget)
22
widget.background()
23
end
24
25
return {
26
name = name,
27
create = create,
28
refresh = refresh,
29
options = options,
30
update = update,
31
background = background
32
}
Copied!
The create function loads the file loadable.lua in the folder /WIDGETS/<widget name>/, and calls it immediately as described in the previous section. It passes zone and options as arguments to loadable.lua. This scripts adds the functions refresh, update and (optionally) background to the widget table:
1
-- loadable.lua
2
local zone, options = ...
3
4
-- The widget table will be returned to the main script.
5
local widget = { }
6
7
function widget.refresh(event, touchState)
8
-- refresh code here
9
end
10
11
function widget.update(opt)
12
options = opt
13
-- update code here
14
end
15
16
function widget.background()
17
-- background code here
18
end
19
20
-- Return to the create(...) function in the main script
21
return widget
Copied!
zone and options are stored in the closure of loadable.lua, therefore they do not need to be added to the widget table, as is commonly done.
Obviously, the bulk of the widget's code goes in loadable.lua, and is only loaded if the widgets is in fact being used. Therefore, if the widget is not used, only the small amount of code in main.lua is loaded into the radio's memory.
For an example of a widget that uses the above design pattern, please have a look at the EventDemo widget that is included on the SD card with EdgeTX for color screen radios.
Export as PDF
Copy link