Simple Raspberry Pi Game Pad for Ball Catching Game

There are eight general purpose IO pins on the 13*2 pin connectors of the Raspberrypi board and among them four pins has been selected as input and then remaining four pins as output. The input pins are connected to push button and are pulled down using 1K resistors. The output pins are connected to the LEDs through another set of 1K resistors. For this project the Raspberry pi board is loaded with Ubuntu and is remotely accessed using VNC. To access the pins that coming out of the Broadcom controller the C library “bcm2835” has been downloaded and installed.

This game contains four balls and the movement of which can be controlled with the help of the four keys in the gamepad. The game can be opened in a browser window and played with the help of four buttons connected to the pins of the Raspberry pi board. One has to install the browser “Iceweasel” which is the Debian version of the Firefox. The following commands can install the software on the Raspberry pi;
Simple Raspberry Pi Game Pad for Ball Catching Game
sudo apt-get update
sudo apt-get install iceweasel
The game runs in a browser window and it communicates with the game pad through a Named Pipe or FIFO. There are multiple processes running which can read from the game pad and write the required commands to the FIFO for controlling the game. The JavaScript written in the game code simply reads from the FIFO for the input control data.
The parent process here creates four child processes which are dedicated for each of the keys in the gamepad. Whenever a key is pressed or released, they will generate a signal and send it to the parent process with a value. This method of sending a signal with value  helps the parent process to identify which key has been pressed or released.
The NAMED PIPE or FIFO needs to be created using the following command in the same directory where the executable for the gamepad code written in C and the HTML game files written in HTML5 and JavaScript exist.
mkfifo xyz
The FIFO can also be created using the code itself by adding the function mkfifo(), open() as explained in a previous documentation.
As soon as the parent process detects a key press from the value it received along with the signal, it simply writes a particular character corresponding to that key to the NAMED PIPE or FIFO. The HTML game will be reading continuously from the FIFO every 500ms to get the input. Hence both the C code and the HTML file needs to be in the state of execution. The gamepad hardware communicates with the C code only and the C code communicates with the JavaScript using the NAMED PIPE.
The user can write data into the temporary file from the terminal itself using the ‘echo’ command as shown in the following example.
Simple Raspberry Pi Game Pad for Ball Catching Game Schematicecho abcdef >> /tmp/my_fifo
The FIFO writing parent process is written in such a way that it will write some particular characters to the FIFO in respond to the signals received from the child processes. The commands that will be written to the FIFO for controlling the movement of the balls in the game are listed below;
u -> catch the ball in the first column
r -> catch the ball in the second column
l -> catch the ball in the third column
d -> catch the ball in the fourth column
These commands can be written into the FIFO with the help of the system() function.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top