00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <e32base.h>
00025 #include <e32cons.h>
00026 #include <e32std.h>
00027
00028
00029 LOCAL_D CConsoleBase* console;
00030
00031 _LIT(KTxtMainInstructions,"\n\nPress 'F' to start\n 'ESC' to exit.\n");
00032
00034
00035
00036
00037
00038
00040
00041 class CActiveConsole : public CActive
00042 {
00043 public:
00044
00045 CActiveConsole(CConsoleBase* aConsole);
00046 void ConstructL();
00047
00048
00049 ~CActiveConsole();
00050
00051
00052 void RequestCharacter();
00053
00054
00055
00056
00057 void DoCancel();
00058
00059
00060
00061
00062 void RunL();
00063
00064
00065
00066 virtual void ProcessKeyPress(TChar aChar) = 0;
00067
00068 protected:
00069
00070 CConsoleBase* iConsole;
00071 };
00072
00073
00075
00076
00077
00079
00080 class CExampleScheduler : public CActiveScheduler
00081 {
00082 public:
00083 void Error (TInt aError) const;
00084 void WaitForAnyRequest();
00085 void SetActiveObject(CActiveConsole* aActiveConsole);
00086 private:
00087
00088 CActiveConsole* iActiveConsole;
00089 };
00090
00091
00093
00094
00095
00096
00097
00099
00100 class TFibonacciEngine
00101 {
00102 public:
00103 void Calculate (TInt aTerms) ;
00104
00105 TInt iResult ;
00106 } ;
00107
00108
00110
00111
00112
00113
00114
00116
00117 class CFibonacciApplication : public CActiveConsole
00118 {
00119 public:
00120 CFibonacciApplication(CConsoleBase* aConsole) ;
00121 void ConstructL();
00122
00123
00124 static CFibonacciApplication* NewLC(CConsoleBase* aConsole) ;
00125
00126
00127 void ProcessKeyPress(TChar aChar) ;
00128
00129 private:
00130 CConsoleBase* iConsole ;
00131 TFibonacciEngine iFibonacciEngine ;
00132 };
00133
00134
00136
00137
00138
00140
00141 CActiveConsole::CActiveConsole( CConsoleBase* aConsole)
00142 : CActive(CActive::EPriorityUserInput)
00143
00144 {
00145 iConsole = aConsole;
00146 __DECLARE_NAME(_S("CActiveConsole"));
00147
00148 }
00149
00150 void CActiveConsole::ConstructL()
00151 {
00152
00153 CActiveScheduler::Add(this);
00154 }
00155
00156 CActiveConsole::~CActiveConsole()
00157 {
00158
00159 Cancel();
00160 }
00161
00162 void CActiveConsole::DoCancel()
00163 {
00164 iConsole->ReadCancel();
00165 }
00166
00167 void CActiveConsole::RunL()
00168 {
00169
00170 ProcessKeyPress(TChar(iConsole->KeyCode()));
00171 }
00172
00173 void CActiveConsole::RequestCharacter()
00174 {
00175
00176
00177 iConsole->Read(iStatus);
00178 SetActive();
00179 }
00180
00181
00183
00184
00185
00187
00188 void CExampleScheduler::Error(TInt aError) const
00189 {
00190 _LIT(KTxtSchedulerError,"CExampleScheduler - error");
00191 User::Panic(KTxtSchedulerError,aError);
00192 }
00193
00194 void CExampleScheduler::WaitForAnyRequest()
00195 {
00196 if (!(iActiveConsole->IsActive()))
00197 iActiveConsole->RequestCharacter();
00198 CActiveScheduler::WaitForAnyRequest();
00199 }
00200
00201 void CExampleScheduler::SetActiveObject(CActiveConsole* aActiveConsole)
00202 {
00203 iActiveConsole = aActiveConsole;
00204 }
00205
00206
00208
00209
00211
00212 TInt GetValueFromKeyboard (TInt aInitial, TInt aStep, TInt lowerLimit, TInt upperLimit, const TDesC& aPrompt, CConsoleBase* aConsole)
00213 {
00214 TChar input ;
00215 TInt value = aInitial ;
00216
00217 aConsole->Printf(aPrompt) ;
00218 do
00219 {
00220 aConsole->SetPos(0);
00221 _LIT(KFormat1,"%d ");
00222 aConsole->Printf(KFormat1, value);
00223 input = aConsole->Getch() ;
00224 if (input == EKeyUpArrow && value < upperLimit) value = value + aStep ;
00225 if (input == EKeyDownArrow && value > lowerLimit) value = value - aStep ;
00226 if (input == 'c') value = -1;
00227 }
00228 while (input != EKeyEnter && input != 'c') ;
00229
00230 return value ;
00231 }
00232
00233
00235
00236
00237
00239
00240 CFibonacciApplication::CFibonacciApplication(CConsoleBase* aConsole )
00241 : CActiveConsole(aConsole)
00242
00243 {
00244 iConsole = aConsole ;
00245 __DECLARE_NAME(_S("CFibonacciApplication"));
00246 }
00247
00248
00249 CFibonacciApplication* CFibonacciApplication::NewLC(CConsoleBase* aConsole )
00250 {
00251 CFibonacciApplication* self=new (ELeave) CFibonacciApplication(aConsole);
00252 CleanupStack::PushL(self);
00253 self->ConstructL();
00254 return self;
00255 }
00256
00257
00258 void CFibonacciApplication::ConstructL()
00259 {
00260
00261 CActiveScheduler::Add(this);
00262
00263 ((CExampleScheduler*)(CActiveScheduler::Current()))->SetActiveObject(this);
00264 }
00265
00266
00267 void CFibonacciApplication::ProcessKeyPress(TChar aChar)
00268 {
00269
00270
00271
00272 if (aChar == EKeyEscape)
00273 {
00274 CActiveScheduler::Stop();
00275 return;
00276 }
00277
00278
00279
00280
00281 if (aChar == 'f' || aChar == 'F')
00282 {
00283 _LIT(KTxtStartingFibonacci,"\nStarting Fibonacci.... \n");
00284 _LIT(KTxtReturnTermNumber,"\nENTER selects number, and starts calculation\nUP arrow increases num\nDOWN arrow decreases num\nC cancels\n\n");
00285 _LIT(KTxtCalculating,"\nCalculating...\n");
00286 _LIT(KFormat2,"\nResult : %u \n");
00287 _LIT(KTxtCancelled,"Cancelled");
00288
00289 iConsole->Printf(KTxtStartingFibonacci);
00290
00291 TInt iterations = GetValueFromKeyboard(5,1,2,46, KTxtReturnTermNumber, iConsole) ;
00292 if (iterations == -1)
00293 {
00294 iConsole->Printf(KTxtCancelled);
00295 iConsole->Printf(KTxtMainInstructions);
00296 return;
00297 }
00298
00299 iConsole->Printf(KTxtCalculating);
00300 iFibonacciEngine.Calculate(iterations);
00301
00302 iConsole->Printf(KFormat2, iFibonacciEngine.iResult) ;
00303 iConsole->Printf(KTxtMainInstructions);
00304 return;
00305 }
00306
00307
00308 _LIT(KTxtNotRecognised,"\nUnwanted key pressed");
00309 iConsole->Printf(KTxtNotRecognised);
00310 iConsole->Printf(KTxtMainInstructions);
00311 }
00312
00314
00315
00316
00318
00319 void TFibonacciEngine::Calculate (TInt aTerms)
00320 {
00321 TInt iterations = aTerms ;
00322
00323 TInt currentTotal = 1 ;
00324 TInt previousTotal = 0 ;
00325 _LIT(KTxtTooManyIterations,"Too many iterations");
00326 __ASSERT_ALWAYS(iterations<47,User::Panic(KTxtTooManyIterations,iterations));
00327
00328
00329 while (iterations-- > 0)
00330 {
00331
00332 TInt newTotal = currentTotal + previousTotal ;
00333
00334
00335 previousTotal = currentTotal ;
00336 currentTotal = newTotal ;
00337
00338
00339 User::After(1000000) ;
00340 }
00341
00342 iResult = currentTotal ;
00343
00344 }
00345
00346
00348
00350
00351 void doExampleL () ;
00352
00353 void SetupConsoleL();
00354
00355 GLDEF_C TInt E32Main()
00356 {
00357 CTrapCleanup* cleanup=CTrapCleanup::New();
00358 TRAPD(error,SetupConsoleL());
00359 _LIT(KTxtFibonacciExampleError,"Fibonacci example error");
00360 __ASSERT_ALWAYS(!error,User::Panic(KTxtFibonacciExampleError,error));
00361 delete cleanup;
00362 return 0;
00363 }
00364
00365 void SetupConsoleL()
00366 {
00367 _LIT(KTxtFibSingActObj,"Single Active Object");
00368 console=Console::NewL(KTxtFibSingActObj, TSize(KConsFullScreen,KConsFullScreen));
00369 CleanupStack::PushL(console);
00370
00371 console->Printf(KTxtMainInstructions) ;
00372 TRAPD(error, doExampleL());
00373 if (error)
00374 {
00375 _LIT(KFormat3,"failed: leave code=%d");
00376 console->Printf(KFormat3, error);
00377 }
00378 _LIT(KTxtPressAnyKey,"[Press any key to exit]");
00379 console->Printf(KTxtPressAnyKey);
00380 console->Getch();
00381 CleanupStack::PopAndDestroy();
00382 }
00383
00384
00386
00387
00388
00390 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 CFibonacciApplication* fibonacci = CFibonacciApplication::NewLC(console);
00403
00404
00405 fibonacci->RequestCharacter() ;
00406
00407
00408
00409 CActiveScheduler::Start();
00410
00411
00412
00413
00414 CleanupStack::PopAndDestroy();
00415 CleanupStack::PopAndDestroy();
00416 }
00417