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

Writing Joystick Code: Part 2

From FlightGear wiki
Revision as of 14:59, 12 October 2012 by Gijs (Talk | contribs) (WIP template)

Jump to: navigation, search
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 viscious 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 engine

perIndexAxisHandler

wingsweep

weapons

ptt

lights; individual and all

step magnetos


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.