Help with an LUA script.. anyone?
-
I'm trying to use the EDM using only two physical buttons, like in real life. I'm using FSUIPC, and assigning all the functions (short press, long press, both press) to individual buttons works well (so using 5 individual hardware inputs).
As I have no idea of LUA scripting, I asked ChatGPT, but it does not work. Maybe someone with a deeper knowledge could have a look at this.. I'd be grateful:
-- JPI_DualButton_Param.lua
-- Parameter 1 -> Taste A press
-- Parameter -1 -> Taste A release
-- Parameter 2 -> Taste B press
-- Parameter -2 -> Taste B release-- ============= KONFIGURATION =================
hvarA_short = "bksq_JpiButton_1_L_Short"
hvarA_long = "bksq_JpiButton_1_L_Long"
hvarB_short = "bksq_JpiButton_1_R_Short"
hvarB_long = "bksq_JpiButton_1_R_Long"
hvarAB_long = "bksq_JpiButton_1_B_Long"longpress_ms = 2000
-- ============= INTERN ==============
pressedA = false
pressedB = false
startA = 0
startB = 0-- HVAR Setter
local function setHVar(name)
pcall(function()
ipc.writeSTR("H:" .. name, "")
end)
end-- Prüfen ob beide >= longpress
local function bothLong(now)
if not (pressedA and pressedB) then return false end
return (now - startA >= longpress_ms) and (now - startB >= longpress_ms)
end
-- HAUPTFUNKTION – wird per LuaValue(Parameter) aus FSUIPC aufgerufen
function ipcPARAM(param)
local now = ipc.elapsedtime() ---------------------------------------------------------------- -- TASTE A ---------------------------------------------------------------- if param == 1 then -- A pressed pressedA = true startA = now elseif param == -1 then -- A released local dur = now - startA if bothLong(now) then setHVar(hvarAB_long) else if dur >= longpress_ms then setHVar(hvarA_long) else setHVar(hvarA_short) end end pressedA = false startA = 0 end ---------------------------------------------------------------- -- TASTE B ---------------------------------------------------------------- if param == 2 then -- B pressed pressedB = true startB = now elseif param == -2 then -- B released local dur = now - startB if bothLong(now) then setHVar(hvarAB_long) else if dur >= longpress_ms then setHVar(hvarB_long) else setHVar(hvarB_short) end end pressedB = false startB = 0 endend
-
@MatzeH84 From rubbing the sleep from my eyes or the scattering of German (I think) throughout, I'm having trouble figuring out what you're trying to do here.
Are you trying to use the script to send a different input based on which of your 5 hardware buttons you press?
-
Basically we have 5 Hvars now. Left short press, left long press, right short press, right long press and both buttons long press. I have assigned them individually to 5 buttons via FSUIPC and that works.
However I would like to try to use only two buttons and use the script to activate different Hvars depending on if the button is pressed momentarily or held (or if both buttons are pressed together), in order to operate the EDM as it would be in the real unit. -
I think I have a script that will work... as soon as I can get FSUIPC to work with the HVars. I have my HVar file in my community folder, I can activate the HVar via FSUIPC's "Execute Calculator Code," but the ipc.activateHvar function in the lua script is not working. I will reach out to John for help.
-
Alright, I have a working first draft.
joyLetter="B" lButtonNumber=2 rButtonNumber=3 rDepressed=false lDepressed=false rDepressedTime=0 lDepressedTime=0 function EDMButtons() -- Check buttons and get time rNow=ipc.testbutton(joyLetter,rButtonNumber) lNow=ipc.testbutton(joyLetter,lButtonNumber) timeNow=ipc.elapsedtime() -- Update button states if rNow == true and rDepressed == false then -- Was depressed rDepressed = true rDepressedTime=timeNow --ipc.log("Right Depress") elseif rNow == false and rDepressed == true then -- Was released rDepressed = false --ipc.log("Right Release") end if lNow == true and lDepressed == false then -- Was depressed lDepressed = true lDepressedTime=timeNow --ipc.log("Left Depress") elseif lNow == false and lDepressed == true then -- Was released lDepressed = false --ipc.log("Left Release") end -- Begin button logic if rDepressed == true and timeNow - rDepressedTime > 2500 and lDepressed == true and timeNow - lDepressedTime > 2500 then ipc.execCalcCode("(>H:bksq_JpiButton_1_B_Long)") --ipc.log("Long Both Press") rDepressedTime=0 lDepressedTime=0 rDepressed=false lDepressed=false ipc.sleep(500) elseif rDepressed == true and timeNow - rDepressedTime > 2500 then ipc.execCalcCode("(>H:bksq_JpiButton_1_R_Long)") --ipc.log("Long Right Press") rDepressedTime=0 rDepressed=false ipc.sleep(500) elseif lDepressed == true and timeNow - lDepressedTime > 2500 then ipc.execCalcCode("(>H:bksq_JpiButton_1_L_Long)") --ipc.log("Long Left Press") lDepressedTime=0 lDepressed=false ipc.sleep(500) elseif rDepressed == false and rDepressedTime > 0 and lDepressed == false and lDepressedTime > 0 then if ipc.readLvar("L:var_JpiTempUnit") == 0 then ipc.writeLvar("L:var_JpiTempUnit",1) else ipc.writeLvar("L:var_JpiTempUnit",0) end --ipc.log("Short Both Press") rDepressedTime=0 lDepressedTime=0 elseif rDepressed == false and rDepressedTime > 0 then ipc.execCalcCode("(>H:bksq_JpiButton_1_R_Short)") rDepressed=false --ipc.log("Short Right Press") rDepressedTime=0 elseif lDepressed == false and lDepressedTime > 0 then ipc.execCalcCode("(>H:bksq_JpiButton_1_L_Short)") lDepressed=false --ipc.log("Short Left Press") lDepressedTime=0 end end event.timer(200,"EDMButtons")This should hopefully be pretty plug and play. The top three variables, joyLetter and l/rButtonNumber will need to be changed to match whatever device letter FSUIPC has assigned your button box, and the button numbers will need to match what FSUIPC would show if you were trying to bind it.
You will also need a .hvar file in your fsuipc/modules folder in the Community folder of FS 2020 or 2024, whichever you're using, that lists out the HVars in question. It's pretty simple, just a plain text file with the .hvar extension. Mine contains
H:bksq_JpiButton_1_L_Short H:bksq_JpiButton_1_L_Long H:bksq_JpiButton_1_R_Short H:bksq_JpiButton_1_R_Long H:bksq_JpiButton_1_B_Longand is named Bonanza.hvar, should work ok. I did this for the Bonanza, obviously, but if you name yours Baron or something that will match at least a sub string of the model name being loaded (e.g. Black Square A36TC Bonanza Professional N3518X for me right now) it will find it.
Drop the top script into a .lua file with a name of your choice and have FSUIPC run it when it loads your aircraft profile. The L and R buttons you assign will need a brief hold; I suspect it's because the script runs at 5Hz, so a very quick press might not read it. I didn't dabble with the polling rate too much, as I was having issues with phantom presses after long holds, so I stuck with 5Hz and after a long press is detected I simply wait 500ms before it resumes again to give you time to release the button.
The script is probably very inelegant and I'm half worried that by posting it, Nick will run me out of the forums for committing crimes against the compiler, but I think it does the job. Refinements are welcome.
I set the threshold for a long press to be 2.5 seconds, so that's also something you can change if you like.