Added a Qt window for output

This commit is contained in:
mandlm 2017-10-17 22:08:05 +02:00
parent bc7521ac95
commit 9006032f27
8 changed files with 184 additions and 36 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
build*/
*.user

View File

@ -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
@ -9,7 +15,12 @@ add_library(rc-switch
add_executable(qSniff
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)

26
source/mainwindow.cpp Normal file
View File

@ -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);
}

26
source/mainwindow.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
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

56
source/mainwindow.ui Normal file
View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>452</width>
<height>312</height>
</rect>
</property>
<property name="windowTitle">
<string>qSniff</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListWidget" name="listWidget"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Clear</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -1,40 +1,12 @@
#include "rc-switch/RCSwitch.h"
#include <wiringPi.h>
#include <iostream>
#include <QApplication>
#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();
}

40
source/sniffthread.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "sniffthread.h"
#include <wiringPi.h>
#include "rc-switch/RCSwitch.h"
#include <sstream>
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);
}
}

16
source/sniffthread.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <QThread>
#include <QString>
class SniffThread : public QThread
{
Q_OBJECT
protected:
void run() override;
signals:
void dataReceived(QString data);
};