| 1 | /******************************************************************************
|
|---|
| 2 | * Qwt Widget Library
|
|---|
| 3 | * Copyright (C) 1997 Josef Wilgen
|
|---|
| 4 | * Copyright (C) 2002 Uwe Rathmann
|
|---|
| 5 | *
|
|---|
| 6 | * This library is free software; you can redistribute it and/or
|
|---|
| 7 | * modify it under the terms of the Qwt License, Version 1.0
|
|---|
| 8 | *****************************************************************************/
|
|---|
| 9 |
|
|---|
| 10 | #include "qwt_sampling_thread.h"
|
|---|
| 11 | #include <qelapsedtimer.h>
|
|---|
| 12 |
|
|---|
| 13 | class QwtSamplingThread::PrivateData
|
|---|
| 14 | {
|
|---|
| 15 | public:
|
|---|
| 16 | QElapsedTimer timer;
|
|---|
| 17 | double msecsInterval;
|
|---|
| 18 | };
|
|---|
| 19 |
|
|---|
| 20 | //! Constructor
|
|---|
| 21 | QwtSamplingThread::QwtSamplingThread( QObject* parent )
|
|---|
| 22 | : QThread( parent )
|
|---|
| 23 | {
|
|---|
| 24 | m_data = new PrivateData;
|
|---|
| 25 | m_data->msecsInterval = 1e3; // 1 second
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | //! Destructor
|
|---|
| 29 | QwtSamplingThread::~QwtSamplingThread()
|
|---|
| 30 | {
|
|---|
| 31 | delete m_data;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /*!
|
|---|
| 35 | Change the interval (in ms), when sample() is called.
|
|---|
| 36 | The default interval is 1000.0 ( = 1s )
|
|---|
| 37 |
|
|---|
| 38 | \param msecs Interval
|
|---|
| 39 | \sa interval()
|
|---|
| 40 | */
|
|---|
| 41 | void QwtSamplingThread::setInterval( double msecs )
|
|---|
| 42 | {
|
|---|
| 43 | if ( msecs < 0.0 )
|
|---|
| 44 | msecs = 0.0;
|
|---|
| 45 |
|
|---|
| 46 | m_data->msecsInterval = msecs;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | /*!
|
|---|
| 50 | \return Interval (in ms), between 2 calls of sample()
|
|---|
| 51 | \sa setInterval()
|
|---|
| 52 | */
|
|---|
| 53 | double QwtSamplingThread::interval() const
|
|---|
| 54 | {
|
|---|
| 55 | return m_data->msecsInterval;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /*!
|
|---|
| 59 | \return Time (in ms) since the thread was started
|
|---|
| 60 | \sa QThread::start(), run()
|
|---|
| 61 | */
|
|---|
| 62 | double QwtSamplingThread::elapsed() const
|
|---|
| 63 | {
|
|---|
| 64 | if ( m_data->timer.isValid() )
|
|---|
| 65 | return m_data->timer.nsecsElapsed() / 1e6;
|
|---|
| 66 |
|
|---|
| 67 | return 0.0;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /*!
|
|---|
| 71 | Terminate the collecting thread
|
|---|
| 72 | \sa QThread::start(), run()
|
|---|
| 73 | */
|
|---|
| 74 | void QwtSamplingThread::stop()
|
|---|
| 75 | {
|
|---|
| 76 | m_data->timer.invalidate();
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /*!
|
|---|
| 80 | Loop collecting samples started from QThread::start()
|
|---|
| 81 | \sa stop()
|
|---|
| 82 | */
|
|---|
| 83 | void QwtSamplingThread::run()
|
|---|
| 84 | {
|
|---|
| 85 | m_data->timer.start();
|
|---|
| 86 |
|
|---|
| 87 | /*
|
|---|
| 88 | We should have all values in nsecs/qint64, but
|
|---|
| 89 | this would break existing code. TODO ...
|
|---|
| 90 | Anyway - for QThread::usleep we even need microseconds( usecs )
|
|---|
| 91 | */
|
|---|
| 92 | while ( m_data->timer.isValid() )
|
|---|
| 93 | {
|
|---|
| 94 | const qint64 timestamp = m_data->timer.nsecsElapsed();
|
|---|
| 95 | sample( timestamp / 1e9 ); // seconds
|
|---|
| 96 |
|
|---|
| 97 | if ( m_data->msecsInterval > 0.0 )
|
|---|
| 98 | {
|
|---|
| 99 | const double interval = m_data->msecsInterval * 1e3;
|
|---|
| 100 | const double elapsed = ( m_data->timer.nsecsElapsed() - timestamp ) / 1e3;
|
|---|
| 101 |
|
|---|
| 102 | const double usecs = interval - elapsed;
|
|---|
| 103 |
|
|---|
| 104 | if ( usecs > 0.0 )
|
|---|
| 105 | QThread::usleep( qRound( usecs ) );
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | #include "moc_qwt_sampling_thread.cpp"
|
|---|