The Raspberry pi is a mini computer designed on a single board with all the essential components needed to run an operating system. The card comes with an RCA connector that can be used to connect it directly to a TV screen based on PAL and NTSC standards. The card also has an HDMI connector output that can be used to connect the card to an HD TV. One can also use remote login to access the Raspberry pi and view the GUI (Text User Interface) on the PC screen. The Raspberry pi board is also very easy to interface with external devices or circuits through its pinouts. This makes Raspberry pi a suitable platform for playing and developing interesting games.
This article discusses how to develop a simple graphical game using HTML5 language and JavaScript and interface it with a custom external game pad hardware connected to the pins of the Raspberry pi board.
((wysiwyg_imageupload:11064:))
There are eight general purpose IO pins on the 13*2 pin connectors of the Raspberrypi board and among them four pins have been selected as input and the remaining four pins have been selected as output. The input pins are connected to the button and pulled low 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 accessed remotely using VNC . To access the pins leaving the Broadcom controller the C library “bcm2835” was downloaded and installed .
This game contains four balls whose movement can be controlled with the help of the four gamepad keys. The game can be opened in a browser window and played with the help of four buttons connected to the pins on the Raspberry pi board. It is necessary to install the “Iceweasel” browser, which is the Debian version of Firefox. The following commands can install the software on Raspberry pi;
sudo apt-get update
sudo apt-get install icewease eu
The game runs in a browser window and communicates with the game pad via a Named Pipe or FIFO. There are several processes running that can read the game pad and write the necessary commands to the FIFO to control the game. The JavaScript written in the game code simply reads the FIFO for input control data.
The parent process here creates four child processes that are dedicated to each of the gamepad keys. Whenever a key is pressed or released, they 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 identify which key was pressed or released.
Fig. 2: Raspberry Pi gamepad block diagram
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
FIFO can also be created using the code itself by adding the mkfifo , open function as explained in a previous documentation .
As soon as the parent process detects a keystroke from the value received along with the signal, it simply writes a specific character corresponding to that key into the NAMED PIPE or FIFO. The HTML game will continuously read the FIFO every 500 ms to obtain input. Therefore, both the C code and the HTML file need to be in a running state. Gamepad hardware only communicates with C code, and C code communicates with JavaScript using NAMED PIPE.
The user can write data to the temporary file from the terminal itself using the 'echo' command as shown in the following example.
echo abcdef >> /tmp/my_fifo
The FIFO writing parent process is written in such a way that it will write some specific characters to the FIFO in response to the signals received from the child processes. The commands that will be written in the FIFO to control the movement of the balls in the game are listed below;
you -> 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 in FIFO with the help of system function.
system(“echo you >> xyz);
system(“echo r >> xyz );
system ( “eco l >> xyz );
system( “echo d >> xyz );
Even if there is any keystroke, the parent process writes a character 'x' every 500ms to keep the JavaScript running.
Fig. 3: On-screen catch-ball game scoring using Raspberry Pi
Circuit diagrams
Simple Raspberry Pi Game Circuit Diagram for Ball Catch Game |
|
Project video