|
| 1 | +-- load msp.lua |
| 2 | +assert(loadScript("/SCRIPTS/BF/msp_sp.lua"))() |
| 3 | + |
1 | 4 | SetupPages = { |
2 | 5 | { |
3 | 6 | title = "PIDs", |
@@ -69,5 +72,103 @@ MenuBox = { x=1, y=10, w=100, x_offset=36, h_line=10, h_offset=3 } |
69 | 72 | SaveBox = { x=20, y=12, w=100, x_offset=4, h=30, h_offset=5 } |
70 | 73 | NoTelem = { 36, 55, "No Telemetry", BLINK } |
71 | 74 |
|
| 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 | + |
72 | 173 | local run_ui = assert(loadScript("/SCRIPTS/BF/ui.lua"))() |
73 | | -return {run=run_ui} |
| 174 | +return { init=init, run=run_ui, background=run_bg } |
0 commit comments