03. Types of switches
X-Plane can be controlled in two different ways: by writing to datarefs or by sending commands. It is a common mistake trying to write to datarefs when actually X-Plane must be controlled through commands, see the specific wiki page about this issue. However, some third-party aircrafts in X-Plane must be controlled through datarefs, while others use commands.
For this reason, both methods are supported by the library. This page lists they available objects and shows how to use them.
The libary provides three basic set of switches, that map to the most common switch types in X-Plane:
-
A two position switch. That's the basic on/off switch used, for example, for landing lights.
Two position switches are supported through the following objects:
- If X-Plane requires one command to turn it on and another one to turn it off: FlightSimOnOffCommandSwitch
- If you want to only send a command to X-Plane when the switch is turned ON: FlightSimOnCommandSwitch
- If you want to only send a command to X-Plane when the switch is turned OFF: FlightSimOffCommandSwitch
- If you control X-Plane by writing values 0 and 1 to a dataref whenever the switch position changes: FlightSimOnOffDatarefSwitch
-
A multi-position switch. These are, for example, rotary switches or toggle switches with a center position.
The following objects of this library support multi-position switches:
- If you need to send "up" and "down" commands to X-Plane to change the switch position: FlightSimWriteDatarefSwitch
- If you need to write a value for each switch position into a dataref whenever the switch position changes: FlightSimWriteDatarefSwitch
-
Pushbuttons. These are buttons or switches that do not lock when they're actuated, like FMC keys or cabin crew call
The FlightSimPushbutton object sends a single command with "BEGIN" and "END" to X-Plane when the pushbutton is pressed or released.
- Common Features
- FlightSimOnOffCommandSwitch
- FlightSimOnCommandSwitch
- FlightSimOffCommandSwitch
- FlightSimUpDownCommandSwitch
- FlightSimPushbutton
- FlightSimOnOffDatarefSwitch
- FlightSimWriteDatarefSwitch
All switches have some common methods that can be used for debugging or to implement special handling:
switch.setDebug(boolean debugOn);
Enables or disables debugging functions for this switch. Check the debug section for further information.
float switch.getValue();
Returns the current value of the switch. This can be 0
and 1
for two-position
switches and pushbuttons or a specific value for multi-position switches.
void switch.onChange(void (*fptr)(float value));
void switch.onChange(void (*fptr)(float value, void *context), void *context);
These functions register a callback function that is called whenever the switch position changes. The first form allows to specify a callback function that only receives the new value of the switch. The second form permits specifying an additional context, which allows sharing the same callback function between various switches.
The Demo_Callbacks
from the library examples shows how to use the onChange
functions.