Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
eichenberger committed Oct 25, 2019
0 parents commit b49cc48
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 0 deletions.
35 changes: 35 additions & 0 deletions LEDToggle.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
QT += quick
CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
main.cpp \
led.cpp \
gpio.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
led.h \
gpio.h
103 changes: 103 additions & 0 deletions gpio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <QDirIterator>
#include <QDebug>

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include <linux/gpio.h>

#include "gpio.h"

Gpio::Gpio(QString name, QObject *parent) : QObject(parent), gpio_fd(-1)
{
int gpio = -1;
QString gpiochip;

QString devdir = "/dev/";
QDir dev(devdir);
dev.setFilter(QDir::Files | QDir::Hidden | QDir::System);
dev.setNameFilters(QStringList("gpiochip*"));

QStringList fileList = dev.entryList();
for (int i = 0; i < fileList.count() && gpio == -1; i++) {
QString fileName = devdir + fileList[i];
QFile f(fileName);
f.open(QIODevice::ReadWrite);

qDebug() << f.fileName();

struct gpiochip_info cinfo;
int ret = ioctl(f.handle(), GPIO_GET_CHIPINFO_IOCTL, &cinfo);
if (ret == -1) {
qDebug() << "Failed to issue CHIPINFO IOCTL";
return;
}

/* Loop over the lines and print info */
for (uint32_t i = 0; i < cinfo.lines; i++) {
struct gpioline_info linfo;

memset(&linfo, 0, sizeof(linfo));
linfo.line_offset = i;

ret = ioctl(f.handle(), GPIO_GET_LINEINFO_IOCTL, &linfo);
if (ret == -1) {
qDebug() << "Failed to issue LINEINFO IOCTL for " << i;
continue;
}

if (QString(linfo.name) == name) {
qDebug() << "GPIO " << name << " found";
gpiochip = f.fileName();
gpio = static_cast<int>(i);
break;
}
}
}
if (gpio == -1) {
qDebug() << "No gpio found";
return;
}
QFile f(gpiochip);
f.open(QIODevice::ReadWrite);
struct gpiohandle_request req;
req.lineoffsets[0] = static_cast<uint32_t>(gpio);
req.lines = 1;
req.flags = GPIOHANDLE_REQUEST_OUTPUT;
req.default_values[0] = 0;
strcpy(req.consumer_label, "qtgpio");
int ret = ioctl(f.handle(), GPIO_GET_LINEHANDLE_IOCTL, &req);
if (ret == -1) {
qDebug() << "Could not set gpio" << gpio << " as output";
return;
}
gpio_fd = req.fd;
}

Gpio::~Gpio()
{
close(gpio_fd);
}

void Gpio::enable()
{
setGpio(1);
}

void Gpio::disable()
{
setGpio(0);
}

void Gpio::setGpio(uint8_t value)
{
struct gpiohandle_data data;
data.values[0] = value;
int ret = ioctl(gpio_fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
if (ret == -1) {
qDebug() << "Could not set gpio value " << value;
}
}
22 changes: 22 additions & 0 deletions gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef GPIO_H
#define GPIO_H

#include <QObject>
#include <QString>

class Gpio : public QObject
{
Q_OBJECT
public:
Gpio(QString name, QObject *parent = nullptr);
~Gpio();

void enable();
void disable();

void setGpio(uint8_t value);
private:
int gpio_fd;
};

#endif // GPIO_H
21 changes: 21 additions & 0 deletions led.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <QDebug>
#include <QString>

#include "led.h"

Led::Led(QObject *parent) : QObject(parent)
{
gpio = new Gpio(QString("led"));
}

void Led::enable()
{
qDebug() << "Enable LED";
gpio->enable();
}

void Led::disable()
{
qDebug() << "Disable LED";
gpio->disable();
}
25 changes: 25 additions & 0 deletions led.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef LED_H
#define LED_H

#include <QObject>
#include <QPointer>

#include "gpio.h"

class Led : public QObject
{
Q_OBJECT
public:
explicit Led(QObject *parent = nullptr);

signals:

public slots:
void enable();
void disable();

private:
QPointer<Gpio> gpio;
};

#endif // LED_H
27 changes: 27 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include "led.h"

int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));

if (qEnvironmentVariableIsEmpty("QTGLESSTREAM_DISPLAY")) {
qputenv("QT_QPA_EGLFS_PHYSICAL_WIDTH", QByteArray("213"));
qputenv("QT_QPA_EGLFS_PHYSICAL_HEIGHT", QByteArray("120"));

QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
}

qmlRegisterType<Led>("led", 1, 0, "Led");

QGuiApplication app(argc, argv);

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;

return app.exec();
}
83 changes: 83 additions & 0 deletions main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
import QtQuick.Controls.Styles 1.4

import led 1.0

Window {
id: window
visible: true
title: qsTr("Hello World")

Led {
id: led
}

Rectangle {
color: "#00508c"
anchors.fill: parent
ColumnLayout {
anchors.fill: parent

Image {
id: image1
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
source: "toradex.png"
}

Rectangle {
id: virtualled
width: 100
height: 100
radius: 50
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
color: "#ff0000"
}

RowLayout {
id: rowLayout
Button {
id: button
text: qsTr("ON")
Layout.minimumHeight: 100
display: AbstractButton.TextBesideIcon
Layout.fillWidth: true
background: Rectangle {
anchors.fill: parent
color: parent.pressed ? "#00dd00" : "#00ff00"
}

onClicked: {
led.enable();
virtualled.color = "#00ff00";
}
}

Button {
id: button1
text: qsTr("OFF")
Layout.minimumHeight: 100
display: AbstractButton.TextBesideIcon
Layout.fillWidth: true

background: Rectangle {
anchors.fill: parent
color: parent.pressed ? "#dd0000" : "#ff0000"
}

onClicked: {
led.disable();
virtualled.color = "#ff0000";
}
}
}
}
}
}

/*##^## Designer {
D{i:0;autoSize:true;height:480;width:640}
}
##^##*/
6 changes: 6 additions & 0 deletions qml.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>toradex.png</file>
</qresource>
</RCC>
Binary file added toradex.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b49cc48

Please sign in to comment.