Skip to content

Commit

Permalink
Read lua input buffer until empty
Browse files Browse the repository at this point in the history
  • Loading branch information
klutvott123 committed Jan 22, 2022
1 parent 9523c75 commit cea9054
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 37 deletions.
17 changes: 8 additions & 9 deletions src/SCRIPTS/BF/MSP/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,24 @@ function mspReceivedReply(payload)
end
if idx > protocol.maxRxBufferSize then
mspRemoteSeq = seq
return true
return false
end
mspStarted = false
-- check CRC
if mspRxCRC ~= payload[idx] and version == 0 then
return nil
end
return mspRxBuf
return true
end

function mspPollReply()
while true do
local ret = protocol.mspPoll()
if type(ret) == "table" then
local mspData = protocol.mspPoll()
if mspData == nil then
return nil
elseif mspReceivedReply(mspData) then
mspLastReq = 0
return mspRxReq, ret
else
break
end
return mspRxReq, mspRxBuf
end
end
return nil
end
17 changes: 9 additions & 8 deletions src/SCRIPTS/BF/MSP/crsf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local CRSF_FRAMETYPE_MSP_REQ = 0x7A -- response request using msp
local CRSF_FRAMETYPE_MSP_RESP = 0x7B -- reply with 60 byte chunked binary
local CRSF_FRAMETYPE_MSP_WRITE = 0x7C -- write with 60 byte chunked binary

crsfMspCmd = 0
local crsfMspCmd = 0

protocol.mspSend = function(payload)
local payloadOut = { CRSF_ADDRESS_BETAFLIGHT, CRSF_ADDRESS_RADIO_TRANSMITTER }
Expand All @@ -27,15 +27,16 @@ protocol.mspWrite = function(cmd, payload)
end

protocol.mspPoll = function()
local command, data = crossfireTelemetryPop()
if command == CRSF_FRAMETYPE_MSP_RESP then
if data[1] == CRSF_ADDRESS_RADIO_TRANSMITTER and data[2] == CRSF_ADDRESS_BETAFLIGHT then
while true do
local cmd, data = crossfireTelemetryPop()
if cmd == CRSF_FRAMETYPE_MSP_RESP and data[1] == CRSF_ADDRESS_RADIO_TRANSMITTER and data[2] == CRSF_ADDRESS_BETAFLIGHT then
local mspData = {}
for i=3, #(data) do
mspData[i-2] = data[i]
for i = 3, #data do
mspData[i - 2] = data[i]
end
return mspReceivedReply(mspData)
return mspData
elseif cmd == nil then
return nil
end
end
return nil
end
11 changes: 7 additions & 4 deletions src/SCRIPTS/BF/MSP/ghst.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ protocol.mspWrite = function(cmd, payload)
end

protocol.mspPoll = function()
local type, data = ghostTelemetryPop()
if type == GHST_FRAMETYPE_MSP_RESP then
return mspReceivedReply(data)
while true do
local type, data = ghostTelemetryPop()
if type == GHST_FRAMETYPE_MSP_RESP then
return data
elseif type == nil then
return nil
end
end
return nil
end
33 changes: 18 additions & 15 deletions src/SCRIPTS/BF/MSP/sp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,23 @@ local function smartPortTelemetryPop()
end

protocol.mspPoll = function()
local sensorId, frameId, dataId, value = smartPortTelemetryPop()
if (sensorId == SMARTPORT_REMOTE_SENSOR_ID or sensorId == FPORT_REMOTE_SENSOR_ID) and frameId == REPLY_FRAME_ID then
local payload = {}
payload[1] = bit32.band(dataId,0xFF)
dataId = bit32.rshift(dataId,8)
payload[2] = bit32.band(dataId,0xFF)
payload[3] = bit32.band(value,0xFF)
value = bit32.rshift(value,8)
payload[4] = bit32.band(value,0xFF)
value = bit32.rshift(value,8)
payload[5] = bit32.band(value,0xFF)
value = bit32.rshift(value,8)
payload[6] = bit32.band(value,0xFF)
return mspReceivedReply(payload)
while true do
local sensorId, frameId, dataId, value = smartPortTelemetryPop()
if (sensorId == SMARTPORT_REMOTE_SENSOR_ID or sensorId == FPORT_REMOTE_SENSOR_ID) and frameId == REPLY_FRAME_ID then
local payload = {}
payload[1] = bit32.band(dataId, 0xFF)
dataId = bit32.rshift(dataId, 8)
payload[2] = bit32.band(dataId, 0xFF)
payload[3] = bit32.band(value, 0xFF)
value = bit32.rshift(value, 8)
payload[4] = bit32.band(value, 0xFF)
value = bit32.rshift(value, 8)
payload[5] = bit32.band(value, 0xFF)
value = bit32.rshift(value, 8)
payload[6] = bit32.band(value, 0xFF)
return payload
elseif sensorId == nil then
return nil
end
end
return nil
end
3 changes: 3 additions & 0 deletions src/SCRIPTS/BF/protocols.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local supportedProtocols =
maxRxBufferSize = 6,
saveMaxRetries = 2,
saveTimeout = 500,
requestTimeout = 80,
cms = {},
},
crsf =
Expand All @@ -19,6 +20,7 @@ local supportedProtocols =
maxRxBufferSize = 58,
saveMaxRetries = 2,
saveTimeout = 150,
requestTimeout = 20,
cms = {},
},
ghst =
Expand All @@ -29,6 +31,7 @@ local supportedProtocols =
maxRxBufferSize = 6, -- Rx -> Tx (Pop)
saveMaxRetries = 2,
saveTimeout = 250,
requestTimeout = 20,
cms = {},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/SCRIPTS/BF/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local uiMsp =
local uiState = uiStatus.init
local prevUiState
local pageState = pageStatus.display
local requestTimeout = 80
local requestTimeout = protocol.requestTimeout
local currentPage = 1
local currentField = 1
local saveTS = 0
Expand Down

0 comments on commit cea9054

Please sign in to comment.