Ceci est une ancienne révision du document !
In the previous part of this series, we saw various ways in which our application can run on a Raspberry Pi, turning it either into a lightweight terminal or an equally lightweight application server. In this part, we will focus on what makes the Raspberry Pi unique, and will build a Free Vision interface to make use of the General Purpose I/O (GPIO) port on this small board computer.
Our test scenario will be to turn the RPi into a lighting manager, that controls three separate lighting circuits from a Free Vision interface. Each circuit will be controlled by a simple On/Off press button.
The physical circuit
Both the Raspberry Pi models 2 and 3 retain a similar GPIO pin assignment to the earlier B+ models. The 40-pin header has two parallel rows of pins, with the odd numbers inboard and the even closer to the card’s edge. I tend to use the last (right-hand) pins of the outboard row, specifically pins 34, 36, 38 and 40 that are attached to Ground, GPIO ports 16, 20 and 21, respectively.
The circuits we will be connecting to the RPi to get the software working will be simple LEDs, though in a real circuit these would be replaced by a high-impedance adapter (probably an optical isolator and a relay or a triac) that would allow us to control normal AC electric circuits. For the time being, we will be connecting the three GPIO ports to one LED’s positive leg each, while the negative legs get connected to a common ground on pin 34.
LEDs need to avoid high current values, with about 15 mA being a safe limit for most robust parts. On the other hand, the RPi’s GPIO ports have even lower working values (2 to 12 mA) both as a source (providing current to drive an external circuit) or as a sink (ground return). This means we need to insert some way of limiting current in series with the LEDs - or risk overloading both the LEDs and the Raspberry Pi itself. A simple way of doing this is to insert a simple resistor into the common ground. We will be using a 1 kΩ part here, though 2.2 kΩ would probably be safer.
On this diagram drawn with Fritzing, green, yellow and blue will be used respectively for circuits 1, 2 and 3. Fritzing had only a representation of the RPi 2 in its library, but this is for all practical purposes interchangeable with the RPi 3.
To make connections to the RPi, we would need electric cords with female connectors to slip over the RPi’s male pins. However, a typical breadboard will need cords with male pins at their ends. I had no cords with male connectors so, instead of soldering normal cords directly to the Raspberry Pi, I connected several to a short strip of header that can then be placed and removed at will from the computer.
Controlling the GPIO from Free Pascal
There are several projects to build a Pascal unit that controls the GPIO. However, just to make things more interesting, I thought it would be fun to write our own. It is actually quite simple - under Ubuntu as under Raspbian - to control the GPIO pins on a RPi, since the Linux kernel has been patched to give access through the /sys filesystem. We could actually do it with the command-line - though we will need root access to do so. To set up GPIO port 16 for output, for example, we need to:
echo 16 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio16/direction
We can actually see which ports are activated at a point in time by listing the contents of directory /sys/class/gpio/gpio* - each time a port is activated, a corresponding link appears here.
Now, to turn on GPIO 16 (switch it to 5V output), or turn it back off (0V):
echo 1 > /sys/class/gpio/gpio16/value
echo 0 > /sys/class/gpio/gpio16/value
Finally, to release the port:
echo 16 > /sys/class/gpio/unexport
As can be seen, the /sys interface really helps gain access to the ports, in a way that can easily be ported to any programming language that has access to the file system. Let us do so for Pascal, in a unit appropriately called Gpio (above).
It is easier to reference port numbers and values as strings; if they are passed as integers, each value would need to be converted into a string when building file names. The first two routines are to set up a port, either for digital input (setup_input), or output (setup_output). It will both export the port and write its direction: ‘in’ or ‘out’ respectively. Within the implementation section of our unit, we can start by writing a short procedure that serves to append a string to a given file, thus packaging this functionality away from the main procedures (next page, top right).
Finally, procedure release will unexport the port. All of these are rather simple to code, and the complete unit is available at this link: http://pastebin.com/Vnj6ZCqP . A simple program to test this unit could be as follows:
To compile and execute this program, we will need to compile both the unit and the program itself, and then execute the binary file as root:
fpc gpio.pas
fpc test10.pas
sudo ./test10
Creating a Free Vision interface
Let us Integrate our new Gpio unit into a short Free Vision application to control the Rasberry Pi’s three LEDs. We do not need a complete interface, so let us dispense with creating a menu bar and simply load a Dialog box with four buttons: three to control each circuit on GPIO 16, 20 and 21; and a last button to quit the application.
Our program will need to access Gpio, and the standard Free Vision units:
uses
gpio, App, Objects, Menus, Drivers, Views, Dialogs, MsgBox, StdDlg;
The application itself will need only a bespoke constructor, that creates and launches our LightsDialog:
Finally, the LightsDialog will need a HandleEvent procedure to respond to button presses. This is a tad tedious, since we will need to detect a button press for each button, and for each of those determine if we are going from an On state to Off, or vice-versa. It begins in this way (next page).
The complete code for this program is available here: http://pastebin.com/KdGuJexk .
In this seventh part of our series on Free Pascal, we saw how the Raspberry Pi can be wired up to control several LEDs. We then wrote a simple unit in Pascal to access the GPIO port, and finally used the unit inside a Free Vision application to produce a text-based user interface that, quite frankly, is elegant and functional. As an exercise, this project has been quite complete since it combines elements at a very low level close to the hardware, with a very clean object-oriented style of programming. The end result is actually quite practical, since the application can be accessed directly through the Raspberry Pi if it is connected to a screen and keyboard, or over an SSH connection either wired or wireless (if a model 3 is used).
To go further, the interested reader could modify the application so that the second circuit switches on at the press of a button, and then turns off when a set time-period has elapsed. A slider could be provided to fine tune the delay time. The third circuit could also be set up to turn on and off at specific times during the day.
From the hardware’s standpoint, our three trusty LEDs may be replaced with something more substantial. However, going on to an AC circuit with voltage levels of 110 to 250 V should really not be attempted unless one is a qualified electrical installer - much caution needs to be exercised when using AC since it can quite readily kill you, either directly or by causing a fire. This is also true even with the 12 VDC used in cars. Proceed at your own risk, and please do your homework first. Having someone who is qualified stand by and check your work - before switching it on - is surely a smart move.
It is also wise to remember that any current over 10 to 15 mA going in or coming out can seriously damage the Raspberry Pi, so an optical isolator or some equivalent means of disconnecting the RPi from the load’s level of current will be a must.