The simFlight Network Forums

# lua joystick example ?

## Recommended Posts

Hi,

I am trying to under stand the n = ipc.buttons methode.

I understand that n contains a U32 bit mask but how do I compare the bits with the logic methodes ?

What I don't understand is for example in logic.And(y,z) what's y and what's z.

Ok they are U32 bit's but how are they represented in the methode as binary or a value ?

I tried this which doesn't work (joystick is #4)

```-- "push_rot.lua" by Gerhard Gubler July, 2010

while 1 do

n = ipc.buttons(4)

if logic.And(00000000000000000000000000000001,n) then
dis = "pushed 0"
ipc.display(dis)
end
if logic.And(00000000000000000000000000000100,n) then
dis = "pushed 2"
ipc.display(dis)
end
if logic.And(00000000000000000000000000001000,n) then
dis = "pushed 3"
ipc.display(dis)
end

ipc.sleep(100)

end```

any help appreciated.

Gery

##### Share on other sites

I am trying to under stand the n = ipc.buttons methode.

I understand that n contains a U32 bit mask but how do I compare the bits with the logic methodes ?

What I don't understand is for example in logic.And(y,z) what's y and what's z.

If you don't understand the concept of "bits" in computers, why not just stick to using the ipc.testbutton function?

y and z are two values -- numbers or variables containing numbers. "And" is a logical operation which gives a 1 bit in the result where both numbers have 1's, and a 0 otherwise.

Please use Wiki to find out more about computers, bits and logic. Or refer to my article "About bits, numbers and hexadecimal " in the Announcements in this Forum. Or, if all that fails, stick to using the easier functions. (But you really do need to learn a little about computers if you are going to try programming, so it would be worth an effort).

if logic.And(00000000000000000000000000000001,n) then

The decimal number 00000000000000000000000000000001 is exactly the same as the number 1. Why bother with all those leading zeroes? Page 1 of a book is easier known as page 1 than page 00000000000000000000000000000001, isn't it?

The value 1 will represent Button 0, because 2^0 = 1 (i.e 2 to the power 0 = 1).

if logic.And(00000000000000000000000000000100,n) then

The decimal number 00000000000000000000000000000100 is the same as 100 (one hundred). In hexadecimal that is 0x64, which has 3 bits set, bit 2 (worth 4 = 2 squared), bit 5 (worth 32 = 2^5) and bit 6 (worth 64 = 2^6). Notice that 64 + 32 + 4 = 100.

So here you are testing for buttons 2, 5 and 6 all together.

if logic.And(00000000000000000000000000001000,n) then

I'll let you figure that one. Suffice to say you are testing for 6 buttons there! ;-)

Pete

##### Share on other sites

If you don't understand the concept of "bits" in computers, why not just stick to using the ipc.testbutton function?

You might not want to belive me now or at least think shame on you, but I am actually an software engineer. I even programm assembly for PIC's etc.

I'm rather new to lua programming though and like most hight level languages it must first be lerned.

I assumed the parameters in logic.And() to be a bitfield. But I guess you understand that.

Offcourse using ipc.testbutton is an alternative. But one thing I don't understand with that function. As in the description it must have assigned any macro or lua function in order for fsuipc to watch it. Why ? if I want an allready running lua script to take care of the button handling it means I have to assing a dummy function doing nothing to the button in order to use the ipc.testbutton function ?

That's why using ipc.buttons seamed to have less overhead.

anyway i'll give it a try and thanx for the lesson.

Gery

##### Share on other sites

I assumed the parameters in logic.And() to be a bitfield. But I guess you understand that.

I don't think Lua supports Binary notation (like 0B00100...), you could check in the Lua reference manual available on the Lua website, as referenced in my Lua packaages. Even if it did it wouldn't need leading zeroes. It certainly supports decimal (no prefix) and hexadecimal (prefix 0x). The initial 0 is needed in languages to distinguish a number from a name -- names start with an alpha.

Offcourse using ipc.testbutton is an alternative. But one thing I don't understand with that function. As in the description it must have assigned any macro or lua function in order for fsuipc to watch it. Why ?

Sorry, I don't understand the question. What do you mean " it must have assigned any macro or lua function in order for fsuipc to watch it"?

if I want an allready running lua script to take care of the button handling it means I have to assing a dummy function doing nothing to the button in order to use the ipc.testbutton function ?

That's why using ipc.buttons seamed to have less overhead.

Sorry, I've no idea where you are coming from with that. The only difference between testbutton() and buttons() is that testbutton reads the 32 bit button state, makes the 32 bit mask with the bit you specify, and does the AND for you. It is MORE efficient for testing a single button, not less, because all of that is done in high-speed compact compiled C whereas you doing it separately in Lua is a series of interpretations. The buttons() function is provided for those dealing with multiple button states, such as those encountered with multiway switches.

Regards

Pete

##### Share on other sites

Sorry, I don't understand the question. What do you mean " it must have assigned any macro or lua function in order for fsuipc to watch it"?
Tests a scanned button. ―joynum‖ is a joystick number, the same as shown in FSUIPC‘s Button assignments tab. Provided the joystick is one being scanned by FSUIPC (i.e. it has a button assignment), this function returns the state of the specified button number (0–31) as TRUE or FALSE.

I unserstud this section in the lua library documentation for ipc.testbutton, that a button had to have any function assigned in order for fsuipc to scann (not watch) the buttons state. That's why I made a empty lua script and assigned it to the buttons i wanted to scann the state of. But apparently it's not really needed as I have tested in between.

Basically my testbutton script works now. The only problem (not fsuipc related) is that the button on the rotary encoders I use only work on every second detent.

```-- "push_rot.lua" by Gerhard Gubler July, 2010
-- free to use, change and distribute at own risk.

while 1 do
was_pushed = 0
rotated = 0
while ipc.testbutton(4,28) do
was_pushed = 1
if ipc.testbutton(4,2) and ipc.testbutton(4,28) then
dis = "pushed 2 and 28"
ipc.display(dis)
rotated = 1
end
if ipc.testbutton(4,3) and ipc.testbutton(4,28) then
dis = "pushed 3 and 28"
ipc.display(dis)
rotated = 1
end
end

if was_pushed == 1 and rotated == 0 then
dis = "pushed 28"
ipc.display(dis)
end

if ipc.testbutton(4,2) then
dis = "pushed 2"
ipc.display(dis)
end
if ipc.testbutton(4,3) then
dis = "pushed 3"
ipc.display(dis)
end

ipc.sleep(10)

end
```

Still needs optimisation but is generally working. The idea of this is to make buttons for the MCP where a push means activate an autopilot function and push+rotate means increas/decreas values *10.

Gery

##### Share on other sites

I unserstud this section in the lua library documentation for ipc.testbutton, that a button had to have any function assigned in order for fsuipc to scann (not watch) the buttons state.

FSUIPC only scans joysticks which it needs to scan because there are assignments made to them. The word "function" here is not a programming term, it is an FS function, something you are using the joystick for in FS. If FSUIPC is not scanning the joystick it cannot supply you with button states.

And in any case the same wording is used in both the descriptions of "testbutton" and "buttons" functions, so I don't understand why you'd think one was different to the other in this regard.

Basically my testbutton script works now. The only problem (not fsuipc related) is that the button on the encoders I use only work on every second detent.

The encoder is using a pulse -- each 'click" either makes or breaks the contact, alternately. You will therefore see the button turning on and off alternately. If you want to act on every 'click' you need to remember the previous state and act only on a change. If you act every time you see it "pressed" (set) then if you leave the dial in an "on" position it will repetitively tell you it is set forever. You cannot treat a rotary as a momentary button like most joystick buttons.

The idea of this is to make buttons for the MCP where a push means activate an autopilot function and push+rotate means increas/decreas values *10.

You can of course do such things via a Lua plug-in, and it makes a good educational exercise, but I'm pretty sure it would in fact be easier and quicker to do it via normal FSUIPC assignments, even if it also meant a little bit of INI file editing for conditionals.

Regards

Pete

##### Share on other sites

FSUIPC only scans joysticks which it needs to scan because there are assignments made to them

It appeares to me that fsuipc also scans joysticks that had an assignment to them if removed it's still scanned.

The encoder is using a pulse -- each 'click" either makes or breaks the contact, alternately. You will therefore see the button turning on and off alternately.

I organized a different rotary encoder Elma E37, sold at Leo Bodmars web shop. With this model the button stays on even when rotated hench works perfectly with the lua script.

You can of course do such things via a Lua plug-in, and it makes a good educational exercise, but I'm pretty sure it would in fact be easier and quicker to do it via normal FSUIPC assignments, even if it also meant a little bit of INI file editing for conditionals.

Do you mean the fsuipc.ini file ? Do I find information about that in the documentation or can you give me a hint how ?

so I don't understand why you'd think one was different to the other in this regard.

Yes, my mistake. I undertand it now.

Cheers,

Gery

##### Share on other sites

Do you mean the fsuipc.ini file ? Do I find information about that in the documentation or can you give me a hint how ?

The format of button assignments in the INI file, and ways of making them conditional (on other buttons, or on offsets) is all covered in the FSUIPC Advanced User's document, in the section on button programming.

Regards

Pete

## Create an account

Register a new account