Cours:TPgpioQT : Différence entre versions
(→Préparation du projet) |
(→Préparation du projet) |
||
| Ligne 25 : | Ligne 25 : | ||
*Dans l'arborescence de votre projet, | *Dans l'arborescence de votre projet, | ||
**Ajouter des fichiers existants | **Ajouter des fichiers existants | ||
| + | |||
| + | |||
| + | ==Sorties== | ||
| + | |||
| + | On utilisera la classe Lampe pour piloter des sorties. | ||
| + | |||
| + | {|style="vertical-align:middle; width:100%; text-align:left; " | ||
| + | |- | ||
| + | | {{boîte déroulante/début|titre=[[Media:lampe.h|lampe.h]]}} | ||
| + | <source lang=cpp> | ||
| + | #ifndef LAMPE_H | ||
| + | #define LAMPE_H | ||
| + | |||
| + | #include <QObject> | ||
| + | #include "gpio.h" | ||
| + | using namespace exploringRPi; | ||
| + | |||
| + | class Lampe : public QObject | ||
| + | { | ||
| + | Q_OBJECT | ||
| + | public: | ||
| + | Lampe(int nbBroche); | ||
| + | |||
| + | private : | ||
| + | bool etat; | ||
| + | GPIO broche; | ||
| + | |||
| + | signals: | ||
| + | void nouvelEtat(bool etat); | ||
| + | public slots: | ||
| + | void allumer(); | ||
| + | void eteindre(); | ||
| + | void setValue(bool e); | ||
| + | }; | ||
| + | |||
| + | #endif // LAMPE_H | ||
| + | </source> | ||
| + | {{boîte déroulante/fin}} | ||
| + | ||{{boîte déroulante/début|titre=lampe.cpp}} | ||
| + | <source lang=cpp> | ||
| + | #include "lampe.h" | ||
| + | |||
| + | Lampe::Lampe(int nbBroche) : QObject(), broche(nbBroche) | ||
| + | { | ||
| + | broche.setDirection(OUTPUT); | ||
| + | setValue(false); | ||
| + | } | ||
| + | |||
| + | void Lampe::allumer() | ||
| + | { | ||
| + | setValue(true); | ||
| + | } | ||
| + | |||
| + | void Lampe::eteindre() | ||
| + | { | ||
| + | setValue(false); | ||
| + | } | ||
| + | |||
| + | void Lampe::setValue(bool e) | ||
| + | { | ||
| + | if (e==true) broche.setValue(HIGH); | ||
| + | else broche.setValue(LOW); | ||
| + | etat = e; | ||
| + | emit nouvelEtat(etat); | ||
| + | } | ||
| + | </source> | ||
| + | {{boîte déroulante/fin}} | ||
| + | |} | ||
| + | |||
| + | {{Question|Faire le diagramme de classe d'après le fichier lampe.h}} | ||
Version du 15 septembre 2022 à 11:26
Retour à la liste des Tds
GPIO
On dispose d'un connecteur avec un certain nombre de GPIO sur la carte rpi.
Ce sont des broches configurables qui peuvent servir de :
- entrée logique
- sortie logique
- fonction particulière :
- entrée analogique (pas sur les rpi actuelles)
- i2c
- spi
- pwm
- liaison série
- ...
Préparation du projet
- Créer un nouveau projet de type "Widget application"
- Choisir le kit "Rpi"
- Télécharger/décompresser et placer les fichiers de l'archive suivant dans votre projet
- Dans l'arborescence de votre projet,
- Ajouter des fichiers existants
Sorties
On utilisera la classe Lampe pour piloter des sorties.
#ifndef LAMPE_H
#define LAMPE_H
#include <QObject>
#include "gpio.h"
using namespace exploringRPi;
class Lampe : public QObject
{
Q_OBJECT
public:
Lampe(int nbBroche);
private :
bool etat;
GPIO broche;
signals:
void nouvelEtat(bool etat);
public slots:
void allumer();
void eteindre();
void setValue(bool e);
};
#endif // LAMPE_H
|
lampe.cpp #include "lampe.h"
Lampe::Lampe(int nbBroche) : QObject(), broche(nbBroche)
{
broche.setDirection(OUTPUT);
setValue(false);
}
void Lampe::allumer()
{
setValue(true);
}
void Lampe::eteindre()
{
setValue(false);
}
void Lampe::setValue(bool e)
{
if (e==true) broche.setValue(HIGH);
else broche.setValue(LOW);
etat = e;
emit nouvelEtat(etat);
}
|