Jump to content
The simFlight Network Forums

LUA file and button / keypress assignments


Recommended Posts

Hi there,

 

As mentioned in the User Guide, some 3rd-party aircraft "use the more recent technique of having gauges in XML. Those are also not susceptible to "Mouse Macros". That's exactly the case with SibWings Antonov An-2 which initiates LVars through swan2.DLL. So, I guess I won't be able to use mouse macros for this aircraft, as also been proven by numerous tests from my side with the latest versions of the aircraft and FSUIPC (registered). 

 

So, I've decided to use another approach by making a LUA plugin similar to A2A Spitfire but it contains more than one function tailored specifically for that particular aircraft model. I would be happy to limit myself to default FSX controls managed through FSUIPC.INI but the usage of a LUA plugin is necessary since some of the switches are bound to LVars only - like the inertial clutch and the oil shutter mechanism.

 

How can I assign buttons and keypresses on my Saitek X52 Pro to be able to call different functions from the same LUA file without using macros or LINDA? Is it ever possible through FSUIPC.INI?!

 

Thanks!

 

Rustam

Link to comment
Share on other sites

How can I assign buttons and keypresses on my Saitek X52 Pro to be able to call different functions from the same LUA file without using macros or LINDA? Is it ever possible through FSUIPC.INI?!

 

You can either have multiple small Lua plug-ins, one for each assignable function, and just assign those buttons appropriately, or you can have one larger Lua plug-in,loaded initially (perhaps by an [Auto] entry in the INI file), and have that do different things according to the PARAM value, the Parameter you provide when assigning a button or switch,

 

In the former case you assign to "Lua name" in the second to "LuaValue name".

 

Your choice. Whatever you find easier. I doubt there's much difference in performance or efficiency.

 

Pete

Link to comment
Share on other sites

Thank you, Pete!

 

I've followed your advice and this is what I have achieved so far:

 

FSUIPC.INI section:

[Profile.SW_AN2]
1=Antonov

[Buttons.SW_AN2]
0=P0,26,CL1:R,1
1=P0,24,CL1:R,2

[Auto.SW_AN2]
1=Lua SW_AN2

[LuaFiles]
1=SW_AN2

Contents of the LUA file:

-- PARAMETERS
-- ----------
-- 1 = Inertial Clutch Charge
-- 2 = Inertial Clutch On
-- 0 = Inertial Clutch Off


        
-- Set to Zero
    ipc.writeLvar("L:AN2_inertial_clutch",0)


-- Set to Charge
    if ipcPARAM == 1 then


ipc.writeLvar("L:AN2_inertial_clutch",1)
ipc.writeLvar("L:play_sound",11)


end


-- Set to On 
if ipcPARAM == 2 then


ipc.writeLvar("L:AN2_inertial_clutch",-1)
ipc.writeLvar("L:play_sound",11)


end


-- Set to Off 
if ipcPARAM == 0 then


ipc.writeLvar("L:AN2_inertial_clutch",0)
ipc.writeLvar("L:play_sound",0)


end

Filename: SW_AN2.lua.

 

The file sits inside FSX Modules folder. However, whatever I do I can't initiate the ipcPARAM == 2. Parameters 0 and 1 work and LVar "play_sound" plays as it should though.

 

Any guess what can cause the... problem?

 

Thanks!

Link to comment
Share on other sites

The Lua file you have written runs once, then terminates. There is nothing there keeping it running, so why bother to have it loaded in an [Auto] section? It will there presumable operate with a zero parameter? Is that what you intend? I assume so because you've no assignment with parameter 0.

 

I offered two alternative ways of doing what you wanted and you seem to have tried to combine both. Why?

 

Why are you writing the same L:Var at the start, every time,  to "set to zero"? Maybe there's not enough time between the two writes to the same mechanism?

 

Apart from that, there is no difference for the Lua actions between parameters 0, 1 or 2, so any differences you are seeing must be functions of the add-on. Try using the Lua debug/trace option (in the Logging tab) to see exactly which lines are actioned and what the values are.

 

Pete

Link to comment
Share on other sites

Just FYI,

 

I assign buttons and switches all the time to L:Variables via the 'normal' FSUIPC4 procedure.

 

All one needs to do is create a MCRO file that declares the L:vars and the type of action desired and

place that file 'some_name.MCRO' into the Modules folder.

 

FSUIPC4 than reads that file and adds those L:Vars to the list of assignable controls/commands.

 

I only use Lua scripts when I need to scale a variable or do some other action that  isn't feasible using the

normal assignment  process.

 

   Paul

Link to comment
Share on other sites

Pete,

Thank you for replies!
 

The Lua file you have written runs once, then terminates. There is nothing there keeping it running, so why bother to have it loaded in an [Auto] section?

 

Absolutely! Changed...
 

In the former case you assign to "Lua name" in the second to "LuaValue name".

 

I think I need to read and practice more with "LuaValue" method.

After some trial and error this is what I have for now. Although this doesn't work as required with a 3-position clutch switch:

-- LVar values:
-- off = 0
-- charge = 1
-- on = -1

if ipcPARAM == 0 then

   local p = ipc.readLvar("L:AN2_inertial_clutch") 

   if p < 1 then p = p + 1 end

      ipc.writeLvar("L:AN2_inertial_clutch",p)

end

As I said, this code works but partially - the clutch stops when p = 1. But since the switch has 3 positions, I can't figure how to return it to any other position when p = 1 using the same button. So, i guess i have no other choice than use two buttons with different parameters, or (if it's ever possible) to assign different parameters to the same button inside FSUIPC.INI.

Paul, thank you too! In case of this particular model, unfortunately, I can't use macros. They simply don't initiate except with default FSX aircraft. So, i guess my only resort is to use Lua plugins.

Thanks!

Link to comment
Share on other sites

As I said, this code works but partially - the clutch stops when p = 1. But since the switch has 3 positions, I can't figure how to return it to any other position when p = 1 using the same button. So, i guess i have no other choice than use two buttons with different parameters, or (if it's ever possible) to assign different parameters to the same button inside FSUIPC.INI.

 

You can make as many assignments to the same button as you wish. FSUIPC executes them in order. But if you have a 3 position switch, surely each position is detected seperately -- or at least one position = "released" and the other two different button numbers? If not, then the wiring of the switch is all wrong, with all three positions, or at least two of them, wired together!

 

Paul, thank you too! In case of this particular model, unfortunately, I can't use macros. They simply don't initiate except with default FSX aircraft. So, i guess my only resort is to use Lua plugins.
 
I think you are rather confused here. The FSUIPC macro facility is a method of allowing a single or sequence of commands, whether L:Var writes or simple control or keyboard assignments, to be stored in files (.mcro files) with names which can be assigned in the Buttons & Keys tab.
 
Maybe you are assuming that "macros" always means "mouse macros". Mouse macros are a specific type of macro which operates mouse action areas in gauges which are suited. But that is only one type of macro (and the most recent addition to the range of things macros can do as well).
 
I think you need to review some of the documentation and examples. Also browse through others' work in the User Contributions subforum.
 
Pete
Link to comment
Share on other sites

 

You can make as many assignments to the same button as you wish. FSUIPC executes them in order. But if you have a 3 position switch, surely each position is detected seperately -- or at least one position = "released" and the other two different button numbers? If not, then the wiring of the switch is all wrong, with all three positions, or at least two of them, wired together!

 

I'm not sure about the wiring of the switch but, in fact, it uses both mouse buttons as in a real-life engine startup procedure - LMB as a toggle for LVar positions 0 (OFF) and 1 (to charge actuator preparing it for engine startup when ampermeter shows at least 8 ampers), and RMB as a toggle for positions 0 and -1 (ON) from which the switch automatically springs back to position 0 after a couple of seconds (when the engine is started). My code emulates LMB actions well but fails to switch from pos 0 to -1 (note the sign!) and back since p = 1 (see: my code above). See also the attached image to visualize positions on this particular gauge.

 

The same problem exists with other switches like the fuel selector valve which has even 4 positions (All OFF, Left, Right, All ON). But here the developers used a native FSX control (FUEL_SELECTORS_SET, if memory serves me well) instead of a local variable. Hence using a syntax like if p == ipc.control(xxxxx) to check for the state of FSX control, similar to LVar check in my code, returns a LUA error "attempt to compare nil with number" which, AFAIU, is due to... FSX control doesn't return an explicit parameter value but rather gives a specific instruction / command. 

 

I think you are rather confused here. The FSUIPC macro facility is a method of allowing a single or sequence of commands, whether L:Var writes or simple control or keyboard assignments, to be stored in files (.mcro files) with names which can be assigned in the Buttons & Keys tab.

 
Maybe you are assuming that "macros" always means "mouse macros". Mouse macros are a specific type of macro which operates mouse action areas in gauges which are suited. But that is only one type of macro (and the most recent addition to the range of things macros can do as well).
 
I think you need to review some of the documentation and examples. Also browse through others' work in the User Contributions subforum.
 
Oh, indeed, I meant mouse macros only... I better go back to re-read the manual. Thanks for heads-up!!! 

post-16558-0-21297300-1442919941.png

Link to comment
Share on other sites

I'm not sure about the wiring of the switch but, in fact, it uses both mouse buttons as in a real-life engine startup procedure

 

This is confusing! So you are NOT trying to assign actions to a real hardware 3 position switch? You are just using ordinary buttons?

 

If you want one button to move through different controls each time you press it you just need to make use of the offset condition. Use a free user offset, such as 66C0, increment it cyclically with an Offset Byte Cyclic Increment control, and make the condition based on that value.

 

The example in the FSUIPC4 Advanced User's guide uses two buttons. here, I've pasted it for you to save you the bother (but you should read the section on button programming in any case):

 

Finally, for clever switching you may want to consider using one button to adjust an FSUIPC4 offset value which

then, via offset conditions, selects between a number of alternative button and/or key assignments. To assist in this,

offsets 66C0 to 66FF are reserved purely for you to do with as you like. The offset cyclic increment/decrement

controls allow, say, a byte value in offset 66C0 to cycle throgh a number of vlues, then each value selects particular

actions for defined keys or buttons. The entries in Buttons or Keys might look like this:

 

31 =P174,10,Cx510066C0,x00030001

32=B66C0=0 P117,6,C1 030,0

33=B66C0=1 P117,6,C1 034,0

34=B66C0=2 P117,6,C1 038,0

35=B66C0=3 P117,6,C1 042,0

 

Here the value in the Byte at offset 66C0 is cycled from 0–3, and back to 0, by button 174,10, and this value, in turn,

selects what happens with button 1 17,6.

These are real examples related to programming of a Go-Flight GF45 unit for different frequency adjustments

 

 
The two button method allows selection of any of the positions, becase the extra button preselects the one to be actioned. But you could use just one button if you always want to operate the complete cycle. For that you'd simply assign the cycllc increment action to the same button, so the above example becomes:
 
32=B66C0=0 P117,6,C1 030,0

33=B66C0=1 P117,6,C1 034,0

34=B66C0=2 P117,6,C1 038,0

35=B66C0=3 P117,6,C1 042,0

36 =P117,6,Cx510066C0,x00030001
 
Note that I moved the increment to the end, so it executes last. Assuming 66C0 startsequalling zero.
 
Pete
Link to comment
Share on other sites

 

 

This is confusing! So you are NOT trying to assign actions to a real hardware 3 position switch? You are just using ordinary buttons?

 

If by "ordinary buttons" you mean joystick buttons, yes, I am.

I don't have any 3-position switch on my Saitek X52 Pro, unfortunately, but use a HAT3 switch with up/down/left/right buttons instead.

 

So, at first I was thinking to assign all 3 positions of the clutch switch to a single button. But then it seemed totally unrealistic and illogical to me - a user may simply revoke his decision to start the engine and may want to return the switch to OFF (from LVar param = 1 to 0) without even going further to LVar param = -1 as an extra (unnecessary) step. So, two buttons are OK since this also complies with real-world operation of the aircraft - one button (ipcPARAM = 0) will toggle the switch from 0 to 1 (and back) while the other one (ipcPARAM = 1) will control positions 0 and -1, respectively. The latter doesn't work... yet! 

 

And thanks for info about offsets!!! I'll check that and report back.

 

All the best,

 

Rustam

Link to comment
Share on other sites

If by "ordinary buttons" you mean joystick buttons, yes, I am.

I don't have any 3-position switch on my Saitek X52 Pro, unfortunately, but use a HAT3 switch with up/down/left/right buttons instead.

 

Right. So I misunderstood your questions right from the start when I tohught you were trying to assign a 3 way switch.

 

 Pete
Link to comment
Share on other sites

  • 5 years later...

is there a way to create a toggle macro on one button that can cycle through 3 different commands on one button.
for example: have one button set to shift+e, the first time you hit it.
press the same button again and go to the next command Shift+p,
press the same button a thrid time and go to the 3rd command alt+c,
press the button again and start the whole loop all over again?
if so could you write the code, I'm a complete noob to Lua.
I would even pay to have this done.
email me: lonesbeats@gmail.com

Link to comment
Share on other sites

@lonesbeats@gmail.com First, you posted in a topic that has been closed for over 5 years and is not really related. You would be better off creating a new topic with an appropriate title.

What you would like to do is quite straightforward using the event.button function and a global variable to hold the current state (or number of button presses).
You can use and adapt the following lua script. Makle sure to add it top the [Auto] section of your FSUIPC ini file so that it auto-starts.

Quote

local myJoyLetter="T"
local myButtonNo=20
buttonPressCount = 0

function buttonFunction(joynum, button, downup)
  buttonPressCount = buttonPressCount + 1
  if buttonPressCount > 3 then
      buttonPressCount = 1
  end
  
  if buttonPressCount == 1 then
      ipc.log("Action for first button press")
  elseif buttonPressCount == 2 then
      ipc.log("Action for second button press")
  else
      ipc.log("Action for third button press")
  end  
end

event.button(myJoyLetter, myButtonNo, "buttonFunction")

John

Link to comment
Share on other sites

  • 4 months later...
On 9/20/2015 at 5:01 PM, Pete Dowson said:

 

You can either have multiple small Lua plug-ins, one for each assignable function, and just assign those buttons appropriately, or you can have one larger Lua plug-in,loaded initially (perhaps by an [Auto] entry in the INI file), and have that do different things according to the PARAM value, the Parameter you provide when assigning a button or switch,

In the former case you assign to "Lua name" in the second to "LuaValue name".

Your choice. Whatever you find easier. I doubt there's much difference in performance or efficiency.

Pete

Hi Pete, Hi John,

I just found the post above, when i tried to assign LUA-function calls to axes ranges or buttons.
What i'm trying to do is using an aircraft specific lua script containing multiple functions and assigning those calls to buttons or ranges of axes.
In general i'm able to get the introduced parameter... via:
image.png.b4d595493cf3a6623ca2447ef3fca365.png

There is a small but.
Only parameters i can assign are integers (as it seems).
But as i'd like to pass the function name of my desire, that of course doesn't work based on integers.
And keeping them in a function table and accessing it via the table index will give awful and really not useable code, i'm trying to find an approach to do so.

Of course i know, that there are also the

  • evt-files containing the events and 
  • mcro-files directly accessing the L:Vars

But i didn't find the right place, where is mentioned how i'm able to call a lua and access a dedicated function call in there.
And as splitting all the function over hundreds of files makes it really awful to maintain, i hope you can point me into the right direction.

Or is there any other way how to assign a function call from a lua to ranges?
LINDA only allows to assign to buttons but not to analogue ranges which i'd like to use.

Thanks for your guidance and best regards,

Joe

Link to comment
Share on other sites

11 hours ago, joeherwig said:

But as i'd like to pass the function name of my desire, that of course doesn't work based on integers.
And keeping them in a function table and accessing it via the table index will give awful and really not useable code, i'm trying to find an approach to do so.

I don't see an issue with using a function look-up-table, where you get the function to be used based on the integer parameter.

11 hours ago, joeherwig said:

Or is there any other way how to assign a function call from a lua to ranges?

You could monitor the offset that the axis changes (using event.offset), and call your function based on the value received. However, I think using the integer parameter and a look-up-table would be preferable.

Link to comment
Share on other sites

Hi,

Just one hint for your implementation: I found out some time ago, that having more smaller LUA programs handling button and other events is a lot better in terms of performance than packing all your functions into one large LUA. And as a side efect: A smaller LUA program is much easier to read and maintain, than a loooong LUA script.

Rgds
Reinhard

 

Link to comment
Share on other sites

@John Dowson the lookup table requires, that I need to maintain the functions on at least two places. So a change of a function name like fixing a typo requires multiple synced changes. Adding new functions has to be done at the end of the table which gives a really weird order and parameters in without any 'near' to other functions of the same context just because I added them not directly afterwards. 

Setting up needs to count indices within the table to understand, what I have to setup as parameter just to execute a function. So if you test "what did I assign" in FSUIPC, you'll just see function 73. And if I turn the potentiometer a bit further function 126. 🤪

That is what I meant with not easy to maintain. It is simply nerd stuff then, not feasible for average users. 

@aua668

Thanks for the hint. Probably you know LINDA for really easy assignments to standard buttons and vrinsight components. 

As linked, I'd like to provide an easy to import and flexible to use module that simply doesn't work with tens or hundreds of files for just one aircraft. 

 

 

Link to comment
Share on other sites

On 9/22/2015 at 9:48 AM, rustam said:

Paul, thank you too! In case of this particular model, unfortunately, I can't use macros. They simply don't initiate except with default FSX aircraft. So, i guess my only resort is to use Lua plugins.

Hi,

I think you missed that simple solution, which I also use if I just have to set LVars by buttons. The macros mentioned by Paul are NOT mouse macros (which don't work in your aircraft) but just simple LVar macros, which ALWAYS work. You will find more details in the Advanced User Manual.

The MCRO file looks like this example:
 

[Macros]
1=L:idle eng=Set
2=L:idlebit=Set
3=L:B407_Eng_Start_Switch=Set

Adding such a macro file to your FSUIPC directory allows you then to simply assign these commands to any button. Beside the Set function there are several others too (Toggle, etc.). Using this method simply needs no programming, if you just have to set some LVars.

Rgds
Reinhard

 

Link to comment
Share on other sites

15 hours ago, aua668 said:

Adding such a macro file to your FSUIPC directory allows you then to simply assign these commands to any button. Beside the Set function there are several others too (Toggle, etc.). Using this method simply needs no programming, if you just have to set some LVars.

 

HI Reinhard,

Thanks for pointing that out... But i didn't miss that.
As you can see here, working with LUA functions offers much more like displaying text on VRinsight panels, making decisions based on other values etc.
That is ways more then just "send value to LVar".

Some examples: 

Link to comment
Share on other sites

18 hours ago, joeherwig said:

he lookup table requires, that I need to maintain the functions on at least two places.

You could always put it in a separate file and 'require' it.

Also, the parameter is just a 4byte (int) parameter. If your functions names are 4 characters or less, you can encode the function name as a hex sting and then use that. I've attached a simple example, in which you can assign to LuaValue with the parameters x464E31 for FN1 (46=F, 4E=N, 31=1) and x464e32 for FN2:

function FN1()
    ipc.log("Function FN1() called!")
end
function FN2()
    ipc.log("Function FN2() called!")
end

local function bin2hex(s)
    s=string.gsub(s,"(.)",function (x) return string.format("%02X ",string.byte(x)) end)
    return s
end

-- Populate the conversion array - needs to be only done once
local hex_to_char = {}
for idx = 0, 255 do
    hex_to_char[("%02X"):format(idx)] = string.char(idx)
    hex_to_char[("%02x"):format(idx)] = string.char(idx)
end

function valueFunction(param)
    ipc.log("valueFunction: parameter="..param)
-- Convert parameter to hex string
   hexString = string.format("%x",param)
   ipc.log("valueFunction: parameter as hex string ="..hexString)
-- Convert hex string to ascII chars
   ascString = hexString:gsub("(..)", hex_to_char)
   ipc.log("valueFunction: parameter as ascII string ="..ascString)
-- Call function from ascII string name
   _G[ascString]()

end


event.param("valueFunction")

 

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.