Hi fellow wiki editors!

To help newly registered users get more familiar with the wiki (and maybe older users too) there is now a {{Welcome to the wiki}} template. Have a look at it and feel free to add it to new users discussion pages (and perhaps your own).

I have tried to keep the template short, but meaningful. /Johan G

Difference between revisions of "Writing Joystick Code: Part 2"

From FlightGear wiki
Jump to: navigation, search
Line 5: Line 5:
 
Make sure you have read [[Input device]] and [[Writing Joystick Code: Part 1]] first.
 
Make sure you have read [[Input device]] and [[Writing Joystick Code: Part 1]] first.
  
These are what you use instead of ''Your code goes here'' amd ''Ctrl-Alt button released''. You just need to put them in the right place - inbetween the <script> and </script> tags.
+
These are what you use instead of ''Your code goes here'' amd ''Ctrl-Alt button released''.
 +
You just need to put them in the right place - inbetween the <script> and </script> tags.
  
 
Please note that some of them have concepts that will be explained in a later section.
 
Please note that some of them have concepts that will be explained in a later section.
Line 15: Line 16:
 
     Code for <mod-up>
 
     Code for <mod-up>
  
Where Action is what you want to do, R means use with a repeatable button, NR means use with a non-repeatable button, and U is the code for <mod-up>. If it is empty, a <mod-up> is not needed.
+
Where Action is what you want to do, R means use with a repeatable button, NR means use with a non-repeatable button,
 +
and U is the code for <mod-up>. If it is empty, a <mod-up> is not needed.
  
 
Some Actions will have code for use with repeatable and non-repeatable buttons.
 
Some Actions will have code for use with repeatable and non-repeatable buttons.
Line 23: Line 25:
 
If the action is upside-down for you, swap the + and - signs.
 
If the action is upside-down for you, swap the + and - signs.
  
Also, sometimes the 1 is replaced by a number, which specifies by how much the action should be carried out. If the action is too viscious for you, make the number smaller.
+
Also, sometimes the 1 is replaced by a number, which specifies by how much the action should be carried out.
 +
If the action is too vicious for you, make the number smaller.
  
 
=== The snippets ===
 
=== The snippets ===
Line 189: Line 192:
 
   setProp(("controls/lighting/xxx"), false)
 
   setProp(("controls/lighting/xxx"), false)
 
   
 
   
  Where xxx = beacon, landing-lights, landing-light, landing-light[1] logo-lights, nav-lights, strobe, taxi-light, taxi-lights, cabin-lights, map-lights.  
+
  Where xxx = beacon, landing-lights, landing-light, landing-light[1] logo-lights, nav-lights, strobe, taxi-light,
 +
taxi-lights, cabin-lights, map-lights.  
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
 
  Instrument lights level up R
 
  Instrument lights level up R
Line 233: Line 237:
  
 
   
 
   
If you find a line is too long for your page width, then you can press Enter and go to a new line. FG will be happy - it needs a tag or a ; at the end of a line to tell it that the current line of code is finished.
+
If you find a line is too long for your page width, then you can press Enter and go to a new line.
 +
FG will be happy - it needs a tag or a ; at the end of a line to tell it that the current line of code is finished.
 
   setprop("controls/engines/engine/mixture", getprop ("controls/engines/engine/mixture") + 0.05)
 
   setprop("controls/engines/engine/mixture", getprop ("controls/engines/engine/mixture") + 0.05)
 
becomes
 
becomes
 
   setprop("controls/engines/engine/mixture",
 
   setprop("controls/engines/engine/mixture",
 
     getprop ("controls/engines/engine/mixture") + 0.05)
 
     getprop ("controls/engines/engine/mixture") + 0.05)
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.
+
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.
  
  

Revision as of 01:32, 13 October 2012

WIP.png Work in progress
This article or section will be worked on in the upcoming hours or days.
See history for the latest developments.

Code snippets

Make sure you have read Input device and Writing Joystick Code: Part 1 first.

These are what you use instead of Your code goes here amd Ctrl-Alt button released. You just need to put them in the right place - inbetween the <script> and </script> tags.

Please note that some of them have concepts that will be explained in a later section.

Each snippet has the following format:

Action R/NR
   Code for action
U
   Code for <mod-up>

Where Action is what you want to do, R means use with a repeatable button, NR means use with a non-repeatable button, and U is the code for <mod-up>. If it is empty, a <mod-up> is not needed.

Some Actions will have code for use with repeatable and non-repeatable buttons.

Notice that some commands have a +1 or a -1 in brackets to specify the direction of the action, and a 0 to say stop doing that.

If the action is upside-down for you, swap the + and - signs.

Also, sometimes the 1 is replaced by a number, which specifies by how much the action should be carried out. If the action is too vicious for you, make the number smaller.

The snippets


Gear down NR
  controls.gearDown(1)
U
  controls.gearDown(0)

Gear up NR
  controls.gearDown(-1)
U
  controls.gearDown(0)

Flaps down one notch NR
  controls.gearDown(1)
U
  controls.gearDown(0)

Flaps up one notch NR
  controls.gearDown(-1)
U
  controls.gearDown(0)

Deploy spoilers NR
  controls.stepSpoilers(1)
U
  controls.stepSpoilers(0)

Retract spoilers NR
  controls.stepSpoilers(-1)
U
  controls.stepSpoilers(0)

Cycle View NR
  view.stepView(1)
U

Elevator trim down R
  controls.elevatorTrim(0.75)
U

Elevator trim up R
  controls.elevatorTrim(-0.75)
U

Elevator Trim down NR
  interpolate("/controls/flight/elevator-trim", 1, 30 * (1 - getprop("controls/flight/elevator-trim")))
U
  interpolate("/controls/flight/elevator-trim", getprop("controls/flight/elevator-trim"), 0)

Elevator trim up NR
  interpolate("/controls/flight/elevator-trim", -1, 30 * (1 - getprop("controls/flight/elevator-trim")))
U
  interpolate("/controls/flight/elevator-trim", getprop("controls/flight/elevator-trim"), 0)

These commands are all on one line.
To make it go faster, make the 30 smaller.

Aileron trim left R
  controls.aileronTrim(-0.75)
U

Aileron trim right R
  controls.aileronTrim(0.75)
U

Aileron trim left NR
  interpolate("/controls/flight/aileron-trim", -1, 30 * (1 - getprop("controls/flight/aileron-trim")))
U
  interpolate("/controls/flight/aileron-trim", getprop("controls/flight/aileron-trim"), 0)

Aileron trim right NR
  interpolate("/controls/flight/aileron-trim", 1, 30 * (1 - getprop("controls/flight/aileron-trim")))
U
  interpolate("/controls/flight/aileron-trim", getprop("controls/flight/aileron-trim"), 0)

To make it go faster, make the 30 smaller.

If you want rudder-trim, use either the repeatable or non-repeatable above, changing elevator/aileron to rudder

Apply all brakes NR
  controls.applyBrakes(1)
U
  controls.applyBrakes(0)

Apply left brake NR
  controls.applyBrakes(1, -1)
U
  controls.applyBrakes(0, -1)

Apply Right brake NR
  controls.applyBrakes(1, 1)
U
  controls.applyBrakes(0, 1)

Start Stopwatch NR
  globals["__dlg:stopwatch-dialog"].start()
U  

Stop Stopwatch NR
  globals["__dlg:stopwatch-dialog"].stop()
U  

Reset Stopwatch NR
  globals["__dlg:stopwatch-dialog"].reset()
U  

Make mixture richer NR
  <![CDATA[ 
    if(getprop("controls/engines/engine/mixture") < 1 ) {
      setprop("controls/engines/engine/mixture", getprop ("controls/engines/engine/mixture") + 0.05)
    }
  ]]>
U

Make mixture leaner NR
  <![CDATA[
    if(getprop("controls/engines/engine/mixture") > 0 ) {
       setprop("controls/engines/engine/mixture", getprop("controls/engines/engine/mixture") - 0.05)
    }
  ]]>
U

Start all engine all R
  controls.startEngine(1)
U
  controls.startEngine(0)

Start specific engine R
  controls.startEngine(1, x)
U
  controls.startEngine(0, x)

x is the engine number. First engine is 0.

Wingsweep

Each aircraft that implements Wingsweep has a specific number of steps between fully forward and fully swept-back.
You set the wingsweep by specifying how many steps the wings must sweep forwards or backwards.

Wingsweep forward NR
  controls.wingSweep(n)
U

Wingsweep backwards NR
  controls.wingSweep(-n)
U

Where n is the number of steps. This means that the number of steps implemented per button press is hard-coded
in your joystick file. You could of course use a modifier for larger (or smaller) steps.
In this case you would have different values of n.

weapons

Toggle all lights on/off NR
  controls.toggleLights()
U

Switch specific lights on NR
  setProp(("controls/lighting/xxx"), true)
U

Switch specific lights off NR
  setProp(("controls/lighting/xxx"), false)

Where xxx = beacon, landing-lights, landing-light, landing-light[1] logo-lights, nav-lights, strobe, taxi-light,
taxi-lights, cabin-lights, map-lights. 

Instrument lights level up R
  <![CDATA[ 
    if(getprop("controls/lighting/instruments-norm") < 1 ) {
      setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") + 0.05)
    }
  ]]>
U

Instrument lights level down R
  <![CDATA[ 
    if(getprop("controls/lighting/instruments-norm") > 0 ) {
      setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") - 0.05)
    }
  ]]>
U

Instrument lights level up NR
  <![CDATA[ 
    if(getprop("controls/lighting/instruments-norm") < 1 ) {
      setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") + 0.2)
    }
  ]]>
U

Instrument lights level down NR
  <![CDATA[ 
    if(getprop("controls/lighting/instruments-norm") > 0 ) {
      setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") - 0.2)
    }
  ]]>
U

Step magnetos up NR
  controls.stepMagnetos(1)
U

Step magnetos down NR
  controls.stepMagnetos(-1)
U


If you find a line is too long for your page width, then you can press Enter and go to a new line. FG will be happy - it needs a tag or a ; at the end of a line to tell it that the current line of code is finished.

 setprop("controls/engines/engine/mixture", getprop ("controls/engines/engine/mixture") + 0.05)

becomes

 setprop("controls/engines/engine/mixture",
    getprop ("controls/engines/engine/mixture") + 0.05)

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.




Go back to Writing Joystick Code: Part 1.




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