forked from mvaldesshc/advanced-edgetx-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.lua
346 lines (319 loc) · 12.1 KB
/
set.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
-- This is the settings screen
local shared = ...
local minimalSwitches = { 'On', 'Disabled' }
local validSwitches = { 'sa', 'sb', 'sc', 'sd', 'se', 'sf', 'sg', 'sh', 'On', 'Disabled' }
valuesMap = { [0] = 'Up', [25] = 'Up-Mid', [50] = 'Mid', [75] = 'Mid-Dn', [100] = 'Dn' }
options = {
{ name = 'Arm',
settings = shared.switchSettings.arm,
switchRange = validSwitches
}, { name = 'Prearm',
settings = shared.switchSettings.prearm,
switchRange = validSwitches
}, { name = 'Acro',
settings = shared.switchSettings.acro,
switchRange = validSwitches
}, { name = 'Angle',
settings = shared.switchSettings.angle,
switchRange = validSwitches
}, { name = 'Horizon',
settings = shared.switchSettings.horizon,
switchRange = validSwitches
}, { name = 'Turtlemode',
settings = shared.switchSettings.turtle,
switchRange = validSwitches
}, { name = 'Pitmode',
settings = shared.switchSettings.pitmode,
switchRange = validSwitches
}, { name = 'GPS',
settings = shared.switchSettings.gps,
switchRange = minimalSwitches
}
}
local currentOption = options[1]
local danglingSetting = {'switch', 'target'}
function clearDanglingSettings()
danglingSetting = {'switch', 'target'}
end
local oldMonitorValues = {}
function monitor_change(array)
for idx, switch in pairs(array) do
if not (switch == 'On' or switch == 'Disabled') then -- continue to next iteration
actual = getValue(switch)
if oldMonitorValues[idx] and oldMonitorValues[idx] ~= actual then
oldMonitorValues = {}
return { idx = idx, switch = switch, value = actual }
end
oldMonitorValues[idx] = actual -- store for future comparison
end
end
end
function normalizeValue(value)
return (value + 1024) / 20.48
end
function commitSettings()
if danglingSetting.switch then
currentOption.settings.switch = danglingSetting.switch
end
if danglingSetting.target then
currentOption.settings.target = danglingSetting.target
end
clearDanglingSettings()
loadfile("/SCRIPTS/TELEMETRY/saveTable.lua")(shared.switchSettings, "/SCRIPTS/TELEMETRY/savedData.txt")
end
mainMenu = {
optionHoverIdx = 1, -- Main select (arm, prearm, etc)
mainMenuScroll = 0,
pagesize = 6,
configitems = 8,
currentSwitch = '',
currentTartget = '',
targetsAvailable = function()
return not(currentOption.settings.switch == 'Disabled' or currentOption.settings.switch == 'On')
end,
run = function(self)
local linePixelHeight = 9
local marginToHeadline = 1
-- Draw Headline
lcd.drawText(1, 1, 'CONFIG', LEFT + INVERS)
-- setup pagescrolling
if self.pagesize + self.mainMenuScroll - self.optionHoverIdx < 0 then
self.mainMenuScroll = self.mainMenuScroll + 1
end
if self.optionHoverIdx - self.mainMenuScroll <= 0 then
self.mainMenuScroll = self.mainMenuScroll - 1
end
for idx, item in pairs(options) do
if self.optionHoverIdx == idx then
currentOption = item -- store reference to item for saving / manipulation
end
if idx > self.mainMenuScroll then -- skip off screen items on top, off screen on bottom is irrelevant
---------- THIS xx xx --------------- e.g. 'arm'
local offset = (idx - self.mainMenuScroll) * linePixelHeight + marginToHeadline
lcd.drawText(2, offset, item.name) -- draw optionname e.g. 'arm'
---------- xx THIS xx --------------- e.g. 'sa'
lcd.drawText(screen.w - 48, offset, string.upper(item.settings.switch), --draw swichname e.g. 'sa'
(self.optionHoverIdx == idx and state ~= mainMenuTargetSelection) and INVERS or 0
)
---------- xx xx THIS --------------- e.g. 'up'
local targetAvailableForIteration = not( item.settings.switch == 'Disabled' or item.settings.switch == 'On' )
if targetAvailableForIteration then
lcd.drawText( --draw switch target e.g. 'up'
screen.w - 33, offset,
valuesMap[
(self.optionHoverIdx == idx and self.editing and state == mainMenuTargetSelection)
and danglingSetting.target -- display dangling target if it is being edited
or item.settings.target -- display stored value in all other cases
],
(self.optionHoverIdx == idx and state ~= mainMenuSwitchSelection) and INVERS + (
self.editing and state == mainMenuTargetSelection and BLINK or 0
) or 0
)
end
end
end
end
}
switchMenu = {
submenuSwitchHoverIdx = 0,
run = function(self)
local headline = ' '..currentOption.name..' '
--(currentOption.switchRange == minimalSwitches and ' [ On | Off ]' or ' [ Switch | On | Off ]')
lcd.drawText(
0,
1,
headline,
INVERS + LEFT
)
catch = monitor_change(currentOption.switchRange)
if catch then
self.submenuSwitchHoverIdx = catch.idx
danglingSetting.target = normalizeValue(catch.value) -- also store switch position
end
for idx, switch in pairs (currentOption.switchRange) do
-- hover over saved switch upon first entering the screen
if self.submenuSwitchHoverIdx == 0 and switch == currentOption.settings.switch then
self.submenuSwitchHoverIdx = idx -- if the switch is detected by the monitor we will also take it's value,
end
-- y position
local itemsPerRow = 3
local row = math.ceil(idx / itemsPerRow) + 1
local yoffset = row * 9
if switch == 'On' or switch == 'Disabled' then -- assign special place at the bottom for 'On' and 'Disabled' Menuitems
yoffset = currentOption.switchRange == minimalSwitches and 30 or 50
itemsPerRow = 2
end
-- x position
local column = (idx-1) % itemsPerRow + 1
local itemWidth = ( screen.w / itemsPerRow )
local xoffset = itemWidth * column - itemWidth / 2
-- draw
lcd.drawText(xoffset, yoffset,
-- let the user know we sometimes also store the value if it was detected by displaying it behind the switch
string.upper(switch)..(
self.submenuSwitchHoverIdx == idx and (
danglingSetting.target and ' ('..valuesMap[danglingSetting.target]..')' or ''
) or ''
),
CENTER + SMLSIZE + (self.submenuSwitchHoverIdx == idx and INVERS or 0))
if self.submenuSwitchHoverIdx == idx then
danglingSetting.switch = switch -- store switch for saving
end
end
end
}
mainMenuSwitchSelection = {
pagesize = mainMenu.pagesize,
configitems = mainMenu.configitems,
run = function(self)
self.optionHoverIdx = mainMenu.optionHoverIdx
self.mainMenuScroll = mainMenu.mainMenuScroll
mainMenu.run(self) -- run mainmenu with settings from this scope
end
}
mainMenuTargetSelection = {
editing = false,
pagesize = mainMenu.pagesize,
configitems = mainMenu.configitems,
run = function(self)
self.optionHoverIdx = mainMenu.optionHoverIdx
self.mainMenuScroll = mainMenu.mainMenuScroll
-- update Target
if self.editing then
if not(danglingSetting.target) then
danglingSetting.target = currentOption.settings.target
end
catch = monitor_change({currentOption.settings.switch})
if catch then
danglingSetting.target = normalizeValue(catch.value)
end
end
mainMenu.run(self) -- run mainmenu with settings from this scope
end
}
state = mainMenu
local states = {
[mainMenu] = {
[EVT_ROT_RIGHT] = (function() -- scroll menu down
-- print('main menu event rotate right')
if mainMenu.optionHoverIdx < mainMenu.configitems then
mainMenu.optionHoverIdx = mainMenu.optionHoverIdx +1
end
end),
[EVT_ROT_LEFT] = (function() -- scroll menu up
-- print('main menu event rotate left')
mainMenu.optionHoverIdx = mainMenu.optionHoverIdx <= 1 and 1 or mainMenu.optionHoverIdx - 1
end),
[EVT_ENTER_BREAK] = (function() -- enter switchmenu or mainMenuSubselected
-- print('main menu event enter')
state = mainMenu.targetsAvailable() and mainMenuSwitchSelection or switchMenu
end),
[EVT_EXIT_BREAK] = (function() -- exit screen
-- print('main menu event exit')
shared.changeScreen(-1)
end),
},
[mainMenuSwitchSelection] = {
[EVT_ROT_RIGHT] = (function() -- toggle switch/target
-- print('main menu event rotate right')
state = mainMenuTargetSelection
end),
[EVT_ROT_LEFT] = (function() -- toggle switch/target
-- print('main menu event rotate left')
state = mainMenuTargetSelection
end),
[EVT_ENTER_BREAK] = (function() -- enter switchmenu or save target
-- print('main menu event enter')
state = switchMenu
end),
[EVT_EXIT_BREAK] = (function() -- enter mainMenu
-- print('main menu event exit')
state = mainMenu
end),
},
[mainMenuTargetSelection] = {
[EVT_ROT_RIGHT] = (function() -- toggle switch/target
-- print('main menu event rotate right')
if mainMenuTargetSelection.editing then
danglingSetting.target = (danglingSetting.target - 25) % 125 -- 25 : normalized step, 100: max value
else
state = mainMenuSwitchSelection
end
end),
[EVT_ROT_LEFT] = (function() -- toggle switch/target
-- print('main menu event rotate left')
if mainMenuTargetSelection.editing then
danglingSetting.target = (danglingSetting.target + 25) % 125 -- 25 : normalized step, 100: max value
else
state = mainMenuSwitchSelection
end
end),
[EVT_ENTER_BREAK] = (function() -- enter switchmenu or save target
-- print('main menu event enter')
if mainMenuTargetSelection.editing then
mainMenuTargetSelection.editing = false
commitSettings()
if not mainMenu.targetsAvailable() then
state = mainMenuSwitchSelection
end
else
mainMenuTargetSelection.editing = true
end
end),
[EVT_EXIT_BREAK] = (function() -- enter mainMenu
-- print('main menu event exit')
clearDanglingSettings()
if mainMenuTargetSelection.editing then
mainMenuTargetSelection.editing = false
else
state = mainMenu
end
end),
},
[switchMenu] = {
[EVT_ROT_RIGHT] = (function() -- scroll to next switch
-- print('main menu event rotate right')
switchMenu.submenuSwitchHoverIdx = switchMenu.submenuSwitchHoverIdx + 1
local maxItems = currentOption.switchRange == minimalSwitches and 2 or 10 -- handle overflow
if switchMenu.submenuSwitchHoverIdx > maxItems
then switchMenu.submenuSwitchHoverIdx = 1
end
danglingSetting.target = nil --clear monitored target
end),
[EVT_ROT_LEFT] = (function() -- scroll to previous switch
-- print('main menu event rotate left')
switchMenu.submenuSwitchHoverIdx = switchMenu.submenuSwitchHoverIdx - 1
if switchMenu.submenuSwitchHoverIdx < 1 then
local maxItems = currentOption.switchRange == minimalSwitches and 2 or 10 -- handle overflow
switchMenu.submenuSwitchHoverIdx = maxItems
end
danglingSetting.target = nil --clear monitored target
end),
[EVT_ENTER_BREAK] = (function() -- save switch and enter switchmenu or mainMenuSubselected
-- print('main menu event enter')
local storedTarget = danglingSetting.target ~= nil
commitSettings()
switchMenu.submenuSwitchHoverIdx = 0
state = ( -- jump directly to the mainMenu if a target was detected or after the selection none are Available anymore
not mainMenu.targetsAvailable()
or storedTarget
) and mainMenu or mainMenuSwitchSelection
end),
[EVT_EXIT_BREAK] = (function() -- enter switchmenu or mainMenuSubselected
-- print('main menu event exit')
clearDanglingSettings()
switchMenu.submenuSwitchHoverIdx = 0
state = mainMenu.targetsAvailable() and mainMenuSwitchSelection or mainMenu
end),
},
}
function shared.run(event)
-- handle events
local destination = states[state][event]
if(destination) then -- check if the state cares about the event
destination() -- run events for state
end
-- draw screen
lcd.clear()
state:run() -- run state
end