forked from Tpimp/qgpio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
55 lines (48 loc) · 1.87 KB
/
Copy pathmain.cpp
File metadata and controls
55 lines (48 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <QtGui/QGuiApplication>
#include <QQmlContext>
#include "qtquick2applicationviewer.h"
#include "qgpiowatcher.h"
#include "qgpio.h"
#include <QDebug>
#include <QTimer>
#include <QtQml>
#include "qsoftpwm.h"
int QGpioWatcher::THREAD_SLEEP_TIME = 3000;
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
qmlRegisterType<QGpio>("com.embedded.io",1,0,"QGpio");
QList<int> inPins;
inPins << 204 << 199; // CAREFUL TO SUPPLY VALID Input Capable PINS
QGpioWatcher gpio_thread;
gpio_thread.start();
QQmlContext * root_c(viewer.rootContext());
foreach(const int & pin_number, inPins)
{
QGpio * pin_created(gpio_thread.exportPin(pin_number,QGpio::In,QGpio::Both,&app));
pin_created->setActiveLow(0);
pin_created->setEdgeType(QGpio::Both);
if(pin_created) // connect c++ slot
{
QObject::connect(pin_created,&QGpio::valueChanged,[=](const bool & new_value){
qDebug() << "Pin " << pin_created->pinNumber() << "changed value to " << new_value;
});
QString pin_name("GPIO");
pin_name += QString::number(pin_number);
qDebug() << "Creating context property link" << pin_name;
root_c->setContextProperty(pin_name, pin_created);
}
else
qDebug() << "Pin "<< pin_number << " is null!";
}
// objects must be exposed to qml through QList<QObject*>
// we must cast a list
root_c->setContextProperty("GpioList",QVariant::fromValue(gpio_thread.exportedObjects()));
root_c->setContextProperty("GpioWatcher",&gpio_thread);
viewer.setMainQmlFile(QStringLiteral("qml/Gpio_Test/main.qml"));
viewer.showExpanded();
QObject::connect(&app, &QGuiApplication::aboutToQuit, &gpio_thread, &QThread::terminate);
int ret_val(app.exec());
return ret_val;
}