issue117:tutoriel2
                Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| issue117:tutoriel2 [2017/01/31 11:09] – créée auntiee | issue117:tutoriel2 [2017/02/07 11:54] (Version actuelle) – auntiee | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | 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. | + | **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.  | + | 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 | + | Dans la partie précédente de cette série, nous avons vu plusieurs façons de faire tourner notre application sur un Raspberry Pi, le transformant, | 
| + | |||
| + | Notre scénario de test transformera le RPi en gestionnaire d' | ||
| + | |||
| + | **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.  | 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. | + | 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.** | 
| + | |||
| + | Le circuit matériel | ||
| + | |||
| + | Les modèles 2 et 3 du Raspberry Pi ont conservé une disposition de broches GPIO similaire à celle des modèles B+ plus anciens. Le connecteur 40 broches a deux rangées parallèles de picots, avec les numéros impairs à l' | ||
| + | |||
| + | Les circuits qui seront connectés au RPi pour faire fonctionner le logiciel seront de simples LED, bien que, dans un vrai circuit, celles-ci seraient remplacées par un adaptateur à haute impédance (probablement un optocoupleur, | ||
| - | 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. | + | **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. | 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. | + | 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 | + | Les forts courants doivent être évités sur les LED, 15 mA étant une limite de sécurité pour les appareils les plus robustes. Par ailleurs, les port GPIO du RPi ont plutôt des valeurs de travail plus basses (entre 2 et 12 mA), comme source (fournissant du courant à un circuit extérieur) ou en absorption (retour à la masse). Ceci signifie que nous avons besoin d' | 
| + | |||
| + | Sur le diagramme tracé avec Fritzing, le vert, le jaune et le bleu seront utilisés respectivement pour les circuits 1, 2 et 3. Fritzing n'a qu'une représentation du RPi 2 dans sa bibliothèque, | ||
| + | |||
| + | Pour réaliser les connexions au RPi, nous aurons besoin de câbles électriques avec des connecteurs femelles pour les embrocher dans les picots mâles du RPi. Cependant, une plaque d' | ||
| + | |||
| + | **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, | There are several projects to build a Pascal unit that controls the GPIO. However, just to make things more interesting, | ||
| Ligne 26: | Ligne 42: | ||
| Now, to turn on GPIO 16 (switch it to 5V output), or turn it back off (0V): | Now, to turn on GPIO 16 (switch it to 5V output), or turn it back off (0V): | ||
| + | |||
| + | echo 1 > / | ||
| + | |||
| + | echo 0 > / | ||
| + | |||
| + | Piloter les GPIO depuis Free Pascal | ||
| + | |||
| + | Il y a plusieurs projets pour construire une « unit » en Pascal qui pilote les GPIO. Cependant, pour rendre les choses plus intéressantes, | ||
| + | |||
| + | echo 16 > / | ||
| + | |||
| + | echo out > / | ||
| + | |||
| + | Nous voyons bien que les ports sont activés à un moment donné en listant les contenus du répertoire / | ||
| + | |||
| + | Maintenant, pour activer GPIO 16 (mettre la sortie à 5 V), ou l' | ||
| echo 1 > / | echo 1 > / | ||
| Ligne 31: | Ligne 63: | ||
| echo 0 > / | echo 0 > / | ||
| - | Finally, to release the port: | + | **Finally, to release the port: | 
| echo 16 > / | echo 16 > / | ||
| Ligne 37: | Ligne 69: | ||
| 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). | 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), | + | 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), | 
| - | 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:// | + | Enfin, pour libérer ce port : | 
| + | |||
| + | echo 16 > / | ||
| + | |||
| + | Comme vous le voyez, l' | ||
| + | |||
| + | C'est plus facile de référencer les numéros des ports et les valeurs sous forme de chaînes ; s'ils sont passés en entiers, chaque valeur devra être convertie en chaîne pour construire le nom des fichiers. Les deux premières routines sont là pour paramétrer un port, soit en entrée numérique (setup_input), | ||
| + | |||
| + | **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:// | ||
| A simple program to test this unit could be as follows: | 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: | 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** | ||
| + | |||
| + | Enfin, la procédure release « dé-exportera » le port. Tout ceci est plutôt simple à coder, et l'unit complète est disponible par ce lien : http:// | ||
| + | |||
| + | Un programme simple pour tester cette unit pourrait être : | ||
| + | |||
| + | Pour compiler et exécuter ce programme, nous devrons compiler à la fois l'unit et le programme lui-même, et, ensuite, exécuter le fichier binaire comme root : | ||
| fpc gpio.pas | fpc gpio.pas | ||
| Ligne 50: | Ligne 102: | ||
| sudo ./test10 | sudo ./test10 | ||
| - | Creating a Free Vision interface | + | |
| + | **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. | 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. | ||
| Ligne 62: | Ligne 115: | ||
| The application itself will need only a bespoke constructor, | The application itself will need only a bespoke constructor, | ||
| - | 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). | + | 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:// | + | Créer une interface avec Free Vision | 
| + | |||
| + | Intégrons notre nouvelle unit Gpio dans une application en Free Vision pour piloter les trois LED du Raspberry Pi. Nous n' | ||
| + | |||
| + | Notre programme aura besoin d' | ||
| + | |||
| + | uses | ||
| + | gpio, | ||
| + | App, Objects, Menus, Drivers, Views, Dialogs, MsgBox, StdDlg; | ||
| + | |||
| + | L' | ||
| + | |||
| + | Enfin, LightsDialog aura besoin d'une procédure HandleEvent pour répondre aux appuis sur les boutons. Ceci est un brin pénible, car nous aurons besoin de détecter un appui sur un bouton pour chaque bouton et, pour chacun d'eux, de déterminer si nous passons d' | ||
| + | |||
| + | **The complete code for this program is available here: http:// | ||
| 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). | 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. | + | 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.** | 
| + | |||
| + | Le code complet de ce programme est disponible ici : http:// | ||
| + | |||
| + | Dans cette septième partie de notre série sur Free Pascal, nous avons vu comment câbler le Raspberry Pi pour piloter plusieurs LED. Puis, nous avons écrit une unit simple en Pascal pour accéder aux ports GPIO, et, enfin, nous avons utilisé l'unit dans une application Free Vision pour produire une interface utilisateur en mode texte qui, franchement, | ||
| + | |||
| + | Pour approfondir, | ||
| + | |||
| + | **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.** | ||
| + | |||
| + | D'un point de vue matériel, nos trois bonnes LED peuvent être remplacées par des choses plus conséquentes. Cependant, se lancer dans un circuit sur le secteur avec des tensions de 110 V à 250 V ne devrait pas se faire, sauf à être un électricien qualifié ; beaucoup plus de précautions doivent être prises en utilisant le secteur car il peut être mortel, directement ou par incendie. C'est tout aussi vrai pour le 12 V DC des voitures. Faites-le à vos propres risques, et, s'il vous plaît, faites des recherches d' | ||
| + | |||
| + | Il est aussi avisé de se rappeler que tout courant de 10 à 15 mA entrant ou sortant du Raspberry Pi peut l' | ||
| + | |||
| + | //Textes en noir dans les zones saumon :// | ||
| + | |||
| + | __p 29 à droite__ | ||
| + | |||
| + | **The complete code to set up a port for writing is rather simple:** | ||
| + | |||
| + | Le code complet pour paramétrer un port en écriture est plutôt simple : | ||
| + | |||
| + | **Other procedures used are port_write to write to a GPIO port, and function port_read to read from a port that has been configured in the ‘in’ direction: | ||
| + | |||
| + | Les autres procédures utilisées sont port_write pour écrire sur un port GPIO et la fonction port_read pour lire sur un port qui a été configuré dans la direction « in » (entrée) : | ||
| + | |||
| + | __p 30 à gauche__ | ||
| + | |||
| + | **The LightsDialog type will need a few more elements to work. We will need to keep track of the state of each circuit (True for on, False for off). If we wish to be able to change the caption on each button to reflect the state of the circuit, we will also need to access these buttons from the main Dialog object, so include buttons 1 , 2 and 3 in its declaration: | ||
| + | |||
| + | Le type LightsDialog a besoin de quelques éléments supplémentaires pour fonctionner. Nous devons garder une trace de l' | ||
| + | |||
| + | **We will also need several constants, both to identify which GPIO ports will be used to control each circuit, and to store the command identification codes that each button will emit when pressed: | ||
| + | |||
| + | Nous aurons aussi besoin de plusieurs constantes, à la fois pour identifier quels ports GPIO seront utilisés pour piloter chaque circuit, et pour enregistrer les codes d' | ||
| + | |||
| + | __p30 à droite__ | ||
| + | |||
| + | **The LightsDialog can be initialized in a very simple manner. All we need to do is set up the buttons, and then add some code at the end of this constructor to initialize button states for the software, and set up the hardware correctly: | ||
| + | |||
| + | LightsDialog peut être initialisé de manière très simple. Nous devons simplement paramétrer les boutons et, ensuite, ajouter du code à la fin de ce constructeur pour initialiser l' | ||
| - | 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. | ||
issue117/tutoriel2.1485857388.txt.gz · Dernière modification : 2017/01/31 11:09 de auntiee
                
                