Skip to content

Commit c5d4a28

Browse files
authored
Merge pull request #51 from betaflight/refactored_background_process
Refactored background process into separate file.
2 parents 703df32 + d9af94b commit c5d4a28

File tree

4 files changed

+102
-205
lines changed

4 files changed

+102
-205
lines changed

Horus.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,6 @@ drawScreenTitle = function (title)
7878
--lcd.drawText(LCD_W-40, 5, page.."/"..pages, MENU_TITLE_COLOR)
7979
end
8080

81-
return {run=run_ui}
81+
local background = assert(loadScript("/SCRIPTS/BF/background.lua"))()
82+
83+
return { init=background.init, run=run_ui, background=background.run_bg }

X7.lua

Lines changed: 3 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
-- load msp.lua
2-
assert(loadScript("/SCRIPTS/BF/msp_sp.lua"))()
3-
41
SetupPages = {
52
{
63
title = "PIDs",
@@ -72,103 +69,7 @@ MenuBox = { x=1, y=10, w=100, x_offset=36, h_line=10, h_offset=3 }
7269
SaveBox = { x=20, y=12, w=100, x_offset=4, h=30, h_offset=5 }
7370
NoTelem = { 36, 55, "No Telemetry", BLINK }
7471

75-
-- ---------------------------------------
76-
-- BACKGROUND PROCESS
77-
-- ---------------------------------------
78-
79-
local INTERVAL = 100 -- 100 = 1 second, 200 = 2 seconds, ...
80-
local MSP_SET_RTC = 246
81-
local MSP_TX_INFO = 186
82-
local sensorName = "Tmp1" -- T1 is never 0 in Betaflight
83-
84-
local lastRunTS
85-
local oldSensorValue
86-
local sensorId
87-
local mspMsgQueued = false
88-
89-
local function getTelemetryId(name)
90-
local field = getFieldInfo(name)
91-
if field then
92-
return field['id']
93-
else
94-
return -1
95-
end
96-
end
97-
98-
local function init()
99-
sensorId = getTelemetryId(sensorName)
100-
oldSensorValue = 0
101-
lastRunTS = 0
102-
end
103-
104-
local function run_bg()
105-
106-
-- run in intervals
107-
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
108-
109-
mspMsgQueued = false
110-
111-
-- ------------------------------------
112-
-- SYNC DATE AND TIME
113-
-- ------------------------------------
114-
115-
-- get sensor value
116-
local newSensorValue = getValue(sensorId)
117-
118-
if type(newSensorValue) == "number" then
119-
120-
-- Send datetime when the telemetry connection is available
121-
-- assuming when sensor value higher than 0 there is an telemetry connection
122-
-- only send datetime one time after telemetry connection became available
123-
-- or when connection is restored after e.g. lipo refresh
124-
if oldSensorValue == 0 and newSensorValue > 0 then
125-
local now = getDateTime()
126-
local year = now.year;
127-
128-
values = {}
129-
values[1] = bit32.band(year, 0xFF)
130-
year = bit32.rshift(year, 8)
131-
values[2] = bit32.band(year, 0xFF)
132-
values[3] = now.mon
133-
values[4] = now.day
134-
values[5] = now.hour
135-
values[6] = now.min
136-
values[7] = now.sec
137-
138-
-- send msp message
139-
mspSendRequest(MSP_SET_RTC, values)
140-
mspMsgQueued = true
141-
end
142-
143-
oldSensorValue = newSensorValue
144-
end
145-
146-
147-
-- ------------------------------------
148-
-- SEND RSSI VALUE
149-
-- ------------------------------------
150-
151-
if mspMsgQueued == false then
152-
local rssi, alarm_low, alarm_crit = getRSSI()
153-
values = {}
154-
values[1] = rssi
155-
156-
-- send msp message
157-
mspSendRequest(MSP_TX_INFO, values)
158-
mspMsgQueued = true
159-
end
160-
161-
lastRunTS = getTime()
162-
end
163-
164-
-- process queue
165-
mspProcessTxQ()
166-
167-
end
168-
169-
--
170-
-- END
171-
--
172-
17372
local run_ui = assert(loadScript("/SCRIPTS/BF/ui.lua"))()
174-
return { init=init, run=run_ui, background=run_bg }
73+
local background = assert(loadScript("/SCRIPTS/BF/background.lua"))()
74+
75+
return { init=background.init, run=run_ui, background=background.run_bg }

X9.lua

Lines changed: 3 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
-- load msp.lua
2-
assert(loadScript("/SCRIPTS/BF/msp_sp.lua"))()
3-
41
-- pages
52
SetupPages = {
63
{
@@ -73,103 +70,7 @@ MenuBox = { x=40, y=12, w=120, x_offset=36, h_line=8, h_offset=3 }
7370
SaveBox = { x=40, y=12, w=120, x_offset=4, h=30, h_offset=5 }
7471
NoTelem = { 70, 55, "No Telemetry", BLINK }
7572

76-
-- ---------------------------------------
77-
-- BACKGROUND PROCESS
78-
-- ---------------------------------------
79-
80-
local INTERVAL = 100 -- 100 = 1 second, 200 = 2 seconds, ...
81-
local MSP_SET_RTC = 246
82-
local MSP_TX_INFO = 186
83-
local sensorName = "Tmp1" -- T1 is never 0 in Betaflight
84-
85-
local lastRunTS
86-
local oldSensorValue
87-
local sensorId
88-
local mspMsgQueued = false
89-
90-
local function getTelemetryId(name)
91-
local field = getFieldInfo(name)
92-
if field then
93-
return field['id']
94-
else
95-
return -1
96-
end
97-
end
98-
99-
local function init()
100-
sensorId = getTelemetryId(sensorName)
101-
oldSensorValue = 0
102-
lastRunTS = 0
103-
end
104-
105-
local function run_bg()
106-
107-
-- run in intervals
108-
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
109-
110-
mspMsgQueued = false
111-
112-
-- ------------------------------------
113-
-- SYNC DATE AND TIME
114-
-- ------------------------------------
115-
116-
-- get sensor value
117-
local newSensorValue = getValue(sensorId)
118-
119-
if type(newSensorValue) == "number" then
120-
121-
-- Send datetime when the telemetry connection is available
122-
-- assuming when sensor value higher than 0 there is an telemetry connection
123-
-- only send datetime one time after telemetry connection became available
124-
-- or when connection is restored after e.g. lipo refresh
125-
if oldSensorValue == 0 and newSensorValue > 0 then
126-
local now = getDateTime()
127-
local year = now.year;
128-
129-
values = {}
130-
values[1] = bit32.band(year, 0xFF)
131-
year = bit32.rshift(year, 8)
132-
values[2] = bit32.band(year, 0xFF)
133-
values[3] = now.mon
134-
values[4] = now.day
135-
values[5] = now.hour
136-
values[6] = now.min
137-
values[7] = now.sec
138-
139-
-- send msp message
140-
mspSendRequest(MSP_SET_RTC, values)
141-
mspMsgQueued = true
142-
end
143-
144-
oldSensorValue = newSensorValue
145-
end
146-
147-
148-
-- ------------------------------------
149-
-- SEND RSSI VALUE
150-
-- ------------------------------------
151-
152-
if mspMsgQueued == false then
153-
local rssi, alarm_low, alarm_crit = getRSSI()
154-
values = {}
155-
values[1] = rssi
156-
157-
-- send msp message
158-
mspSendRequest(MSP_TX_INFO, values)
159-
mspMsgQueued = true
160-
end
161-
162-
lastRunTS = getTime()
163-
end
164-
165-
-- process queue
166-
mspProcessTxQ()
167-
168-
end
169-
170-
--
171-
-- END
172-
--
173-
17473
local run_ui = assert(loadScript("/SCRIPTS/BF/ui.lua"))()
175-
return { init=init, run=run_ui, background=run_bg }
74+
local background = assert(loadScript("/SCRIPTS/BF/background.lua"))()
75+
76+
return { init=background.init, run=run_ui, background=background.run_bg }

common/background.lua

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
-- load msp.lua
2+
assert(loadScript("/SCRIPTS/BF/msp_sp.lua"))()
3+
4+
local INTERVAL = 100 -- 100 = 1 second, 200 = 2 seconds, ...
5+
local MSP_SET_RTC = 246
6+
local MSP_TX_INFO = 186
7+
local sensorName = "Tmp1" -- T1 is never 0 in Betaflight
8+
9+
local lastRunTS
10+
local oldSensorValue
11+
local sensorId
12+
local mspMsgQueued = false
13+
14+
local function getTelemetryId(name)
15+
local field = getFieldInfo(name)
16+
if field then
17+
return field['id']
18+
else
19+
return -1
20+
end
21+
end
22+
23+
local function init()
24+
sensorId = getTelemetryId(sensorName)
25+
oldSensorValue = 0
26+
lastRunTS = 0
27+
end
28+
29+
local function run_bg()
30+
-- run in intervals
31+
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
32+
33+
mspMsgQueued = false
34+
35+
-- ------------------------------------
36+
-- SYNC DATE AND TIME
37+
-- ------------------------------------
38+
39+
-- get sensor value
40+
local newSensorValue = getValue(sensorId)
41+
42+
if type(newSensorValue) == "number" then
43+
44+
-- Send datetime when the telemetry connection is available
45+
-- assuming when sensor value higher than 0 there is an telemetry connection
46+
-- only send datetime one time after telemetry connection became available
47+
-- or when connection is restored after e.g. lipo refresh
48+
if oldSensorValue == 0 and newSensorValue > 0 then
49+
local now = getDateTime()
50+
local year = now.year;
51+
52+
values = {}
53+
values[1] = bit32.band(year, 0xFF)
54+
year = bit32.rshift(year, 8)
55+
values[2] = bit32.band(year, 0xFF)
56+
values[3] = now.mon
57+
values[4] = now.day
58+
values[5] = now.hour
59+
values[6] = now.min
60+
values[7] = now.sec
61+
62+
-- send msp message
63+
mspSendRequest(MSP_SET_RTC, values)
64+
mspMsgQueued = true
65+
end
66+
67+
oldSensorValue = newSensorValue
68+
end
69+
70+
71+
-- ------------------------------------
72+
-- SEND RSSI VALUE
73+
-- ------------------------------------
74+
75+
if mspMsgQueued == false then
76+
local rssi, alarm_low, alarm_crit = getRSSI()
77+
values = {}
78+
values[1] = rssi
79+
80+
-- send msp message
81+
mspSendRequest(MSP_TX_INFO, values)
82+
mspMsgQueued = true
83+
end
84+
85+
lastRunTS = getTime()
86+
end
87+
88+
-- process queue
89+
mspProcessTxQ()
90+
91+
end
92+
93+
return { init=init, run_bg=run_bg }

0 commit comments

Comments
 (0)