From 388bc43b8b847c48b2183cf7b5bb808b3eb87324 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Mon, 16 Oct 2017 15:23:54 +0200 Subject: [PATCH] Separated Qt and WiringPi code in blink thread --- source/CMakeLists.txt | 4 +++- source/blinkthread.cpp | 15 +++++++-------- source/blinkthread.h | 6 ++++-- source/led.cpp | 22 ++++++++++++++++++++++ source/led.h | 15 +++++++++++++++ source/mainwindow.cpp | 2 +- 6 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 source/led.cpp create mode 100644 source/led.h diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index c17cbcf..83bbd0f 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -15,7 +15,9 @@ add_executable(${PROJECT_NAME} mainwindow.cpp mainwindow.h mainwindow.ui - blinkthread.cpp + led.cpp + led.h + blinkthread.cpp blinkthread.h distancethread.cpp distancethread.h diff --git a/source/blinkthread.cpp b/source/blinkthread.cpp index f062fa8..52e7bbd 100644 --- a/source/blinkthread.cpp +++ b/source/blinkthread.cpp @@ -1,22 +1,21 @@ #include "blinkthread.h" -#include +#include "led.h" -BlinkThread::BlinkThread() +BlinkThread::BlinkThread(unsigned int pin) + : m_led(pin) { - wiringPiSetup(); - pinMode(m_blinkPin, OUTPUT); } void BlinkThread::run() { while (true) { - digitalWrite(m_blinkPin, HIGH); + m_led.on(); emit ledOn(); - delay(500); + msleep(500); - digitalWrite(m_blinkPin, LOW); + m_led.off(); emit ledOff(); - delay(500); + msleep(500); } } diff --git a/source/blinkthread.h b/source/blinkthread.h index cf5882b..c779244 100644 --- a/source/blinkthread.h +++ b/source/blinkthread.h @@ -1,5 +1,6 @@ #pragma once +#include "led.h" #include class BlinkThread : public QThread @@ -7,10 +8,11 @@ class BlinkThread : public QThread Q_OBJECT private: - const int m_blinkPin = 15; + Led m_led; public: - BlinkThread(); + BlinkThread() = delete; + BlinkThread(unsigned int pin); protected: void run() override; diff --git a/source/led.cpp b/source/led.cpp new file mode 100644 index 0000000..0be6c97 --- /dev/null +++ b/source/led.cpp @@ -0,0 +1,22 @@ +#include "led.h" +#include + +#include + +Led::Led(unsigned int pin) + : m_pin(pin) +{ + wiringPiSetup(); + pinMode(m_pin, OUTPUT); +} + +void Led::on() const +{ + digitalWrite(m_pin, HIGH); +} + +void Led::off() const +{ + digitalWrite(m_pin, LOW); +} + diff --git a/source/led.h b/source/led.h new file mode 100644 index 0000000..f264855 --- /dev/null +++ b/source/led.h @@ -0,0 +1,15 @@ +#pragma once + +class Led +{ +private: + unsigned int m_pin = 0; + +public: + Led(unsigned int pin); + + void on() const; + void off() const; +}; + + diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index b9bba8d..5584ab8 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -14,7 +14,7 @@ MainWindow::MainWindow(QWidget *parent) : ui->progressBar->setFormat("%v cm"); ui->progressBar->setValue(0); - BlinkThread *blinkThread = new BlinkThread(); + BlinkThread *blinkThread = new BlinkThread(15); connect(blinkThread, &BlinkThread::ledOn, this, &MainWindow::ledOn); connect(blinkThread, &BlinkThread::ledOff, this, &MainWindow::ledOff); blinkThread->start();