Skip to content

Commit bc77701

Browse files
authored
Merge pull request #46 from basdelfos/msp-tx-info
Syncing date and time to BF with MSP over SmartPort
2 parents fc03833 + 4660df2 commit bc77701

File tree

3 files changed

+205
-5
lines changed

3 files changed

+205
-5
lines changed

X7.lua

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

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 = "VFAS"
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+
72173
local run_ui = assert(loadScript("/SCRIPTS/BF/ui.lua"))()
73-
return {run=run_ui}
174+
return { init=init, run=run_ui, background=run_bg }

X9.lua

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
-- load msp.lua
2+
assert(loadScript("/SCRIPTS/BF/msp_sp.lua"))()
3+
4+
-- pages
15
SetupPages = {
26
{
37
title = "PIDs",
@@ -69,5 +73,103 @@ MenuBox = { x=40, y=12, w=120, x_offset=36, h_line=8, h_offset=3 }
6973
SaveBox = { x=40, y=12, w=120, x_offset=4, h=30, h_offset=5 }
7074
NoTelem = { 70, 55, "No Telemetry", BLINK }
7175

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 = "VFAS"
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+
72174
local run_ui = assert(loadScript("/SCRIPTS/BF/ui.lua"))()
73-
return {run=run_ui}
175+
return { init=init, run=run_ui, background=run_bg }

common/ui.lua

Lines changed: 0 additions & 3 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
-- getter
52
local MSP_RC_TUNING = 111
63
local MSP_PID = 112

0 commit comments

Comments
 (0)