Separated Qt and WiringPi code in blink thread

This commit is contained in:
Michael Mandl 2017-10-16 15:23:54 +02:00 committed by mandlm
parent d2e774fe5c
commit 388bc43b8b
6 changed files with 52 additions and 12 deletions

View file

@ -15,6 +15,8 @@ add_executable(${PROJECT_NAME}
mainwindow.cpp
mainwindow.h
mainwindow.ui
led.cpp
led.h
blinkthread.cpp
blinkthread.h
distancethread.cpp

View file

@ -1,22 +1,21 @@
#include "blinkthread.h"
#include <wiringPi.h>
#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);
}
}

View file

@ -1,5 +1,6 @@
#pragma once
#include "led.h"
#include <QThread>
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;

22
source/led.cpp Normal file
View file

@ -0,0 +1,22 @@
#include "led.h"
#include <wiringPi.h>
#include <iostream>
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);
}

15
source/led.h Normal file
View file

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

View file

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