00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "CommonFramework.h"
00033
00034
00035
00036 #include <e32math.h>
00037
00038 void RandomDelay(TInt64& aSeed, TInt )
00039 {
00040
00041 if (aSeed==0)
00042 {
00043 TTime time;
00044 time.HomeTime();
00045 aSeed=time.Int64();
00046 }
00047
00048 TReal randomZeroToOne;
00049 randomZeroToOne = Math::FRand(aSeed);
00050 TReal realDelay;
00051 realDelay = randomZeroToOne * randomZeroToOne * 2000000;
00052 TInt32 intDelay;
00053 Math::Int(intDelay, realDelay);
00054 TTimeIntervalMicroSeconds32 delayMicroSeconds;
00055 delayMicroSeconds=intDelay;
00056 User::After(delayMicroSeconds);
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 class TAppRunner
00073 {
00074 public:
00075 TAppRunner();
00076 void NotifyFinished();
00077 void NotifyStarted();
00078
00079 TInt iActiveObjects;
00080 };
00081
00082 TAppRunner::TAppRunner()
00083 {
00084 iActiveObjects=0;
00085 }
00086
00087 void TAppRunner::NotifyStarted()
00088 {
00089 iActiveObjects++;
00090 }
00091
00092 void TAppRunner::NotifyFinished()
00093 {
00094 iActiveObjects--;
00095 if (iActiveObjects==0) CActiveScheduler::Stop();
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 class CPeriodicRunner : public CBase
00112 {
00113 public:
00114
00115 static CPeriodicRunner* NewL(TInt aTickInterval, TInt aTotalTicks, TAppRunner& aAppRunner);
00116 ~CPeriodicRunner();
00117 protected:
00118 CPeriodicRunner(TInt aTickInterval, TInt aTotalTicks, TAppRunner& aAppRunner);
00119 private:
00120 void ConstructL();
00121
00122 static TInt Tick(TAny* aObject);
00123 void DoTick();
00124 private:
00125
00126 TAppRunner& iAppRunner;
00127 TInt iTotalTicks;
00128 TInt iTickInterval;
00129
00130
00131 TTime iStartTime;
00132 CPeriodic* iPeriodic;
00133
00134
00135 TInt iTicksLeft;
00136 TInt iTimerNumber;
00137
00138
00139 TInt64 iDelaySeed;
00140 };
00141
00142
00143 CPeriodicRunner::CPeriodicRunner(TInt aTickInterval, TInt aTotalTicks, TAppRunner& aAppRunner)
00144 : iAppRunner(aAppRunner), iTotalTicks(aTotalTicks), iTickInterval(aTickInterval)
00145 {}
00146
00147
00148 void CPeriodicRunner::ConstructL()
00149 {
00150 iStartTime.HomeTime();
00151 iPeriodic =CPeriodic::NewL(0);
00152 iAppRunner.NotifyStarted();
00153 iTimerNumber = iAppRunner.iActiveObjects;
00154 iTicksLeft = iTotalTicks;
00155
00156 iPeriodic->Start(iTickInterval,iTickInterval,TCallBack(Tick, this));
00157 }
00158
00159
00160 CPeriodicRunner* CPeriodicRunner::NewL(TInt aTickInterval, TInt aTotalTicks, TAppRunner& aAppRunner)
00161 {
00162 CPeriodicRunner* self=new (ELeave) CPeriodicRunner(aTickInterval, aTotalTicks, aAppRunner);
00163 CleanupStack::PushL(self);
00164 self->ConstructL();
00165 CleanupStack::Pop();
00166 return self;
00167 }
00168
00169
00170 CPeriodicRunner::~CPeriodicRunner()
00171 {
00172 TTimeIntervalMicroSeconds elapsedTime;
00173 TTime currentTime;
00174
00175 currentTime.HomeTime();
00176
00177 elapsedTime = currentTime.MicroSecondsFrom(iStartTime);
00178
00179 _LIT(KFormat1,"Periodic timer %d finished after: %Ld microseconds for %d %dus ticks\n");
00180 console->Printf(KFormat1,iTimerNumber,elapsedTime.Int64(),iTotalTicks,iTickInterval);
00181
00182
00183
00184
00185 iPeriodic->Cancel();
00186 delete iPeriodic;
00187
00188 iAppRunner.NotifyFinished();
00189 }
00190
00191
00192 TInt CPeriodicRunner::Tick(TAny* aObject)
00193 {
00194 ((CPeriodicRunner*)aObject)->DoTick();
00195 return 1;
00196 }
00197
00198
00199 void CPeriodicRunner::DoTick()
00200 {
00201 iTicksLeft--;
00202 _LIT(KFormat2,"Periodic timer %d: %d ticks done\n");
00203 console->Printf(KFormat2, iTimerNumber, iTotalTicks - iTicksLeft);
00204 if(iTicksLeft==0)
00205 {
00206 delete this;
00207 }
00208 RandomDelay(iDelaySeed,iTimerNumber);
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 class CHeartbeatRunner : public CBase, public MBeating
00229 {
00230 public:
00231
00232 static CHeartbeatRunner* NewL(TInt aTickInterval, TInt aTotalTicks, TAppRunner& aAppRunner);
00233 ~CHeartbeatRunner();
00234 protected:
00235 CHeartbeatRunner(TInt aTickInterval, TInt aTotalTicks, TAppRunner& aAppRunner);
00236 private:
00237 void ConstructL();
00238
00239 void Beat();
00240 void Synchronize();
00241 private:
00242
00243 TAppRunner& iAppRunner;
00244 TInt iTotalTicks;
00245 TInt iTickInterval;
00246
00247 TTime iStartTime;
00248 CHeartbeat* iHeartbeat;
00249
00250 TInt iTicksLeft;
00251 TInt iTimerNumber;
00252
00253 TInt64 iDelaySeed;
00254 };
00255
00256
00257 CHeartbeatRunner::CHeartbeatRunner(TInt aTickInterval, TInt aTotalTicks, TAppRunner& aAppRunner)
00258 :iAppRunner(aAppRunner), iTotalTicks(aTotalTicks), iTickInterval(aTickInterval)
00259 {}
00260
00261
00262 void CHeartbeatRunner::ConstructL()
00263 {
00264 iStartTime.HomeTime();
00265 iHeartbeat=CHeartbeat::NewL(0);
00266 iAppRunner.NotifyStarted();
00267 iTimerNumber = iAppRunner.iActiveObjects;
00268 iTicksLeft = iTotalTicks;
00269
00270 iHeartbeat->Start(ETwelveOClock,this);
00271 }
00272
00273
00274 CHeartbeatRunner* CHeartbeatRunner::NewL(TInt aTickInterval, TInt aTotalTicks, TAppRunner& aAppRunner)
00275 {
00276 CHeartbeatRunner* self=new (ELeave) CHeartbeatRunner(aTickInterval, aTotalTicks, aAppRunner);
00277 CleanupStack::PushL(self);
00278 self->ConstructL();
00279 CleanupStack::Pop();
00280 return self;
00281 }
00282
00283
00284 CHeartbeatRunner::~CHeartbeatRunner()
00285 {
00286 TTimeIntervalMicroSeconds elapsedTime;
00287 TTime currentTime;
00288 currentTime.HomeTime();
00289
00290 elapsedTime = currentTime.MicroSecondsFrom(iStartTime);
00291
00292 _LIT(KFormat3,"Heartbeat timer %d finished after: %Ld microseonds for %d %dus ticks\n");
00293 console->Printf(KFormat3,iTimerNumber,elapsedTime.Int64(),iTotalTicks,iTickInterval);
00294
00295
00296 iHeartbeat->Cancel();
00297 delete iHeartbeat;
00298
00299 iAppRunner.NotifyFinished();
00300 }
00301
00302
00303 void CHeartbeatRunner::Beat()
00304 {
00305 iTicksLeft--;
00306 if(iTicksLeft<=0)
00307 delete this;
00308 else
00309 RandomDelay(iDelaySeed,iTimerNumber);
00310 }
00311
00312
00313 void CHeartbeatRunner::Synchronize()
00314 {
00315 TInt ticksMissed = 0;
00316
00317 TTime desiredTime = iStartTime + TTimeIntervalMicroSeconds((iTotalTicks - iTicksLeft) * iTickInterval);
00318 TTime currentTime;
00319 currentTime.HomeTime();
00320 TTimeIntervalMicroSeconds missedTime = currentTime.MicroSecondsFrom(desiredTime);
00321
00322 TInt64 missedTimeInt = missedTime.Int64();
00323
00324 ticksMissed = (missedTimeInt / iTickInterval);
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334 _LIT(KFormat4,"Ticks done %d\n");
00335 console->Printf(KFormat4, (iTotalTicks -iTicksLeft));
00336
00337 iTicksLeft = iTicksLeft - ticksMissed;
00338 TTimeIntervalMicroSeconds elapsedTime;
00339 elapsedTime = currentTime.MicroSecondsFrom(iStartTime);
00340 _LIT(KFormat5,"Elapsed time: %Ld microseconds\n");
00341 console->Printf(KFormat5, elapsedTime.Int64());
00342
00343 _LIT(KFormat6,"Synchronize heartbeat timer %d: ticks missed %d: left %d: done now %d\n");
00344 console->Printf(KFormat6,
00345 iTimerNumber,
00346 ticksMissed,
00347 iTicksLeft,
00348 ((iTotalTicks - iTicksLeft) <= iTotalTicks) ? iTotalTicks - iTicksLeft : iTotalTicks
00349 );
00350
00351 if(iTicksLeft<=0)
00352 {
00353 delete this;
00354 }
00355 }
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 void doExampleL()
00367 {
00368
00369 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
00370
00371
00372 CleanupStack::PushL(scheduler);
00373
00374
00375 CActiveScheduler::Install(scheduler);
00376
00377
00378 TAppRunner appRunner;
00379
00380
00381 TInt TickInterval = 1000000;
00382
00383
00384
00385
00386 for (TInt total_ticks=4; total_ticks<=6; total_ticks+=2)
00387 {
00388
00389
00390
00391 CPeriodicRunner::NewL(TickInterval, total_ticks, appRunner);
00392
00393
00394
00395
00396 CHeartbeatRunner::NewL(TickInterval, total_ticks, appRunner);
00397 }
00398
00399 CActiveScheduler::Start();
00400 CleanupStack::PopAndDestroy();
00401 }
00402