diff options
author | Daniel Stenberg <daniel@haxx.se> | 2004-05-19 10:21:42 +0000 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2004-05-19 10:21:42 +0000 |
commit | e4e33376710e0f865a653fa28450b6e196fa2186 (patch) | |
tree | 31506d86d611f328e6894fdd517b9d99ab4d7032 /apps/plugins/calculator.c | |
parent | 367aac95190e541b3fe71f3cd371c24d89aac844 (diff) | |
download | rockbox-e4e33376710e0f865a653fa28450b6e196fa2186.tar.gz rockbox-e4e33376710e0f865a653fa28450b6e196fa2186.zip |
I am a wannabe code-police
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@4646 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/calculator.c')
-rw-r--r-- | apps/plugins/calculator.c | 326 |
1 files changed, 206 insertions, 120 deletions
diff --git a/apps/plugins/calculator.c b/apps/plugins/calculator.c index f12710eb1b..b1df913e27 100644 --- a/apps/plugins/calculator.c +++ b/apps/plugins/calculator.c | |||
@@ -85,7 +85,7 @@ Limitations: | |||
85 | - Right now, only accept "num, operator (+,-,*,/), num, =" input sequence. | 85 | - Right now, only accept "num, operator (+,-,*,/), num, =" input sequence. |
86 | Input "3, +, 5, -, 2, =", the calculator will only do 5-2 and result = 3 | 86 | Input "3, +, 5, -, 2, =", the calculator will only do 5-2 and result = 3 |
87 | You have to input "3, +, 5, =, -, 2, =" to get 3+5-2 = 6 | 87 | You have to input "3, +, 5, =, -, 2, =" to get 3+5-2 = 6 |
88 | 88 | ||
89 | - "*,/" have no priority. Actually you can't input 3+5*2 yet. | 89 | - "*,/" have no priority. Actually you can't input 3+5*2 yet. |
90 | 90 | ||
91 | User Instructions: | 91 | User Instructions: |
@@ -120,8 +120,8 @@ F3: equal to "=" | |||
120 | #define X_3_POS (X_2_POS + REC_WIDTH) /* x3 = 66 */ | 120 | #define X_3_POS (X_2_POS + REC_WIDTH) /* x3 = 66 */ |
121 | #define X_4_POS (X_3_POS + REC_WIDTH) /* x4 = 88 */ | 121 | #define X_4_POS (X_3_POS + REC_WIDTH) /* x4 = 88 */ |
122 | #define X_5_POS (X_4_POS + REC_WIDTH) /* x5 = 110, column 111 left blank */ | 122 | #define X_5_POS (X_4_POS + REC_WIDTH) /* x5 = 110, column 111 left blank */ |
123 | 123 | ||
124 | #define TEXT_1_POS (Y_1_POS-10) /* y1 = 2 */ /* blank height = 12 */ | 124 | #define TEXT_1_POS (Y_1_POS-10) /* y1 = 2 */ /* blank height = 12 */ |
125 | #define TEXT_2_POS (Y_2_POS-8) /* y2 = 15 */ /* blank height = 9 */ | 125 | #define TEXT_2_POS (Y_2_POS-8) /* y2 = 15 */ /* blank height = 9 */ |
126 | #define TEXT_3_POS (Y_3_POS-8) /* y3 = 25 */ | 126 | #define TEXT_3_POS (Y_3_POS-8) /* y3 = 25 */ |
127 | #define TEXT_4_POS (Y_4_POS-8) /* y4 = 35 */ | 127 | #define TEXT_4_POS (Y_4_POS-8) /* y4 = 35 */ |
@@ -130,23 +130,26 @@ F3: equal to "=" | |||
130 | 130 | ||
131 | #define SIGN(x) ((x)<0?-1:1) | 131 | #define SIGN(x) ((x)<0?-1:1) |
132 | #define ABS(x) ((x)<0?-(x):(x)) | 132 | #define ABS(x) ((x)<0?-(x):(x)) |
133 | 133 | ||
134 | static struct plugin_api* rb; | 134 | static struct plugin_api* rb; |
135 | 135 | ||
136 | enum {basicButtons, sciButtons} buttonGroup; | 136 | enum { |
137 | basicButtons, | ||
138 | sciButtons | ||
139 | } buttonGroup; | ||
137 | unsigned char* buttonChar[2][5][5] = { | 140 | unsigned char* buttonChar[2][5][5] = { |
138 | { { "MR" , "M+" , "2nd" , "CE" , "C" }, | 141 | { { "MR" , "M+" , "2nd" , "CE" , "C" }, |
139 | { "7" , "8" , "9" , "/" , "sqr" }, | 142 | { "7" , "8" , "9" , "/" , "sqr" }, |
140 | { "4" , "5" , "6" , "*" , "x^2" }, | 143 | { "4" , "5" , "6" , "*" , "x^2" }, |
141 | { "1" , "2" , "3" , "-" , "1/x" }, | 144 | { "1" , "2" , "3" , "-" , "1/x" }, |
142 | { "0" , "+/-", "." , "+" , "=" } }, | 145 | { "0" , "+/-", "." , "+" , "=" } }, |
143 | 146 | ||
144 | { { "n!" , "PI" , "1st" , "sin" , "asi" }, | 147 | { { "n!" , "PI" , "1st" , "sin" , "asi" }, |
145 | { "7" , "8" , "9" , "cos" , "aco" }, | 148 | { "7" , "8" , "9" , "cos" , "aco" }, |
146 | { "4" , "5" , "6" , "tan" , "ata" }, | 149 | { "4" , "5" , "6" , "tan" , "ata" }, |
147 | { "1" , "2" , "3" , "ln" , "e^x" }, | 150 | { "1" , "2" , "3" , "ln" , "e^x" }, |
148 | { "0" , "+/-", "." , "log" , "x^y" } } | 151 | { "0" , "+/-", "." , "log" , "x^y" } } |
149 | }; | 152 | }; |
150 | enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C , | 153 | enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C , |
151 | btn_7 , btn_8 , btn_9 , btn_div , btn_sqr , | 154 | btn_7 , btn_8 , btn_9 , btn_div , btn_sqr , |
152 | btn_4 , btn_5 , btn_6 , btn_time , btn_square , | 155 | btn_4 , btn_5 , btn_6 , btn_time , btn_square , |
@@ -177,7 +180,7 @@ unsigned char buf[19];/* 18 bytes of output line, | |||
177 | buf[0] is operator | 180 | buf[0] is operator |
178 | buf[1] = 'M' if memTemp is not 0 | 181 | buf[1] = 'M' if memTemp is not 0 |
179 | buf[2] = ' ' | 182 | buf[2] = ' ' |
180 | 183 | ||
181 | if SCIENTIFIC_FORMAT | 184 | if SCIENTIFIC_FORMAT |
182 | buf[2]-buf[12] or buf[3]-buf[13] = result; | 185 | buf[2]-buf[12] or buf[3]-buf[13] = result; |
183 | format X.XXXXXXXX | 186 | format X.XXXXXXXX |
@@ -324,7 +327,7 @@ double cordicTable[51][2]= { | |||
324 | 3.552713678800501e-15}, | 327 | 3.552713678800501e-15}, |
325 | {1.7763568394002504646778106689453125e-15, | 328 | {1.7763568394002504646778106689453125e-15, |
326 | 1.776356839400250e-15}, | 329 | 1.776356839400250e-15}, |
327 | {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16} | 330 | {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16} |
328 | }; | 331 | }; |
329 | 332 | ||
330 | void doMultiple(double* operandOne, int* powerOne, | 333 | void doMultiple(double* operandOne, int* powerOne, |
@@ -338,41 +341,55 @@ void oneOperand(void); | |||
338 | /* ----------------------------------------------------------------------- | 341 | /* ----------------------------------------------------------------------- |
339 | Handy funtions | 342 | Handy funtions |
340 | ----------------------------------------------------------------------- */ | 343 | ----------------------------------------------------------------------- */ |
341 | void cleartypingbuf(void){ | 344 | void cleartypingbuf(void) |
345 | { | ||
342 | int k; | 346 | int k; |
343 | for( k=1; k<=(DIGITLEN+1); k++) | 347 | for( k=1; k<=(DIGITLEN+1); k++) |
344 | typingbuf[k] = 0; | 348 | typingbuf[k] = 0; |
345 | typingbuf[0] = ' '; | 349 | typingbuf[0] = ' '; |
346 | typingbufPointer = typingbuf+1; | 350 | typingbufPointer = typingbuf+1; |
347 | } | 351 | } |
348 | void clearbuf(void){ | 352 | void clearbuf(void) |
353 | { | ||
349 | int k; | 354 | int k; |
350 | for(k=0;k<18;k++) buf[k]=' '; | 355 | for(k=0;k<18;k++) |
356 | buf[k]=' '; | ||
351 | buf[18] = 0; | 357 | buf[18] = 0; |
352 | } | 358 | } |
353 | void clearResult(void){ | 359 | void clearResult(void) |
360 | { | ||
354 | result = 0; | 361 | result = 0; |
355 | power = 0; | 362 | power = 0; |
356 | modifier = 0.1; | 363 | modifier = 0.1; |
357 | } | 364 | } |
358 | void clearInput(void){ | 365 | |
366 | void clearInput(void) | ||
367 | { | ||
359 | calStatus = cal_normal; | 368 | calStatus = cal_normal; |
360 | clearResult(); | 369 | clearResult(); |
361 | cleartypingbuf(); | 370 | cleartypingbuf(); |
362 | } | 371 | } |
363 | void clearOperand(void){ | 372 | |
373 | void clearOperand(void) | ||
374 | { | ||
364 | operand = 0; | 375 | operand = 0; |
365 | operandPower = 0; | 376 | operandPower = 0; |
366 | } | 377 | } |
367 | void clearMemTemp(void){ | 378 | |
379 | void clearMemTemp(void) | ||
380 | { | ||
368 | memTemp = 0; | 381 | memTemp = 0; |
369 | memTempPower = 0; | 382 | memTempPower = 0; |
370 | } | 383 | } |
371 | void clearOper(void){ | 384 | |
385 | void clearOper(void) | ||
386 | { | ||
372 | oper = ' '; | 387 | oper = ' '; |
373 | operInputted = false; | 388 | operInputted = false; |
374 | } | 389 | } |
375 | void clearMem(void){ | 390 | |
391 | void clearMem(void) | ||
392 | { | ||
376 | clearInput(); | 393 | clearInput(); |
377 | clearMemTemp(); | 394 | clearMemTemp(); |
378 | clearOperand(); | 395 | clearOperand(); |
@@ -380,7 +397,8 @@ void clearMem(void){ | |||
380 | btn = BUTTON_NONE; | 397 | btn = BUTTON_NONE; |
381 | } | 398 | } |
382 | 399 | ||
383 | void switchOperands(void){ | 400 | void switchOperands(void) |
401 | { | ||
384 | double tempr = operand; | 402 | double tempr = operand; |
385 | int tempp = operandPower; | 403 | int tempp = operandPower; |
386 | operand = result; | 404 | operand = result; |
@@ -392,7 +410,8 @@ void switchOperands(void){ | |||
392 | /* ----------------------------------------------------------------------- | 410 | /* ----------------------------------------------------------------------- |
393 | Initiate calculator | 411 | Initiate calculator |
394 | ----------------------------------------------------------------------- */ | 412 | ----------------------------------------------------------------------- */ |
395 | void cal_initial (void){ | 413 | void cal_initial (void) |
414 | { | ||
396 | int i,j,w,h; | 415 | int i,j,w,h; |
397 | rb->lcd_setfont(FONT_SYSFIXED); | 416 | rb->lcd_setfont(FONT_SYSFIXED); |
398 | rb->lcd_clear_display(); | 417 | rb->lcd_clear_display(); |
@@ -419,8 +438,10 @@ void cal_initial (void){ | |||
419 | } | 438 | } |
420 | 439 | ||
421 | /* initially, invert button "5" */ | 440 | /* initially, invert button "5" */ |
422 | m = 2; n = 1; | 441 | m = 2; |
423 | prev_m = m; prev_n = n; | 442 | n = 1; |
443 | prev_m = m; | ||
444 | prev_n = n; | ||
424 | rb->lcd_invertrect( X_0_POS + n*REC_WIDTH + 1, | 445 | rb->lcd_invertrect( X_0_POS + n*REC_WIDTH + 1, |
425 | Y_1_POS + m*REC_HEIGHT + 1, | 446 | Y_1_POS + m*REC_HEIGHT + 1, |
426 | REC_WIDTH - 1, REC_HEIGHT - 1); | 447 | REC_WIDTH - 1, REC_HEIGHT - 1); |
@@ -435,14 +456,15 @@ void cal_initial (void){ | |||
435 | } | 456 | } |
436 | 457 | ||
437 | /* ----------------------------------------------------------------------- | 458 | /* ----------------------------------------------------------------------- |
438 | mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm | 459 | mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm |
439 | in it's private case for sqrt. | 460 | in it's private case for sqrt. |
440 | Thanks BlueChip for his intro text and Dave Straayer for the actual name. | 461 | Thanks BlueChip for his intro text and Dave Straayer for the actual name. |
441 | ----------------------------------------------------------------------- */ | 462 | ----------------------------------------------------------------------- */ |
442 | double mySqrt(double square){ | 463 | double mySqrt(double square) |
464 | { | ||
443 | int k = 0; | 465 | int k = 0; |
444 | double temp = 0; | 466 | double temp = 0; |
445 | double root= ABS(square+1)/2; | 467 | double root= ABS(square+1)/2; |
446 | 468 | ||
447 | while( ABS(root - temp) > MINIMUM ){ | 469 | while( ABS(root - temp) > MINIMUM ){ |
448 | temp = root; | 470 | temp = root; |
@@ -454,56 +476,81 @@ double mySqrt(double square){ | |||
454 | return root; | 476 | return root; |
455 | } | 477 | } |
456 | /* ----------------------------------------------------------------------- | 478 | /* ----------------------------------------------------------------------- |
457 | transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method | 479 | transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method |
458 | transcendFunc can do sin,cos,log,exp | 480 | transcendFunc can do sin,cos,log,exp |
459 | input parameter is angle | 481 | input parameter is angle |
460 | ----------------------------------------------------------------------- */ | 482 | ----------------------------------------------------------------------- */ |
461 | void transcendFunc(char* func, double* tt, int* ttPower){ | 483 | void transcendFunc(char* func, double* tt, int* ttPower) |
484 | { | ||
462 | double t = (*tt)*PI/180; int tPower = *ttPower; | 485 | double t = (*tt)*PI/180; int tPower = *ttPower; |
463 | |||
464 | if (tPower < -998) {calStatus = cal_normal; return;} | ||
465 | if (tPower > 8) {calStatus = cal_error; return;} | ||
466 | *ttPower = 0; | ||
467 | calStatus = cal_normal; | ||
468 | |||
469 | int sign = 1; | 486 | int sign = 1; |
470 | int n = 50; /* n <=50, tables are all <= 50 */ | 487 | int n = 50; /* n <=50, tables are all <= 50 */ |
471 | int j; | 488 | int j; |
472 | double x,y,z,xt,yt,zt; | 489 | double x,y,z,xt,yt,zt; |
473 | 490 | ||
474 | if( func[0] =='s' || func[0] =='S') sign = SIGN(t); | 491 | if (tPower < -998) { |
475 | else { /* if( func[0] =='c' || func[0] =='C') */ sign = 1; } | 492 | calStatus = cal_normal; |
493 | return; | ||
494 | } | ||
495 | if (tPower > 8) { | ||
496 | calStatus = cal_error; | ||
497 | return; | ||
498 | } | ||
499 | *ttPower = 0; | ||
500 | calStatus = cal_normal; | ||
501 | |||
502 | if( func[0] =='s' || func[0] =='S') | ||
503 | sign = SIGN(t); | ||
504 | else { | ||
505 | /* if( func[0] =='c' || func[0] =='C') */ | ||
506 | sign = 1; | ||
507 | } | ||
476 | t = ABS(t); | 508 | t = ABS(t); |
477 | 509 | ||
478 | while (tPower > 0){ t *= 10; tPower--; } | 510 | while (tPower > 0){ |
479 | while (tPower < 0){ t /= 10; tPower++; } | 511 | t *= 10; |
512 | tPower--; | ||
513 | } | ||
514 | while (tPower < 0) { | ||
515 | t /= 10; | ||
516 | tPower++; | ||
517 | } | ||
480 | j = 0; | 518 | j = 0; |
481 | while (t > j*2*PI) {j++;} | 519 | while (t > j*2*PI) {j++;} |
482 | t -= (j-1)*2*PI; | 520 | t -= (j-1)*2*PI; |
483 | if (PI/2 < t && t < 3*PI/2){ | 521 | if (PI/2 < t && t < 3*PI/2){ |
484 | t = PI - t; | 522 | t = PI - t; |
485 | if (func[0] =='c' || func[0] =='C') sign = -1; | 523 | if (func[0] =='c' || func[0] =='C') |
524 | sign = -1; | ||
486 | } | 525 | } |
487 | else if ( 3*PI/2 <= t && t <= 2*PI) t -= 2*PI; | 526 | else if ( 3*PI/2 <= t && t <= 2*PI) |
527 | t -= 2*PI; | ||
488 | 528 | ||
489 | x = 0.60725293500888; y = 0; z = t; | 529 | x = 0.60725293500888; y = 0; z = t; |
490 | for (j=1;j<n+2;j++){ | 530 | for (j=1;j<n+2;j++){ |
491 | xt = x - SIGN(z) * y*cordicTable[j-1][0]; | 531 | xt = x - SIGN(z) * y*cordicTable[j-1][0]; |
492 | yt = y + SIGN(z) * x*cordicTable[j-1][0]; | 532 | yt = y + SIGN(z) * x*cordicTable[j-1][0]; |
493 | |||
494 | zt = z - SIGN(z) * cordicTable[j-1][1]; | 533 | zt = z - SIGN(z) * cordicTable[j-1][1]; |
495 | x = xt;y=yt;z=zt; | 534 | x = xt; |
535 | y=yt; | ||
536 | z=zt; | ||
496 | } | 537 | } |
497 | if( func[0] =='s' || func[0] =='S') {*tt = sign*y; return;} | 538 | if( func[0] =='s' || func[0] =='S') { |
498 | else /* if( func[0] =='c' || func[0] =='C')*/ {*tt = sign*x; return;} | 539 | *tt = sign*y; |
499 | 540 | return; | |
541 | } | ||
542 | else /* if( func[0] =='c' || func[0] =='C')*/ { | ||
543 | *tt = sign*x; | ||
544 | return; | ||
545 | } | ||
546 | |||
500 | } | 547 | } |
501 | /* ----------------------------------------------------------------------- | 548 | /* ----------------------------------------------------------------------- |
502 | add in scientific number format | 549 | add in scientific number format |
503 | ----------------------------------------------------------------------- */ | 550 | ----------------------------------------------------------------------- */ |
504 | void doAdd (double* operandOne, int* powerOne, | 551 | void doAdd (double* operandOne, int* powerOne, |
505 | double operandTwo, int powerTwo){ | 552 | double operandTwo, int powerTwo) |
506 | 553 | { | |
507 | if ( *powerOne >= powerTwo ){ | 554 | if ( *powerOne >= powerTwo ){ |
508 | if (*powerOne - powerTwo <= DIGITLEN+1){ | 555 | if (*powerOne - powerTwo <= DIGITLEN+1){ |
509 | while (powerTwo < *powerOne){ | 556 | while (powerTwo < *powerOne){ |
@@ -532,20 +579,23 @@ void doAdd (double* operandOne, int* powerOne, | |||
532 | multiple in scientific number format | 579 | multiple in scientific number format |
533 | ----------------------------------------------------------------------- */ | 580 | ----------------------------------------------------------------------- */ |
534 | void doMultiple(double* operandOne, int* powerOne, | 581 | void doMultiple(double* operandOne, int* powerOne, |
535 | double operandTwo, int powerTwo){ | 582 | double operandTwo, int powerTwo) |
536 | (*operandOne) *= operandTwo; | 583 | { |
537 | (*powerOne) += powerTwo; | 584 | (*operandOne) *= operandTwo; |
585 | (*powerOne) += powerTwo; | ||
538 | } | 586 | } |
539 | 587 | ||
540 | /* ----------------------------------------------------------------------- | 588 | /* ----------------------------------------------------------------------- |
541 | Handles all one operand calculations | 589 | Handles all one operand calculations |
542 | ----------------------------------------------------------------------- */ | 590 | ----------------------------------------------------------------------- */ |
543 | void oneOperand(void){ | 591 | void oneOperand(void) |
592 | { | ||
544 | int k = 0; | 593 | int k = 0; |
545 | if (buttonGroup == basicButtons){ | 594 | if (buttonGroup == basicButtons){ |
546 | switch(CAL_BUTTON){ | 595 | switch(CAL_BUTTON){ |
547 | case btn_sqr: | 596 | case btn_sqr: |
548 | if (result<0) calStatus = cal_error; | 597 | if (result<0) |
598 | calStatus = cal_error; | ||
549 | else{ | 599 | else{ |
550 | if (power%2 == 1){ | 600 | if (power%2 == 1){ |
551 | result = (mySqrt(result*10))/10; | 601 | result = (mySqrt(result*10))/10; |
@@ -563,9 +613,10 @@ void oneOperand(void){ | |||
563 | result *= result; | 613 | result *= result; |
564 | calStatus = cal_normal; | 614 | calStatus = cal_normal; |
565 | break; | 615 | break; |
566 | 616 | ||
567 | case btn_rec: | 617 | case btn_rec: |
568 | if (result==0) calStatus = cal_error; | 618 | if (result==0) |
619 | calStatus = cal_error; | ||
569 | else{ | 620 | else{ |
570 | power = -power; | 621 | power = -power; |
571 | result = 1/result; | 622 | result = 1/result; |
@@ -586,10 +637,17 @@ void oneOperand(void){ | |||
586 | transcendFunc("cos", &result, &power); | 637 | transcendFunc("cos", &result, &power); |
587 | break; | 638 | break; |
588 | case sci_fac: | 639 | case sci_fac: |
589 | if (power<0 || power>8 || result<0 ) calStatus = cal_error; | 640 | if (power<0 || power>8 || result<0 ) |
590 | else if(result == 0) {result = 1; power = 0; } | 641 | calStatus = cal_error; |
642 | else if(result == 0) { | ||
643 | result = 1; | ||
644 | power = 0; | ||
645 | } | ||
591 | else{ | 646 | else{ |
592 | while(power > 0) {result *= 10; power--;} | 647 | while(power > 0) { |
648 | result *= 10; | ||
649 | power--; | ||
650 | } | ||
593 | if ( ( result - (int)result) > MINIMUM ) | 651 | if ( ( result - (int)result) > MINIMUM ) |
594 | calStatus = cal_error; | 652 | calStatus = cal_error; |
595 | else { | 653 | else { |
@@ -614,7 +672,8 @@ void oneOperand(void){ | |||
614 | /* ----------------------------------------------------------------------- | 672 | /* ----------------------------------------------------------------------- |
615 | Handles all two operands calculations | 673 | Handles all two operands calculations |
616 | ----------------------------------------------------------------------- */ | 674 | ----------------------------------------------------------------------- */ |
617 | void twoOperands(void){ | 675 | void twoOperands(void) |
676 | { | ||
618 | switch(oper){ | 677 | switch(oper){ |
619 | case '-': | 678 | case '-': |
620 | doAdd(&operand, &operandPower, -result, power); | 679 | doAdd(&operand, &operandPower, -result, power); |
@@ -629,7 +688,8 @@ void twoOperands(void){ | |||
629 | if ( ABS(result) > MINIMUM ){ | 688 | if ( ABS(result) > MINIMUM ){ |
630 | doMultiple(&operand, &operandPower, 1/result, -power); | 689 | doMultiple(&operand, &operandPower, 1/result, -power); |
631 | } | 690 | } |
632 | else calStatus = cal_error; | 691 | else |
692 | calStatus = cal_error; | ||
633 | break; | 693 | break; |
634 | default: /* ' ' */ | 694 | default: /* ' ' */ |
635 | switchOperands(); /* counter switchOperands() below */ | 695 | switchOperands(); /* counter switchOperands() below */ |
@@ -646,26 +706,34 @@ void moveButton(void){ | |||
646 | switch(btn){ | 706 | switch(btn){ |
647 | case BUTTON_LEFT: | 707 | case BUTTON_LEFT: |
648 | case BUTTON_LEFT | BUTTON_REPEAT: | 708 | case BUTTON_LEFT | BUTTON_REPEAT: |
649 | if (n == 0) n = 4; | 709 | if (n == 0) |
650 | else n--; | 710 | n = 4; |
711 | else | ||
712 | n--; | ||
651 | break; | 713 | break; |
652 | 714 | ||
653 | case BUTTON_RIGHT: | 715 | case BUTTON_RIGHT: |
654 | case BUTTON_RIGHT | BUTTON_REPEAT: | 716 | case BUTTON_RIGHT | BUTTON_REPEAT: |
655 | if (n == 4) n = 0; | 717 | if (n == 4) |
656 | else n++; | 718 | n = 0; |
719 | else | ||
720 | n++; | ||
657 | break; | 721 | break; |
658 | 722 | ||
659 | case BUTTON_UP: | 723 | case BUTTON_UP: |
660 | case BUTTON_UP | BUTTON_REPEAT: | 724 | case BUTTON_UP | BUTTON_REPEAT: |
661 | if (m == 0) m = 4; | 725 | if (m == 0) |
662 | else m--; | 726 | m = 4; |
727 | else | ||
728 | m--; | ||
663 | break; | 729 | break; |
664 | 730 | ||
665 | case BUTTON_DOWN: | 731 | case BUTTON_DOWN: |
666 | case BUTTON_DOWN | BUTTON_REPEAT: | 732 | case BUTTON_DOWN | BUTTON_REPEAT: |
667 | if (m == 4) m = 0; | 733 | if (m == 4) |
668 | else m++; | 734 | m = 0; |
735 | else | ||
736 | m++; | ||
669 | break; | 737 | break; |
670 | } | 738 | } |
671 | 739 | ||
@@ -685,14 +753,15 @@ void moveButton(void){ | |||
685 | Y_1_POS + m*REC_HEIGHT + 1, | 753 | Y_1_POS + m*REC_HEIGHT + 1, |
686 | REC_WIDTH - 1, REC_HEIGHT - 1); | 754 | REC_WIDTH - 1, REC_HEIGHT - 1); |
687 | 755 | ||
688 | prev_m = m; prev_n = n; | 756 | prev_m = m; |
689 | 757 | prev_n = n; | |
690 | } | 758 | } |
691 | /* ----------------------------------------------------------------------- | 759 | /* ----------------------------------------------------------------------- |
692 | Print buttons when switching 1st and 2nd | 760 | Print buttons when switching 1st and 2nd |
693 | int group = {basicButtons, sciButtons} | 761 | int group = {basicButtons, sciButtons} |
694 | ----------------------------------------------------------------------- */ | 762 | ----------------------------------------------------------------------- */ |
695 | void printButtonGroups(int group){ | 763 | void printButtonGroups(int group) |
764 | { | ||
696 | int i,j,w,h; | 765 | int i,j,w,h; |
697 | for (i = 0; i < 5; i++){ | 766 | for (i = 0; i < 5; i++){ |
698 | for (j = 3; j <= 4; j++){ | 767 | for (j = 3; j <= 4; j++){ |
@@ -725,7 +794,8 @@ void printButtonGroups(int group){ | |||
725 | /* ----------------------------------------------------------------------- | 794 | /* ----------------------------------------------------------------------- |
726 | flash the button pressed | 795 | flash the button pressed |
727 | ----------------------------------------------------------------------- */ | 796 | ----------------------------------------------------------------------- */ |
728 | void flashButton(int b){ | 797 | void flashButton(int b) |
798 | { | ||
729 | int i = b/5; int j = b - i*5; | 799 | int i = b/5; int j = b - i*5; |
730 | int k; | 800 | int k; |
731 | for (k=1*2;k>0;k--){ | 801 | for (k=1*2;k>0;k--){ |
@@ -745,13 +815,15 @@ void flashButton(int b){ | |||
745 | /* ----------------------------------------------------------------------- | 815 | /* ----------------------------------------------------------------------- |
746 | pos is the position that needs animation. pos = [1~18] | 816 | pos is the position that needs animation. pos = [1~18] |
747 | ----------------------------------------------------------------------- */ | 817 | ----------------------------------------------------------------------- */ |
748 | void deleteAnimation(int pos){ | 818 | void deleteAnimation(int pos) |
819 | { | ||
749 | int k; | 820 | int k; |
750 | if (pos<1 || pos >18) return; | 821 | if (pos<1 || pos >18) |
822 | return; | ||
751 | pos--; | 823 | pos--; |
752 | rb->lcd_fillrect(1+pos*6, TEXT_1_POS, 6, 8); | 824 | rb->lcd_fillrect(1+pos*6, TEXT_1_POS, 6, 8); |
753 | rb->lcd_update_rect(1+pos*6, TEXT_1_POS, 6, 8); | 825 | rb->lcd_update_rect(1+pos*6, TEXT_1_POS, 6, 8); |
754 | 826 | ||
755 | for (k=1;k<=4;k++){ | 827 | for (k=1;k<=4;k++){ |
756 | rb->sleep(HZ/32); | 828 | rb->sleep(HZ/32); |
757 | rb->lcd_clearrect(1+pos*6, TEXT_1_POS, 6, 8); | 829 | rb->lcd_clearrect(1+pos*6, TEXT_1_POS, 6, 8); |
@@ -773,7 +845,8 @@ formatResult() change result to standard format: 0.xxxx | |||
773 | if result is close to 0, let it be 0; | 845 | if result is close to 0, let it be 0; |
774 | if result is close to 1, let it be 0.1 and power++; | 846 | if result is close to 1, let it be 0.1 and power++; |
775 | ----------------------------------------------------------------------- */ | 847 | ----------------------------------------------------------------------- */ |
776 | void formatResult(void){ | 848 | void formatResult(void) |
849 | { | ||
777 | int resultsign = SIGN(result); | 850 | int resultsign = SIGN(result); |
778 | result = ABS(result); | 851 | result = ABS(result); |
779 | if(result > MINIMUM ){ /* doesn't check power, might have problem | 852 | if(result > MINIMUM ){ /* doesn't check power, might have problem |
@@ -785,21 +858,31 @@ void formatResult(void){ | |||
785 | 858 | ||
786 | if (result<1){ | 859 | if (result<1){ |
787 | while( (int)(result*10) == 0 ){ | 860 | while( (int)(result*10) == 0 ){ |
788 | result *= 10; power--; modifier *= 10; | 861 | result *= 10; |
862 | power--; | ||
863 | modifier *= 10; | ||
789 | } | 864 | } |
790 | } | 865 | } |
791 | else{ /* result >= 1 */ | 866 | else{ /* result >= 1 */ |
792 | while( (int)result != 0 ){ | 867 | while( (int)result != 0 ){ |
793 | result /= 10; power++; modifier /= 10; | 868 | result /= 10; |
869 | power++; | ||
870 | modifier /= 10; | ||
794 | } | 871 | } |
795 | } /* if result<1 */ | 872 | } /* if result<1 */ |
796 | 873 | ||
797 | if (result > (1-MINIMUM)){ | 874 | if (result > (1-MINIMUM)){ |
798 | result = 0.1; power++; modifier /= 10; | 875 | result = 0.1; |
876 | power++; | ||
877 | modifier /= 10; | ||
799 | } | 878 | } |
800 | result *= resultsign; | 879 | result *= resultsign; |
801 | } | 880 | } |
802 | else{ result = 0; power = 0; modifier = 0.1; } | 881 | else { |
882 | result = 0; | ||
883 | power = 0; | ||
884 | modifier = 0.1; | ||
885 | } | ||
803 | } | 886 | } |
804 | 887 | ||
805 | /* ----------------------------------------------------------------------- | 888 | /* ----------------------------------------------------------------------- |
@@ -808,15 +891,18 @@ case SCIENTIFIC_FORMAT, let temppower = 1; | |||
808 | case temppower > 0: print '.' in the middle | 891 | case temppower > 0: print '.' in the middle |
809 | case temppower <= 0: print '.' in the begining | 892 | case temppower <= 0: print '.' in the begining |
810 | ----------------------------------------------------------------------- */ | 893 | ----------------------------------------------------------------------- */ |
811 | void result2typingbuf(void){ | 894 | void result2typingbuf(void) |
895 | { | ||
812 | bool haveDot = false; | 896 | bool haveDot = false; |
813 | char tempchar = 0; | 897 | char tempchar = 0; |
814 | int k; | 898 | int k; |
815 | double tempresult = ABS(result); /* positive num makes things simple */ | 899 | double tempresult = ABS(result); /* positive num makes things simple */ |
816 | 900 | ||
817 | int temppower; | 901 | int temppower; |
818 | if(SCIENTIFIC_FORMAT) temppower = 1; /* output x.xxxx format */ | 902 | if(SCIENTIFIC_FORMAT) |
819 | else temppower = power; | 903 | temppower = 1; /* output x.xxxx format */ |
904 | else | ||
905 | temppower = power; | ||
820 | 906 | ||
821 | double tempmodifier = 1; | 907 | double tempmodifier = 1; |
822 | int count; | 908 | int count; |
@@ -862,7 +948,7 @@ void result2typingbuf(void){ | |||
862 | tempmodifier = tempmodifier/10; | 948 | tempmodifier = tempmodifier/10; |
863 | while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){ | 949 | while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){ |
864 | count++; | 950 | count++; |
865 | 951 | ||
866 | } | 952 | } |
867 | tempresult -= tempmodifier*count; | 953 | tempresult -= tempmodifier*count; |
868 | tempresult = ABS(tempresult); | 954 | tempresult = ABS(tempresult); |
@@ -919,7 +1005,7 @@ void printResult(void){ | |||
919 | 1005 | ||
920 | result2typingbuf(); | 1006 | result2typingbuf(); |
921 | clearbuf(); | 1007 | clearbuf(); |
922 | 1008 | ||
923 | buf[0] = oper; | 1009 | buf[0] = oper; |
924 | buf[1] = ( ABS(memTemp) > MINIMUM )?'M':' '; | 1010 | buf[1] = ( ABS(memTemp) > MINIMUM )?'M':' '; |
925 | buf[2] = ' '; | 1011 | buf[2] = ' '; |
@@ -1012,18 +1098,18 @@ void typingProcess(void){ | |||
1012 | } | 1098 | } |
1013 | if (typingbufPointer!=typingbuf+DIGITLEN+1){ | 1099 | if (typingbufPointer!=typingbuf+DIGITLEN+1){ |
1014 | typingbufPointer++; | 1100 | typingbufPointer++; |
1015 | 1101 | ||
1016 | {/* result processing */ | 1102 | {/* result processing */ |
1017 | if (calStatus == cal_typing) power++; | 1103 | if (calStatus == cal_typing) power++; |
1018 | if (CAL_BUTTON != btn_0) | 1104 | if (CAL_BUTTON != btn_0) |
1019 | result= result + | 1105 | result= result + |
1020 | SIGN(result)* | 1106 | SIGN(result)* |
1021 | (7+n-3*(m-1))*modifier; | 1107 | (7+n-3*(m-1))*modifier; |
1022 | modifier /= 10; | 1108 | modifier /= 10; |
1023 | } | 1109 | } |
1024 | } | 1110 | } |
1025 | else /* last byte always '\0' */ | 1111 | else /* last byte always '\0' */ |
1026 | *typingbufPointer = 0; | 1112 | *typingbufPointer = 0; |
1027 | break; | 1113 | break; |
1028 | default: /* cal_error, cal_exit */ | 1114 | default: /* cal_error, cal_exit */ |
1029 | break; | 1115 | break; |
@@ -1051,7 +1137,7 @@ void doDelete(void){ | |||
1051 | } | 1137 | } |
1052 | case cal_typing: | 1138 | case cal_typing: |
1053 | typingbufPointer--; | 1139 | typingbufPointer--; |
1054 | 1140 | ||
1055 | {/* result processing */ /* 0-9, '.' */ | 1141 | {/* result processing */ /* 0-9, '.' */ |
1056 | /* if deleting '.', do nothing */ | 1142 | /* if deleting '.', do nothing */ |
1057 | if ( *typingbufPointer != '.'){ | 1143 | if ( *typingbufPointer != '.'){ |
@@ -1061,16 +1147,16 @@ void doDelete(void){ | |||
1061 | ((*typingbufPointer)- '0')*modifier; | 1147 | ((*typingbufPointer)- '0')*modifier; |
1062 | } | 1148 | } |
1063 | } | 1149 | } |
1064 | 1150 | ||
1065 | *typingbufPointer = 0; | 1151 | *typingbufPointer = 0; |
1066 | 1152 | ||
1067 | /* if (only one digit left and it's 0) | 1153 | /* if (only one digit left and it's 0) |
1068 | or no digit left, change status*/ | 1154 | or no digit left, change status*/ |
1069 | if ( typingbufPointer == typingbuf+1 || | 1155 | if ( typingbufPointer == typingbuf+1 || |
1070 | ( typingbufPointer == typingbuf+2 && | 1156 | ( typingbufPointer == typingbuf+2 && |
1071 | *(typingbufPointer-1) == '0' )) | 1157 | *(typingbufPointer-1) == '0' )) |
1072 | calStatus = cal_normal; | 1158 | calStatus = cal_normal; |
1073 | break; | 1159 | break; |
1074 | default: /* normal, error, exit */ | 1160 | default: /* normal, error, exit */ |
1075 | break; | 1161 | break; |
1076 | } | 1162 | } |
@@ -1099,7 +1185,7 @@ void basicButtonsProcess(void){ | |||
1099 | memTemp = result; memTempPower = power; | 1185 | memTemp = result; memTempPower = power; |
1100 | calStatus = cal_normal; | 1186 | calStatus = cal_normal; |
1101 | break; | 1187 | break; |
1102 | 1188 | ||
1103 | case btn_C: clearMem(); break; | 1189 | case btn_C: clearMem(); break; |
1104 | case btn_CE: clearInput(); break; | 1190 | case btn_CE: clearInput(); break; |
1105 | 1191 | ||
@@ -1107,7 +1193,7 @@ void basicButtonsProcess(void){ | |||
1107 | buttonGroup = sciButtons; | 1193 | buttonGroup = sciButtons; |
1108 | printButtonGroups(buttonGroup); | 1194 | printButtonGroups(buttonGroup); |
1109 | break; | 1195 | break; |
1110 | 1196 | ||
1111 | /* one operand calculation, may be changed to | 1197 | /* one operand calculation, may be changed to |
1112 | like sin, cos, log, etc */ | 1198 | like sin, cos, log, etc */ |
1113 | case btn_sqr: | 1199 | case btn_sqr: |
@@ -1116,7 +1202,7 @@ void basicButtonsProcess(void){ | |||
1116 | formatResult(); /* not necessary, just for safty */ | 1202 | formatResult(); /* not necessary, just for safty */ |
1117 | oneOperand(); | 1203 | oneOperand(); |
1118 | break; | 1204 | break; |
1119 | 1205 | ||
1120 | case_btn_equal: /* F3 shortkey entrance */ | 1206 | case_btn_equal: /* F3 shortkey entrance */ |
1121 | case btn_equal: | 1207 | case btn_equal: |
1122 | formatResult(); | 1208 | formatResult(); |
@@ -1124,7 +1210,7 @@ void basicButtonsProcess(void){ | |||
1124 | operInputted = false; | 1210 | operInputted = false; |
1125 | if (oper != ' ') twoOperands(); | 1211 | if (oper != ' ') twoOperands(); |
1126 | break; | 1212 | break; |
1127 | 1213 | ||
1128 | case btn_div: | 1214 | case btn_div: |
1129 | case btn_time: | 1215 | case btn_time: |
1130 | case btn_minus: | 1216 | case btn_minus: |
@@ -1136,7 +1222,7 @@ void basicButtonsProcess(void){ | |||
1136 | formatResult(); | 1222 | formatResult(); |
1137 | operand = result; | 1223 | operand = result; |
1138 | operandPower = power; | 1224 | operandPower = power; |
1139 | 1225 | ||
1140 | break; | 1226 | break; |
1141 | 1227 | ||
1142 | case btn_sign: | 1228 | case btn_sign: |
@@ -1150,7 +1236,7 @@ void basicButtonsProcess(void){ | |||
1150 | case BUTTON_F2: | 1236 | case BUTTON_F2: |
1151 | if (calStatus == cal_error) break; | 1237 | if (calStatus == cal_error) break; |
1152 | if (!operInputted) {twoOperands(); operInputted = true;} | 1238 | if (!operInputted) {twoOperands(); operInputted = true;} |
1153 | switch (oper){ | 1239 | switch (oper){ |
1154 | case ' ': | 1240 | case ' ': |
1155 | case '/': oper = '+'; flashButton(btn_add); break; | 1241 | case '/': oper = '+'; flashButton(btn_add); break; |
1156 | case '+': oper = '-'; flashButton(btn_minus); break; | 1242 | case '+': oper = '-'; flashButton(btn_minus); break; |
@@ -1216,7 +1302,7 @@ void sciButtonsProcess(void){ | |||
1216 | case BUTTON_F2: | 1302 | case BUTTON_F2: |
1217 | if (calStatus == cal_error) break; | 1303 | if (calStatus == cal_error) break; |
1218 | if (!operInputted) {twoOperands(); operInputted = true;} | 1304 | if (!operInputted) {twoOperands(); operInputted = true;} |
1219 | switch (oper){ | 1305 | switch (oper){ |
1220 | case ' ': oper = '+'; break; | 1306 | case ' ': oper = '+'; break; |
1221 | case '/': oper = '+'; deleteAnimation(1); break; | 1307 | case '/': oper = '+'; deleteAnimation(1); break; |
1222 | case '+': oper = '-'; deleteAnimation(1); break; | 1308 | case '+': oper = '-'; deleteAnimation(1); break; |
@@ -1244,7 +1330,7 @@ void sciButtonsProcess(void){ | |||
1244 | Main(); | 1330 | Main(); |
1245 | ----------------------------------------------------------------------- */ | 1331 | ----------------------------------------------------------------------- */ |
1246 | enum plugin_status plugin_start(struct plugin_api* api, void* parameter) | 1332 | enum plugin_status plugin_start(struct plugin_api* api, void* parameter) |
1247 | { | 1333 | { |
1248 | TEST_PLUGIN_API(api); | 1334 | TEST_PLUGIN_API(api); |
1249 | (void)parameter; | 1335 | (void)parameter; |
1250 | rb = api; | 1336 | rb = api; |