Lua data sharing across scripts

Overview:

OpenTX considers all function, mix, and telemetry scripts to be 'permanent' scripts that share the same runtime environment. They are typically loaded at power up or when a new model is selected. However, they are also reinitialized when a script is added or removed during model editing.

Lua scoping rules:

Any variable or function not declared local is implicitly global. Care must be taken to avoid unintentional global declarations, and ensure that the globals you intentionally declare have unique names to avoid conflicts with scripts written by others.

Example:

This example consists of three scripts
  • count-dn.lua - this is a mix script than can be run stand alone to announce time remaining based on a user-defined switch and duration. It updates two global variables (gCountUp and gCountDown). It also creates output values (ctup and ctdn) which are for demonstration purposes only.
  • count-up.lua - this is an optional function script which will do count up announcements based on harded coded values.
  • shocount.lua - this is an optional telemetry script which simply shows the current values of the gCountUp and gCountDown variables.

Installation:

  • count-dn.lua
    • copy to /SCRIPTS/MIXES
    • configure on the transmitter CUSTOM SCRIPT page
      • suggested switch = "SA"
      • suggested mins = 3
      • suggested sw_high = 0
    • screen image:
  • count-up.lua
    • copy to /SCRIPTS/FUNCTIONS
    • configure on the transmitter SPECIAL FUNCTIONS page
      • suggested switch SA(down)
    • screen image:
  • shocount.lua
    • copy to /SCRIPTS/TELEMETRY
    • configure on the transmitter TELEMETRY page
    • screen image:

Script sources:

count-dn.lua

1
-- these globals can be referenced in function and telemetry scripts
2
gCountUp = 0
3
gCountDown = 0
4
5
local target
6
local running = false
7
local complete = false
8
local announcements = { 720, 660, 600, 540, 480, 420, 360, 300, 240, 180, 120, 105, 90, 75, 60, 55, 50, 45, 40, 35, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
9
local annIndex -- index into the announcements table (1 based)
10
local minUnit -- used by playNumber() for unit announcement
11
12
local input =
13
{
14
{ "switch", SOURCE}, -- switch used to activate count down
15
{ "mins", VALUE, 1, 12, 2 }, -- minutes to count down
16
{ "sw_high", VALUE, 0, 1, 1 } -- 0 = active when low, otherwise active when high
17
}
18
19
local output = {"ctup", "ctdn" }
20
21
local function init()
22
local version = getVersion()
23
if version < "2.1" then
24
minUnit = 16 -- unit for minutes in OpenTX 2.0
25
elseif version < "2.2" then
26
minUnit = 23 -- unit for minutes in OpenTX 2.1
27
else
28
minUnit = 25 -- unit for minutes in OpenTX 2.2
29
end
30
end
31
32
local function countdownIsRunning(switch, sw_high)
33
-- evaluate switch - return true if we should be counting down
34
if (sw_high > 0) then
35
return (switch > -1000)
36
else
37
return (switch < 1000)
38
end
39
end
40
41
local function run(switch, mins, sw_high)
42
local timenow = getTime() -- 10ms tick count
43
local minutes
44
local seconds
45
46
if (not countdownIsRunning(switch, sw_high)) then
47
running = false
48
complete = false
49
return 0, 0 -- ***** NOTE: early exit *****
50
end
51
52
if (complete) then
53
return 0, 0 -- must reset the switch before we go again
54
end
55
56
if (not running) then
57
running = true
58
target = timenow + ((mins * 60) * 100)
59
annIndex = 1
60
end
61
62
gCountDown = math.floor(((target - timenow) / 100) + .7) -- + is adj. to for announcement lag
63
gCountUp = (mins * 60) - gCountDown
64
65
while gCountDown < announcements[annIndex] do
66
annIndex = annIndex + 1 -- catch up
67
end
68
69
if gCountDown == announcements[annIndex] then
70
minutes = math.floor(gCountDown / 60)
71
seconds = gCountDown % 60
72
if minutes > 0 then
73
playNumber(minutes, minUnit, 0)
74
end
75
if seconds > 0 then
76
playNumber(seconds, 0, 0)
77
end
78
annIndex = annIndex + 1
79
end
80
81
if gCountDown <= 0 then
82
playNumber(0,0,0)
83
running = false
84
gCountDown = 0
85
complete = true
86
end
87
88
return gCountUp * 10.24, gCountDown * 10.24
89
end
90
91
return { input=input, output=output, init=init, run=run }
Copied!

count-up.lua

1
gCountUp = 0
2
3
local min = 5
4
local max = 30
5
local last = 0
6
local announcements = { 5, 10, 15, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 }
7
local annIndex = 1
8
9
local function run(e)
10
if not (gCountUp == last) then
11
last = gCountUp
12
for key, value in pairs(announcements) do
13
if value == last then
14
playNumber(last, 0, 0)
15
end
16
end
17
end
18
end
19
20
return{run=run}
Copied!

shocount.lua

1
-- these globals can be referenced in mix, function, and telemetry scripts
2
gCountUp = 0
3
gCountDown = 0
4
5
local function run(e)
6
lcd.clear()
7
lcd.drawText(1,1,"OpenTx Lua Data Sharing",0)
8
9
lcd.drawText(1,11,"gCountUp:", 0)
10
lcd.drawText(lcd.getLastPos()+2,11,gCountUp,0)
11
lcd.drawText(1, 21, "gCountDown:", 0)
12
lcd.drawText(lcd.getLastPos()+2,21,gCountDown,0)
13
end
14
15
return{run=run}
Copied!