This post explains how you can use a LUA routine to access the status of the buttons/switches/selectors/gear and flaps lever and trim wheel (herewith named collectively as Buttons) on 3 Saitek panels using FSUIPC and Linda. The first section presents a quick instruction to start gathering information based on lua program attached to this post. The second section gives a simple example of a Lua program that access the Saitek panel buttons status and select specific FSX/P3D actions accordingly. The third section goes more into the details about how this LUA programs was made.
You need a registered version of FSUIPC for this to work.
Section 1 – Quick Start
Download and install the relevant LUA routines;
Attached to this post are 3 Lua programs corresponding to the 3 Saitek panels. You just need to install the ones corresponding to the panel you own. The 3 Lua programs are:
Note; I have renamed the 3 programs with a .txt exension for upload as the system doesnt allow to upload .lua files. You will need to change the extension back to .lua before using them.
To install them, just drop the ones corresponding to the panel you own into the “module” directory of FSX/P3D.
Automatic start of the LUA programs
The LUA programs selected above must run in permanence to scan the status of the buttons and map (copy) this information into specific address of the FSUIPC offsets. To inform FSUIPC that those programs must start and run continuously, it is necessary to add the following lines to the FSUIPC.ini files (located in the module directory of FSX/P3D).
On the next start-up of FSUIPC, the Lua program will start to scan the status of the buttons and selector on Saitek panels.
Checking with FSUIPC logging facility that the Lua programs are in place and operating
FSUIPC has a built in logging facility that can be useful to check the proper operation of the Lua programs and the correct mapping of switches to the offset address.
Go to the FSUIPC Logging tab and on the right part of the screen, enter the following information:
Offset Type of data Remark
3358 U32 Saitek Radio panel offset address
335C U32 Saitek Multi Panel offset address
3360 U32 Saitek Switches Panel offset address
Then click on “FS window” and you should have a green message on the top left of your screen. Move some selectors or push buttons on each of your panels and see if the values on screen change according to the position of the buttons. An annex at the end of this post gives the value of each button.
Note1 : The knob just send a short pulse so the number on screen will just change very rapidly and come back to the value prior to moving the knob when you stop moving it.
Note 2: The buttons just change status while they are pressed, they doesn’t indicate the status of the corresponding function. Ex: the Autopilot button will just send a ON status when you press the button. So this is not intended to test if functions like the AP are engaged or not. It’s just to test the instantaneous position of the hardware buttons.
Make sure you have version 4.934 as the previous one has a problem that killed lua programs on start-up and caused me a couple of evening of head scratching... 4.934 solved this as soon as I installed it.
Accessing the information from within a LUA program in Linda
Now everything is in place and you have the possibility to access the information on your panel switch.
For simple assignation of a single button, you usually doesn’t need complex programming as FSX, Spad or Linda menu all allow you to directly select FSX/P3D commands. On the other hand, if you want to combine the status from different buttons to trigger a specific FSX/P3D action, then you need to use a Lua program (you can also use FSUIPC conditional button programming but Lua is more flexible).
Using Linda, it is possible to assign user created Lua program to a button so the program is called when the button is push (or a switch is move to the “on” position or a selector is put in a specific position). The Lua program allows you to test the status of others buttons and makes your program act accordingly to send the proper FSX/P3D command.
In Linda, you can create individual LUA program module (a module can contain several Lua program) for each aircraft. Linda includes also a text editor that makes creation of Lua program easy. Some functions in the Lua library allow reading specific address in FSUIPC offsets. Each address that will be assigned to the 3 Saitek panels covered here will contain between 18 to 20 bits of useful information (not considering the LED display) that correspond to all the buttons, switches, selectors and gear and flaps lever of the panel. The switches panel for instance contains 20 bits of information that you can test. The address to use in order to read each panel information are:
0x3358 = Radio panel
0x335C = Multi panel
0x3360 = Switch panel
Note : You can assign other address if those one are already used for other purposed on your setup. You will need to modify the HIDxxx.lua program that correspond to the device you want to change.
Note: The "0x" in the adress above just means that its written in hexadecimal notation. You will need to include the 0x within Lua program otherwise it will consider that the address provided is decimal. Please note that in the FSUIPC logging page, you don’t need to include the 0x as this page always consider that the value entered is hexadecimal.
There are two main functions within Lua programs that can help you to read buttons status.
First, the following Lua program line read all the bytes of information from the Saitek Radio panel and put it in a variable named “pos”.
pos = ipc.readUB(0x3358)
But the value returned is not quite useful by itself as it represents the cumulative sum of all the individual bits for switches that are in the “on” position resulting in a large number of possible values. You then need to isolate the value that you are specifically interested by using the following function (here represented in a separate line).
pos = logic.And(pos,mask).
Here mask correspond to the specific value (or sum of values) you are interested in. For instance, if you are interested to test if the position of the magneto switch is at the “right” position on the Saitek switch panel, then the value for the mask is 32768 (see annex for all possible values) and the Lua program line becomes
Pos = logic.And(pos,32768)
Section 2 : Example for the Duke piston or Douglas C-47 Magneto selector
In the Duke Piston from Real air (and Douglas C-47 from Manfred Jahn), the magneto animation in the VC are reversed compared to the Saitek switch magneto button for the “left” and “right” position which create a weird jump in the VC while you move the magneto selector on your Saitek panel.
- Duke Piston/Douglas C-47 VC magneto selector order: Off – Left – Right – Both – Start
- Saitek Switch panel magneto selector order: Off – Right – Left – Both - Start
So the objective is to program a function in a Linda module that will reverse the operation of the left and right magneto position between the Saitek panel and the VC. In addition, we want to test the position of the upper mode button for the Saitek radio panel to determine if we want to operate the motor 1 or 2. Spad has a built-in functionality that allows using several switches (ex: Aternator, fuel pump, cowl, de ice) to act on motor 1 or on motor 2 according to the position of the top selector on the Radio panel and we want to replicate this functionality here.
First you need to create a module for your targeted aircraft if it’s not already existing. See Linda documentation to do this.
Then you need to copy the following program (or use the Magneto.txt file provided) into the aircraft module using Linda editor screen. Again, refer to Linda documentation to see how to open the editor.
The title between double hash characters is recognized by Linda as a heading separator for functions. It makes easier to identify function you have created.
Comments (text preceded by --) in the program below provide detailed comments for each steps.
-- ## Magneto ##
Function MagLeftVC ()
-- This function handle the reversal of the magneto in the VC panel compared to the Saitek selector.
-- It is activated when the selector on the Saitek switch panel will be put at the left position.
-- It will result in positionning the VC magneto selector to the "right" position.
-- The Saitek Radio top left selector at position COM1 means that the left engine is considered. If the selector is at the COM2 position, then the right engine will be treated.
-- The information about the radio panel status is at offset 0x3358
-- reading the offset to get all Radio panel switches current status
pos = ipc.readUB(0x3358)
-- The next line of lua code will isolate the bits of information we are interested in
-- mask = 1 would be usefull ot test if the top left selector at the COM1 position
-- mask = 2 would be appropriate to test if the top left selector at the COM2 position
-- Mask = 3 accept both COM1 and COM2 and will return either 1 or 2 as the selector
-- can be only in one of the two positions at a given time.
-- If the selector is not at COM1 or COM2, then the logic.And function will return zero and no action will be done.
pos = logic.And(pos,3)
-- the next group of lines will send the command to put the magneto for left or right motor in the right position in the VC.
-- 66400 is the Magneto1_set command for FSX/P3D
-- 66401 is the Magneto2_set command for FSX/P3D
-- The parameter 2 is for the “right” position of the magneto
if pos ==1 then ipc.control(66400,2)
else if pos ==2 then ipc.control(66401,2)
Function MagRightVC ()
-- same approach as for the left mag function, without comments here
pos = ipc.readUB(0x3358)
pos = logic.And(pos,3)
if pos ==1 then ipc.control(66400,1)
elseif pos ==2 then ipc.control(66401,1)
Section 3 – Nut and Bolts.
This section provides some additional explanation on how the HIDRadio.lua program was created from the HIDDemo.lua program provided by Peter.
Step 1: Obtain HIDDemo.lua
We need to have a Lua program running continuously to scan the Saitek panel switch status then map this information in FSUIPC offset. The offset can then be read from a Lua program in a Linda module for an aircraft. Peter Dowson provided an example of a Lua program that scan HID device like the Saitek panels and write the information on offset. We just want to use his demo and modify it a bit.
The HidDemo.lua program is available in the complete install of FSUIPC4.
Or you can download the latest HidDemo.lua example from : http://forum.simflight.com/topic/68257-latest-lua-package-for-fsuipc-and-wideclient/
Step 2: Create HIDRadio.lua
Make a copy of HIDDemo.lua and name it HIDRadio.lua (we will give only one example herewith but the same approach works for the switch and the multi panel also).
Step 3 – Adjust HID device identification
At the beginning of the HIDDemo.lua program, there is two lines that identify the Saitek panel (HID device) you want to communicate with. You have to insert the hexadecimal code for the vendor and for the product. For instance the lines for the Saitek radio panel are:
-- Saitek Radio panel
Vendor = 0x06A3
Product = 0x0D05
If you dont know the HID device codes, there is a utility provided by Peter Dowson called HIDscanner.exe that gives a report about all devices connected to your PC and their vendor rand product codes.
I already run this utility to find the relevant information of the 3 following Saitek Products.
Radio Panel :
Vendor = 0x06A3
Product = 0x0D05
Multi Panel :
Product = 0x0D06
Switch Panel :
Product = 0x0D67
The HIDScanner.exe utility is available at the same link provided above on Peter support page.
Step 4 : Change the size and destination of information blocks
In HIDDemo.exe, you will find a loop of instruction toward the end of the program that read the status of the buttons on your HID device and copy it to a specific offset. The loop is initially programmed for reading 8 blocks composed of 4 Bytes of information. For the Saitek panel switches, you only need to do it once as all information fit into a double world (32 bits of information). So I cleaned the iteration loop (from 1 to 8 and adjusted the HIDDemo.lua program to do a single read of a 32 bits of information that is sufficient for the Saitek panels.
Next, you need to select the destination for the button information in the FSUIPC offset table. There is an ipc.writeUD instruction in the HIDDemo.lua program that was modified to correspond to an offset that was used for mapping.
The possible offset for the 9 contiguous blocks of 4 bytes (32 bits of information) are: 0x3340, 0x3344, 0x3348, 0x334C, 0x3350, 0x3354, 0x3358, 0x335C, 0x3360. See the “FSUIPC4 Offset Status.pdf” document provided with FSUIPC documentation. The Saitek version of HIDDemo.lua use the 3 last virtual joystick addresses so Joystick 71,72 and 73 located at 0x3358, 0x335C and 0x3360 respectively.
Step 5 : Install your newly created Lua program
Just copy them it in the “module” directory of FSX or P3D. If you have started with the quick start portion of this document, you have already done this.
Step 6 : Automatic startup of HIDRadio.lua
To start automatically your Lua program add the following lines in FSUIPC.ini. If you have applied the quick start instruction, its already done.
Step 7 : Create custom lua program
See the example provided for the Duke above.
Annex: Saitek panels reference of “buttons” value
Saitek Radio Panel (20 bit of information + 2 bit unused)
Top left mode selector
Bottom Left mode selector
16384 = Top right ACT/STB button
32768 = Bottom right ACT/STB button
65536 = Clockwise turn of the top knob
131072 (2E17)=Counter-clockwise turn of the top knob
1048576 (2E20) = Clockwise turn of the bottom knob
2097152 (2E21) = Counter-clockwise turn of the bottom knob
Saitek Multi Panel (20 bit of information)
32= Clockwise rotation of knob
64= Counter clockwise rotation of knob
128= AP button
256= HDG button
512= NAV button
1024= IAS button
2048= ALT button
4096= VS button
8192= APR button
16384= REV button
32768= Auto Throttle switch ON
65536= Flaps up (2^16)
131072= Flaps down (2^17)
262144= Elevator Trim Pitch up (2^18)
524288= Elevator Trim Pitch Down (2^19)
Saitek Switch panel (20 bit of information)
Note: The minimum value returned corresponds to the position of the landing gear handle plus the position of the magneto knob. 262144 is returned if the gear lever is UP and 524288 if the gear lever is DOWN.
16= De Ice
32= Pitot heat
8192= Off magneto
16384= Left Magneto
32768= Right Magneto
65536= Both Magneto
131072= Start Magneto
Landing gear lever
262144= Landing gear Up
524288 (2E19)= Landing Gear Down