Vérification de fonctionnement de la librairie cpp2835 avec Qt
Cet exemple est équivalent à l'exemple Qt et bcm2835 : Vérification. Reportez vous à ce dernier pour la connection à mettre en place sur le connecteru GPIO 40 broches. Vous pouvez également vous y reporter pour explorer la création de projets sous Qt, l'insertion de librairies externes, le concept signal/slot de Qt, etc...
Je ne donne donc ici que le code de cet exemple. Pour une meilleure compréhension du fichier projet ".PRO" :
- La librairie cpp est installée dans le répertoire "/home/pi/cpp2835"
- Le projet est créé dans le répertoire "/home/pi/projects/testcpp2835"
Code source
Fichier projet "testcpp2835.pro" :
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
maindialog.cpp
HEADERS += \
maindialog.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
unix:!macx: LIBS += -L$$PWD/../../cpp2835/lib/ -lCpp2835
INCLUDEPATH += $$PWD/../../cpp2835/headers
DEPENDPATH += $$PWD/../../cpp2835/headers
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
maindialog.cpp
HEADERS += \
maindialog.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
unix:!macx: LIBS += -L$$PWD/../../cpp2835/lib/ -lCpp2835
INCLUDEPATH += $$PWD/../../cpp2835/headers
DEPENDPATH += $$PWD/../../cpp2835/headers
Fichier "maindialog.h" :
#ifndef MAINDIALOG_H
#define MAINDIALOG_H
#include <QDialog>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include "Cpp2835_export.h"
class MainDialog : public QDialog
{
Q_OBJECT
public:
MainDialog(QWidget *parent = nullptr);
~MainDialog();
private:
C2835Driver* m_pDriver;
QHBoxLayout* m_pLayout;
QPushButton* m_pButton;
QLabel* m_pLabel;
bool m_pDigitalState;
public slots:
void onClick();
};
#endif // MAINDIALOG_H
#define MAINDIALOG_H
#include <QDialog>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include "Cpp2835_export.h"
class MainDialog : public QDialog
{
Q_OBJECT
public:
MainDialog(QWidget *parent = nullptr);
~MainDialog();
private:
C2835Driver* m_pDriver;
QHBoxLayout* m_pLayout;
QPushButton* m_pButton;
QLabel* m_pLabel;
bool m_pDigitalState;
public slots:
void onClick();
};
#endif // MAINDIALOG_H
Fichier "maindialog.cpp" :
#include "maindialog.h"
MainDialog::MainDialog(QWidget *parent)
: QDialog(parent)
{
m_pDriver = nullptr;
m_pLayout = new QHBoxLayout();
m_pButton = new QPushButton();
m_pLabel = new QLabel();
m_pButton->setText("Trigger");
m_pLabel->setText(QString::number(0));
m_pLayout->addWidget(m_pButton);
m_pLayout->addWidget(m_pLabel);
setLayout(m_pLayout);
m_pDigitalState = false;
m_pDriver = new C2835Driver();
m_pDriver->Initialize();
m_pDriver->SetDigitalBitFunction(GPIO_4, GPIO_FUNCTION_OUTPUT);
m_pDriver->SetDigitalBitFunction(GPIO_17, GPIO_FUNCTION_INPUT);
m_pDriver->SetDigitalBitPullUpDirection(GPIO_17, GPIO_PUD_DIRECTION_DOWN);
connect(m_pButton, SIGNAL(clicked()), this, SLOT(onClick()));
}
MainDialog::~MainDialog()
{
if(m_pDriver != nullptr)
{
m_pDriver->Terminate();
delete m_pDriver;
}
}
void MainDialog::onClick()
{
// Variable recevant la valeur relue sur l'entrée numérique
bool DigitalReadValue = true;
// Inversion de la valeur à écrire sur la sortie numérique
m_pDigitalState = !m_pDigitalState;
// Ecriture sortie numérique
m_pDriver->WriteDigitalOuput(GPIO_4, m_pDigitalState);
// Lecture de l'entrée numérique
m_pDriver->ReadDigitalInput(GPIO_17, &DigitalReadValue);
// Mise à jour du label avec la valeur lue
m_pLabel->setText(DigitalReadValue ? "1" : "0");
}
MainDialog::MainDialog(QWidget *parent)
: QDialog(parent)
{
m_pDriver = nullptr;
m_pLayout = new QHBoxLayout();
m_pButton = new QPushButton();
m_pLabel = new QLabel();
m_pButton->setText("Trigger");
m_pLabel->setText(QString::number(0));
m_pLayout->addWidget(m_pButton);
m_pLayout->addWidget(m_pLabel);
setLayout(m_pLayout);
m_pDigitalState = false;
m_pDriver = new C2835Driver();
m_pDriver->Initialize();
m_pDriver->SetDigitalBitFunction(GPIO_4, GPIO_FUNCTION_OUTPUT);
m_pDriver->SetDigitalBitFunction(GPIO_17, GPIO_FUNCTION_INPUT);
m_pDriver->SetDigitalBitPullUpDirection(GPIO_17, GPIO_PUD_DIRECTION_DOWN);
connect(m_pButton, SIGNAL(clicked()), this, SLOT(onClick()));
}
MainDialog::~MainDialog()
{
if(m_pDriver != nullptr)
{
m_pDriver->Terminate();
delete m_pDriver;
}
}
void MainDialog::onClick()
{
// Variable recevant la valeur relue sur l'entrée numérique
bool DigitalReadValue = true;
// Inversion de la valeur à écrire sur la sortie numérique
m_pDigitalState = !m_pDigitalState;
// Ecriture sortie numérique
m_pDriver->WriteDigitalOuput(GPIO_4, m_pDigitalState);
// Lecture de l'entrée numérique
m_pDriver->ReadDigitalInput(GPIO_17, &DigitalReadValue);
// Mise à jour du label avec la valeur lue
m_pLabel->setText(DigitalReadValue ? "1" : "0");
}