Board index FlightGear Support Hardware

Writing Joystick Code - wiki article  Topic is solved

Joysticks, pedals, monitors.

Writing Joystick Code - wiki article

Postby macnab » Fri Oct 12, 2012 6:57 am

I have written 3 wiki articles about writing code for joysticks. A third is on its way.

Here are the links:
http://wiki.flightgear.org/Writing_Joys ... de:_Part_1
http://wiki.flightgear.org/Writing_Joys ... de:_Part_2
http://wiki.flightgear.org/Writing_Joys ... de:_Part_3

Any suggestions/problems/complaints/kudos can be posted here.

Part 4, how to number axes for each OS is under production.
Last edited by macnab on Sun Oct 14, 2012 9:24 am, edited 1 time in total.
macnab
 
Posts: 886
Joined: Tue Aug 02, 2011 7:20 am
Location: Johannesburg, South Africa
Callsign: ZS-ILH
Version: Git
OS: Win7Pro 64bit SP1

Re: Writing Joystick Code - wiki article

Postby Affix » Fri Oct 12, 2012 7:47 am

Philosopher pointed out to me that it is best to use the <number> tags when describing the axis to help with system compatibility when I was fixing the X52-Pro bindings for mac.

Your Axis should look like this.

Code: Select all
<axis>
   <number>
      <mac>X</mac>
      <unix>X</unix>
      <windows>X</windows>
   </number>

<!-- Axis Code -->
</axis>


Maybe it would be work adding that to your article to help with future compatibility improvements.

Nice article all the same though :)
Affix
 
Posts: 4
Joined: Wed Oct 10, 2012 4:40 pm
Location: Edinburgh, Scotland
Callsign: Affix
IRC name: Affux
Version: 2.8.0
OS: Mac OSX / Linux

Re: Writing Joystick Code - wiki article

Postby macnab » Fri Oct 12, 2012 8:19 am

I am going to point this out in Part 3. At the moment I am just getting them to write their own code for themselves.

In fact, I'm still researching where the Windows Mac and Linux numbers differ, because some of them are the same.

Glad you like the article.
macnab
 
Posts: 886
Joined: Tue Aug 02, 2011 7:20 am
Location: Johannesburg, South Africa
Callsign: ZS-ILH
Version: Git
OS: Win7Pro 64bit SP1

Re: Writing Joystick Code - wiki article

Postby Philosopher » Fri Oct 12, 2012 1:52 pm

Wow! Very nice! I like how you covered the stopwatch, I would've never thought of that.

Now a couple comments and suggestions:

This is the code for the throttle control, for the others, just replace throttle with propeller, mixture or carbHeat.

I would personally say "just replace throttleAxis with propellerAxis..." to make it clearer what exactly you are saying to replace.

<script>
Command number 1;
Command number 2;
Command number 3
</script>

I think you need a comma on the last (nasal) line here, though maybe not. I personally think it is a good rule to have one on every line here, even though you might be able to get away with it.

And for grammatical reasons, I would rephrase this:
Notice the choice of place to press Enter, and the indent in the second line. Make it easy for people to read.

As this:
Notice where I put the newline and how I indented the second line. Both are designed to make it easy for people to read, as part of being a programmer is making your code legible for others.


Otherwise, very nice article, I thoroughly enjoyed reading it and I thought it was very clear and should help beginners a lot.
Thanks,
Philosopher
(inactive but lurking occasionally...)
Philosopher
 
Posts: 1590
Joined: Sun Aug 12, 2012 6:29 pm
Location: Stuck in my head...
Callsign: AFTI
Version: Git
OS: Mac OS X 10.7.5

Re: Writing Joystick Code - wiki article

Postby macnab » Fri Oct 12, 2012 2:05 pm

Thanks for the (good) suggestions.

As for the semi-colon on the last line, I was taught in C# that it makes the interpreter process an empty line, thus wasting time.
macnab
 
Posts: 886
Joined: Tue Aug 02, 2011 7:20 am
Location: Johannesburg, South Africa
Callsign: ZS-ILH
Version: Git
OS: Win7Pro 64bit SP1

Re: Writing Joystick Code - wiki article

Postby Affix » Sat Oct 13, 2012 2:39 pm

macnab wrote in Fri Oct 12, 2012 8:19 am:In fact, I'm still researching where the Windows Mac and Linux numbers differ, because some of them are the same.



If you look at my post (viewtopic.php?f=24&t=17880) You can see that Axis 2 of this stick has been switched with Axis 5 in OSX. However the Axis numbers remain the same for linux. Don't know if that will help you with your research. If you need any help researching the differences I use both OSX and Linux and could probably test a few things for you.
Affix
 
Posts: 4
Joined: Wed Oct 10, 2012 4:40 pm
Location: Edinburgh, Scotland
Callsign: Affix
IRC name: Affux
Version: 2.8.0
OS: Mac OSX / Linux

Re: Writing Joystick Code - wiki article

Postby macnab » Sat Oct 13, 2012 3:04 pm

Thanks. How many different joysticks do you have so you can give me the axis numbers for both OSs. :D

I say this because I went through the (few) xml files that use <number> and the number-swap between OSs is not constant - it is not always true that axes 3 and 4 swap between Windows and Linux. Sometimes they do, sometimes they don't. Sometimes other axes swap. It has something to do with the way the OS talks to the joystick. Pity the manufacturers couldn't reach some sort of agreement, the way they all call the hat switch and axis, when it is just a bunch of switches.

What I am going to do is ask in the forum for people to reply with the list of axis numbers reported by their OS for their joystick. (It's not much work at all.) I'll get what I can from the existing xml files, and then in Part 4 I'll do entries for each joystick with a list of axis numbers for each OS. With the instructions on how to use <number>.

A bit of work, but in the long run it will make life easier (for everybody who reads the wiki.) I could even edit all the existing xml files, post them and ask for them to be committed to Git.

That will keep me off the streets for a whi :mrgreen: le.
macnab
 
Posts: 886
Joined: Tue Aug 02, 2011 7:20 am
Location: Johannesburg, South Africa
Callsign: ZS-ILH
Version: Git
OS: Win7Pro 64bit SP1

Re: Writing Joystick Code - wiki article

Postby Hooray » Mon Oct 15, 2012 1:39 pm

I was looking through this, and wanted to thank you for creating this series of tutorials - I do know that it can take lots of time and thinking to write good beginner tutorials, so it's really appreciated.
Also, I have added some minor changes to your tutorial #3 - namely, using php syntaxhighlighting sections - and some fixes regarding omitted semicolons.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11375
Joined: Tue Mar 25, 2008 8:40 am

Re: Writing Joystick Code - wiki article

Postby macnab » Mon Oct 15, 2012 1:51 pm

I saw, thanks.

Do you know what to do about line-wrap? If you have a nice wide screen it is no problem, but if I make my viewing window smaller, with indented text, I see that long lines of text carry on past the end of the "box."

The recourse seems to be to write short lines of text. :)
macnab
 
Posts: 886
Joined: Tue Aug 02, 2011 7:20 am
Location: Johannesburg, South Africa
Callsign: ZS-ILH
Version: Git
OS: Win7Pro 64bit SP1

Re: Writing Joystick Code - wiki article  

Postby macnab » Mon Oct 15, 2012 2:00 pm

Found the answer at MediaWiki site. (<code></code>.

Will give it a good read and then climb back into my articles. And do the syntax-highlighting where necessary.
macnab
 
Posts: 886
Joined: Tue Aug 02, 2011 7:20 am
Location: Johannesburg, South Africa
Callsign: ZS-ILH
Version: Git
OS: Win7Pro 64bit SP1

Re: Writing Joystick Code - wiki article

Postby Hooray » Mon Oct 15, 2012 2:04 pm

I have added a navigation bar to all articles.
Regarding some of the longer code snippets: That's what I was going to address next - overall, you are covering already lots of ground in your articles, including some of the more involved programming concepts in Nasal.

Personally, I don't think that Joystick bindings should contain huge portions of complex code. Thus, I would find it preferable, to start using a handful helpers for some of the more common purposes, and move them to separate functions (or even classes), so that the joystick bindings themselves would just refer to them.

Another benefit here would be that joystick bindings are generally parsed for each invocation of their binding - so reducing the amount of code in bindings, is definitely a good idea. In addition, there's tons of code in the form of setprop("foo", getprop("bar")) - this is something that could be easily moved to separate helpers.

You may want to get in touch with Philosopher, who's been using a technique to load Nasal snippets from separate files, via io.load_nasal().
Also, I feel that it would actually be great if you could work on a "standard" library for joystick scripting purposes, and then simply move it a dedicade Nasal module, such as in $FG_ROOT/Nasal/joystick.nas

Such general purpose routines/helpers could then be parametrized in your bindings and simply invoked there. Given your background in JS scripting and Nasal now, I am actually pretty confident that, possibly with Philosopher's help, you could send a merge request to move key function to joystick.nas - so that the bindings themselves would become really short and comprehensible.

I *guess* there are really only 10-20 general purpose routines required to handle most use cases. Especially, because bindings would then merely need to specify a property, a condition (predicate) and a factor to modify the original property.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11375
Joined: Tue Mar 25, 2008 8:40 am

Re: Writing Joystick Code - wiki article

Postby Hooray » Mon Oct 15, 2012 2:13 pm

As an aside, I don't know what version of FG you guys (macnab & Philosopher) are using, but if you can use the latest development code (snapshots), you'll see that there's a joystick configuration dialog available in the menu, that was recently added by Stuart.

Now, as you probably know, we also have a Nasal console available, which provides support for writing code directly in the simulator.
Both of these dialogs are conventional XML dialogs with embedded Nasal sections.

So, given that Joystick XML bindings are re-evaluated whenever their bindings are invoked, it would in fact be possible to directly write Joystick code WITHIN the simulator, by copying the Nasal editor (from the Nasal console) into the Joystick configuration dialog, and by providing an option to directly edit your bindings there, saving them to a file in $FG_HOME and reloading the bindings via an fgcommand (also available via the DEBUG menu IIRC).

In other words, by copying/pasting some stuff from the Nasal console to the Joystick Config dialog, we could be able to provide an integrated "Joystick Scripting Editor" so that people can create their bindings directly in FG.

BTW: I wouldn't be suggesting this if I didn't have the impression that the two of you (macnab & Philosopher) seem to understand the Nasal side of things pretty well already :D
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11375
Joined: Tue Mar 25, 2008 8:40 am

Re: Writing Joystick Code - wiki article

Postby macnab » Mon Oct 15, 2012 2:32 pm

Hmm. This will take quite a bit of planning.

What I think you are saying in the previous post is, to use an extreme example, is have code in joystick.nas so that instead of
Code: Select all
    if(getprop("controls/engines/engine/mixture") > 0 ) {
       setprop("controls/engines/engine/mixture", getprop("controls/engines/engine/mixture") - 0.05)

in the joystick binding, it would be something along the lines of
Code: Select all
 decMixture(0.05)


As for your second post, I will have to look at the joystick configuration dialog before I can make a meaningful comment. It means I have to do all the necessary downloads to be able to compile the snapshots. Or do they contain pre-compiled code? I've never looked into it before.

I'm using FG.2.8

I'm sure Philosopher will read this and add his comments/suggestions.
macnab
 
Posts: 886
Joined: Tue Aug 02, 2011 7:20 am
Location: Johannesburg, South Africa
Callsign: ZS-ILH
Version: Git
OS: Win7Pro 64bit SP1

Re: Writing Joystick Code - wiki article

Postby Hooray » Mon Oct 15, 2012 2:56 pm

The Joystick Configuration Dialog was committed to FGDATA around 24/08/2012: http://www.mail-archive.com/flightgear- ... 38132.html
I'd suggest to have a look and see for yourself- according to my understanding, it is definitely overlapping with your tutorials, and it should be easier to customize/extend the new dialog than writing additional tutorials from scratch.
In fact, most *users* will probably find it easier to customize things at runtime, directly within the simulator, without having to use external tools.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11375
Joined: Tue Mar 25, 2008 8:40 am

Re: Writing Joystick Code - wiki article

Postby Philosopher » Mon Oct 15, 2012 3:12 pm

Hooray wrote in Fri Sep 12, 2008 6:51 pm:BTW: I wouldn't be suggesting this if I didn't have the impression that the two of you (macnab & Philosopher) seem to understand the Nasal side of things pretty well already :D


Haha, you got THAT right ;). I'm using 2.6 right now on a Mac but I also have 2.4.

Very nice article, macnab! You certainly have a knack for this! And yes, I am reading this :D. I might suggest moving some of you article that covers Nasal more in general to the Nasal section, and maybe an already existing article.

Here's how I do my loading of code:
Code: Select all
var namespace = cmdarg().getNode("module").getValue();
io.load_nasal(getprop("/sim/fg-root") ~ "/Input/Joysticks/Saitek/Cyborg-X/functions.nas", namespace); #import functions first
io.load_nasal(getprop("/sim/fg-root") ~ "/Input/Joysticks/Saitek/Cyborg-X/buttons.nas", namespace);
var config = gui.Dialog.new(Joystick ~ "/dialog","Input/Joysticks/Saitek/Cyborg-X/Dialog-Cyborg");

Note that subscripting a scalar results in a ASCII value, hence the -48 in line 3.

As for the Joystick Configuration GUI w/ code editor, I think that would be a great idea. It would probably be available as an "advanced" option and you could access the "regular" bindings created by Stuart from both modes. My idea is like this: have a drop-down to select which button/axis and have a "binding type" selector, aka what fgcommand it uses, and have different configurations for the various fgcommands. E.g. if it was a "property-assign" command it might look like this (in uh... GUI code?):

#where [option0|option1] is a drop-down list with those options:
[Button|Axis] [0|1|2|3|...] [press|mod-up]: #select type, number, and press/mod-up for buttons
command: [nasal|property-assign|property-toggle...] #property-assign is selected
property: [field]
value: [field]

And so on for the others. For a nasal binding there would be a code window like the Nasal console that would load/save it in the correct place. For all binding types there would be a button called "Test Binding" which would run the binding in the correct namespace only if it's a button (as it wouldn't work for axes).
Last edited by Philosopher on Mon Oct 15, 2012 11:54 pm, edited 1 time in total.
Thanks,
Philosopher
(inactive but lurking occasionally...)
Philosopher
 
Posts: 1590
Joined: Sun Aug 12, 2012 6:29 pm
Location: Stuck in my head...
Callsign: AFTI
Version: Git
OS: Mac OS X 10.7.5

Next

Return to Hardware

Who is online

Users browsing this forum: No registered users and 1 guest