Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/SCRIPTS/CRSFSimulator/csrfsimulator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ updateVtxAdminOpts(txDevice)

local reconnectDelay = 500 -- 5 s
local startTime = nil -- first push/pop
local lastPushAt = nil
local mismatchClearDelay = 1000 -- 10 s

local function isRxAvailable()
Expand Down Expand Up @@ -1113,9 +1114,17 @@ end
-- ============================================================================

local function mockPush(command, data)
local now = getTime()
if not startTime then
startTime = getTime()
startTime = now
end

-- EdgeTX holds one outbound frame until the next CRSF pulse sends it
if now == lastPushAt then
print(shim.tableConcat({ "CRSFSIM push busy t=", now, " cmd=", command, " field=", data and data[3] or "-" }))
return false
end
lastPushAt = now

-- One log line per frame so tests can count wire traffic
print(shim.tableConcat({
Expand Down
31 changes: 21 additions & 10 deletions src/SCRIPTS/ELRS/crsf_session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -484,18 +484,26 @@ function CRSFSession:writeField(field)
frameType, payload = params.encodeWriteInt(self.deviceId, self.handsetId, field)
end

self:_write(frameType, payload)

if not self._acceptUnsolicited then
self:_reloadRelated(field)
end
end

-- The radio's one Lua outbound slot may be busy; tick() retries from the queue
function CRSFSession:_write(frameType, payload)
local now = getTime()
if self._writeHead > self._writeTail and now - self._lastWriteAt >= WRITE_SPACING then
crsf.push(frameType, payload)
if
self._writeHead > self._writeTail
and now - self._lastWriteAt >= WRITE_SPACING
and crsf.push(frameType, payload)
then
self._lastWriteAt = now
else
self._writeTail = self._writeTail + 1
self._writeQueue[self._writeTail] = { frameType, payload }
end

if not self._acceptUnsolicited then
self:_reloadRelated(field)
end
end

-- ============================================================================
Expand Down Expand Up @@ -564,7 +572,7 @@ end

function CRSFSession:suppressCriticalErrors()
self.status.flags = 0
crsf.push(params.encodeSuppressCriticalErrors(self.deviceId, self.handsetId))
self:_write(params.encodeSuppressCriticalErrors(self.deviceId, self.handsetId))
end

-- ============================================================================
Expand Down Expand Up @@ -608,16 +616,19 @@ function CRSFSession:tick()
end

if self._writeHead <= self._writeTail then
if now - self._lastWriteAt >= WRITE_SPACING then
local write = self._writeQueue[self._writeHead]
local write = self._writeQueue[self._writeHead]
if now - self._lastWriteAt >= WRITE_SPACING and crsf.push(write[1], write[2]) then
self._writeQueue[self._writeHead] = nil
self._writeHead = self._writeHead + 1
if self._writeHead > self._writeTail then
self._writeHead = 1
self._writeTail = 0
end
crsf.push(write[1], write[2])
self._lastWriteAt = now
-- Settle from the actual send, not from when it was queued
if not self._acceptUnsolicited then
self:_afterWrite()
end
end
return
end
Expand Down
Loading