Skip to content

Commit 249aa8f

Browse files
committed
Made vtx freq editable
Can step through available frequencies for bands / channels. If Cleanflight/Betaflight has 'SetFreqByMHzMsp' support then any frequency in MHz can be set (via band 'U').
1 parent 6c3ded4 commit 249aa8f

File tree

4 files changed

+606
-56
lines changed

4 files changed

+606
-56
lines changed

src/SCRIPTS/BF/HORUS/vtx.lua

Lines changed: 204 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ return {
44
write = 89, -- MSP_VTX_SET_CONFIG
55
eepromWrite = true,
66
reboot = false,
7-
saveMaxRetries = 2,
8-
saveTimeout = 300, -- 3s
97
title = "VTX",
108
minBytes = 5,
9+
prevBandVal = 0,
10+
prevChanVal = 0,
11+
prevFreqVal = 0,
12+
lastFreqUpdTS = 0,
13+
freqModCounter = 0,
1114
text= {
1215
{ t = "Band", x = 36, y = 110 },
1316
{ t = "Channel", x = 36, y = 155 },
@@ -18,12 +21,12 @@ return {
1821
},
1922
fields = {
2023
-- Band
21-
{ x = 130, y = 110, min=1, max=5, vals = { 2 }, to = MIDSIZE,
22-
table = { "A", "B", "E", "F", "R" },
23-
upd = function(self) self.updateVTXFreq(self) end },
24+
{ x = 130, y = 110, min=0, max=5, vals = { 2 }, to = MIDSIZE,
25+
table = { [0]="U", "A", "B", "E", "F", "R" },
26+
upd = function(self) self.handleBandChanUpdate(self) end },
2427
-- Channel
2528
{ x = 130, y = 155, min=1, max=8, vals = { 3 }, to = MIDSIZE,
26-
upd = function(self) self.updateVTXFreq(self) end },
29+
upd = function(self) self.handleBandChanUpdate(self) end },
2730
-- Power
2831
{ x = 350, y = 110, min=1, vals = { 4 }, to = MIDSIZE,
2932
upd = function(self) self.updatePowerTable(self) end },
@@ -33,9 +36,10 @@ return {
3336
-- Proto
3437
{ x = 130, y = 68, vals = { 1 }, to = MIDSIZE,
3538
write = false, ro = true,
36-
table = {[3]="SmartAudio",[4]="Tramp",[255]="None"} },
39+
table = { [1]="RTC6705",[3]="SmartAudio",[4]="Tramp",[255]="None"} },
3740
-- Freq
38-
{ x = 350, y = 68, min=5000, max=6000, ro=true, to = MIDSIZE },
41+
{ x = 350, y = 68, min = 5000, max = 5999, vals = { 6 }, to = MIDSIZE,
42+
upd = function(self) self.handleFreqValUpdate(self) end },
3943
},
4044
freqLookup = {
4145
{ 5865, 5845, 5825, 5805, 5785, 5765, 5745, 5725 }, -- Boscam A
@@ -45,35 +49,215 @@ return {
4549
{ 5658, 5695, 5732, 5769, 5806, 5843, 5880, 5917 }, -- RaceBand
4650
},
4751
postLoad = function (self)
48-
if self.values[2] == 0 or self.values[3] == 0 or self.values[4] == 0 then
52+
if (self.values[2] or 0) < 0 or (self.values[3] or 0) == 0 or (self.values[4] or 0) == 0 then
4953
self.values = {}
54+
else
55+
self.prevBandVal = 0 -- reset value trackers
56+
self.prevChanVal = 0
57+
self.prevFreqVal = 0
58+
local rFreq
59+
if (self.values[7] or 0) > 0 then
60+
rFreq = math.floor(self.values[6] + (self.values[7] * 256))
61+
else
62+
rFreq = 0
63+
end
64+
if (self.values[2] or 0) > 0 then -- band != 0
65+
if rFreq > 0 then
66+
self.prevFreqVal = rFreq
67+
self.prevBandVal = self.values[2]
68+
self.prevChanVal = self.values[3]
69+
self.fields[1].min = 0 -- make sure 'U' band allowed
70+
self.eepromWrite = true
71+
self.fields[6].value = rFreq
72+
self.values[6] = rFreq
73+
else -- if user freq not supported then
74+
self.fields[1].min = 1 -- don't allow 'U' band
75+
self.eepromWrite = false -- don't write EEPROM on older Betaflight versions
76+
end
77+
else -- band == 0
78+
if rFreq > 0 then
79+
self.prevFreqVal = rFreq
80+
self.fields[1].min = 0 -- make sure 'U' band allowed
81+
self.eepromWrite = true
82+
self.fields[6].value = rFreq
83+
self.values[6] = rFreq
84+
-- set chan via freq / 100
85+
self.prevChanVal = clipValue(math.floor((rFreq - 5100) / 100),
86+
self.fields[2].min, self.fields[2].max)
87+
self.fields[2].value = self.prevChanVal
88+
self.values[3] = self.prevChanVal
89+
else
90+
self.values = {}
91+
end
92+
end
5093
end
5194
end,
5295
preSave = function(self)
5396
local valsTemp = {}
54-
local channel = (self.values[2]-1)*8 + self.values[3]-1
55-
valsTemp[1] = bit32.band(channel,0xFF)
56-
valsTemp[2] = bit32.rshift(channel,8)
57-
valsTemp[3] = self.values[4]
58-
valsTemp[4] = self.values[5]
97+
if self.values then
98+
local channel
99+
if self.values[2] > 0 then -- band != 0
100+
channel = (self.values[2]-1)*8 + self.values[3]-1
101+
elseif self.fields[6].value then -- band == 0
102+
channel = self.fields[6].value
103+
else
104+
channel = 24
105+
end
106+
valsTemp[1] = bit32.band(channel,0xFF)
107+
valsTemp[2] = bit32.rshift(channel,8)
108+
valsTemp[3] = self.values[4]
109+
valsTemp[4] = self.values[5]
110+
end
59111
return valsTemp
60112
end,
113+
-- find closest value in freq table that is above/below given freq
114+
findNextInFreqTable = function(self, newFreq)
115+
local startBand
116+
local endBand
117+
local incFlag -- freq increasing or decreasing
118+
if newFreq > self.prevFreqVal then
119+
incFlag = 1
120+
startBand = 1
121+
endBand = self.fields[1].max
122+
else
123+
incFlag = -1
124+
startBand = self.fields[1].max
125+
endBand = 1
126+
end
127+
local curBand = self.values[2]
128+
local curChan = self.values[3]
129+
local selBand = 0
130+
local selChan = 0
131+
local selFreq = 0
132+
local diffVal = 9999
133+
local fVal
134+
local minChan = self.fields[2].min
135+
local maxChan = self.fields[2].max
136+
-- need to scan bands in same "direction" as 'incFlag'
137+
-- so same-freq selections will be handled properly (F8 & R7)
138+
for band=startBand,endBand,incFlag do
139+
for chan=minChan,maxChan do
140+
if band ~= curBand or chan ~= curChan then -- don't reselect same band/chan
141+
fVal = self.freqLookup[band][chan]
142+
if incFlag > 0 then
143+
if fVal >= self.prevFreqVal and fVal - self.prevFreqVal < diffVal then
144+
-- if same freq then only select if "next" band:
145+
if fVal ~= self.prevFreqVal or band > curBand then
146+
selBand = band
147+
selChan = chan
148+
selFreq = fVal
149+
diffVal = fVal - self.prevFreqVal
150+
end
151+
end
152+
else
153+
if fVal <= self.prevFreqVal and self.prevFreqVal - fVal < diffVal then
154+
-- if same freq then only select if "previous" band:
155+
if fVal ~= self.prevFreqVal or band < curBand then
156+
selBand = band
157+
selChan = chan
158+
selFreq = fVal
159+
diffVal = self.prevFreqVal - fVal
160+
end
161+
end
162+
end
163+
end
164+
end
165+
end
166+
return selFreq, selBand, selChan
167+
end,
168+
-- returns the next user-frequency value in MHz; implements an
169+
-- "exponential" modification rate so dialing in values is faster
170+
getNextUserFreqValue = function(self, newFreq)
171+
local now = getTime() -- track rate of change for possible mod speedup
172+
if now < self.lastFreqUpdTS + 15 then
173+
self.freqModCounter = self.freqModCounter + (15-(self.lastFreqUpdTS-now)) -- increase counter for mod speedup
174+
else
175+
self.freqModCounter = 0 -- no mod speedup
176+
end
177+
local uFreq
178+
if self.freqModCounter > 65 then -- rate is fast enough; do mod speedup
179+
if newFreq > self.prevFreqVal then
180+
uFreq = clipValue(newFreq + math.floor(self.freqModCounter / 65),
181+
self.fields[6].min, self.fields[6].max)
182+
else
183+
uFreq = clipValue(newFreq - math.floor(self.freqModCounter / 65),
184+
self.fields[6].min, self.fields[6].max)
185+
end
186+
else
187+
uFreq = newFreq
188+
end
189+
self.lastFreqUpdTS = now
190+
return uFreq
191+
end,
61192
updatePowerTable = function(self)
62193
if self.values and not self.fields[3].table then
63-
if self.values[1] == 3 then
194+
if self.values[1] == 1 then -- RTC6705
195+
self.fields[3].table = { 25, 200 }
196+
self.fields[3].max = 2
197+
self.fields[4].t = nil -- don't display Pit field
198+
self.fields[4].table = { [0]="", "" }
199+
elseif self.values[1] == 3 then -- SmartAudio
64200
self.fields[3].table = { 25, 200, 500, 800 }
65201
self.fields[3].max = 4
66-
elseif self.values[1] == 4 then
202+
elseif self.values[1] == 4 then -- Tramp
67203
self.fields[3].table = { 25, 100, 200, 400, 600 }
68204
self.fields[3].max = 5
69205
end
70206
end
71207
end,
72-
updateVTXFreq = function(self)
208+
handleBandChanUpdate = function(self)
73209
if (#(self.values) or 0) >= self.minBytes then
74-
if (self.fields[2].value or 0) > 0 and (self.fields[3].value or 0) > 0 then
75-
self.fields[6].value = self.freqLookup[self.values[2]][self.values[3]]
210+
if (self.values[3] or 0) > 0 then
211+
if self.values[2] ~= self.prevBandVal or self.values[3] ~= self.prevChanVal then
212+
if self.values[2] > 0 then -- band != 0
213+
self.prevFreqVal = self.freqLookup[self.values[2]][self.values[3]]
214+
else -- band == 0; set freq via channel*100
215+
self.prevFreqVal = math.floor(5100 + (self.values[3] * 100))
216+
end
217+
self.fields[6].value = self.prevFreqVal
218+
self.values[6] = self.prevFreqVal
219+
self.prevBandVal = self.values[2]
220+
self.prevChanVal = self.values[3]
221+
end
222+
end
223+
end
224+
end,
225+
handleFreqValUpdate = function(self)
226+
if (#(self.values) or 0) >= self.minBytes and (self.fields[6].value or 0) > 0 then
227+
local newFreq = self.fields[6].value
228+
if newFreq ~= self.prevFreqVal then
229+
if self.values[2] == 0 then
230+
-- band == 0
231+
local uFreq = self.getNextUserFreqValue(self, newFreq)
232+
self.prevFreqVal = uFreq
233+
if uFreq ~= newFreq then
234+
self.fields[6].value = uFreq
235+
self.values[6] = uFreq
236+
end
237+
-- set channel value via freq/100
238+
self.prevChanVal = clipValue(math.floor((self.prevFreqVal - 5100) / 100),
239+
self.fields[2].min, self.fields[2].max)
240+
self.fields[2].value = self.prevChanVal
241+
self.values[3] = self.prevChanVal
242+
else
243+
-- band != 0; find closest freq in table that is above/below dialed freq
244+
local selFreq, selBand, selChan = self.findNextInFreqTable(self, newFreq)
245+
if selFreq > 0 then
246+
self.prevFreqVal = selFreq
247+
self.prevBandVal = selBand
248+
self.prevChanVal = selChan
249+
self.fields[6].value = selFreq -- using new freq from table
250+
self.values[6] = selFreq
251+
self.fields[1].value = self.prevBandVal -- set band value for freq
252+
self.values[2] = self.prevBandVal
253+
self.fields[2].value = self.prevChanVal -- set channel value for freq
254+
self.values[3] = self.prevChanVal
255+
else
256+
self.fields[6].value = self.prevFreqVal -- if no match then revert freq
257+
self.values[6] = self.prevFreqVal
258+
end
259+
end
76260
end
77261
end
78262
end
79-
}
263+
}

0 commit comments

Comments
 (0)