Initial import

pull/2/head
mandlm 2017-10-15 12:30:15 +02:00
commit 94611f2381
10 changed files with 258 additions and 0 deletions

2
.gitignore vendored Normal file
View File

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

23
source/CMakeLists.txt Normal file
View File

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

22
source/blinkthread.cpp Normal file
View File

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

22
source/blinkthread.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <QThread>
class BlinkThread : public QThread
{
Q_OBJECT
private:
const int m_blinkPin = 15;
public:
BlinkThread();
protected:
void run() override;
signals:
void ledOn();
void ledOff();
};

43
source/distancethread.cpp Normal file
View File

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

22
source/distancethread.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <QThread>
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);
};

15
source/main.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "mainwindow.h"
#include <QApplication>
#include <iostream>
int main(int argc, char **argv)
{
std::cout << "qEcho" << std::endl;
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

45
source/mainwindow.cpp Normal file
View File

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

27
source/mainwindow.h Normal file
View File

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

37
source/mainwindow.ui Normal file
View File

@ -0,0 +1,37 @@
<?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>359</width>
<height>189</height>
</rect>
</property>
<property name="windowTitle">
<string>qEcho</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>24</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>