Controlling Hardware using GUI in Raspberry Pi

The Graphical User Interface (GUI) helps the user to communicate with the system effortlessly. The GUI is considered as the front end of an application. In a Linux operating system each hardware device is represented as a file. The device can be controlled by simply reading and writing into that file. The hardware of an operating system is on the one side and the user trying to access the hardware is on the other side, and in between them there might be several layers of process running which communicates each other using inter process communication methods. The GUI is the process which the user can use to communicate with all these process layers and finally communicate with the Hardware. This project demonstrates how to create a GUI using the QT which can control the LEDs connected to the Raspberrypi board.

Controlling Hardware using GUI in Raspberry PiThe Raspberry pi is a device which uses the Broadcom controller chip which is a SoC (System on Chip). This SoC has the ARM11 processor which runs on 700 MHz at its core. The operating systems like Archlinux ARM, OpenELEC, Pidora, Raspbmc, RISC OS and the Raspbian and also Ubuntu versions are available for the Raspberrypi board. Linux operating systems especially Ubuntu is preferred for all kind of programming and development. The Raspberrypi is a board actually designed for helping computer education for remote schools but it is a nice platform for programmers especially beginners to explore various coding techniques.

This project basically requires two programs which are meant to send data in between them, and a named pipe which will be created by anyone of them. The entire system can be represented with the help of the following diagram:
In the above figure LED_CONTROL is the process which creates the NAMED PIPE and always trying to read data from the pipe. The process LED_GUI is the process which has been created using QT and when it runs, it will create a GUI window with a few buttons corresponding to each LEDs. As the user clicks on those buttons, the GUI writes a corresponding code to the NAMED PIPE.
 
The functions used in the coding of the LED_CONTROL for creating the NAMED PIPE and for the reading and writing operations are explained in the following section.
mkfifo ()
The function mkfifo() creates a temporary file at the required directory with the required access permissions set on it. The prototype of the function is declared as the following;
int mkfifo ( const char *pathname, mode_t mode );
The first argument is the required pathname of the directory where the named pipe needs to be created. The second argument is the user permission that needs to be set on the file. Using the value 0777 as the second argument allows permission to all users of the system to read from and write into that named pipe. For using this function in the C code, two header files <sys/types.h> and <sys/stat.h> should be included.
mkfifo (“/tmp/my_fifo”, 0777 );
The above function call creates a named pipe called “my_fifo” in the location “/tmp” with access permission to all the users of the system.
open ()
The open () function opens a particular file at a specified path with the required flags set and it returns a file descriptor corresponding to that file to the process which called the open function. Using that file descriptor the process can access the file.
The prototype for the open () function is declared as the following;
Controlling Hardware using GUI in Raspberry Pi Schematic
int open ( const char *pathname, int flags );
The first argument is the path of the location where the file needs to be opened exists and the second parameter is the flags that need to be set for the file. The flags that set on the file decide the way in which the file can be accessed.
open ( “/tmp/my_fifo”,  ( O_RDONLY | O_NONBLOCK )  );
The above function call opens the file named “my_fifo” in the location “/tmp” with the flags set to access the file as Read Only. The function call will returns a small positive integer value called file descriptor using which the process can access the file. The header files needed to be included when using this function is <sys/types.h> and <fcntl.h>.

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