Button connection tutorial

From Sim Innovations Wiki
Jump to navigation Jump to search

Description

Buttons have a momentary state, this means that once released they return to their default state. Other than a switch, which remains in it's set position and can have multiple positions. For a button to register an input, the I/O pin has to be pulled to ground. That means that one of the button wires has to be connected to ground (GND), and the other wire is attached to an I/O pin. You can find the button API function here: hw_button_add

An example for a button is the frequency flip button, which flips the standby frequency to the active frequency and the active frequency to the standby frequency. In these examples we are going to create a button which does this for COM1 and two buttons for COM1 and NAV1, both in FSX, Prepar3D and X-Plane.

Examples

One button example

In the first example we will connect one button. When connecting a button, one of the wire goes to ground (GND). The other wire has to be connected to one of the I/O pins.

As we said, we are going to make this button flip the frequency. To do this you can use the example script you find below. To experiment some more, you can change the event or command to something else, for example the transponder ident.

-- Callback function which is called when the button is pressed
function button_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Flip frequency COM1")

  -- Flip the COM1 frequency for FSX and Prepar3D
  fsx_event("COM_STBY_RADIO_SWAP")

  -- Flip the COM1 frequency for X-Plane
  xpl_command("sim/radios/com1_standy_flip")
end

-- Create a button with the name 'Com swap'
hw_button_add("Com swap", button_pressed)

In Air Manager the configuration can look like this:
Rasppibutton1configed.jpg

Two buttons example

Two buttons is just as easy as connecting one. In this example we connect both buttons to the same ground (GND) pin. The other wires go to the I/O pins. In the example script we will let button one do the frequency flip for COM1 and button two is going to do the frequency flip for NAV1.

-- Callback function which is called when the button is pressed
function button_1_pressed()
  -- The following print function is not necessary but can help you to verify that button one has been pressed
  print("Flip frequency COM1, this is button one")

  -- Flip the COM1 frequency for FSX and Prepar3D
  fsx_event("COM_STBY_RADIO_SWAP")

  -- Flip the COM1 frequency for X-Plane
  xpl_command("sim/radios/com1_standy_flip")
end

function button_2_pressed()
  -- The following print function is not necessary but can help you to verify that button two has been pressed
  print("Flip frequency NAV1, this is button two")

  -- Flip the COM1 frequency for FSX and Prepar3D
  fsx_event("NAV1_RADIO_SWAP")

  -- Flip the COM1 frequency for X-Plane
  xpl_command("sim/radios/nav1_standy_flip")
end

-- Create a button with the name 'Com swap' and and 'Nav swap'
hw_button_add("Com swap", button_1_pressed)
hw_button_add("Nav swap", button_2_pressed)

In Air Manager the configuration can look like this:
Rasppibutton2configed.jpg

Hardware setup

This shows the schematic setup of a button connection. When the button is actuated, it will pull the I/O pin to ground.

Button schematic.png