-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestScalar.h
114 lines (91 loc) · 2.02 KB
/
TestScalar.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once
#include <QPlot/KnightRiderWidget.h>
#include <QPlot/GradientSelector.h>
#include <QThread>
#include <QMutex>
#include <atomic>
namespace Ui
{
class TestScalar;
}
class OscillatorThread : public QThread
{
Q_OBJECT
public:
explicit OscillatorThread(QObject *parent = 0)
: QThread(parent)
{}
void oscillate(double min, double max)
{
QMutexLocker locker(&mutex_);
terminate_helper();
terminating_.store(false);
min_ = min;
max_ = max;
if (!isRunning())
start(HighPriority);
}
void terminate_wait()
{
QMutexLocker locker(&mutex_);
terminate_helper();
}
void set_wait_time(uint16_t time)
{
wait_ms_.store(time);
}
void set_period(double period)
{
period_.store(period);
}
signals:
void val_update(double val);
protected:
void run() {
QTime myTimer;
myTimer.start();
while (!terminating_.load())
{
int nMilliseconds = myTimer.elapsed();
double periods = double(nMilliseconds) / period_;
double remainder = periods - std::floor(periods);
double sinval = std::sin(periods * M_2_PI);
double val = (max_ - min_) * (1. + sinval) / 2. + min_;
emit val_update(val);
if (!terminating_.load())
QThread::msleep(wait_ms_.load());
}
}
private:
QMutex mutex_;
std::atomic<bool> terminating_ {false};
std::atomic<uint16_t> wait_ms_ {50};
double min_ {0.};
double max_ {100.};
std::atomic<double> period_ {1000.0};
void terminate_helper()
{
terminating_.store(true);
wait();
}
};
class TestScalar : public QWidget
{
Q_OBJECT
public:
TestScalar(QWidget* parent = 0);
protected:
void closeEvent(QCloseEvent*);
private slots:
void update();
void on_pushGradientSelector_clicked();
void on_pushStart_clicked();
void on_pushStop_clicked();
void on_doublePeriod_valueChanged(double);
void on_spinUpdateFrequency_valueChanged(int);
void val_update(double val);
private:
Ui::TestScalar* ui;
QPlot::Gradients g;
OscillatorThread oscillator_;
};