diff --git a/.gitignore b/.gitignore index a5309e6..b30f005 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build*/ +*.user diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 48305e7..baae8a1 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -1,5 +1,11 @@ cmake_minimum_required(VERSION 3.7) +find_package(Qt5Widgets) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTOUIC ON) + add_definitions(-DRPI) add_library(rc-switch @@ -8,8 +14,13 @@ add_library(rc-switch ) add_executable(qSniff - qsniff.cpp + qsniff.cpp + mainwindow.cpp + mainwindow.h + mainwindow.ui + sniffthread.cpp + sniffthread.h ) -target_link_libraries(qSniff rc-switch wiringPi) +target_link_libraries(qSniff rc-switch wiringPi Qt5::Widgets) diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp new file mode 100644 index 0000000..5dc32b1 --- /dev/null +++ b/source/mainwindow.cpp @@ -0,0 +1,26 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include "sniffthread.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + connect(ui->pushButton, &QPushButton::clicked, ui->listWidget, &QListWidget::clear); + + SniffThread *sniffThread = new SniffThread(); + connect(sniffThread, &SniffThread::dataReceived, this, &MainWindow::dataReceived); + sniffThread->start(); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::dataReceived(QString data) +{ + ui->listWidget->addItem(data); +} diff --git a/source/mainwindow.h b/source/mainwindow.h new file mode 100644 index 0000000..1f888cf --- /dev/null +++ b/source/mainwindow.h @@ -0,0 +1,26 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private slots: + void dataReceived(QString data); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/source/mainwindow.ui b/source/mainwindow.ui new file mode 100644 index 0000000..9a3b8e5 --- /dev/null +++ b/source/mainwindow.ui @@ -0,0 +1,56 @@ + + + MainWindow + + + + 0 + 0 + 452 + 312 + + + + qSniff + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 100 + 16777215 + + + + Clear + + + + + + + + + + + diff --git a/source/qsniff.cpp b/source/qsniff.cpp index 2f12d28..592dd4a 100644 --- a/source/qsniff.cpp +++ b/source/qsniff.cpp @@ -1,40 +1,12 @@ -#include "rc-switch/RCSwitch.h" -#include -#include +#include +#include "mainwindow.h" int main(int argc, char **argv) { - std::cout << "qSniff" << std::endl; + QApplication app(argc, argv); - wiringPiSetup(); + MainWindow mainWindow; + mainWindow.show(); - RCSwitch mySwitch = RCSwitch(); - - mySwitch.enableReceive(6); - - while (true) - { - if (mySwitch.available()) - { - int value = mySwitch.getReceivedValue(); - - if (value == 0) - { - std::cout << "Unknown encoding" << std::endl; - } - else - { - std::cout << "Received " << value << " / "; - std::cout << mySwitch.getReceivedBitlength() << " bit "; - std::cout << "Protocol: " << mySwitch.getReceivedProtocol(); - std::cout << std::endl; - } - - mySwitch.resetAvailable(); - } - - delay(100); - } - - return 0; + return app.exec(); } diff --git a/source/sniffthread.cpp b/source/sniffthread.cpp new file mode 100644 index 0000000..c9e48f7 --- /dev/null +++ b/source/sniffthread.cpp @@ -0,0 +1,40 @@ +#include "sniffthread.h" +#include +#include "rc-switch/RCSwitch.h" +#include + +void SniffThread::run() +{ + wiringPiSetup(); + + RCSwitch mySwitch = RCSwitch(); + + mySwitch.enableReceive(6); + + while (true) + { + if (mySwitch.available()) + { + int value = mySwitch.getReceivedValue(); + + if (value == 0) + { + emit dataReceived("Unknown encoding"); + } + else + { + std::stringstream result; + result << "Received " << value << " / "; + result << mySwitch.getReceivedBitlength() << " bit "; + result << "Protocol: " << mySwitch.getReceivedProtocol(); + + emit dataReceived(result.str().c_str()); + } + + mySwitch.resetAvailable(); + } + + delay(100); + } +} + diff --git a/source/sniffthread.h b/source/sniffthread.h new file mode 100644 index 0000000..8c34d48 --- /dev/null +++ b/source/sniffthread.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +class SniffThread : public QThread +{ + Q_OBJECT + +protected: + void run() override; + +signals: + void dataReceived(QString data); +}; +