1 // Copyright (c) 2012 Dennco Project
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // Created by tkawata on 2/25/2012.
19 #include "qtdntimekeeperimpl.h"
24 class SleeperThread : public QThread
27 static void msleep(unsigned long msecs)
29 QThread::msleep(msecs);
33 DNTimeKeeperImpl *DNTimeKeeperImpl::create()
35 return new QtDNTimeKeeperImpl;
38 QtDNTimeKeeperImpl::QtDNTimeKeeperImpl() : mIntervalTime(1000),mAdjustTime(0),mPrevTime(0),mStartTime(0),mTickTime(0)
42 QtDNTimeKeeperImpl::~QtDNTimeKeeperImpl()
46 bool QtDNTimeKeeperImpl::setIntevalSec(float time)
48 mIntervalTime = time * 1000;
53 bool QtDNTimeKeeperImpl::sleepUntilNextInterval()
59 qint64 currentTime = QDateTime::currentMSecsSinceEpoch();
60 qint64 dt = currentTime - mPrevTime;
61 qint64 wt = mIntervalTime - dt + (int)mAdjustTime;
62 if (wt < 1) // wait at least 1ms.
66 SleeperThread::msleep(wt);
67 qint64 t = QDateTime::currentMSecsSinceEpoch();
69 mAdjustTime += mIntervalTime - (t - mPrevTime);
70 if (mAdjustTime < -mIntervalTime * 0.3)
72 mAdjustTime = -mIntervalTime * 0.3;
76 mTickTime = (mPrevTime - mStartTime)/1000.0;
81 float QtDNTimeKeeperImpl::getTickTime()
86 bool QtDNTimeKeeperImpl::start()
88 mStartTime = QDateTime::currentMSecsSinceEpoch();
89 mPrevTime = mStartTime;
95 bool QtDNTimeKeeperImpl::stop()