Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f891bee
Fixed a segmentation fault.
Feb 20, 2014
9d5571e
Forgotten to add the files!
Feb 21, 2014
5329a02
Deactivated the adcreader.
Feb 22, 2014
8ad8870
SetRange no longer exists.
berndporr Feb 15, 2017
d7eccb5
Crashed under QT5 because it loaded the QT4 QWT library.
berndporr Feb 24, 2017
e8dd1e3
Updated links
berndporr Feb 1, 2018
d25effd
Added reference to qwt
berndporr Feb 28, 2018
ede2550
Added package reference to QT5
berndporr Feb 28, 2018
192a7d1
Added screenshot
berndporr Feb 28, 2018
ddda7e4
Fixed formatting
berndporr Feb 28, 2018
ef4da67
Fixed spelling mishtake!
berndporr Feb 28, 2018
97e960d
Fixed old link.
berndporr Feb 12, 2020
71ee77f
Removed the ref to startx
berndporr Jan 20, 2021
c36ddc8
Added nice comment
berndporr Jan 20, 2021
26004b8
Changed to CMake
berndporr Jan 28, 2021
793dc26
Min cmake vers is now 3.7.0
berndporr Feb 2, 2021
e163b44
Fixed some leftovers in the readme
berndporr Feb 20, 2021
b4ca288
Showing it in the size as needed!
berndporr Feb 20, 2021
b8c20e5
Got rid of a few leftovers
berndporr Feb 20, 2021
33c12fd
got rid of signal & slot
berndporr Mar 8, 2022
4cdc145
New screenshot
berndporr Mar 8, 2022
fcc1c8e
Fixed range
berndporr Mar 8, 2022
0416023
Moved var init into the class
berndporr Mar 8, 2022
2e4be21
No need of a lambda function.
berndporr Mar 8, 2022
d7d6e6a
Fixed a few spelling issues.
berndporr Mar 9, 2022
324884f
Old project name fixed.
berndporr Mar 10, 2022
459bc88
Merge branch 'master' of github.com:berndporr/qwt-example
berndporr Mar 10, 2022
fe35fae
Removed the old memmove command
berndporr Apr 21, 2022
8ddd544
Now with cpp timer
berndporr Feb 8, 2024
a9c438c
Stopping sensor on exit.
berndporr Feb 8, 2024
51e2f01
Added mutex
berndporr Feb 8, 2024
6b01141
Moved both timing measures into window.cpp
berndporr Feb 11, 2024
15a3cf7
Moved the mutex closer to the action!
berndporr Feb 11, 2024
6d08e75
Added a separate class for the fake sensor
berndporr Jan 31, 2025
045e0aa
Fixed bug where the function wasn't overridden.
berndporr Feb 6, 2025
6314b22
Added missing libraries
berndporr Mar 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.7.0)

project(qwt-example-app VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt5 COMPONENTS Widgets REQUIRED)

add_executable(qwt-example
window.cpp
main.cpp
)

target_link_libraries(qwt-example Qt5::Widgets)
target_link_libraries(qwt-example qwt-qt5)

install(TARGETS qwt-example)
162 changes: 162 additions & 0 deletions CppTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#ifndef __CPP_TIMER_H_
#define __CPP_TIMER_H_

/**
* GNU GENERAL PUBLIC LICENSE
* Version 3, 29 June 2007
*
* (C) 2020-2024, Bernd Porr <mail@bernporr.me.uk>
*
* This is inspired by the timer_create man page.
**/

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <thread>
#include <sys/timerfd.h>

/**
* Enumeration of CppTimer types
**/
enum cppTimerType_t
{
PERIODIC,
ONESHOT
};

/**
* Timer class which repeatedly fires. It's wrapper around the
* POSIX per-process timer.
**/
class CppTimer
{

public:
/**
* Starts the timer. The timer fires first after
* the specified time in nanoseconds and then at
* that interval in PERIODIC mode. In ONESHOT mode
* the timer fires once after the specified time in
* nanoseconds.
* @param nanosecs Time in nanoseconds
* @param type Either PERIODIC or ONESHOT
**/
virtual void startns(long nanosecs, cppTimerType_t type = PERIODIC) {
if (running) return;
fd = timerfd_create(CLOCK_MONOTONIC, 0);
if (fd < 0)
throw("Could not start timer");
switch (type)
{
case (PERIODIC):
//starts after specified period of nanoseconds
its.it_value.tv_sec = nanosecs / 1000000000;
its.it_value.tv_nsec = nanosecs % 1000000000;
its.it_interval.tv_sec = nanosecs / 1000000000;
its.it_interval.tv_nsec = nanosecs % 1000000000;
break;
case (ONESHOT):
//fires once after specified period of nanoseconds
its.it_value.tv_sec = nanosecs / 1000000000;
its.it_value.tv_nsec = nanosecs % 1000000000;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
break;
}
if (timerfd_settime(fd, 0, &its, NULL) == -1)
throw("Could not start timer");
uthread = std::thread(&CppTimer::worker,this);
}

/**
* Starts the timer. The timer fires first after
* the specified time in milliseconds and then at
* that interval in PERIODIC mode. In ONESHOT mode
* the timer fires once after the specified time in
* milliseconds.
* @param millisecs Time in milliseconds
* @param type Either PERIODIC or ONESHOT
**/
virtual void startms(long millisecs, cppTimerType_t type = PERIODIC) {
if (running) return;
fd = timerfd_create(CLOCK_MONOTONIC, 0);
if (fd < 0)
throw("Could not start timer");
switch (type)
{
case (PERIODIC):
//starts after specified period of milliseconds
its.it_value.tv_sec = millisecs / 1000;
its.it_value.tv_nsec = (millisecs % 1000) * 1000000;
its.it_interval.tv_sec = millisecs / 1000;
its.it_interval.tv_nsec = (millisecs % 1000) * 1000000;
break;
case (ONESHOT):
//fires once after specified period of milliseconds
its.it_value.tv_sec = millisecs / 1000;
its.it_value.tv_nsec = (millisecs % 1000) * 1000000;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
break;
}
if (timerfd_settime(fd, 0, &its, NULL) == -1)
throw("Could not start timer");
uthread = std::thread(&CppTimer::worker,this);
}

/**
* Stops the timer by disarming it. It can be re-started
* with start().
**/
virtual void stop() {
if (!running) return;
running = false;
uthread.join();
}

/**
* Destructor disarms the timer, deletes it and
* disconnect the signal handler.
**/
virtual ~CppTimer() {
stop();
}

protected:
/**
* Abstract function which needs to be implemented by the children.
* This is called every time the timer fires.
**/
virtual void timerEvent() = 0;

private:
int fd = 0;
struct itimerspec its;
bool running = false;
std::thread uthread;
void worker() {
running = true;
while (running) {
uint64_t exp;
const long int s = read(fd, &exp, sizeof(uint64_t));
if (s != sizeof(uint64_t) ) {
running = false;
return;
}
timerEvent();
}
// disarm
struct itimerspec itsnew;
itsnew.it_value.tv_sec = 0;
itsnew.it_value.tv_nsec = 0;
itsnew.it_interval.tv_sec = 0;
itsnew.it_interval.tv_nsec = 0;
timerfd_settime(fd, 0, &itsnew, &its);
close(fd);
fd = -1;
}
};

#endif
13 changes: 0 additions & 13 deletions QwtExample.pro

This file was deleted.

38 changes: 17 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
QwtExample
===========
# QwtExample

A simple example program using Qt/Qwt widgets to be used as a base for students doing Raspberry Pi data acquisition.
A simple example program using QT and Qwt widgets.

Qt is a cross platform framework for developing graphical applications, for more information please visit the links below:
* [Qt Homepage](http://qt-project.org/)
* [Qt 4.8 Class List](http://qt-project.org/doc/qt-4.8/classes.html)
* [Wikpedia](http://en.wikipedia.org/wiki/Qt_%28framework%29)
* [Signals and Slots](http://qt-project.org/doc/qt-4.8/signalsandslots.html)
![alt tag](screenshot.png)

Qwt is a technical widget library based on Qt, please see:
* [Qwt Hompage](http://qwt.sourceforge.net/)
## Required packages

Install the QT5 and Qwt development packages:

Making it work
--------------
```
apt-get install qtdeclarative5-dev-tools qt5-qmake qt5-qmake-bin qtbase5-dev qtbase5-dev-tools
apt-get install libqwt-qt5-dev
```

To clone the git repository:
## Build it

git clone https://github.com/glasgow-bio/qwt-example

To build:

cd qwt-example
qmake
```
cmake .
make
```

To run (assuming you are logged into the RPi over ssh and no X-server is running):
## Run it

startx ./QwtExample
```
./qwt-example
```
20 changes: 20 additions & 0 deletions fakesensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __FAKESENSOR_H
#define __FAKESENSOR_H

#include "CppTimer.h"
#include <math.h>

class FakeSensor : public CppTimer {
public:
virtual void fakeSensorHasData(double inVal) = 0;
void timerEvent() {
const double inVal = gain * sin( M_PI * (float)(count++) / 50.0 );
fakeSensorHasData(inVal);
}
private:
int count = 0;
static constexpr double gain = 7.5;
};


#endif
8 changes: 3 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#include <window.h>
#include "window.h"

#include <QApplication>

// Main program
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

// create the window
Window window;
window.showMaximized();

// call the window.timerEvent function every 40 ms
window.startTimer(40);
window.show();

// execute the application
return app.exec();
Expand Down
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading