source: pacpusframework/branches/0.1.x/src/DBITEPlayerLib/DbtPlyEngine.cpp

Last change on this file was 152, checked in by Marek Kurdej, 11 years ago

Major update.
Renamed: addInput -> addInputs, addOutput -> addOutputs and made pure virtual (=0).
Transformed macro definitions into template methods: ADD_INPUT -> ComponentBase::addInput, ADD_OUTPUT -> ComponentBase::addOutput, GET_INPUT -> ComponentBase::getTypedInput, GET_OUTPUT -> ComponentBase::getTypedOutput.
Fixed: added public/protected set/get methods in ComponentBase, made member fields private.

  • Property svn:executable set to *
File size: 10.3 KB
Line 
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
25static const char * kPropertyDataDirectory = "datadir";
26
27static const char * kPropertyLoggerConfiguration = "log-config-file";
28
29static const char * kPropertyReplayMode = "replay_mode";
30static const char * kPropertyReplayModeLastData = "1";
31static const char * kPropertyReplayModeAllData = "2";
32
33typedef float SpeedType;
34static const SpeedType kInitialSpeed = 1.0;
35static const SpeedType kMinSpeed = 1.0 / 32;
36static const SpeedType kMaxSpeed = 32;
37
38////////////////////////////////////////////////////////////////////////////////
39/// Constructor
40DbtPlyEngine::DbtPlyEngine(QString name)
41 : ComponentBase(name)
42 , mCurrentState(StoppedState::getInstance())
43 , mSpeed(kInitialSpeed)
44 , mIsReverse(false)
45{
46 LOG_TRACE("constructor");
47
48 sync_ = new QSemaphore(1);
49 sync_->acquire();
50 direction_ = 1;
51 tMinMaxSem_ = new QSemaphore(1);
52 tMinMaxSem_->release();
53 tDbtMin_ = numeric_limits<road_time_t>::max();
54 tDbtMax_ = 0;
55 dataDir_ = QString::null;
56}
57
58////////////////////////////////////////////////////////////////////////////////
59/// Destructor.
60DbtPlyEngine::~DbtPlyEngine()
61{
62 LOG_TRACE("destructor");
63}
64
65void DbtPlyEngine::addInputs()
66{
67 // empty: no inputs
68}
69
70void DbtPlyEngine::addOutputs()
71{
72 // empty: no outputs
73}
74
75////////////////////////////////////////////////////////////////////////////////
76/// Returns the directory where the data are stored.
77/// @return QString::null if directory is invalid.
78QString DbtPlyEngine::getDataDir()
79{
80 return dataDir_;
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
115 while (isActive()) {
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
157ComponentBase::COMPONENT_CONFIGURATION DbtPlyEngine::configureComponent(XmlComponentConfig config)
158{
159 // datadir
160 dataDir_ = config.getProperty(kPropertyDataDirectory);
161 LOG_INFO("property " << kPropertyDataDirectory << "=\""
162 << dataDir_ << "\"");
163 if (dataDir_.isNull()) {
164 LOG_FATAL("The data directory '" << name() << "' is invalid or unavailable!");
165 }
166
167 dataDir_ = QDir::toNativeSeparators(dataDir_);
168 if (!dataDir_.endsWith(QDir::separator())) {
169 dataDir_ += QDir::separator();
170 }
171
172 ////////////////////////////////////////////////////////////////////////////////
173 // logger configuration
174 QString loggerConfig = config.getProperty(kPropertyLoggerConfiguration);
175 LOG_INFO("property " << kPropertyLoggerConfiguration << "=\""
176 << loggerConfig << "\"");
177 if (!loggerConfig.isNull()) {
178 LOG_INFO("configuring logger with file '" << loggerConfig << "'");
179 LogConfigurator::configureLoggerWithFile(loggerConfig.toStdString().c_str());
180 }
181
182 ////////////////////////////////////////////////////////////////////////////////
183 // Replay Mode
184 QString replayModeValue = config.getProperty(kPropertyReplayMode);
185 LOG_INFO("property " << kPropertyReplayMode << "=\""
186 << replayModeValue << "\"");
187 if (replayModeValue.isNull()) {
188 LOG_INFO("property " << kPropertyReplayMode << " unset."
189 << " Set to default = 1.");
190 replayMode_ = PlayModeLastData;
191 } else {
192 if (kPropertyReplayModeLastData == replayModeValue.toStdString()) {
193 replayMode_ = PlayModeLastData;
194 } else if (kPropertyReplayModeAllData == replayModeValue.toStdString()) {
195 replayMode_ = PlayModeAllData;
196 } else {
197 LOG_WARN("unknown " << kPropertyReplayMode << " '" << replayModeValue << "'."
198 << " Set to default = 1.");
199 replayMode_ = PlayModeLastData;
200 }
201 }
202
203 return ComponentBase::CONFIGURED_OK;
204}
205
206////////////////////////////////////////////////////////////////////////////////
207/// The start function of the engine
208void DbtPlyEngine::startActivity()
209{
210 LOG_INFO("Starting...");
211
212 setActive(true);
213 start();
214}
215
216////////////////////////////////////////////////////////////////////////////////
217/// The stop function of the engine
218void DbtPlyEngine::stopActivity()
219{
220 LOG_TRACE("stopping activity...");
221
222 setActive(false);
223}
224
225////////////////////////////////////////////////////////////////////////////////
226/// Slot.
227/// called by each file manager to provide min and max time of their data
228void DbtPlyEngine::tMinMax(road_time_t tMin, road_time_t tMax)
229{
230 tMinMaxSem_->acquire();
231 if (tDbtMin_>tMin) {
232 tDbtMin_= tMin;
233 }
234 if (tDbtMax_<tMax) {
235 tDbtMax_ = tMax;
236 }
237 Q_EMIT timeMinMax(tDbtMin_, tDbtMax_);
238 tMinMaxSem_->release();
239}
240
241////////////////////////////////////////////////////////////////////////////////
242/// Slot.
243/// called when the time slider is pressed
244void DbtPlyEngine::pauseEvent()
245{
246 LOG_DEBUG("Clicked: pause");
247 mCurrentState->pause(*this);
248}
249
250////////////////////////////////////////////////////////////////////////////////
251/// Slot.
252/// called when the time slider is released
253void DbtPlyEngine::playEvent()
254{
255 LOG_DEBUG("Clicked: play");
256
257 // get user interface
258 ComponentManager * mgr = ComponentManager::getInstance();
259 DbtPlyUserInterface * ui = dynamic_cast<DbtPlyUserInterface *>(mgr->getComponent("dbiteUserInterface"));
260 assert(ui);
261 // get time value from slider
262 lastTDbt_ = ui->getTime() + tDbtMin_;
263
264 mCurrentState->play(*this);
265}
266
267////////////////////////////////////////////////////////////////////////////////
268void DbtPlyEngine::stopEvent()
269{
270 LOG_DEBUG("Clicked: stop");
271 mCurrentState->stop(*this);
272}
273
274////////////////////////////////////////////////////////////////////////////////
275void DbtPlyEngine::speedUpEvent()
276{
277 LOG_DEBUG("Clicked: speed Up");
278 mCurrentState->speedUp(*this);
279}
280
281////////////////////////////////////////////////////////////////////////////////
282void DbtPlyEngine::speedDownEvent()
283{
284 LOG_DEBUG("Clicked: speed down");
285 mCurrentState->speedDown(*this);
286}
287
288////////////////////////////////////////////////////////////////////////////////
289const DbtPlyEngineState * DbtPlyEngine::getState()
290{
291 return mCurrentState;
292}
293
294////////////////////////////////////////////////////////////////////////////////
295void DbtPlyEngine::setState(DbtPlyEngineState * newState)
296{
297 assert(newState);
298 LOG_DEBUG(mCurrentState->toString() << " => " << newState->toString());
299 mCurrentState = newState;
300
301 Q_EMIT displayStateSig(mCurrentState, mSpeed);
302
303 LOG_DEBUG("tDbt = " << tDbt_ << "\tlastTDbt = " << lastTDbt_);
304 LOG_DEBUG("tNow = " << tNow_ << "\tlastTNow = " << lastTNow_);
305}
306
307void DbtPlyEngine::speedUp()
308{
309 mSpeed *= 2;
310 if (mSpeed > kMaxSpeed) {
311 mSpeed = kMaxSpeed;
312 }
313 assert(kMinSpeed <= mSpeed);
314 assert(mSpeed <= kMaxSpeed);
315 LOG_INFO("event: Speed Up, speed = " << mSpeed);
316 Q_EMIT displayStateSig(mCurrentState, mSpeed);
317}
318
319void DbtPlyEngine::speedDown()
320{
321 mSpeed /= 2;
322 if (mSpeed < kMinSpeed) {
323 mSpeed = kMinSpeed;
324 }
325 assert(kMinSpeed <= mSpeed);
326 assert(mSpeed <= kMaxSpeed);
327 LOG_INFO("event: Speed Up, speed = " << mSpeed);
328 Q_EMIT displayStateSig(mCurrentState, mSpeed);
329}
330
331void DbtPlyEngine::reset()
332{
333 lastTNow_ = road_time();
334 lastTDbt_ = tDbtMin_;
335 mSpeed = kInitialSpeed;
336 Q_EMIT stopfile();
337
338 // get user interface
339 ComponentManager * mgr = ComponentManager::getInstance();
340 DbtPlyUserInterface * ui = dynamic_cast<DbtPlyUserInterface *>(mgr->getComponent("dbiteUserInterface"));
341 assert(ui);
342 // reset time value of the slider
343 ui->resetTime();
344}
345
346} // namespace pacpus
Note: See TracBrowser for help on using the repository browser.