Jump to content
The simFlight Network Forums

Make a Function Toggle LUA


Recommended Posts

After failing to be able to use acceleration on my Heading rotary , I have tried to make it toggle between 1 deg steps and 10 deg steps by pressing the button on the rotary.

It's for the Aerosoft Airbus and there is a script for toggling the ALT but not the HDG.  Therefore I copied the ALT script and adjusted to toggle the HDG  but  can't get it to toggle.

Here is the relevant section...  much obliged if someone can help...

AB_HDGSTEP_set = 1

function AB_HDG_Step_tog ()
         var = ipc.readLvar("AB_HDGSTEP_set")
    if var == 1 then
        AB_HDG_Step_1 ()
    else
        AB_HDG_Step_10 ()
    end
end

function AB_HDG_Step_1 ()
         LVarSet = ("AB_HDGSTEP_set")
         ipc.writeLvar(LVarSet, 0)
    Anim = ("AB_AP_HDG_Knob_Ani")
    ipc.writeLvar(Anim, 1)
    ipc.sleep(30)
    ipc.writeLvar(Anim, 0)
end

function AB_HDG_Step_10 ()
         LVarSet = ("AB_HDGSTEP_set")
         ipc.writeLvar(LVarSet, 1)
    Anim = ("AB_AP_HDG_Knob_Ani")
    ipc.writeLvar(Anim, -1)
    ipc.sleep(30)
    ipc.writeLvar(Anim, 0)
end

eb01db8b96357d10cd27f7116b416bf6.png

....and the log

598dc7e21a4b75e47e56dce62fd04383.png

 

The button animation doesn't toggle either (as an indication) but does work both ways in/out if set manually in the script.

  The full Lua  section ---         AB_Triple_HDG.lua

Everything else works great...   press Twice for heading set mode  and press longer for heading managed mode

 

 

 

Link to comment
Share on other sites

14 hours ago, aerostar17 said:

After failing to be able to use acceleration on my Heading rotary , I have tried to make it toggle between 1 deg steps and 10 deg steps by pressing the button on the rotary.

There are assignable controls added by FSUIPC for this:

Heading Bug Dec Fast (–10)
Heading Bug Inc Fast (+10)

These are in the drop-down list in Button and Key assignments. For a full list of the additional controls provided by FSUIPC, please check the list in the Advanced User's document.

Pete

 

  • Like 1
Link to comment
Share on other sites

Thanks for replying, Pete.    I am using TripleUse.lua on this rotary which has a push button which I use for  Heading...

1) Press longer = Managed Mode 

2) Double Press = Set Mode

and want  3) Single Press = Toggle Step 1 or 10 Degrees

I can't think of a way to integrate with the FSUIPC controls as far as toggling with one button press... would I still need a Lua script?

If not tied to TripleUse.lua then the button toggles would act additionally with button presses..    It's really the  Toggle part that's not working...  switching between the two steps. The HDG degrees do inc/dec if set independently in the script.

 

 

PS-   Can the  (example)    Rotaries.lua     be adapted to work with a home made 'LeoBodnar 32 interface'  button box to enable Rotary acceleration as this would be the best option, maybe or if there is another rotary acceleration method then I wouldn't need the Press Toggle ... 

Thanks,

Ian

Link to comment
Share on other sites

55 minutes ago, aerostar17 said:

I can't think of a way to integrate with the FSUIPC controls as far as toggling with one button press... would I still need a Lua script?

With a one button press for alternately using 1 step or 10 step, use the button flag and make it conditional. There's an example of toggling the button press action in the Advanced User's guide, thus:

 

For every possible “normal” button (16 joysticks x 32 buttons = 512 buttons)
FSUIPC maintains a “Flag” (F). Each time any button is pressed (goes from off to on) FSUIPC toggles its flag. This
makes the buttons flag a sort of “latching” switch. You can test it in any parenthesised condition by preceding the
condition by F, thus:
N=CP(F+j2,b2) …
This says the rest of this parameter is obeyed if the Flag associated with j2,b2 is set. A condition (F–j2,b2) tests for the
Flag being clear. Note that the actual current state of the button j2,b2 is not relevant. All that matters is whether it last
left its Flag set or clear.


So for example

1=CP(F+4,2)4,2,C12345
2=CP(F-4,2)4,2,C54321

says alternately send control 12345 and 54321 each time Joy 4, button 2 gets pressed.

1 hour ago, aerostar17 said:

It's really the  Toggle part that's not working...  switching between the two steps. The HDG degrees do inc/dec if set independently in the script.

Ah, maybe I didn't read it so deeply.   Without me spending time trying to decipher the code, why doesn't the Lua tracve logging help you. The line numbers and variable values are shown. They don't help me because the Lua section you posted doesn't have enough line numbers -- the logged ones don't match.

If you help me look at the part you think should "toggle" then I can maybe help.

1 hour ago, aerostar17 said:

PS-   Can the  (example)    Rotaries.lua     be adapted to work with a home made 'LeoBodnar 32 interface'  button box to enable Rotary acceleration as this would be the best option, maybe or if there is another rotary acceleration method then I wouldn't need the Press Toggle ... 

The LeoBodnar cards I use work with standar rotaries which send o press or release indication ("ground" or "live") for each click.  They're the easier ones to handle. If your rotaries are of the phased type which need more decoding then I don't know whether the Bodnar microcode does the decoding or whether you have to.

Assuming they are the former type, or the board does handle them okay, then obviously you can use the same method as in Triple Use to decide on the interpretation of the button on/off changes based on how fast they arrive -- slow = signle, fast = 10's. Though in the latter case you would probably discard some of the changes as on/offs will be arriving too fast for decient control.

Pete

 

  • Like 1
Link to comment
Share on other sites

Apologies Pete for messing the Log was trying different things then editing my post... 

I have stripped out the unnecessary parts and this is the part that's not toggling/switching...  I have posted the corresponding Log

where it can be seen that on    button flag 4    it always stays at    ' AB_HDG_Step_10'

I have also included the full  Lua file attached...   not very big.

 

e3bbb65b29f00767f3896fa2a0df13f3.png

 

9206489cbe7cce08adbe5dbaa4a13c87.png

 

Here is the Lua file -              AB_Triple_HDG.lua

 

Ian

Link to comment
Share on other sites

Hi ,  Pete ...   I have replaced the   AB_HDGSTEP_set    with the     AB_AP_ALTSTEP    which is a working   LVar  as a test and now it toggles  and works good...   now the only thing is when I toggle the  HDG step  it also toggles the ALT step   as they are now sharing the same LVar...     I think  that means that I need to properly define the      AB_HDGSTEP_set       LVar as it's probably not being recognised,   I created it.   I thought by naming it and giving it a value outside the Function that would define it.     

Can I ask how I should define the LVar        AB_HDGSTEP_set       so that it is recognised and usable   ?

Thanks again, 

Ian

Link to comment
Share on other sites

11 hours ago, aerostar17 said:

I have replaced the   AB_HDGSTEP_set    with the     AB_AP_ALTSTEP    which is a working   LVar  as a test and now it toggles  and works good...   now the only thing is when I toggle the  HDG step  it also toggles the ALT step   as they are now sharing the same LVar.

Two things I noticed in your Lua. You define "LVarSet" several times but then use the literal name in your ipc.Lvar calls anyway -- except once.

Second, are the LVars already existing in the aircraft you have loaded? They are shown by the Lvar logging?

If "AB_AP_ALTSTEP exists, are you sure there isn't a matching "AB_AP_HDGSTEP"? Because I would have thought just "HDG" without the AP prefix would really be the actual current aircraft heading, not the AP dial.

Quote

 

..     I think  that means that I need to properly define the      AB_HDGSTEP_set       LVar as it's probably not being recognised,   I created it.   I thought by naming it and giving it a value outside the Function that would define it.     

Can I ask how I should define the LVar        AB_HDGSTEP_set       so that it is recognised and usable   ?

 

You can't create LVars in Lua, they are created by the code in your add-on aircraft.

If all you want is a value for your use, to remember what mode you are in, you should use a Lua "GLOBAL" -- see the ipc.set and ipc.get functions. They use a global thread, parallel to your Lua thread, and shares those values to any running Lua thread. They will be remembered over each event calling your Lua functions.

Pete

 

  • Like 1
Link to comment
Share on other sites

 

Whoopee...   thanks Pete,  using    ipc.get    and   .set   works a treat,  it's now all working...   can't figure how the   AB_AP_ALTSTEP    worked though as it is using    readLvar   and   writeLvar  ...

20 hours ago, Pete Dowson said:

Assuming they are the former type, or the board does handle them okay, then obviously you can use the same method as in Triple Use to decide on the interpretation of the button on/off changes based on how fast they arrive -- slow = signle, fast = 10's.

Now I may try and figure out again how to use   TripleUse.lua   for the  Rotary Acceleration    and then I wouldn't need the button toggle which would free up another button option...   I'm pretty sure it's the standard rotaries I'm using.  Any pointers would be welcome

  do you use    Rotary Acceleration   and if so, what method do you use... ?

I hope I'm not over stepping with my questions...   

once again though , 

Thank you Pete for your help...

Ian

Link to comment
Share on other sites

5 minutes ago, aerostar17 said:

can't figure how the   AB_AP_ALTSTEP    worked though as it is using    readLvar   and   writeLvar  ...

Must be the function that LVar performs in the aircraft model -- for the AP ALT adjustment. you were changing that setting and iterpreting the change for your toggle as well.

6 minutes ago, aerostar17 said:

do you use    Rotary Acceleration   and if so, what method do you use... ?

My MCP is part of my PFC cockpit, and my own driver for it handles all that by program. The only rotaries i have which are outside of the cockpit are a couple of GoFlight RP48's, which have 4 rotaies each. The GoFlight firmware in the device looks after acceleration -- turn it fast and you get a different button number indication. Sothey effectively have 4 button presses each.

The acceleration using a rotary is really the same as for a single button provided you remember two things:

1. The button indications you see alternate between "on" and "off" for each successive "click". So you need to handle the change not whether it is on or off.

2. The presses will arrive fast if you turn it fast. May be too fast for your aircraft gauges to keep up. You might need to start with a parameter you can easily change when testing to only act on every nth change in the 'fast' mode (i.e. when they are arriving with less than say 500 mSecs apart).

With Lua it is likely that you'll miss some anyway, because only one occurrence is queued for each event registered. To handle that a bit more predictable you might want two event.button calls registered per button -- one for "when pressed" and the other for "when released". They can refer to the same function in your program of course.

Pete

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.