Skip to content

elrsV3.lua: added editing of parameter type string #2995

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

Open
wants to merge 5 commits into
base: 3.x.x-maintenance
Choose a base branch
from
Open
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
66 changes: 63 additions & 3 deletions src/lua/elrsV3.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@ local function getField(line)
end
end

local subFieldIndex = 1
local stringPossibleChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_#-. "
local maxStringLength = 16
local function incrCharInTextField(field, step)
local c = string.sub(field.value, subFieldIndex, subFieldIndex)
local idx = string.find(stringPossibleChars, c, 1, true)
idx = (idx + step + #stringPossibleChars - 1) % #stringPossibleChars + 1
c = string.sub(stringPossibleChars, idx, idx)
field.value = string.sub(field.value, 1, subFieldIndex - 1) .. c .. string.sub(field.value, subFieldIndex + 1, string.len(field.value))
end

local function incrSubField(step)
local field = getField(lineIndex)
subFieldIndex = subFieldIndex + step
local maxlength = maxStringLength
if (field.maxlen) then
maxlength = field.maxlen
end
if (subFieldIndex > maxlength) then
subFieldIndex = 1
elseif (subFieldIndex < 1) then
subFieldIndex = #field.value
end
if (subFieldIndex > #field.value) then
field.value = field.value .. " "
end
end

local function incrField(step)
local field = getField(lineIndex)
local min, max = 0, 0
Expand All @@ -81,6 +109,8 @@ local function incrField(step)
elseif field.type == 9 then
min = 0
max = #field.values - 1
elseif field.type == 10 then
return incrCharInTextField(field, step)
end

local newval = field.value
Expand Down Expand Up @@ -290,7 +320,32 @@ local function fieldStringLoad(field, data, offset)
end

local function fieldStringDisplay(field, y, attr)
lcd.drawText(COL2, y, field.value, attr)
if (bit32.band(attr, BLINK) > 0) then -- editing
local s1 = string.sub(field.value, 1, subFieldIndex - 1)
local w1 = lcd.sizeText(s1)
Copy link

@j-venegas j-venegas Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to change lcd.sizeText() for lcd.getLastPos() to get the script working on my bw radio.

lcd.drawText(COL2, y, s1)
local c = string.sub(field.value, subFieldIndex, subFieldIndex)
local wc = lcd.sizeText(c)
lcd.drawText(COL2 + w1, y, c, attr)
local s2 = string.sub(field.value, subFieldIndex + 1, #field.value)
lcd.drawText(COL2 + w1 + wc, y, s2)
else
lcd.drawText(COL2, y, field.value, attr)
end
end

local function trim(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

local function fieldStringSave(field)
local frame = { deviceId, handsetId, field.id }
field.value = trim(field.value)
for i = 1, #field.value do
frame[#frame + 1] = string.byte(field.value, i)
end
frame[#frame + 1] = 0
crossfireTelemetryPush(0x2D, frame)
end

local function fieldFolderOpen(field)
Expand Down Expand Up @@ -424,7 +479,7 @@ local functions = {
nil,
{ load=fieldFloatLoad, save=fieldIntSave, display=fieldFloatDisplay }, --9 FLOAT(8)
{ load=fieldTextSelLoad, save=fieldIntSave, display=nil }, --10 SELECT(9)
{ load=fieldStringLoad, save=nil, display=fieldStringDisplay }, --11 STRING(10) editing NOTIMPL
{ load=fieldStringLoad, save=fieldStringSave, display=fieldStringDisplay }, --11 STRING(10) editing NOTIMPL
{ load=nil, save=fieldFolderOpen, display=fieldFolderDisplay }, --12 FOLDER(11)
{ load=fieldStringLoad, save=nil, display=fieldStringDisplay }, --13 INFO(12)
{ load=fieldCommandLoad, save=fieldCommandSave, display=fieldCommandDisplay }, --14 COMMAND(13)
Expand Down Expand Up @@ -714,7 +769,7 @@ local function handleDevicePageEvent(event)
local field = getField(lineIndex)
if field and field.name then
-- Editable fields
if not field.grey and field.type < 10 then
if not field.grey and field.type < 11 then -- include STRING
edit = not edit
if not edit then
reloadRelatedFields(field)
Expand All @@ -732,8 +787,13 @@ local function handleDevicePageEvent(event)
incrField(1)
elseif event == EVT_VIRTUAL_PREV then
incrField(-1)
elseif event == EVT_VIRTUAL_NEXT_PAGE then
incrSubField(1)
elseif event == EVT_VIRTUAL_PREV_PAGE then
incrSubField(-1)
end
else
subFieldIndex = 1
if event == EVT_VIRTUAL_NEXT then
selectField(1)
elseif event == EVT_VIRTUAL_PREV then
Expand Down
2 changes: 2 additions & 0 deletions src/lua/mockup/elrsmock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ return {
{name='master', id=17, type=16, value='f00fcb'},
{name='Float Tst', id=18, type=8, value=-15, step=5, prec=1000, min=-50, max=50, unit='flt', fmt='%.3fflt'},

{name='String', id=19, type=10, value='test'},

{name="----BACK----", type=14, parent=255},
{name="----EXIT----", type=14, exit = true}
}, "0/500 C", "ExpressLRS TX"