How to Use Signal Values and Messages to Read Multiple Inputs

Those who have worked on simple microcontrollers know how difficult it is to continuously check for an input pin status without affecting the other task of the microcontroller like displaying and SSD, playing a music etc. In a Multitasking environment of an Operating system like Ubuntu a separate process can be created for checking the status of input pin and then notify the main process whenever their status changes. Multi-tasking Operating Systems can run several processes at a time creating and effect of parallel processing with the help of the high speed processor. The Linux Operating Systems provides Multi-User-Multitasking.

How to Use Signal Values and Messages to Read Multiple InputsThe Raspberrypi is a mini-computer board which is powerful enough to run large operating systems like Linux, Mac and Windows. The Linux operating systems like Archlinux ARM, OpenELEC, Pidora, Raspbmc, RISC OS and the Raspbian and also Ubuntu versions are available for the Raspberrypi board. The 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. Linux operating systems especially Ubuntu is preferred for all kind of programming and development.

In this particular project a Parent creates only one Childs process which will then create its own Child Processes. The Grand Child Processes of the Parent processes are then used to read the status of the four input pins independently. The Child processes are made to send a signal ‘SIGUSR1’ to their own Parent process whenever the status of the input pin changes. Each Child process sends a different value along with the signals they sent. As soon as their Parent process receives a signal, it reads the value from the signal and sends the status in the form of a message along with a signal to the original Parent process. This forms a Process System made up of 4 Grand Child process, a Child Process and a Parent process where the Parent is free to do its work, but the Child can get the attention of the Parent by sending signal.

Four separate input reading processes has been created as Child process using the fork () function for each of the input pins. This Child processes continuously reads the particular input pin and sends a signal to the Parent process using kill () function whenever the status of the pin changes.

There are 26 connectors which can be taken out from the connector port of the Raspberrypi board. All the connector pins are taken out using 13*2 pin female connectors and at the other end of their wire 26 pin Burg stick male connectors are attached. The Burg stick male connectors allow each pin out from the Raspberrypi board to be plugged into the holes of a breadboard.  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. In this project the Raspberrypi 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.

For separate input reading  and output writing the functions from the library <bcm2835.h> is used. The function ‘bcm2835_gpio_fsel ()’ is used to set the pins as input or output and the function ‘bcm2835_gpio_set_pud ()’ is used to turn off the pull up/down from the pin. The function ‘bcm2835_gpio_lev ()’ is used to read the value of the pin and ‘bcm2835_gpio_write ()’ is used to write the value to the output pin.

In this project the function written to set the required function as Signal Handler using the ‘sigaction ()’ function is ‘sig_set_handler ()’. This function can be used to set a particular function as the Signal Handler for a particular signal number and hence the function has only two arguments, one for the signal number and the other for the function that need to be set as the Signal Handler. The prototype of the function is given below;

void sig_set_handler ( int signo, void *handler );

How to Use Signal Values and Messages to Read Multiple InputsFor example to set the following function as the Signal Handler using the ‘sig_set_handler ()’ for a signal ‘SIGUSR1’

void button_signal_handler ( int sig, siginfo_t *siginfo, void *context );

The code uses the following statement;

sig_set_handler ( SIGUSR1, &button_signal_handler );

A function called ‘sigqueue ()’ is available in the <signal.h> which can be used to send values or messages along with the signals. In this project a function is written based on the ‘sigqueue ()’ to send value along with signals to another process. The function is called ‘sig_send_val ()’ and can be used to send a particular signal to a particular process along with a value. The function has three arguments, one for the process id, one for the signal number and the other for the value to be sent.

 

For more detail: How to Use Signal Values and Messages to Read Multiple Inputs


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