Skip to content

Commit f78dba8

Browse files
authored
Unload background when not used + other minor changes (#292)
Unload background when not used + other minor changes
2 parents eea97b7 + d354232 commit f78dba8

File tree

4 files changed

+51
-63
lines changed

4 files changed

+51
-63
lines changed

src/SCRIPTS/BF/background.lua

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local INTERVAL = 50 -- in 1/100th seconds
22

33
local MSP_TX_INFO = 186
44

5-
local lastRunTS
5+
local lastRunTS = 0
66
local sensorId = -1
77
local dataInitialised = false
88
local data_init = nil
@@ -21,10 +21,6 @@ local function modelActive(sensorValue)
2121
return type(sensorValue) == "number" and sensorValue > 0
2222
end
2323

24-
local function init()
25-
lastRunTS = 0
26-
end
27-
2824
local function run_bg()
2925
-- run in intervals
3026
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
@@ -37,7 +33,7 @@ local function run_bg()
3733
data_init = assert(loadScript(SCRIPT_HOME .. "/data_init.lua"))()
3834
end
3935

40-
dataInitialised = data_init.init();
36+
dataInitialised = data_init();
4137

4238
if dataInitialised then
4339
data_init = nil
@@ -52,7 +48,7 @@ local function run_bg()
5248
rssi = 255
5349
end
5450

55-
values = {}
51+
local values = {}
5652
values[1] = rssi
5753

5854
protocol.mspWrite(MSP_TX_INFO, values)
@@ -68,4 +64,4 @@ local function run_bg()
6864
mspProcessTxQ()
6965
end
7066

71-
return { init=init, run_bg=run_bg }
67+
return run_bg

src/SCRIPTS/BF/data_init.lua

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ local function init()
2323
elseif apiVersionReceived and not timeIsSet then
2424
-- only send datetime one time after telemetry connection became available
2525
-- or when connection is restored after e.g. lipo refresh
26-
26+
local values = {}
2727
if apiVersion >= 1.041 then
2828
-- format: seconds after the epoch (32) / milliseconds (16)
2929
local now = getRtcTime()
3030

31-
values = {}
32-
3331
for i = 1, 4 do
3432
values[i] = bit32.band(now, 0xFF)
3533
now = bit32.rshift(now, 8)
@@ -42,7 +40,6 @@ local function init()
4240
local now = getDateTime()
4341
local year = now.year;
4442

45-
values = {}
4643
values[1] = bit32.band(year, 0xFF)
4744
year = bit32.rshift(year, 8)
4845
values[2] = bit32.band(year, 0xFF)
@@ -61,4 +58,4 @@ local function init()
6158
return apiVersionReceived and timeIsSet
6259
end
6360

64-
return { init=init }
61+
return init

src/SCRIPTS/BF/ui.lua

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,9 @@ local globalTextOptions = TEXT_COLOR or 0
4545

4646
local function saveSettings(new)
4747
if Page.values then
48-
local payload = {}
48+
local payload = Page.values
4949
if Page.preSave then
5050
payload = Page.preSave(Page)
51-
else
52-
for i=1,(Page.outputBytes or #Page.values) do
53-
payload[i] = Page.values[i]
54-
end
5551
end
5652
protocol.mspWrite(Page.write, payload)
5753
saveTS = getTime()
@@ -123,11 +119,7 @@ local function processMspReply(cmd,rx_buf)
123119
return
124120
end
125121
if #(rx_buf) > 0 then
126-
Page.values = {}
127-
for i=1,#(rx_buf) do
128-
Page.values[i] = rx_buf[i]
129-
end
130-
122+
Page.values = rx_buf
131123
for i=1,#(Page.fields) do
132124
if (#(Page.values) or 0) >= Page.minBytes then
133125
local f = Page.fields[i]
@@ -152,14 +144,23 @@ local function incMax(val, inc, base)
152144
return ((val + inc + base - 1) % base) + 1
153145
end
154146

147+
local function clipValue(val,min,max)
148+
if val < min then
149+
val = min
150+
elseif val > max then
151+
val = max
152+
end
153+
return val
154+
end
155+
155156
local function incPage(inc)
156157
currentPage = incMax(currentPage, inc, #(PageFiles))
157158
Page = nil
158159
currentField = 1
159160
collectgarbage()
160161
end
161162

162-
local function incLine(inc)
163+
local function incField(inc)
163164
currentField = clipValue(currentField + inc, 1, #(Page.fields))
164165
end
165166

@@ -178,7 +179,7 @@ local function requestPage()
178179
end
179180
end
180181

181-
function drawScreenTitle(screen_title)
182+
local function drawScreenTitle(screen_title)
182183
if radio.resolution == lcdResolution.low then
183184
lcd.drawFilledRectangle(0, 0, LCD_W, 10)
184185
lcd.drawText(1,1,screen_title,INVERS)
@@ -236,21 +237,12 @@ local function drawScreen()
236237
end
237238
end
238239

239-
function clipValue(val,min,max)
240-
if val < min then
241-
val = min
242-
elseif val > max then
243-
val = max
244-
end
245-
return val
246-
end
247-
248240
local function incValue(inc)
249241
local f = Page.fields[currentField]
250242
local idx = f.i or currentField
251243
local scale = (f.scale or 1)
252244
local mult = (f.mult or 1)
253-
f.value = clipValue(f.value + ((inc*mult)/scale), (f.min/scale) or 0, (f.max/scale) or 255)
245+
f.value = clipValue(f.value + ((inc*mult)/scale), ((f.min or 0)/scale), ((f.max or 255)/scale))
254246
f.value = math.floor((f.value*scale)/mult + 0.5)/(scale/mult)
255247
for idx=1, #(f.vals) do
256248
Page.values[f.vals[idx]] = bit32.rshift(math.floor(f.value*scale + 0.5), (idx-1)*8)
@@ -281,7 +273,7 @@ local function drawPopupMenu()
281273
end
282274
end
283275

284-
function run_ui(event)
276+
local function run_ui(event)
285277
local now = getTime()
286278
-- if lastRunTS old than 500ms
287279
if lastRunTS + 50 < now then
@@ -297,10 +289,8 @@ function run_ui(event)
297289
if apiVersion == 0 then
298290
if not background then
299291
background = assert(loadScript("/SCRIPTS/BF/background.lua"))()
300-
background.init()
301-
else
302-
background.run_bg()
303292
end
293+
background()
304294
return 0
305295
else
306296
background = nil
@@ -319,6 +309,9 @@ function run_ui(event)
319309
incMainMenu(1)
320310
elseif event == EVT_VIRTUAL_PREV then
321311
incMainMenu(-1)
312+
elseif event == EVT_VIRTUAL_ENTER then
313+
pageState = pageStatus.display
314+
uiState = uiStatus.pages
322315
end
323316
lcd.clear()
324317
drawScreenTitle("Betaflight Config", 0, 0)
@@ -328,22 +321,16 @@ function run_ui(event)
328321
if radio.resolution == lcdResolution.high then
329322
lineSpacing = 25
330323
end
324+
local currentFieldY = (currentPage-1)*lineSpacing + yMinLim
325+
if currentFieldY <= yMinLim then
326+
mainMenuScrollY = 0
327+
elseif currentFieldY - mainMenuScrollY <= yMinLim then
328+
mainMenuScrollY = currentFieldY - yMinLim
329+
elseif currentFieldY - mainMenuScrollY >= yMaxLim then
330+
mainMenuScrollY = currentFieldY - yMaxLim
331+
end
331332
for i=1, #PageFiles do
332-
local currentFieldY = (currentPage-1)*lineSpacing + yMinLim
333-
if currentFieldY <= yMinLim then
334-
mainMenuScrollY = 0
335-
elseif currentFieldY - mainMenuScrollY <= yMinLim then
336-
mainMenuScrollY = currentFieldY - yMinLim
337-
elseif currentFieldY - mainMenuScrollY >= yMaxLim then
338-
mainMenuScrollY = currentFieldY - yMaxLim
339-
end
340333
local attr = (currentPage == i and INVERS or 0)
341-
if event == EVT_VIRTUAL_ENTER and attr == INVERS then
342-
invalidatePages()
343-
currentPage = i
344-
pageState = pageStatus.display
345-
uiState = uiStatus.pages
346-
end
347334
if ((i-1)*lineSpacing + yMinLim - mainMenuScrollY) >= yMinLim and ((i-1)*lineSpacing + yMinLim - mainMenuScrollY) <= yMaxLim then
348335
lcd.drawText(6, (i-1)*lineSpacing + yMinLim - mainMenuScrollY, PageFiles[i].title, attr)
349336
end
@@ -392,9 +379,9 @@ function run_ui(event)
392379
elseif (not isTelemetryScript and event == EVT_VIRTUAL_NEXT_PAGE) or (isTelemetryScript and event == EVT_VIRTUAL_MENU) then
393380
incPage(1)
394381
elseif event == EVT_VIRTUAL_PREV or event == EVT_VIRTUAL_PREV_REPT then
395-
incLine(-1)
382+
incField(-1)
396383
elseif event == EVT_VIRTUAL_NEXT or event == EVT_VIRTUAL_NEXT_REPT then
397-
incLine(1)
384+
incField(1)
398385
elseif event == EVT_VIRTUAL_ENTER then
399386
if Page then
400387
local field = Page.fields[currentField]

src/SCRIPTS/TELEMETRY/bf.lua

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@ local MENU_TIMESLICE = 100
1717

1818
local lastMenuEvent = 0
1919

20-
function run(event)
21-
lastMenuEvent = getTime()
22-
run_ui(event)
20+
local function run(event)
21+
if background then
22+
background = nil
23+
collectgarbage()
24+
end
25+
lastMenuEvent = getTime()
26+
run_ui(event)
2327
end
2428

25-
function run_bg()
26-
if lastMenuEvent + MENU_TIMESLICE < getTime() then
27-
background.run_bg()
28-
end
29+
local function run_bg()
30+
if lastMenuEvent + MENU_TIMESLICE < getTime() then
31+
if not background then
32+
background = assert(loadScript(SCRIPT_HOME.."/background.lua"))()
33+
collectgarbage()
34+
end
35+
background()
36+
end
2937
end
3038

31-
return { init=background.init, run=run, background=run_bg }
39+
return { run=run, background=run_bg }

0 commit comments

Comments
 (0)