forked from OpenModelica/OpenModelica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qwt_sampling_thread.cpp
112 lines (95 loc) · 2.5 KB
/
qwt_sampling_thread.cpp
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
/******************************************************************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_sampling_thread.h"
#include <qelapsedtimer.h>
class QwtSamplingThread::PrivateData
{
public:
QElapsedTimer timer;
double msecsInterval;
};
//! Constructor
QwtSamplingThread::QwtSamplingThread( QObject* parent )
: QThread( parent )
{
m_data = new PrivateData;
m_data->msecsInterval = 1e3; // 1 second
}
//! Destructor
QwtSamplingThread::~QwtSamplingThread()
{
delete m_data;
}
/*!
Change the interval (in ms), when sample() is called.
The default interval is 1000.0 ( = 1s )
\param msecs Interval
\sa interval()
*/
void QwtSamplingThread::setInterval( double msecs )
{
if ( msecs < 0.0 )
msecs = 0.0;
m_data->msecsInterval = msecs;
}
/*!
\return Interval (in ms), between 2 calls of sample()
\sa setInterval()
*/
double QwtSamplingThread::interval() const
{
return m_data->msecsInterval;
}
/*!
\return Time (in ms) since the thread was started
\sa QThread::start(), run()
*/
double QwtSamplingThread::elapsed() const
{
if ( m_data->timer.isValid() )
return m_data->timer.nsecsElapsed() / 1e6;
return 0.0;
}
/*!
Terminate the collecting thread
\sa QThread::start(), run()
*/
void QwtSamplingThread::stop()
{
m_data->timer.invalidate();
}
/*!
Loop collecting samples started from QThread::start()
\sa stop()
*/
void QwtSamplingThread::run()
{
m_data->timer.start();
/*
We should have all values in nsecs/qint64, but
this would break existing code. TODO ...
Anyway - for QThread::usleep we even need microseconds( usecs )
*/
while ( m_data->timer.isValid() )
{
const qint64 timestamp = m_data->timer.nsecsElapsed();
sample( timestamp / 1e9 ); // seconds
if ( m_data->msecsInterval > 0.0 )
{
const double interval = m_data->msecsInterval * 1e3;
const double elapsed = ( m_data->timer.nsecsElapsed() - timestamp ) / 1e3;
const double usecs = interval - elapsed;
if ( usecs > 0.0 )
QThread::usleep( qRound( usecs ) );
}
}
}
#if QWT_MOC_INCLUDE
#include "moc_qwt_sampling_thread.cpp"
#endif