source: pacpusframework/trunk/src/DBITEPlayerLib/DbtPlyEngine.cpp@ 201

Last change on this file since 201 was 181, checked in by Marek Kurdej, 11 years ago

Minor fixes. Clean-up.

  • Property svn:executable set to *
File size: 9.2 KB
RevLine 
[89]1// %pacpus:license{
2// This file is part of the PACPUS framework distributed under the
3// CECILL-C License, Version 1.0.
4// %pacpus:license}
5/// @version $Id: DbtPlyEngine.cpp 76 2013-01-10 17:05:10Z kurdejma $
6
7#include <Pacpus/DbitePlayer/DbtPlyEngine.h>
8
9#include <Pacpus/kernel/ComponentManager.h>
10#include <Pacpus/kernel/Log.h>
11
12#include <cassert>
13#include <limits>
14#include <QDir>
15
16using namespace std;
17
18// Construction de la fabrique de composant DbtPlyEngine
19//static ComponentFactory<DbtPlyEngine> factory("DbtPlyEngine");
20
21namespace pacpus {
22
23DECLARE_STATIC_LOGGER("pacpus.core.DbtPlyEngine");
24
25typedef float SpeedType;
26static const SpeedType kInitialSpeed = 1.0;
27static const SpeedType kMinSpeed = 1.0 / 32;
28static const SpeedType kMaxSpeed = 32;
29
[181]30static const PlayMode kDefaultPlayMode = PlayModeLastData;
31
[89]32////////////////////////////////////////////////////////////////////////////////
33/// Constructor
34DbtPlyEngine::DbtPlyEngine(QString name)
35 : ComponentBase(name)
36 , mCurrentState(StoppedState::getInstance())
37 , mSpeed(kInitialSpeed)
38 , mIsReverse(false)
39{
40 LOG_TRACE("constructor");
41
42 sync_ = new QSemaphore(1);
43 sync_->acquire();
44 direction_ = 1;
45 tMinMaxSem_ = new QSemaphore(1);
46 tMinMaxSem_->release();
47 tDbtMin_ = numeric_limits<road_time_t>::max();
48 tDbtMax_ = 0;
[179]49
50 namespace po = boost::program_options;
51 addParameters()
52 ("datadir", po::value<string>(&mDataDir)->required(), "set root data directory")
[181]53 ("replay_mode", po::value<unsigned>(&reinterpret_cast<unsigned&>(replayMode_))->default_value(kDefaultPlayMode), "set data replay mode")
[179]54 ("log-config-file", po::value<string>(&mLoggerConfigFile), "set logger configuration file path")
55 ;
[89]56}
57
58////////////////////////////////////////////////////////////////////////////////
59/// Destructor.
60DbtPlyEngine::~DbtPlyEngine()
61{
62 LOG_TRACE("destructor");
63}
64
[152]65void DbtPlyEngine::addInputs()
66{
67 // empty: no inputs
68}
69
70void DbtPlyEngine::addOutputs()
71{
72 // empty: no outputs
73}
74
[89]75////////////////////////////////////////////////////////////////////////////////
76/// Returns the directory where the data are stored.
77/// @return QString::null if directory is invalid.
78QString DbtPlyEngine::getDataDir()
79{
[179]80 return mDataDir.c_str();
[89]81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// Slot.
85/// called when trigger reaches a new timer event
86void DbtPlyEngine::engReceiver()
87{
88 sync_->release();
89}
90
91////////////////////////////////////////////////////////////////////////////////
92/// @returns @b true if the engine is in the PLAY_STATE, @b false otherwise
93bool DbtPlyEngine::isPlaying()
94{
95 return mCurrentState->isPlaying();
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// main loop of the thread
100/// entry point for a new thread after the invocation of QThread::start()
101void DbtPlyEngine::run()
102{
103 LOG_DEBUG("run");
104
105 // We are waiting the first trigger signal to init some values
106 // before entering in the while loop
107 sync_->acquire();
108 lastTDbt_ = tDbtMin_;
109
110 // For statistics purpose
111 static const size_t kDeltaArraySize = 1000;
112 int deltaTDbtTab[kDeltaArraySize];
113 int i = 0;
114
[152]115 while (isActive()) {
[89]116 tNow_ = road_time();
117 float elapsedTime = tNow_ - lastTNow_;
118 int deltaTDbt = direction_ * elapsedTime * mSpeed;
119 tDbt_ = lastTDbt_ + deltaTDbt;
120 deltaTDbtTab[(i++) % kDeltaArraySize] = elapsedTime;
121
122 if ((tDbt_ <= tDbtMax_) && (tDbt_ >= tDbtMin_)) {
123 Q_EMIT play(tDbt_, tNow_, mIsReverse);
124 Q_EMIT curReplayTime(tDbt_);
125 lastTNow_ = tNow_ ;
126 lastTDbt_ = tDbt_ ;
127 } else {
128 lastTNow_ = tNow_ ;
129 lastTDbt_ = tDbt_ ;
130 LOG_INFO("Reading is finished.");
131
132 mCurrentState->stop(*this);
133 }
134
135 // The waiting is placed at the end due to the init before the while loop
136 sync_->acquire();
137 }
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Slot.
142/// change the direction of replaying : normal or reverse
143void DbtPlyEngine::changeDirection(bool reverse)
144{
145 if (reverse) {
146 direction_ = -1;
147 mIsReverse = true;
148 } else {
149 direction_ = +1;
150 mIsReverse = false;
151 }
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Configuration method of the engine
156/// called automatically by the component manager
[152]157ComponentBase::COMPONENT_CONFIGURATION DbtPlyEngine::configureComponent(XmlComponentConfig config)
[89]158{
[179]159 {
160 // data directory: add path separator at end if needed
161 QString dataDir = mDataDir.c_str();
162 dataDir = QDir::toNativeSeparators(dataDir);
163 if (!dataDir.endsWith(QDir::separator())) {
164 dataDir += QDir::separator();
165 mDataDir = dataDir.toStdString();
166 }
[89]167 }
168
169 ////////////////////////////////////////////////////////////////////////////////
170 // logger configuration
[179]171 if (!mLoggerConfigFile.empty()) {
172 LOG_INFO("configuring logger with file '" << mLoggerConfigFile << "'");
173 LogConfigurator::configureLoggerWithFile(mLoggerConfigFile.c_str());
[89]174 }
175
176 return ComponentBase::CONFIGURED_OK;
177}
178
179////////////////////////////////////////////////////////////////////////////////
180/// The start function of the engine
181void DbtPlyEngine::startActivity()
182{
183 LOG_INFO("Starting...");
184
[152]185 setActive(true);
[89]186 start();
187}
188
189////////////////////////////////////////////////////////////////////////////////
190/// The stop function of the engine
191void DbtPlyEngine::stopActivity()
192{
193 LOG_TRACE("stopping activity...");
194
[152]195 setActive(false);
[89]196}
197
198////////////////////////////////////////////////////////////////////////////////
199/// Slot.
200/// called by each file manager to provide min and max time of their data
201void DbtPlyEngine::tMinMax(road_time_t tMin, road_time_t tMax)
202{
203 tMinMaxSem_->acquire();
204 if (tDbtMin_>tMin) {
205 tDbtMin_= tMin;
206 }
207 if (tDbtMax_<tMax) {
208 tDbtMax_ = tMax;
209 }
210 Q_EMIT timeMinMax(tDbtMin_, tDbtMax_);
211 tMinMaxSem_->release();
212}
213
214////////////////////////////////////////////////////////////////////////////////
215/// Slot.
216/// called when the time slider is pressed
217void DbtPlyEngine::pauseEvent()
218{
219 LOG_DEBUG("Clicked: pause");
220 mCurrentState->pause(*this);
221}
222
223////////////////////////////////////////////////////////////////////////////////
224/// Slot.
225/// called when the time slider is released
226void DbtPlyEngine::playEvent()
227{
228 LOG_DEBUG("Clicked: play");
229
230 // get user interface
231 ComponentManager * mgr = ComponentManager::getInstance();
232 DbtPlyUserInterface * ui = dynamic_cast<DbtPlyUserInterface *>(mgr->getComponent("dbiteUserInterface"));
233 assert(ui);
234 // get time value from slider
235 lastTDbt_ = ui->getTime() + tDbtMin_;
236
237 mCurrentState->play(*this);
238}
239
240////////////////////////////////////////////////////////////////////////////////
241void DbtPlyEngine::stopEvent()
242{
243 LOG_DEBUG("Clicked: stop");
244 mCurrentState->stop(*this);
245}
246
247////////////////////////////////////////////////////////////////////////////////
248void DbtPlyEngine::speedUpEvent()
249{
250 LOG_DEBUG("Clicked: speed Up");
251 mCurrentState->speedUp(*this);
252}
253
254////////////////////////////////////////////////////////////////////////////////
255void DbtPlyEngine::speedDownEvent()
256{
257 LOG_DEBUG("Clicked: speed down");
258 mCurrentState->speedDown(*this);
259}
260
261////////////////////////////////////////////////////////////////////////////////
262const DbtPlyEngineState * DbtPlyEngine::getState()
263{
264 return mCurrentState;
265}
266
267////////////////////////////////////////////////////////////////////////////////
268void DbtPlyEngine::setState(DbtPlyEngineState * newState)
269{
270 assert(newState);
271 LOG_DEBUG(mCurrentState->toString() << " => " << newState->toString());
272 mCurrentState = newState;
273
274 Q_EMIT displayStateSig(mCurrentState, mSpeed);
275
276 LOG_DEBUG("tDbt = " << tDbt_ << "\tlastTDbt = " << lastTDbt_);
277 LOG_DEBUG("tNow = " << tNow_ << "\tlastTNow = " << lastTNow_);
278}
279
280void DbtPlyEngine::speedUp()
281{
282 mSpeed *= 2;
283 if (mSpeed > kMaxSpeed) {
284 mSpeed = kMaxSpeed;
285 }
286 assert(kMinSpeed <= mSpeed);
287 assert(mSpeed <= kMaxSpeed);
288 LOG_INFO("event: Speed Up, speed = " << mSpeed);
289 Q_EMIT displayStateSig(mCurrentState, mSpeed);
290}
291
292void DbtPlyEngine::speedDown()
293{
294 mSpeed /= 2;
295 if (mSpeed < kMinSpeed) {
296 mSpeed = kMinSpeed;
297 }
298 assert(kMinSpeed <= mSpeed);
299 assert(mSpeed <= kMaxSpeed);
300 LOG_INFO("event: Speed Up, speed = " << mSpeed);
301 Q_EMIT displayStateSig(mCurrentState, mSpeed);
302}
303
304void DbtPlyEngine::reset()
305{
306 lastTNow_ = road_time();
307 lastTDbt_ = tDbtMin_;
308 mSpeed = kInitialSpeed;
309 Q_EMIT stopfile();
310
311 // get user interface
312 ComponentManager * mgr = ComponentManager::getInstance();
313 DbtPlyUserInterface * ui = dynamic_cast<DbtPlyUserInterface *>(mgr->getComponent("dbiteUserInterface"));
314 assert(ui);
315 // reset time value of the slider
316 ui->resetTime();
317}
318
319} // namespace pacpus
Note: See TracBrowser for help on using the repository browser.