Friday, September 8, 2017

FSX - Add a Keypad for ATC (Part 1)

Tutorial for Adding a Keypad for ATC to FSX — 

Purpose: Microsoft Flight Simulator (FSX) uses numbers to select ATC menu choices. It must be easy to add an additional USB keypad to enter numbers for ATC. Right? No, not so much...

Impulse Buy:  I saw a USB keypad on sale at K-Mart and thought it was a cheap and easy fix for number entry for FSX. When the keypad was plugged into the computer running FSX, the keypad on the keyboard and the new USB keypad both sent the same key strokes to FSX. The PC did not differentiate between USB keyboard/keypad devices, and key strokes from any one will mingle with key strokes from another. The second revelation was that keypad numbers are not the numbers on the top of the keyboard. Numbers are not numbers??? That was not what I expected. Numbers entered on the keypad move the FSX 3D views.

Simple Fix - a software key stroke translator: After searching Google for a simple fix to the problem, it looked like a simple software fix of translating characters from USB on a PC was not going to happen. Many people were asking for translators, for a variety of uses, but nothing simple seemed to exist for PCs.

Maybe a hardware solution: During my searching for a key stroke translator, I saw multiple web pages that either had requests for key stroke translators or some that showed various solutions which all seemed pretty involved.

Other Options: I reviewed my options, which included: 1) abandon the USB keypad idea and go back to flying the simulator; 2) buy a non-USB keypad and wire it up to Mobiflight or other microcomputer solution using the keys to send single button commands to FSX; 3) continue down the USB path in an attempt to tame the beast. I had the idea that having a key stroke translator might have other uses than for FSX. I also wanted the solution to be as inexpensive as possible.

Solution: An Arduino-based key stroke mapper was successfully built to connect the USB keypad to communicate with FSX. The solution will first be presented, then other lessons learned during the process will be presented in future blogs.

Implementation: The key stroke mapper is a device that connects to the PC/Mac USB port on one side and to the USB keypad on the other side. Keypad keystroke signals (USB) are intercepted by the translator, re-mapped, then sent out as USB keystroke signals to the PC/Mac. In this way, any keystroke from the keypad can be mapped to another value and sent to the PC. The hardware/software combination handles the conversion of the key strokes.

Hardware: The key stroke mapper is comprised of two small circuit boards. A MAX3241E circuit board with a built-in USB-A port has built-in USB translation firmware. The keypad plugs into that board, and key strokes from the keypad will be received and signals are sent out through the SPI pins.

The second circuit board is a small Arduino-type Pro Micro which can be programmed to do the key stroke translations. The Pro Micro, first developed by Sparkfun, uses a 32U4 processor that includes USB communication hardware. The Pro Micro board has a micro-USB port that can be connected to a PC for programming using the Arduino IDE. IDE 1.8.4 was used for this project.

Clones of both the MAX3421E and Pro Micro can be purchased for a few dollars from eBay  and were used in this project. Care must be taken with eBay purchases to obtain the Pro Micro with 32U4 processor (with USB), which is not the same as a Pro Mini with an ATmega328 micro controller (same as UNO, without USB).

The MAX3421E is a 3.3V device and the Pro Micro can be obtained as either 5V or 3.3V versions from US vendors. I could not find 3.3V Pro Micro boards on eBay and 5V boards were purchased. This decision added a fair amount of extra work during development. Getting both boards as 3.3V devices would have reduced construction and development time. It turns out that the 5V Pro Micro boards can be converted to 3.3V using a 3.3V voltage regulator, which was done. This allowed the SPI pins to be directly connected between the two boards without the risk of burning up the MAX3421E board with 5V signals. 

The final translator hardware is a MAX3421E circuit board connected through the four SPI pins to the Pro Micro SPI pins, and sharing common ground connections. The PC USB provides the 5V to power the 5V to 3.3V converters for the Pro Micro and MAX3421E boards and the 5V is also routed out the MAX3421E USB connector to power the USB keypad.

Software: USB is not easy. The MAX3421E and Pro Micro 32U4 processors contain USB firmware to allow USB communication through the on-board USB hardware connectors. Serial keyboards were created before USB existed, then ASCII codes were created to standardize codes used in computers. Keyboards were then standardized to send out ASCII-coded values for numbers, letters, and limited hardware controls (carriage return, form feed, end of line, etc). When the USB standard was created, a different coding was created which is referred to as Scan Codes and has many more codes than ASCII. The USB Scan Codes are not the same as ASCII keyboard/keypad codes. HID Keyboard drivers take care of the mapping of USB Scan Codes to/from ASCII characters. Scan Codes for keypad keys are different than keyboard keys, even if the keys seem to be for the same number or function. For this keypad mapper project, the Arduino sketch software handles the translation of USB Scan Codes for specific keys to the desired ASCII codes which are sent from the Pro Micro through USB to the PC, thus re-mapping some of the keypad keys.

End Result: The keypad mapper hardware/software does the following: When the keypad Numlock LED is ON, numbers 0,1,2,...8,9 are sent to the PC as 'keyboard' numbers which will activate FSX ATC numeric commands. This was the original goal of the project: to use a keypad for sending FSX ATC number commands (which is the same as sending 'keyboard' numbers to FSX ATC). Recall that the default USB keypad numbers are not 'numbers' to ATC but rather they change the 3D views in FSX.

When Numlock is OFF, a default USB keypad will send flight stick controls to FSX, or those keys will send cursor controls, such as, move left, move up, home, etc., to a text editor.

For FSX using the mapper (as programmed), the Numlock OFF keypad keys will not send cursor controls, but will send the 3D view controls that were originally sent from the keypad to FSX when the Numlock was ON. This might seem to be a bit of busy work, but to me, when the Numlock LED is ON, numbers are sent, and when the LED is OFF, view commands are sent. However, it does conflict with the normal FSX keypad mapping. If a user does not want to have the Numlock ON-OFF keys changed, that can be handled in the Arduino sketch software.

The USB keypad can be mounted where it is convenient to use for making FSX ATC menu choices. Since the keypad with translator sends out numbers, it could also be used as a generic number entry keypad for other FSX uses, such as, entering frequencies. A selector switch would be needed to select the desired function that handled the numbers from the keypad which would be retained in a register or digital readout. The selector switch could also be programmed as one of the keypad keys.

Part 2 of this series will present the functional hardware and software used in this keypad translator.

(September 8, 2017)





No comments:

Post a Comment