From 94611f2381fe861b728edb556909dde0bf4de950 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sun, 15 Oct 2017 12:30:15 +0200 Subject: [PATCH] Initial import --- .gitignore | 2 ++ source/CMakeLists.txt | 23 ++++++++++++++++++++ source/blinkthread.cpp | 22 +++++++++++++++++++ source/blinkthread.h | 22 +++++++++++++++++++ source/distancethread.cpp | 43 +++++++++++++++++++++++++++++++++++++ source/distancethread.h | 22 +++++++++++++++++++ source/main.cpp | 15 +++++++++++++ source/mainwindow.cpp | 45 +++++++++++++++++++++++++++++++++++++++ source/mainwindow.h | 27 +++++++++++++++++++++++ source/mainwindow.ui | 37 ++++++++++++++++++++++++++++++++ 10 files changed, 258 insertions(+) create mode 100644 .gitignore create mode 100644 source/CMakeLists.txt create mode 100644 source/blinkthread.cpp create mode 100644 source/blinkthread.h create mode 100644 source/distancethread.cpp create mode 100644 source/distancethread.h create mode 100644 source/main.cpp create mode 100644 source/mainwindow.cpp create mode 100644 source/mainwindow.h create mode 100644 source/mainwindow.ui diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b30f005 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build*/ +*.user diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt new file mode 100644 index 0000000..e4561b0 --- /dev/null +++ b/source/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.6) +project(qEcho) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +find_package(Qt5Widgets) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTOUIC ON) + +add_executable(${PROJECT_NAME} + main.cpp + mainwindow.cpp + mainwindow.h + mainwindow.ui + blinkthread.cpp + blinkthread.h + distancethread.cpp + distancethread.h +) + +target_link_libraries(${PROJECT_NAME} wiringPi Qt5::Widgets) + diff --git a/source/blinkthread.cpp b/source/blinkthread.cpp new file mode 100644 index 0000000..f062fa8 --- /dev/null +++ b/source/blinkthread.cpp @@ -0,0 +1,22 @@ +#include "blinkthread.h" +#include + +BlinkThread::BlinkThread() +{ + wiringPiSetup(); + pinMode(m_blinkPin, OUTPUT); +} + +void BlinkThread::run() +{ + while (true) + { + digitalWrite(m_blinkPin, HIGH); + emit ledOn(); + delay(500); + + digitalWrite(m_blinkPin, LOW); + emit ledOff(); + delay(500); + } +} diff --git a/source/blinkthread.h b/source/blinkthread.h new file mode 100644 index 0000000..cf5882b --- /dev/null +++ b/source/blinkthread.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +class BlinkThread : public QThread +{ + Q_OBJECT + +private: + const int m_blinkPin = 15; + +public: + BlinkThread(); + +protected: + void run() override; + +signals: + void ledOn(); + void ledOff(); +}; + diff --git a/source/distancethread.cpp b/source/distancethread.cpp new file mode 100644 index 0000000..308a448 --- /dev/null +++ b/source/distancethread.cpp @@ -0,0 +1,43 @@ +#include "distancethread.h" +#include + +volatile unsigned int timestampHigh = 0; +volatile unsigned int timestampLow = 0; + +DistanceThread::DistanceThread() +{ + wiringPiSetup(); + pinMode(m_triggerPin, OUTPUT); + pinMode(m_echoPin, INPUT); +} + +void DistanceThread::run() +{ + wiringPiISR(m_echoPin, INT_EDGE_BOTH, [] + { + if (digitalRead(29) == HIGH) + { + timestampHigh = micros(); + } + else + { + timestampLow = micros(); + } + }); + + while (true) + { + digitalWrite(m_triggerPin, HIGH); + delayMicroseconds(10); + digitalWrite(m_triggerPin, LOW); + delayMicroseconds(40); + + unsigned int delayUS = timestampLow - timestampHigh; + if (delayUS < 25e3) + { + emit distanceUpdated(delayUS / 0.58); + } + + delay(100); + } +} diff --git a/source/distancethread.h b/source/distancethread.h new file mode 100644 index 0000000..4429153 --- /dev/null +++ b/source/distancethread.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +class DistanceThread : public QThread +{ + Q_OBJECT + +private: + const int m_triggerPin = 28; + const int m_echoPin = 29; + +public: + DistanceThread(); + +protected: + void run() override; + +signals: + void distanceUpdated(double distMeters); +}; + diff --git a/source/main.cpp b/source/main.cpp new file mode 100644 index 0000000..2393b1d --- /dev/null +++ b/source/main.cpp @@ -0,0 +1,15 @@ +#include "mainwindow.h" +#include +#include + +int main(int argc, char **argv) +{ + std::cout << "qEcho" << std::endl; + + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} + diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp new file mode 100644 index 0000000..b9bba8d --- /dev/null +++ b/source/mainwindow.cpp @@ -0,0 +1,45 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +#include "blinkthread.h" +#include "distancethread.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + ui->progressBar->setRange(0, 300); + ui->progressBar->setFormat("%v cm"); + ui->progressBar->setValue(0); + + BlinkThread *blinkThread = new BlinkThread(); + connect(blinkThread, &BlinkThread::ledOn, this, &MainWindow::ledOn); + connect(blinkThread, &BlinkThread::ledOff, this, &MainWindow::ledOff); + blinkThread->start(); + + DistanceThread *distanceThread = new DistanceThread(); + connect(distanceThread, &DistanceThread::distanceUpdated, this, &MainWindow::distanceUpdated); + distanceThread->start(); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::ledOn() +{ + ui->label->setText("On"); +} + +void MainWindow::ledOff() +{ + ui->label->setText("Off"); +} + +void MainWindow::distanceUpdated(double distMeters) +{ + ui->progressBar->setValue(distMeters / 100); +} diff --git a/source/mainwindow.h b/source/mainwindow.h new file mode 100644 index 0000000..6469d49 --- /dev/null +++ b/source/mainwindow.h @@ -0,0 +1,27 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private: + Ui::MainWindow *ui; + +private slots: + void ledOn(); + void ledOff(); + void distanceUpdated(double distMeters); +}; + +#endif // MAINWINDOW_H diff --git a/source/mainwindow.ui b/source/mainwindow.ui new file mode 100644 index 0000000..ff59b8b --- /dev/null +++ b/source/mainwindow.ui @@ -0,0 +1,37 @@ + + + MainWindow + + + + 0 + 0 + 359 + 189 + + + + qEcho + + + + + + + 24 + + + + + + + <html><head/><body><p><br/></p></body></html> + + + + + + + + +