Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for extended CRSF RF Modes #39

Merged
merged 4 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 14 additions & 2 deletions src/SCRIPTS/TELEMETRY/iNav.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ end
loadScript(FILE_PATH .. "reset" .. ext, env)(data)
collectgarbage()

local crsf, distCalc = loadScript(FILE_PATH .. "other" .. ext, env)(config, data, units, getTelemetryId, getTelemetryUnit, FILE_PATH, env, SMLCD)
local crsf, elrs, distCalc = loadScript(FILE_PATH .. "other" .. ext, env)(config, data, units, getTelemetryId, getTelemetryUnit, FILE_PATH, env, SMLCD)
collectgarbage()

-- Request device info from CRSF devices (used to detect TBS/ExpressLRS)
if data.fm_id > -1 then
crossfireTelemetryPush(0x28, { 0x00, 0xEA })
end

local title, gpsDegMin, hdopGraph, icons, rect = loadScript(FILE_PATH .. "func_" .. (HORUS and "h" or "t") .. ext, env)(config, data, modes, dir, SMLCD, FILE_PATH, text, line, rect, fill, frmt, options)
collectgarbage()

Expand Down Expand Up @@ -408,7 +413,7 @@ function inav.background()
else
data.battLow = false
data.battPercentPlayed = 100
-- Initalize variables on flight reset (uses timer3)
-- Initialize variables on flight reset (uses timer3)
tmp = model.getTimer(2)
if tmp.value == 0 then
loadScript(FILE_PATH .. "load" .. ext, env)(config, data, FILE_PATH)
Expand Down Expand Up @@ -440,6 +445,13 @@ function inav.background()
end
data.altMax = math.ceil(data.altMax * (data.alt_unit == 10 and 0.1 or 0.2)) * (data.alt_unit == 10 and 10 or 5)
end

-- Check if we're talking to an ExpressLRS CRSF device
if (data.elrs == nil or data.elrs == 0) and data.fm_id > -1 then
data.elrs = elrs()
collectgarbage()
end

data.bkgd = true
end

Expand Down
62 changes: 62 additions & 0 deletions src/SCRIPTS/TELEMETRY/iNav/elrs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-- Adapted from ELRSv2.lua in ExpressLRS

local deviceId = 0xEE

local function byteToStr(b)
-- Translate b into a string from symbolChars if available, else use string.char
return symbolChars and symbolChars[b] or string.char(b)
end

local function fieldGetString(data, offset, last)
if last then
return fieldStrFF(data, offset, last)
end

local result = ""
while data[offset] ~= 0 do
result = result .. byteToStr(data[offset])
offset = offset + 1
end

return result, offset + 1
end

local function fieldGetValue(data, offset, size)
local result = 0
for i=0, size-1 do
result = bit32.lshift(result, 8) + data[offset + i]
end
return result
end

local function parseDeviceInfoMessage(data)
local offset
local id = data[2]
local devicesName
devicesName, offset = fieldGetString(data, 3)
if deviceId == id then
deviceIsELRS_TX = ((fieldGetValue(data,offset,4) == 0x454C5253) and (deviceId == 0xEE)) or nil -- SerialNumber = 'E L R S' and ID is TX module
return deviceIsELRS_TX
end
end

-- Returns 1 if elrs, -1 if TBS, and 0 if unknown
local function elrs()
-- the request for data has already been sent
local command, data
i = 0
-- give up after 50 tries
while command ~= 0x29 and i < 50 do
FrankPetrilli marked this conversation as resolved.
Show resolved Hide resolved
command, data = crossfireTelemetryPop()
i = i + 1
end
if command == 0x29 then
if parseDeviceInfoMessage(data) then
return 1
end
return -1
end
return 0
end

return elrs
7 changes: 6 additions & 1 deletion src/SCRIPTS/TELEMETRY/iNav/func_h.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ local function title()
if data.rxBatt > 0 and config[14].v == 1 then
text(LCD_W, 0, frmt("%.1fV", data.rxBatt), data.set_flags(RIGHT, tmp))
elseif data.crsf then
text(LCD_W, 0, (data.rfmd == 2 and 150 or (data.telem and 50 or "--")) .. "Hz", data.set_flags(RIGHT,tmp))
--if config[35].v == 1 then
if data.elrs == 1 then
text(LCD_W, 0, (data.rfmd == 8 and 1000 or data.rfmd == 7 and 500 or data.rfmd == 6 and 250 or data.rfmd == 5 and 200 or data.rfmd == 4 and 150 or data.rfmd == 3 and 100 or data.rfmd == 2 and 50 or data.rfmd == 1 and 25 or "--") .. "Hz", data.set_flags(RIGHT, tmp))
else
text(LCD_W, 0, (data.rfmd == 2 and 150 or (data.telem and 50 or "--")) .. "Hz", data.set_flags(RIGHT,tmp))
end
end

-- Data on config menu
Expand Down
6 changes: 5 additions & 1 deletion src/SCRIPTS/TELEMETRY/iNav/func_t.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ local function title()
if data.rxBatt > 0 and data.telem and config[14].v == 1 then
text(LCD_W, 1, frmt("%.1fV", data.rxBatt), SMLSIZE + RIGHT + INVERS)
elseif data.crsf then
text(LCD_W, 1, (data.rfmd == 2 and 150 or (data.telem and 50 or "--")) .. (SMLCD and "" or "Hz"), SMLSIZE + RIGHT + INVERS)
if data.elrs == 1 then
text(LCD_W, 1, (data.rfmd == 8 and 1000 or data.rfmd == 7 and 500 or data.rfmd == 6 and 250 or data.rfmd == 5 and 200 or data.rfmd == 4 and 150 or data.rfmd == 3 and 100 or data.rfmd == 2 and 50 or data.rfmd == 1 and 25 or "--") .. (SMLCD and "" or "Hz"), SMLSIZE + RIGHT + INVERS)
else
text(LCD_W, 1, (data.rfmd == 2 and 150 or (data.telem and 50 or "--")) .. (SMLCD and "" or "Hz"), SMLSIZE + RIGHT + INVERS)
end
end

--[[ Show FPS - Should always be 20fps on Taranis
Expand Down
4 changes: 3 additions & 1 deletion src/SCRIPTS/TELEMETRY/iNav/other.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local config, data, units, getTelemetryId, getTelemetryUnit, FILE_PATH, env, SMLCD = ...
local crsf = nil
local elrs = nil

-- Detect Crossfire
data.fm_id = getTelemetryId("FM") > -1 and getTelemetryId("FM") or getTelemetryId("PV")
Expand All @@ -12,6 +13,7 @@ data.fm_id = getTelemetryId("FM") > -1 and getTelemetryId("FM") or getTelemetryI

if data.fm_id > -1 then
crsf = loadScript(FILE_PATH .. "crsf.luac", env)(config, data, getTelemetryId)
elrs = loadScript(FILE_PATH .. "elrs.luac", env)()
collectgarbage()
end

Expand Down Expand Up @@ -91,4 +93,4 @@ if data.dist_id == -1 or data.simu then
end
end

return crsf, distCalc
return crsf, elrs, distCalc