|
| 1 | +local MSP_VTX_CONFIG = 88 |
| 2 | +local MSP_VTXTABLE_BAND = 137 |
| 3 | +local MSP_VTXTABLE_POWERLEVEL = 138 |
| 4 | + |
| 5 | +local vtxTableAvailable = false |
| 6 | +local vtxConfigReceived = false |
| 7 | +local vtxFrequencyTableReceived = false |
| 8 | +local vtxPowerTableReceived = false |
| 9 | +local vtxTablesReceived = false |
| 10 | +local requestedBand = 1 |
| 11 | +local requestedPowerLevel = 1 |
| 12 | +local vtxTableConfig = {} |
| 13 | +local frequencyTable = {} |
| 14 | +local frequenciesPerBand = 0 |
| 15 | +local bandTable = {} |
| 16 | +local powerTable = {} |
| 17 | + |
| 18 | +local lastRunTS = 0 |
| 19 | +local INTERVAL = 100 |
| 20 | + |
| 21 | +local function processMspReply(cmd, payload) |
| 22 | + if cmd == MSP_VTX_CONFIG then |
| 23 | + vtxConfigReceived = true |
| 24 | + vtxTableAvailable = payload[12] ~= 0 |
| 25 | + vtxTableConfig.bands = payload[13] |
| 26 | + vtxTableConfig.channels = payload[14] |
| 27 | + vtxTableConfig.powerLevels = payload[15] |
| 28 | + end |
| 29 | + if cmd == MSP_VTXTABLE_BAND and payload[1] == requestedBand then |
| 30 | + local i = 1 |
| 31 | + local receivedBand = payload[i] |
| 32 | + i = i + 1 |
| 33 | + local bandNameLength = payload[i] |
| 34 | + i = i + bandNameLength + 1 |
| 35 | + bandTable[receivedBand] = string.char(payload[i]) |
| 36 | + i = i + 2 |
| 37 | + local channels = payload[i] |
| 38 | + i = i + 1 |
| 39 | + frequencyTable[receivedBand] = {} |
| 40 | + for channel = 1, channels do |
| 41 | + local frequency = 0 |
| 42 | + for idx=1, 2 do |
| 43 | + local raw_val = payload[i] |
| 44 | + raw_val = bit32.lshift(raw_val, (idx-1)*8) |
| 45 | + frequency = bit32.bor(frequency, raw_val) |
| 46 | + i = i + 1 |
| 47 | + end |
| 48 | + frequencyTable[receivedBand][channel] = frequency |
| 49 | + end |
| 50 | + requestedBand = requestedBand + 1 |
| 51 | + vtxFrequencyTableReceived = requestedBand > vtxTableConfig.bands |
| 52 | + end |
| 53 | + if cmd == MSP_VTXTABLE_POWERLEVEL and payload[1] == requestedPowerLevel then |
| 54 | + local i = 1 |
| 55 | + local powerLevel = payload[i] |
| 56 | + i = i + 3 |
| 57 | + local powerLabelLength = payload[i] |
| 58 | + i = i + 1 |
| 59 | + powerTable[powerLevel] = '' |
| 60 | + for c = 1, powerLabelLength do |
| 61 | + powerTable[powerLevel] = powerTable[powerLevel]..string.char(payload[i]) |
| 62 | + i = i + 1 |
| 63 | + end |
| 64 | + requestedPowerLevel = requestedPowerLevel + 1 |
| 65 | + vtxPowerTableReceived = requestedPowerLevel > vtxTableConfig.powerLevels |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +local function getVtxTables() |
| 70 | + if lastRunTS + INTERVAL < getTime() then |
| 71 | + lastRunTS = getTime() |
| 72 | + if not vtxConfigReceived then |
| 73 | + protocol.mspRead(MSP_VTX_CONFIG) |
| 74 | + elseif vtxConfigReceived and not vtxTableAvailable then |
| 75 | + return true |
| 76 | + elseif not vtxFrequencyTableReceived then |
| 77 | + protocol.mspWrite(MSP_VTXTABLE_BAND, { requestedBand }) |
| 78 | + elseif not vtxPowerTableReceived then |
| 79 | + protocol.mspWrite(MSP_VTXTABLE_POWERLEVEL, { requestedPowerLevel }) |
| 80 | + else |
| 81 | + vtxTablesReceived = true |
| 82 | + end |
| 83 | + end |
| 84 | + if vtxTablesReceived then |
| 85 | + local f = io.open("/BF/VTX/"..model.getInfo().name..".lua", 'w') |
| 86 | + io.write(f, "return {", "\n") |
| 87 | + io.write(f, " frequencyTable = {", "\n") |
| 88 | + for i = 1, #frequencyTable do |
| 89 | + local frequencyString = " { " |
| 90 | + for k = 1, #frequencyTable[i] do |
| 91 | + frequencyString = frequencyString..tostring(frequencyTable[i][k])..", " |
| 92 | + end |
| 93 | + frequencyString = frequencyString.."}," |
| 94 | + io.write(f, frequencyString, "\n") |
| 95 | + end |
| 96 | + io.write(f, " },", "\n") |
| 97 | + io.write(f, " frequenciesPerBand = "..tostring(vtxTableConfig.channels)..",", "\n") |
| 98 | + local bandString = " bandTable = { [0]=\"U\", " |
| 99 | + for i = 1, #bandTable do |
| 100 | + bandString = bandString.."\""..bandTable[i].."\", " |
| 101 | + end |
| 102 | + bandString = bandString.."}," |
| 103 | + io.write(f, bandString, "\n") |
| 104 | + local powerString = " powerTable = { " |
| 105 | + for i = 1, #powerTable do |
| 106 | + powerString = powerString.."\""..powerTable[i].."\", " |
| 107 | + end |
| 108 | + powerString = powerString.."}," |
| 109 | + io.write(f, powerString, "\n") |
| 110 | + io.write(f, "}", "\n") |
| 111 | + io.close(f) |
| 112 | + assert(loadScript("/BF/VTX/"..model.getInfo().name..".lua", 'c')) |
| 113 | + end |
| 114 | + mspProcessTxQ() |
| 115 | + processMspReply(mspPollReply()) |
| 116 | + return vtxTablesReceived |
| 117 | +end |
| 118 | + |
| 119 | +return getVtxTables |
0 commit comments