issue117:tutoriel2
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue117:tutoriel2 [2017/02/04 08:21] – d52fr | issue117:tutoriel2 [2017/02/07 11:54] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
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. ** | ||
- | Dans la partie précédente de cette série, nous avons vu plusieurs façons | + | Dans la partie précédente de cette série, nous avons vu plusieurs façons |
Notre scénario de test transformera le RPi en gestionnaire d' | Notre scénario de test transformera le RPi en gestionnaire d' | ||
Ligne 12: | Ligne 12: | ||
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. | ||
Ligne 18: | Ligne 24: | ||
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.** | ||
+ | |||
+ | 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 | **Controlling the GPIO from Free Pascal | ||
Ligne 34: | Ligne 46: | ||
echo 0 > / | 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 0 > / | ||
**Finally, to release the port: | **Finally, to release the port: | ||
Ligne 42: | Ligne 70: | ||
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), | ||
+ | |||
+ | 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:// | **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:// | ||
Ligne 53: | Ligne 89: | ||
sudo ./test10** | 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 test10.pas | ||
+ | |||
+ | sudo ./test10 | ||
+ | |||
**Creating a Free Vision interface | **Creating a Free Vision interface | ||
Ligne 67: | Ligne 116: | ||
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).** | ||
+ | |||
+ | 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:// | **The complete code for this program is available here: http:// | ||
Ligne 73: | Ligne 136: | ||
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. | **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.** | 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' | ||
+ | |||
+ |
issue117/tutoriel2.1486192889.txt.gz · Dernière modification : 2017/02/04 08:21 de d52fr