Skip to content

Instantly share code, notes, and snippets.

@MauRiEEZZZ
Forked from ghostface/vtx.lua
Created November 19, 2019 21:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MauRiEEZZZ/0174182fbbbb481f6c8416949ae15ad4 to your computer and use it in GitHub Desktop.
Save MauRiEEZZZ/0174182fbbbb481f6c8416949ae15ad4 to your computer and use it in GitHub Desktop.
VTX Power switching via transmitter switch (Opentx/Betaflight/Tramp/Smartaudio)
--
-- *Fixed version of wkochFPV for BF 3.5
-- Original: https://github.com/betaflight/betaflight/issues/3094#issuecomment-302953603
-- Note: If you put it on a 3-way switch and go from first to third position fast it may not register correctly so better step through slowly
--
-- *Changes:
-- Corrected loading of dependencies
-- Shortened filename to make it compatible with latest opentx restrictions
-- Added a condition in the script for pit mode (vtx power 0 alone didn't work for me)
--
-- *How To (Copy pasted from comment)
-- 1. Install betaflight-tx-lua-scripts as usual
-- 2. Install my script vtx.lua in /SCRIPTS/MIXES
-- 3. In model menu of your taranis go to LUA-scripts page and enable VTXprog.lua and configure as follows:
-- 4. Enter your desired band: 1="A", 2="B", 3="E", 4="F", 5="R" (unify pro!)
-- 5. Enter your desired channel: 1,2,3…
-- 6. Select “global variable 1” (GV1, or another free global variable) as input for TxPower
-- 7.Now configure any switch to adjust GV1 to the desired power level: 0=pit mode, 1=25mW, 2=200mW … (use special functions menu, select ‘adjust GV1’ as action, enter desired value). Use e.g. 0 for your switch neutral position, 1 for switch up, 2 for switch down, if you would like to select 25mw or 200mw based on switch position.
--
-- Load MSP smartport library
SCRIPT_HOME = "/SCRIPTS/BF"
protocol = assert(loadScript(SCRIPT_HOME.."/protocols.lua"))()
radio = assert(loadScript(SCRIPT_HOME.."/radios.lua"))()
assert(loadScript(radio.preLoad))()
assert(loadScript(protocol.transport))()
assert(loadScript(SCRIPT_HOME.."/MSP/common.lua"))()
-- define inputs
local inputs = {
{ "TxPower", SOURCE}, -- store requested TxPower in global variable, e.g. GV1
{ "TxBand", VALUE, 1, 5, 4 },
{ "TxChannel", VALUE, 1, 8, 1 }
}
-- Variables to store parameters last programmed to vtx
local lastPower = 0
local lastBand = 0
local lastChannel = 0
local MSP_VTX_SET_CONFIG = 89
local firstrun = 1
-- Returns payload to send to fc
local function VTXconfig(TxPower, TxBand, TxChannel,TxPitMode)
local channel = (TxBand-1)*8 + TxChannel-1
return { bit32.band(channel,0xFF), bit32.rshift(channel,8), TxPower, TxPitMode } -- last 0 disables PIT mode
end
local function run(TxPower, TxBand, TxChannel)
-- ignore any settings on first run of the script, send only further changes to vtx
if firstrun == 1 then
lastPower = TxPower
lastBand = TxBand
lastChannel = TxChannel
firstrun = 0
end
if (lastPower ~= TxPower) or (lastBand ~= TxBand) or (lastChannel ~= TxChannel) then
if TxPower > 0 then
mspSendRequest(MSP_VTX_SET_CONFIG,VTXconfig(TxPower, TxBand, TxChannel,0))
playFile("eng.wav")
else
mspSendRequest(MSP_VTX_SET_CONFIG,VTXconfig(TxPower, TxBand, TxChannel,1))
playFile("eng.wav")
end
if TxPower == 0 then
mspSendRequest(MSP_VTX_SET_CONFIG,VTXconfig(0, TxBand, TxChannel))
end
lastPower = TxPower
lastBand = TxBand
lastChannel = TxChannel
end
mspProcessTxQ()
return
end
return {input=inputs, run=run}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment