Skip to content

Commit 318cf0a

Browse files
committed
Send date time only one time after telemetry connection became available/restored
1 parent 9fb2bd1 commit 318cf0a

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

X9.lua

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,40 +73,72 @@ MenuBox = { x=40, y=12, w=120, x_offset=36, h_line=8, h_offset=3 }
7373
SaveBox = { x=40, y=12, w=120, x_offset=4, h=30, h_offset=5 }
7474
NoTelem = { 70, 55, "No Telemetry", BLINK }
7575

76-
--
77-
-- THIS CODE HAS TO BE MOVED TO A SEPERATE LUA FILE
78-
--
76+
-- ---------------------------------------
77+
-- BACKGROUND PROCESS
78+
-- ---------------------------------------
7979

80-
local MSP_SET_RTC = 246
80+
local INTERVAL = 100 -- 100 = 1 second, 200 = 2 seconds, ...
81+
local MSP_SET_RTC = 246
82+
local sensorName = "VFAS"
8183

82-
--
84+
local lastRunTS
85+
local oldSensorValue
86+
local sensorId
8387

84-
local INTERVAL = 200 -- 100 = 1 second, 200 = 2 seconds, ...
85-
local lastSendTS = 0
88+
local function getTelemetryId(name)
89+
local field = getFieldInfo(name)
90+
if field then
91+
return field['id']
92+
else
93+
return -1
94+
end
95+
end
96+
97+
local function init()
98+
sensorId = getTelemetryId(sensorName)
99+
oldSensorValue = 0
100+
lastRunTS = 0
101+
end
86102

87103
local function run_bg()
88104

89-
if lastSendTS == 0 or lastSendTS + INTERVAL < getTime() then
90-
local now = getDateTime()
91-
local rssi, alarm_low, alarm_high = getRSSI()
92-
local year = now.year;
93-
94-
local values = {}
95-
values[1] = bit32.band(year, 0xFF)
96-
year = bit32.rshift(year, 8)
97-
values[2] = bit32.band(year, 0xFF)
98-
values[3] = now.mon
99-
values[4] = now.day
100-
values[5] = now.hour
101-
values[6] = now.min
102-
values[7] = now.sec
103-
104-
-- send info
105-
mspSendRequest(MSP_SET_RTC, values)
106-
107-
lastSendTS = getTime()
105+
-- run in intervals
106+
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
107+
108+
-- get sensor value
109+
local newSensorValue = getValue(sensorId)
110+
111+
if type(newSensorValue) == "number" then
112+
113+
-- Send datetime when the telemetry connection is available
114+
-- assuming when sensor value higher than 0 there is an telemetry connection
115+
-- only send datetime one time after telemetry connection became available
116+
-- or when connection is restored after e.g. lipo refresh
117+
if oldSensorValue == 0 and newSensorValue > 0 then
118+
local now = getDateTime()
119+
local year = now.year;
120+
121+
values = {}
122+
values[1] = bit32.band(year, 0xFF)
123+
year = bit32.rshift(year, 8)
124+
values[2] = bit32.band(year, 0xFF)
125+
values[3] = now.mon
126+
values[4] = now.day
127+
values[5] = now.hour
128+
values[6] = now.min
129+
values[7] = now.sec
130+
131+
-- send info
132+
mspSendRequest(MSP_SET_RTC, values)
133+
end
134+
135+
oldSensorValue = newSensorValue
136+
end
137+
138+
lastRunTS = getTime()
108139
end
109140

141+
-- process queue
110142
mspProcessTxQ()
111143

112144
end
@@ -116,4 +148,4 @@ end
116148
--
117149

118150
local run_ui = assert(loadScript("/SCRIPTS/BF/ui.lua"))()
119-
return { run=run_ui, background=run_bg }
151+
return { init=init, run=run_ui, background=run_bg }

0 commit comments

Comments
 (0)