This tutorial explains the virtual COM port of the GamepadBlock. It can be used, for example, to switch the Controller Mode of the GamepadBlock. The Controller Mode itself is explained in more details and also an example for Python will be shown and explained.
The Controller Mode
The controller mode determines how the GamepadBlock polls the terminal inputs. You need to set the three DIP switches according to the controller that you want to attach to the GamepadBlock.
To activate the DIP switch configuration, you need to unplug and reattach the micro USB cable. You can find a table that shows all possible switch settings for the supported controller types on the GamepadBlock page.
The Virtual COM Port of the GamepadBlock
Besides the DIP switch you can also use the virtual COM port that is provided by the GamepadBlock. For example: Imagine you have SNES and NES controllers connected to a single GamepadBlock. Now, you want to activate SNES controllers for SNES emulators and NES controllers for NES emulators. You decide for your project to use a push button to toggle between these two modes.
Besides switching the Controller Mode, the virtual COM port can also be used to read out the currently installed firmware version and also to read out the currently active Controller Mode.
In order to connect to the COM port, you need to use these settings:
Baud rate: 115200
Data bits: 8
Stop Bits: 1
Any terminal program or scripting language that is capable of communicating with serial ports can be used.
Connecting to the COM port via Python
In the following we will use Python to connect for connecting to the COM port from a Linux system. In the example, we use a RetroPie image, which is based on Raspbian.
We will use the PySerial package in the following example. First of all, we need to make sure that we have that package installed. We achieve this with the following command:
sudo apt-get install -y python-pip
sudo pip install pyserial
We need to find the COM port of the GamepadBlock. To get a list of all possible ports you can use this command:
The output of that command lists all TTY ports available on the system. If not sure, which port belongs to the GamepadBlock, you can disconnect the GamepadBlock, list all TTY ports, reconnect the GamepadBlock and list all TTY ports again. The newly added TTY port is the GamepadBlock.
Here is a brief Python script that shows how to connect to the GamepadBlock, and read out the firmware version and currently active controller mode:
serialPort = serial.Serial("/dev/ttyACM0", baudrate=115200, bytesize=8, parity=serial.PARITY_NONE, stopbits=1, timeout=1) # open serial port
serialPort.write("v".encode()) # send v character
readValue = serialPort.read(size=64) # read up to 64 bytes. Timeout after 1 second as set above.
serialPort.write("c".encode()) # send c character
currentMode = serialPort.read(size=64) # read up to 64 bytes. Timeout after 1 second as set above.
serialPort.close() # close serial port
print("Firmware Version is " + str(readValue)) # print firmware version
print("Current controller mode is " + str(currentMode)) # print current controller mode
Assuming that the code above was put into the file comPortDemo.py the output of a call of that file could look like this:
pi@retropie:~ $ ./comPortDemo.py
Firmware Version is 1.1.0
Current controller mode is 1
Once connected to the COM port, the GamepadBlock implements a very simple protocol: Receive one character, reply accordingly
These are the commands that the GamepadBlock supports:
|V||Returns the number of the current firmware version|
|C||Returns the number of the currently active controller mode|
|0||Switch to controller mode 0: Arcade|
|1||Switch to controller mode 1: SNES|
|2||Switch to controller mode 2: NES|
|3||Switch to controller mode 3: Genesis/Megadrive/Master System/Atari|
We have explained the use cases for the virtual COM port of the GamepadBlock. The COM port can be accessed with a baud rate of 115200 and 8N1 configuration. Python can be used to write to and read from the serial port quite easily. The PySerial package makes the communication over a serial COM port easy. The communication protocol of the GamepadBlock was also described.
If you have any question, suggestion or comment feel free to contact us!