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