The Raspberry Pi is a single-board computer designed to help teach computer science to school students. This is a great platform to try out different programming techniques and also learn software tools. 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. You can also use remote login to access the Raspberry p 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 board also comes with an audio jack. All this makes Raspberry pi a suitable platform for playing and developing interesting games.
This article focuses on how to add sound to a game that is coded to be played with a Raspberry pi gamepad . This simple graphical game is coded using HTML5 and JavaScript language and connected to a simple game pad hardware connected to the pins of the Raspberry pi board. This game is added with background music and keyboard tones that can be enjoyed while playing the game with the help of a headset or sound system.
((wysiwyg_imageupload:11067:))
The gamepad is a simple circuit built on a breadboard with a few buttons, LEDs and a set of resistors. 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 uses two MP3 files bgm.mp3 and kpm.mp3, one for the background music and the other to play the keyboard tone. There are so many command line media player software available which can be downloaded and installed on Raspberry pi Ubuntu. MPG321 is a command line audio player that can be installed easily using the following command;
sudo apt-get -y install mpg321
This MPG321 is an MP3 player application and any MP3 file can be played with the help of this application by typing the following format command
mpg321
This software has several options such as volume control, file search, remote operation mode, which makes it perfect for use with any type of front end application, here the HTML game as an example. More details about the options can be found in the man pages;
man mpg321
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 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 game HTML 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 .
The parent process in the C code creates five child processes, among which four of them are dedicated for each of the gamepad keys and the fifth one is for playing the background music. 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
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 background music will continue to play as a separate child process from the start of the game and the keyboard tones will be played by the Parent function whenever it detects a signal from the child processes dedicated to reading the gamepad keys. The Parent process uses the “system” function to play the keyboard tone as shown in the following instruction.
system (“mpg321 kpm.mp3”);
Circuit diagrams
Circuit-Diagram-Add-Sound-Raspberry-Pi-Gamepad |
|
Project video