Skip to content

Commit cdb68f1

Browse files
Merge pull request #2 from raphaelcoeffic/vtx
added VTX setup
2 parents 05d8c83 + 053c5a2 commit cdb68f1

File tree

2 files changed

+142
-30
lines changed

2 files changed

+142
-30
lines changed

BFSetup.lua

+71-15
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ local MSP_SET_RC_TUNING = 204
254254
-- BF specials
255255
local MSP_PID_ADVANCED = 94
256256
local MSP_SET_PID_ADVANCED = 95
257+
local MSP_VTX_CONFIG = 88
258+
local MSP_VTX_SET_CONFIG = 89
257259

258260
local MSP_EEPROM_WRITE = 250
259261

@@ -267,6 +269,24 @@ local MENU_DISP = 5
267269

268270
local gState = PAGE_DISPLAY
269271

272+
local function postReadVTX(page)
273+
if page.values[1] == 3 then -- SmartAudio
274+
page.fields[3].table = { 25, 200, 500, 800 }
275+
page.fields[3].max = 4
276+
elseif page.values[1] == 4 then -- Tramp
277+
page.fields[3].table = { 25, 100, 200, 400, 600 }
278+
page.fields[3].max = 5
279+
else
280+
-- TODO: print label on unavailable (0xFF) vs. unsupported (0)
281+
--page.values = nil
282+
end
283+
end
284+
285+
local function getWriteValuesVTX(values)
286+
local channel = (values[2]-1)*8 + values[3]-1
287+
return { bit32.band(channel,0xFF), bit32.rshift(channel,8), values[4], values[5] }
288+
end
289+
270290
local SetupPages = {
271291
{
272292
title = "PIDs",
@@ -314,23 +334,50 @@ local SetupPages = {
314334
},
315335
read = MSP_RC_TUNING,
316336
write = MSP_SET_RC_TUNING,
337+
},
338+
{
339+
title = "VTX",
340+
text = {},
341+
fields = {
342+
-- Super Rate
343+
{ t = "Band", x = 25, y = 14, sp = 50, i=2, min=1, max=5, table = { "A", "B", "E", "F", "R" } },
344+
{ t = "Channel", x = 25, y = 24, sp = 50, i=3, min=1, max=8 },
345+
{ t = "Power", x = 25, y = 34, sp = 50, i=4, min=1 },
346+
{ t = "Pit", x = 25, y = 44, sp = 50, i=5, min=0, max=1, table = { [0]="OFF", "ON" } },
347+
{ t = "(Dev)", x = 100, y = 14, sp = 32, i=1, ro=true, table = {[3]="SmartAudio",[4]="Tramp"} },
348+
},
349+
read = MSP_VTX_CONFIG,
350+
write = MSP_VTX_SET_CONFIG,
351+
postRead = postReadVTX,
352+
getWriteValues = getWriteValuesVTX,
353+
saveMaxRetries = 0,
354+
saveTimeout = 200 -- 2s
317355
}
318356
}
319357

320358
local currentPage = 1
321359
local currentLine = 1
322360
local saveTS = 0
361+
local saveTimeout = 0
323362
local saveRetries = 0
363+
local saveMaxRetries = 0
324364

325365
local function saveSettings(new)
326366
local page = SetupPages[currentPage]
327367
if page.values then
328-
mspSendRequest(page.write,page.values)
368+
if page.getWriteValues then
369+
mspSendRequest(page.write,page.getWriteValues(page.values))
370+
else
371+
mspSendRequest(page.write,page.values)
372+
end
329373
saveTS = getTime()
330374
if gState == PAGE_SAVING then
331375
saveRetries = saveRetries + 1
332376
else
333377
gState = PAGE_SAVING
378+
saveRetries = 0
379+
saveMaxRetries = page.saveMaxRetries or 2 -- default 2
380+
saveTimeout = page.saveTimeout or 150 -- default 1.5s
334381
end
335382
end
336383
end
@@ -366,7 +413,9 @@ local function processMspReply(cmd,rx_buf)
366413

367414
-- ignore replies to write requests for now
368415
if cmd == page.write then
369-
mspSendRequest(MSP_EEPROM_WRITE,{})
416+
if cmd ~= MSP_VTX_SET_CONFIG then
417+
mspSendRequest(MSP_EEPROM_WRITE,{})
418+
end
370419
return
371420
end
372421

@@ -386,6 +435,10 @@ local function processMspReply(cmd,rx_buf)
386435
for i=1,#(rx_buf) do
387436
page.values[i] = rx_buf[i]
388437
end
438+
439+
if page.postRead ~= nil then
440+
page.postRead(page)
441+
end
389442
end
390443
end
391444

@@ -460,18 +513,22 @@ local function drawScreen(page,page_locked)
460513

461514
local idx = f.i or i
462515
if page.values and page.values[idx] then
463-
lcd.drawText(f.x + spacing, f.y, page.values[idx], text_options)
516+
local val = page.values[idx]
517+
if f.table and f.table[page.values[idx]] then
518+
val = f.table[page.values[idx]]
519+
end
520+
lcd.drawText(f.x + spacing, f.y, val, text_options)
464521
else
465522
lcd.drawText(f.x + spacing, f.y, "---", text_options)
466523
end
467524
end
468525
end
469526

470-
local function clipValue(val)
471-
if val < 0 then
472-
val = 0
473-
elseif val > 255 then
474-
val = 255
527+
local function clipValue(val,min,max)
528+
if val < min then
529+
val = min
530+
elseif val > max then
531+
val = max
475532
end
476533

477534
return val
@@ -486,7 +543,7 @@ local function incValue(inc)
486543
local page = SetupPages[currentPage]
487544
local field = page.fields[currentLine]
488545
local idx = field.i or currentLine
489-
page.values[idx] = clipValue(page.values[idx] + inc)
546+
page.values[idx] = clipValue(page.values[idx] + inc, field.min or 0, field.max or 255)
490547
end
491548

492549
local function drawMenu()
@@ -521,14 +578,13 @@ local function run(event)
521578
end
522579
lastRunTS = now
523580

524-
-- TODO: implement retry + retry counter
525-
if (gState == PAGE_SAVING) and (saveTS + 150 < now) then
526-
if saveRetries < 2 then
581+
if (gState == PAGE_SAVING) and (saveTS + saveTimeout < now) then
582+
if saveRetries < saveMaxRetries then
527583
saveSettings()
528584
else
529-
-- two retries and still no success
585+
-- max retries reached
530586
gState = PAGE_DISPLAY
531-
saveTS = 0
587+
invalidatePages()
532588
end
533589
end
534590

@@ -565,7 +621,7 @@ local function run(event)
565621
local page = SetupPages[currentPage]
566622
local field = page.fields[currentLine]
567623
local idx = field.i or currentLine
568-
if page.values and page.values[idx] then
624+
if page.values and page.values[idx] and (field.ro ~= true) then
569625
gState = EDITING
570626
end
571627
end

BFSetup_Horus.lua

+71-15
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ local MSP_SET_RC_TUNING = 204
254254
-- BF specials
255255
local MSP_PID_ADVANCED = 94
256256
local MSP_SET_PID_ADVANCED = 95
257+
local MSP_VTX_CONFIG = 88
258+
local MSP_VTX_SET_CONFIG = 89
257259

258260
local MSP_EEPROM_WRITE = 250
259261

@@ -267,6 +269,24 @@ local MENU_DISP = 5
267269

268270
local gState = PAGE_DISPLAY
269271

272+
local function postReadVTX(page)
273+
if page.values[1] == 3 then -- SmartAudio
274+
page.fields[3].table = { 25, 200, 500, 800 }
275+
page.fields[3].max = 4
276+
elseif page.values[1] == 4 then -- Tramp
277+
page.fields[3].table = { 25, 100, 200, 400, 600 }
278+
page.fields[3].max = 5
279+
else
280+
-- TODO: print label on unavailable (0xFF) vs. unsupported (0)
281+
--page.values = nil
282+
end
283+
end
284+
285+
local function getWriteValuesVTX(values)
286+
local channel = (values[2]-1)*8 + values[3]-1
287+
return { bit32.band(channel,0xFF), bit32.rshift(channel,8), values[4], values[5] }
288+
end
289+
270290
local SetupPages = {
271291
{
272292
title = "PIDs",
@@ -314,23 +334,50 @@ local SetupPages = {
314334
},
315335
read = MSP_RC_TUNING,
316336
write = MSP_SET_RC_TUNING,
337+
},
338+
{
339+
title = "VTX",
340+
text = {},
341+
fields = {
342+
-- Super Rate
343+
{ t = "Band", x = 35, y = 68, sp = 80, i=2, min=1, max=5, table = { "A", "B", "E", "F", "R" } },
344+
{ t = "Channel", x = 35, y = 96, sp = 80, i=3, min=1, max=8 },
345+
{ t = "Power", x = 35, y = 124, sp = 80, i=4, min=1 },
346+
{ t = "Pit", x = 35, y = 152, sp = 80, i=5, min=0, max=1, table = { [0]="OFF", "ON" } },
347+
{ t = "(Dev)", x = 240, y = 68, sp = 48, i=1, ro=true, table = {[3]="SmartAudio",[4]="Tramp"} },
348+
},
349+
read = MSP_VTX_CONFIG,
350+
write = MSP_VTX_SET_CONFIG,
351+
postRead = postReadVTX,
352+
getWriteValues = getWriteValuesVTX,
353+
saveMaxRetries = 0,
354+
saveTimeout = 200 -- 2s
317355
}
318356
}
319357

320358
local currentPage = 1
321359
local currentLine = 1
322360
local saveTS = 0
361+
local saveTimeout = 0
323362
local saveRetries = 0
363+
local saveMaxRetries = 0
324364

325365
local function saveSettings(new)
326366
local page = SetupPages[currentPage]
327367
if page.values then
328-
mspSendRequest(page.write,page.values)
368+
if page.getWriteValues then
369+
mspSendRequest(page.write,page.getWriteValues(page.values))
370+
else
371+
mspSendRequest(page.write,page.values)
372+
end
329373
saveTS = getTime()
330374
if gState == PAGE_SAVING then
331375
saveRetries = saveRetries + 1
332376
else
333377
gState = PAGE_SAVING
378+
saveRetries = 0
379+
saveMaxRetries = page.saveMaxRetries or 2 -- default 2
380+
saveTimeout = page.saveTimeout or 150 -- default 1.5s
334381
end
335382
end
336383
end
@@ -366,7 +413,9 @@ local function processMspReply(cmd,rx_buf)
366413

367414
-- ignore replies to write requests for now
368415
if cmd == page.write then
369-
mspSendRequest(MSP_EEPROM_WRITE,{})
416+
if cmd ~= MSP_VTX_SET_CONFIG then
417+
mspSendRequest(MSP_EEPROM_WRITE,{})
418+
end
370419
return
371420
end
372421

@@ -386,6 +435,10 @@ local function processMspReply(cmd,rx_buf)
386435
for i=1,#(rx_buf) do
387436
page.values[i] = rx_buf[i]
388437
end
438+
439+
if page.postRead ~= nil then
440+
page.postRead(page)
441+
end
389442
end
390443
end
391444

@@ -466,18 +519,22 @@ local function drawScreen(page,page_locked)
466519

467520
local idx = f.i or i
468521
if page.values and page.values[idx] then
469-
lcd.drawText(f.x + spacing, f.y, page.values[idx], text_options)
522+
local val = page.values[idx]
523+
if f.table and f.table[page.values[idx]] then
524+
val = f.table[page.values[idx]]
525+
end
526+
lcd.drawText(f.x + spacing, f.y, val, text_options)
470527
else
471528
lcd.drawText(f.x + spacing, f.y, "---", text_options)
472529
end
473530
end
474531
end
475532

476-
local function clipValue(val)
477-
if val < 0 then
478-
val = 0
479-
elseif val > 255 then
480-
val = 255
533+
local function clipValue(val,min,max)
534+
if val < min then
535+
val = min
536+
elseif val > max then
537+
val = max
481538
end
482539

483540
return val
@@ -492,7 +549,7 @@ local function incValue(inc)
492549
local page = SetupPages[currentPage]
493550
local field = page.fields[currentLine]
494551
local idx = field.i or currentLine
495-
page.values[idx] = clipValue(page.values[idx] + inc)
552+
page.values[idx] = clipValue(page.values[idx] + inc, field.min or 0, field.max or 255)
496553
end
497554

498555
local function drawMenu()
@@ -526,14 +583,13 @@ local function run(event)
526583
end
527584
lastRunTS = now
528585

529-
-- TODO: implement retry + retry counter
530-
if (gState == PAGE_SAVING) and (saveTS + 150 < now) then
531-
if saveRetries < 2 then
586+
if (gState == PAGE_SAVING) and (saveTS + saveTimeout < now) then
587+
if saveRetries < saveMaxRetries then
532588
saveSettings()
533589
else
534-
-- two retries and still no success
590+
-- max retries reached
535591
gState = PAGE_DISPLAY
536-
saveTS = 0
592+
invalidatePages()
537593
end
538594
end
539595

@@ -577,7 +633,7 @@ local function run(event)
577633
local page = SetupPages[currentPage]
578634
local field = page.fields[currentLine]
579635
local idx = field.i or currentLine
580-
if page.values and page.values[idx] then
636+
if page.values and page.values[idx] and (field.ro ~= true) then
581637
gState = EDITING
582638
end
583639
end

0 commit comments

Comments
 (0)