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
00033
00034
00035 #include "CommonFramework.h"
00036
00037
00038 enum
00039 {
00040 EPanicAlreadyActive=1000,
00041 };
00042
00044
00045
00046
00048 class CTimedMessenger : public CTimer
00049 {
00050 public:
00051
00052 CTimedMessenger();
00053
00054 ~CTimedMessenger();
00055
00056 public:
00057
00058 static CTimedMessenger* NewLC(const TDesC& aGreeting,
00059 TInt aTicksRequested,
00060 TInt aTicksInterval
00061 );
00062 static CTimedMessenger* NewL(const TDesC& aGreeting,
00063 TInt aTicksRequested,
00064 TInt aTicksInterval
00065 );
00066
00067 public:
00068
00069 void ConstructL(const TDesC& aGreeting,
00070 TInt aTicksRequested,
00071 TInt aTicksInterval
00072 );
00073
00074
00075 void IssueRequest();
00076
00077
00078
00079
00080 void DoCancel();
00081
00082
00083
00084
00085 void RunL();
00086
00087 public:
00088
00089 TBufC<20> iGreeting;
00090 TInt iTicksRequested;
00091
00092 TInt iTicksInterval;
00093 TInt iTicksDone;
00094 };
00095
00096
00098
00099
00100
00102 class CExampleScheduler : public CActiveScheduler
00103 {
00104 public:
00105 void Error (TInt aError) const;
00106 };
00107
00108
00110
00111
00112
00113
00114
00116 class CActiveConsole : public CActive
00117 {
00118 public:
00119
00120 CActiveConsole(CConsoleBase* aConsole);
00121 void ConstructL();
00122
00123
00124 ~CActiveConsole();
00125
00126
00127 void RequestCharacter();
00128
00129
00130
00131
00132 void DoCancel();
00133
00134
00135
00136
00137 void RunL();
00138
00139
00140
00141 virtual void ProcessKeyPress(TChar aChar) = 0;
00142
00143 protected:
00144
00145 CConsoleBase* iConsole;
00146 };
00147
00148
00150
00151
00152
00153
00154
00155
00156
00158 class CWriteKeyProcessor : public CActiveConsole
00159 {
00160 public:
00161
00162 CWriteKeyProcessor(CConsoleBase* aConsole);
00163
00164 public:
00165
00166 static CWriteKeyProcessor *NewLC (CConsoleBase* aConsole);
00167 static CWriteKeyProcessor *NewL(CConsoleBase* aConsole);
00168
00169 public:
00170
00171 void ProcessKeyPress(TChar aChar);
00172 };
00173
00174
00176
00177
00178
00180 CTimedMessenger::CTimedMessenger()
00181 : CTimer(CActive::EPriorityStandard)
00182
00183 {};
00184
00185 CTimedMessenger* CTimedMessenger::NewLC(const TDesC& aGreeting,
00186 TInt aTicksRequested,
00187 TInt aTicksInterval
00188 )
00189 {
00190 CTimedMessenger* self=new (ELeave) CTimedMessenger;
00191 CleanupStack::PushL(self);
00192 self->ConstructL(aGreeting,aTicksRequested,aTicksInterval);
00193 return self;
00194 }
00195
00196 CTimedMessenger* CTimedMessenger::NewL(const TDesC& aGreeting,
00197 TInt aTicksRequested,
00198 TInt aTicksInterval
00199 )
00200 {
00201 CTimedMessenger* self = NewLC(aGreeting,aTicksRequested,aTicksInterval);
00202 CleanupStack::Pop();
00203 return self;
00204 }
00205
00206 void CTimedMessenger::ConstructL(const TDesC& aGreeting,
00207 TInt aTicksRequested,
00208 TInt aTicksInterval
00209 )
00210 {
00211
00212 CTimer::ConstructL();
00213
00214 iGreeting = aGreeting;
00215 iTicksRequested = aTicksRequested;
00216 iTicksInterval = aTicksInterval;
00217
00218 CActiveScheduler::Add(this);
00219 }
00220
00221
00222 CTimedMessenger::~CTimedMessenger()
00223 {
00224
00225 Cancel();
00226 }
00227
00228 void CTimedMessenger::DoCancel()
00229 {
00230
00231 CTimer::DoCancel();
00232
00233 iTicksDone = 0;
00234
00235 _LIT(KMsgCancelled,"Outstanding Messenger request cancelled\n");
00236 console->Printf(KMsgCancelled);
00237 }
00238
00239 void CTimedMessenger::IssueRequest()
00240 {
00241
00242 _LIT(KMsgAlreadyActive,"Is already Active");
00243 __ASSERT_ALWAYS(!IsActive(),User::Panic(KMsgAlreadyActive,EPanicAlreadyActive));
00244
00245 CTimer::After( iTicksInterval*1000000);
00246 }
00247
00248 void CTimedMessenger::RunL()
00249 {
00250
00251
00252 iTicksDone++;
00253
00254 _LIT(KFormatString1,"%S \n");
00255 console->Printf(KFormatString1,&iGreeting);
00256
00257 if (iTicksDone < iTicksRequested)
00258 {
00259 IssueRequest();
00260 }
00261 else
00262 {
00263 _LIT(KMsgFinished,"Messenger finished \n");
00264 console->Printf(KMsgFinished);
00265
00266 iTicksDone=0;
00267
00268 CActiveScheduler::Stop();
00269 }
00270 }
00271
00272
00274
00275
00276
00278 void CExampleScheduler::Error(TInt aError) const
00279 {
00280 _LIT(KMsgSchedErr,"CExampleScheduler-error");
00281 User::Panic(KMsgSchedErr,aError);
00282 }
00283
00284
00286
00287
00288
00290 CActiveConsole::CActiveConsole( CConsoleBase* aConsole)
00291 : CActive(CActive::EPriorityUserInput)
00292
00293 {
00294 iConsole = aConsole;
00295 }
00296
00297 void CActiveConsole::ConstructL()
00298 {
00299
00300 CActiveScheduler::Add(this);
00301 }
00302
00303 CActiveConsole::~CActiveConsole()
00304 {
00305
00306 Cancel();
00307 }
00308
00309 void CActiveConsole::DoCancel()
00310 {
00311 iConsole->ReadCancel();
00312 }
00313
00314 void CActiveConsole::RunL()
00315 {
00316
00317 ProcessKeyPress(TChar(iConsole->KeyCode()));
00318 }
00319
00320 void CActiveConsole::RequestCharacter()
00321 {
00322
00323
00324 iConsole->Read(iStatus);
00325 SetActive();
00326 }
00327
00328
00330
00331
00332
00334 CWriteKeyProcessor::CWriteKeyProcessor(CConsoleBase* aConsole)
00335 : CActiveConsole(aConsole)
00336
00337 {};
00338
00339 CWriteKeyProcessor* CWriteKeyProcessor::NewLC(CConsoleBase* aConsole)
00340 {
00341 CWriteKeyProcessor* self=new (ELeave) CWriteKeyProcessor(aConsole);
00342 CleanupStack::PushL(self);
00343 self->ConstructL();
00344 return self;
00345 }
00346
00347 CWriteKeyProcessor* CWriteKeyProcessor::NewL(CConsoleBase* aConsole)
00348 {
00349 CWriteKeyProcessor* self=NewLC(aConsole);
00350 CleanupStack::Pop();
00351 return self;
00352 }
00353
00354 void CWriteKeyProcessor::ProcessKeyPress(TChar aChar)
00355 {
00356
00357 _LIT(KTextEsc,"\n");
00358 if (aChar == EKeyEscape)
00359 {
00360 iConsole->Printf(KTextEsc);
00361 CActiveScheduler::Stop();
00362 return;
00363 }
00364
00365
00366
00367
00368 if (aChar == EKeyEnter)
00369 iConsole->Printf(KTextEsc);
00370 else
00371 {
00372 _LIT(KFormatString2,"%c");
00373 _LIT(KFormatString3,"%d");
00374 if (aChar.IsAlphaDigit()|| aChar.IsSpace())
00375 iConsole->Printf(KFormatString2,TUint(aChar));
00376 else
00377 iConsole->Printf(KFormatString3,TUint(aChar));
00378 }
00379
00380
00381 RequestCharacter();
00382 }
00383
00384
00386
00387
00388
00390 LOCAL_C void doExampleL()
00391 {
00392
00393 CExampleScheduler* exampleScheduler = new (ELeave) CExampleScheduler;
00394
00395
00396 CleanupStack::PushL(exampleScheduler);
00397
00398
00399 CActiveScheduler::Install(exampleScheduler);
00400
00401
00402 _LIT(KTitleMsg,"A single CWriteKeyProcessor active object.\nIt accepts and prints keyboard input to the console.\nPress ESC to end.\n\n");
00403 console->Printf(KTitleMsg);
00404 CWriteKeyProcessor* keyProcesser = CWriteKeyProcessor::NewLC(console);
00405
00406
00407 keyProcesser->RequestCharacter();
00408
00409
00410
00411 CActiveScheduler::Start();
00412
00413
00414
00415
00416 CleanupStack::PopAndDestroy(2);
00417 }
00418