1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2004 Pengxuan Liu (Isaac)
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 00 01 21 22 23 43 44 45 65 66 67 87 88 89 109110111
22 00 |-----------|-----------|-----------|-----------|-----------|
24 |***********|***********|***********|***********|***********|
25 |***********|***********|***********|***********|***********|
27 12 |-----------|-----------|-----------|-----------|-----------|
28 13 |-----------|-----------|-----------|-----------|-----------| y1
32 23 |-----------|-----------|-----------|-----------|-----------| y2
36 33 |-----------|-----------|-----------|-----------|-----------| y3
40 43 |-----------|-----------|-----------|-----------|-----------| y4
44 53 |-----------|-----------|-----------|-----------|-----------| y5
48 63 |-----------|-----------|-----------|-----------|-----------| y6
52 /*---------------------------------------------------------------------------
54 - Scientific number format core code. Support range 10^-999 ~ 10^999
55 - Number of significant figures up to 10
58 - Right now, only accept "num, operator (+,-,*,/), num, =" input sequence.
59 Input "3, +, 5, -, 2, =", the calculator will only do 5-2 and result = 3
60 You have to input "3, +, 5, =, -, 2, =" to get 3+5-2 = 6
62 - "*,/" have no priority. Actually you can't input 3+5*2 yet.
65 use arrow button to move cursor, "play" button to select, "off" button to exit
66 F1: if typing numbers, it's equal to "Del"; otherwise, equal to "C"
67 F2: circle input "+, -, *, /"
70 "MR" : load temp memory
71 "M+" : add currently display to temp memory
72 "C" : reset calculator
73 ---------------------------------------------------------------------------*/
76 #ifdef HAVE_LCD_BITMAP
81 #define REC_HEIGHT 10 /* blank height = 9 */
82 #define REC_WIDTH 22 /* blank width = 21 */
84 #define Y_6_POS (LCD_HEIGHT - 1) /* y6 = 63 */
85 #define Y_5_POS (Y_6_POS - REC_HEIGHT) /* y5 = 53 */
86 #define Y_4_POS (Y_5_POS - REC_HEIGHT) /* y4 = 43 */
87 #define Y_3_POS (Y_4_POS - REC_HEIGHT) /* y3 = 33 */
88 #define Y_2_POS (Y_3_POS - REC_HEIGHT) /* y2 = 23 */
89 #define Y_1_POS (Y_2_POS - REC_HEIGHT) /* y1 = 13 */
90 #define Y_0_POS 0 /* y0 = 0 */
92 #define X_0_POS 0 /* x0 = 0 */
93 #define X_1_POS (X_0_POS + REC_WIDTH) /* x1 = 22 */
94 #define X_2_POS (X_1_POS + REC_WIDTH) /* x2 = 44 */
95 #define X_3_POS (X_2_POS + REC_WIDTH) /* x3 = 66 */
96 #define X_4_POS (X_3_POS + REC_WIDTH) /* x4 = 88 */
97 #define X_5_POS (X_4_POS + REC_WIDTH) /* x5 = 110, column 111 left blank */
99 #define TEXT_1_POS (Y_1_POS-10) /* y1 = 2 */ /* blank height = 12 */
100 #define TEXT_2_POS (Y_2_POS-8) /* y2 = 15 */ /* blank height = 9 */
101 #define TEXT_3_POS (Y_3_POS-8) /* y3 = 25 */
102 #define TEXT_4_POS (Y_4_POS-8) /* y4 = 35 */
103 #define TEXT_5_POS (Y_5_POS-8) /* y5 = 45 */
104 #define TEXT_6_POS (Y_6_POS-8) /* y6 = 55 */
106 #define SIGN(x) ((x)<0?-1:1)
107 #define ABS(x) ((x)<0?-(x):(x))
109 /* variable button definitions */
110 #if CONFIG_KEYPAD == RECORDER_PAD
111 #define CALCULATOR_UP BUTTON_UP
112 #define CALCULATOR_DOWN BUTTON_DOWN
113 #define CALCULATOR_QUIT BUTTON_OFF
114 #define CALCULATOR_INPUT BUTTON_PLAY
115 #define CALCULATOR_CALC BUTTON_F3
116 #define CALCULATOR_OPERATORS BUTTON_F2
117 #define CALCULATOR_CLEAR BUTTON_F1
119 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
120 #define CALCULATOR_UP BUTTON_UP
121 #define CALCULATOR_DOWN BUTTON_DOWN
122 #define CALCULATOR_QUIT BUTTON_OFF
123 #define CALCULATOR_INPUT BUTTON_SELECT
124 #define CALCULATOR_CALC BUTTON_F3
125 #define CALCULATOR_OPERATORS BUTTON_F2
126 #define CALCULATOR_CLEAR BUTTON_F1
128 #elif CONFIG_KEYPAD == ONDIO_PAD
129 #define CALCULATOR_UP BUTTON_UP
130 #define CALCULATOR_DOWN BUTTON_DOWN
131 #define CALCULATOR_QUIT BUTTON_OFF
132 #define CALCULATOR_INPUT_CALC_PRE BUTTON_MENU
133 #define CALCULATOR_INPUT (BUTTON_MENU | BUTTON_REL)
134 #define CALCULATOR_CALC (BUTTON_MENU | BUTTON_REPEAT)
136 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
137 (CONFIG_KEYPAD == IRIVER_H300_PAD)
138 #define CALCULATOR_UP BUTTON_UP
139 #define CALCULATOR_DOWN BUTTON_DOWN
140 #define CALCULATOR_QUIT BUTTON_OFF
141 #define CALCULATOR_INPUT BUTTON_SELECT
142 #define CALCULATOR_CALC BUTTON_ON
143 #define CALCULATOR_OPERATORS BUTTON_MODE
144 #define CALCULATOR_CLEAR BUTTON_REC
146 #define CALCULATOR_RC_QUIT BUTTON_RC_STOP
148 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
149 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
150 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
152 #define CALCULATOR_UP BUTTON_SCROLL_BACK
153 #define CALCULATOR_DOWN BUTTON_SCROLL_FWD
154 #define CALCULATOR_QUIT BUTTON_MENU
155 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
156 #define CALCULATOR_INPUT (BUTTON_SELECT | BUTTON_REL)
157 #define CALCULATOR_CALC (BUTTON_PLAY | BUTTON_REPEAT)
159 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
161 #define CALCULATOR_UP BUTTON_UP
162 #define CALCULATOR_DOWN BUTTON_DOWN
163 #define CALCULATOR_QUIT BUTTON_POWER
164 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
165 #define CALCULATOR_INPUT (BUTTON_SELECT | BUTTON_REL)
166 #define CALCULATOR_CALC BUTTON_PLAY
167 #define CALCULATOR_CLEAR BUTTON_REC
169 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
171 #define CALCULATOR_UP BUTTON_UP
172 #define CALCULATOR_DOWN BUTTON_DOWN
173 #define CALCULATOR_QUIT BUTTON_POWER
174 #define CALCULATOR_INPUT_CALC_PRE BUTTON_MENU
175 #define CALCULATOR_INPUT (BUTTON_MENU | BUTTON_REL)
176 #define CALCULATOR_CALC BUTTON_SELECT
177 #define CALCULATOR_CLEAR BUTTON_A
179 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
180 (CONFIG_KEYPAD == SANSA_C200_PAD)
181 #define CALCULATOR_UP BUTTON_UP
182 #define CALCULATOR_DOWN BUTTON_DOWN
183 #define CALCULATOR_QUIT BUTTON_POWER
184 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
185 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
186 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
187 #define CALCULATOR_CLEAR BUTTON_REC
189 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
191 #define CALCULATOR_UP BUTTON_SCROLL_UP
192 #define CALCULATOR_DOWN BUTTON_SCROLL_DOWN
193 #define CALCULATOR_QUIT BUTTON_POWER
194 #define CALCULATOR_INPUT_CALC_PRE BUTTON_PLAY
195 #define CALCULATOR_INPUT (BUTTON_PLAY | BUTTON_REL)
196 #define CALCULATOR_CALC BUTTON_PLAY
197 #define CALCULATOR_CLEAR BUTTON_REW
199 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
201 #define CALCULATOR_UP BUTTON_UP
202 #define CALCULATOR_DOWN BUTTON_DOWN
203 #define CALCULATOR_QUIT BUTTON_BACK
204 #define CALCULATOR_INPUT_CALC_PRE BUTTON_MENU
205 #define CALCULATOR_INPUT (BUTTON_MENU | BUTTON_REL)
206 #define CALCULATOR_CALC BUTTON_SELECT
207 #define CALCULATOR_CLEAR BUTTON_PLAY
211 static struct plugin_api
* rb
;
217 unsigned char* buttonChar
[2][5][5] = {
218 { { "MR" , "M+" , "2nd" , "CE" , "C" },
219 { "7" , "8" , "9" , "/" , "sqr" },
220 { "4" , "5" , "6" , "*" , "x^2" },
221 { "1" , "2" , "3" , "-" , "1/x" },
222 { "0" , "+/-", "." , "+" , "=" } },
224 { { "n!" , "PI" , "1st" , "sin" , "asi" },
225 { "7" , "8" , "9" , "cos" , "aco" },
226 { "4" , "5" , "6" , "tan" , "ata" },
227 { "1" , "2" , "3" , "ln" , "e^x" },
228 { "0" , "+/-", "." , "log" , "x^y" } }
230 enum { btn_MR
, btn_M
, btn_bas
, btn_CE
, btn_C
,
231 btn_7
, btn_8
, btn_9
, btn_div
, btn_sqr
,
232 btn_4
, btn_5
, btn_6
, btn_time
, btn_square
,
233 btn_1
, btn_2
, btn_3
, btn_minus
, btn_rec
,
234 btn_0
, btn_sign
, btn_dot
, btn_add
, btn_equal
236 enum { sci_fac
, sci_pi
, sci_sci
, sci_sin
, sci_asin
,
237 sci_7
, sci_8
, sci_9
, sci_cos
, sci_acos
,
238 sci_4
, sci_5
, sci_6
, sci_tan
, sci_atan
,
239 sci_1
, sci_2
, sci_3
, sci_ln
, sci_exp
,
240 sci_0
, sci_sign
, sci_dot
, sci_log
, sci_xy
243 #define MINIMUM 0.000000000001 /* e-12 */
245 /* 123456789abcdef */
247 #define DIGITLEN 10 /* must <= 10 */
248 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
249 /* 0.000 00000 0001 */
251 /* DIGITLEN 12345 6789a bcdef */
252 /* power 12 34567 89abc def */
253 /* 10^- 123 45678 9abcd ef */
255 unsigned char buf
[19];/* 18 bytes of output line,
257 buf[1] = 'M' if memTemp is not 0
261 buf[2]-buf[12] or buf[3]-buf[13] = result;
263 buf[13] or buf[14] -buf[17] = power;
267 buf[7]-buf[17] = result;
271 unsigned char typingbuf
[DIGITLEN
+2];/* byte 0 is sign or ' ',
272 byte 1~DIGITLEN are num and '.'
273 byte (DIGITLEN+1) is '\0' */
274 unsigned char* typingbufPointer
= typingbuf
;
276 double result
= 0; /* main operand, format 0.xxxxx */
277 int power
= 0; /* 10^power */
278 double modifier
= 0.1; /* position of next input */
279 double operand
= 0; /* second operand, format 0.xxxxx */
280 int operandPower
= 0; /* 10^power of second operand */
281 char oper
= ' '; /* operators: + - * / */
282 bool operInputted
= false; /* false: do calculation first and
284 true: just replace current oper */
286 double memTemp
= 0; /* temp memory */
287 int memTempPower
= 0; /* 10^^power of memTemp */
289 int m
, n
, prev_m
, prev_n
; /* position index for button */
290 #define CAL_BUTTON (m*5+n)
292 int btn
= BUTTON_NONE
;
293 int lastbtn
= BUTTON_NONE
;
295 /* Status of calculator */
296 enum {cal_normal
, /* 0, normal status, display result */
297 cal_typing
, /* 1, currently typing, dot hasn't been typed */
298 cal_dotted
, /* 2, currently typing, dot already has been typed. */
304 /* constant table for CORDIC algorithm */
305 double cordicTable
[51][2]= {
306 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
307 {1e+00, 7.853981633974483e-01},
308 {5e-01, 4.636476090008061e-01},
309 {2.5e-01, 2.449786631268641e-01},
310 {1.25e-01, 1.243549945467614e-01},
311 {6.25e-02, 6.241880999595735e-02},
312 {3.125e-02, 3.123983343026828e-02},
313 {1.5625e-02, 1.562372862047683e-02},
314 {7.8125e-03, 7.812341060101111e-03},
315 {3.90625e-03, 3.906230131966972e-03},
316 {1.953125e-03, 1.953122516478819e-03},
317 {9.765625e-04, 9.765621895593195e-04},
318 {4.8828125e-04, 4.882812111948983e-04},
319 {2.44140625e-04, 2.441406201493618e-04},
320 {1.220703125e-04, 1.220703118936702e-04},
321 {6.103515625e-05, 6.103515617420877e-05},
322 {3.0517578125e-05, 3.051757811552610e-05},
323 {1.52587890625e-05, 1.525878906131576e-05},
324 {7.62939453125e-06, 7.629394531101970e-06},
325 {3.814697265625e-06, 3.814697265606496e-06},
326 {1.9073486328125e-06, 1.907348632810187e-06},
327 {9.5367431640625e-07, 9.536743164059608e-07},
328 {4.76837158203125e-07, 4.768371582030888e-07},
329 {2.384185791015625e-07, 2.384185791015580e-07},
330 {1.1920928955078125e-07, 1.192092895507807e-07},
331 {5.9604644775390625e-08, 5.960464477539055e-08},
332 {2.98023223876953125e-08, 2.980232238769530e-08},
333 {1.490116119384765625e-08, 1.490116119384765e-08},
334 {7.450580596923828125e-09, 7.450580596923828e-09},
335 {3.7252902984619140625e-09, 3.725290298461914e-09},
336 {1.86264514923095703125e-09, 1.862645149230957e-09},
337 {9.31322574615478515625e-10, 9.313225746154785e-10},
338 {4.656612873077392578125e-10, 4.656612873077393e-10},
339 {2.3283064365386962890625e-10, 2.328306436538696e-10},
340 {1.16415321826934814453125e-10, 1.164153218269348e-10},
341 {5.82076609134674072265625e-11, 5.820766091346741e-11},
342 {2.910383045673370361328125e-11, 2.910383045673370e-11},
343 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
344 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
345 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
346 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
347 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
348 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
349 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
350 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
351 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
352 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
353 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
354 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
355 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
356 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
357 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
360 void doMultiple(double* operandOne
, int* powerOne
,
361 double operandTwo
, int powerTwo
);
362 void doAdd (double* operandOne
, int* powerOne
,
363 double operandTwo
, int powerTwo
);
364 void printResult(void);
365 void formatResult(void);
366 void oneOperand(void);
368 /* -----------------------------------------------------------------------
370 ----------------------------------------------------------------------- */
371 void cleartypingbuf(void)
374 for( k
=1; k
<=(DIGITLEN
+1); k
++)
377 typingbufPointer
= typingbuf
+1;
386 void clearResult(void)
393 void clearInput(void)
395 calStatus
= cal_normal
;
400 void clearOperand(void)
406 void clearMemTemp(void)
415 operInputted
= false;
427 void switchOperands(void)
429 double tempr
= operand
;
430 int tempp
= operandPower
;
432 operandPower
= power
;
437 /* -----------------------------------------------------------------------
439 ----------------------------------------------------------------------- */
440 void cal_initial (void)
443 rb
->lcd_setfont(FONT_SYSFIXED
);
444 rb
->lcd_clear_display();
447 rb
->lcd_drawrect(X_0_POS
, Y_0_POS
, LCD_WIDTH
-1, LCD_HEIGHT
);
448 rb
->lcd_drawline(X_0_POS
, Y_1_POS
-1, X_5_POS
, Y_1_POS
-1);
449 for (i
= 0; i
< 5 ; i
++)
450 rb
->lcd_drawline(X_0_POS
, Y_1_POS
+i
*REC_HEIGHT
,
451 X_5_POS
, Y_1_POS
+i
*REC_HEIGHT
);
452 for (i
= 0; i
< 4 ; i
++)
453 rb
->lcd_drawline(X_1_POS
+i
*REC_WIDTH
, Y_1_POS
,
454 X_1_POS
+i
*REC_WIDTH
, Y_6_POS
);
456 #ifdef CALCULATOR_OPERATORS
457 /* basic operators are available through separate button */
458 buttonGroup
= sciButtons
;
460 buttonGroup
= basicButtons
;
463 for (i
= 0; i
< 5; i
++){
464 for (j
= 0; j
< 5; j
++){
465 rb
->lcd_getstringsize( buttonChar
[buttonGroup
][i
][j
],&w
,&h
);
466 rb
->lcd_putsxy( X_0_POS
+ j
*REC_WIDTH
+ (REC_WIDTH
- w
)/2,
467 TEXT_2_POS
+ i
*REC_HEIGHT
,
468 buttonChar
[buttonGroup
][i
][j
] );
472 /* initially, invert button "5" */
477 rb
->lcd_set_drawmode(DRMODE_COMPLEMENT
);
478 rb
->lcd_fillrect( X_0_POS
+ n
*REC_WIDTH
+ 1,
479 Y_1_POS
+ m
*REC_HEIGHT
+ 1,
480 REC_WIDTH
- 1, REC_HEIGHT
- 1);
481 rb
->lcd_set_drawmode(DRMODE_SOLID
);
484 /* initial mem and output display*/
488 /* clear button queue */
489 rb
->button_clear_queue();
492 /* -----------------------------------------------------------------------
493 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
494 in it's private case for sqrt.
495 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
496 ----------------------------------------------------------------------- */
497 double mySqrt(double square
)
501 double root
= ABS(square
+1)/2;
503 while( ABS(root
- temp
) > MINIMUM
){
505 root
= (square
/temp
+ temp
)/2;
507 if (k
>10000) return 0;
512 /* -----------------------------------------------------------------------
513 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
514 transcendFunc can do sin,cos,log,exp
515 input parameter is angle
516 ----------------------------------------------------------------------- */
517 void transcendFunc(char* func
, double* tt
, int* ttPower
)
519 double t
= (*tt
)*M_PI
/180; int tPower
= *ttPower
;
521 int n
= 50; /* n <=50, tables are all <= 50 */
523 double x
,y
,z
,xt
,yt
,zt
;
526 calStatus
= cal_normal
;
530 calStatus
= cal_error
;
534 calStatus
= cal_normal
;
536 if( func
[0] =='s' || func
[0] =='S'|| func
[0] =='t' || func
[0] =='T')
539 /* if( func[0] =='c' || func[0] =='C') */
553 while (t
> j
*M_TWOPI
) {j
++;}
555 if (M_PI_2
< t
&& t
< 3*M_PI_2
){
557 if (func
[0] =='c' || func
[0] =='C')
559 else if (func
[0] =='t' || func
[0] =='T')
562 else if ( 3*M_PI_2
<= t
&& t
<= M_TWOPI
)
565 x
= 0.60725293500888; y
= 0; z
= t
;
567 xt
= x
- SIGN(z
) * y
*cordicTable
[j
-1][0];
568 yt
= y
+ SIGN(z
) * x
*cordicTable
[j
-1][0];
569 zt
= z
- SIGN(z
) * cordicTable
[j
-1][1];
574 if( func
[0] =='s' || func
[0] =='S') {
578 else if( func
[0] =='c' || func
[0] =='C') {
582 else /*if( func[0] =='t' || func[0] =='T')*/ {
583 if(t
==M_PI_2
||t
==-M_PI_2
){
584 calStatus
= cal_error
;
594 /* -----------------------------------------------------------------------
595 add in scientific number format
596 ----------------------------------------------------------------------- */
597 void doAdd (double* operandOne
, int* powerOne
,
598 double operandTwo
, int powerTwo
)
600 if ( *powerOne
>= powerTwo
){
601 if (*powerOne
- powerTwo
<= DIGITLEN
+1){
602 while (powerTwo
< *powerOne
){
606 *operandOne
+= operandTwo
;
608 /*do nothing if operandTwo is too small*/
611 if (powerTwo
- *powerOne
<= DIGITLEN
+1){
612 while(powerTwo
> *powerOne
){
616 (*operandOne
) += operandTwo
;
618 else{/* simply copy operandTwo if operandOne is too small */
619 *operandOne
= operandTwo
;
620 *powerOne
= powerTwo
;
624 /* -----------------------------------------------------------------------
625 multiple in scientific number format
626 ----------------------------------------------------------------------- */
627 void doMultiple(double* operandOne
, int* powerOne
,
628 double operandTwo
, int powerTwo
)
630 (*operandOne
) *= operandTwo
;
631 (*powerOne
) += powerTwo
;
634 /* -----------------------------------------------------------------------
635 Handles all one operand calculations
636 ----------------------------------------------------------------------- */
637 void oneOperand(void)
640 if (buttonGroup
== basicButtons
){
644 calStatus
= cal_error
;
647 result
= (mySqrt(result
*10))/10;
648 power
= (power
+1) / 2;
651 result
= mySqrt(result
);
654 calStatus
= cal_normal
;
660 calStatus
= cal_normal
;
665 calStatus
= cal_error
;
669 calStatus
= cal_normal
;
673 calStatus
= cal_toDo
;
674 break; /* just for the safety */
677 else{ /* sciButtons */
680 transcendFunc("sin", &result
, &power
);
683 transcendFunc("cos", &result
, &power
);
686 transcendFunc("tan", &result
, &power
);
689 if (power
<0 || power
>8 || result
<0 )
690 calStatus
= cal_error
;
691 else if(result
== 0) {
700 if ( ( result
- (int)result
) > MINIMUM
)
701 calStatus
= cal_error
;
703 k
= result
; result
= 1;
705 doMultiple(&result
, &power
, k
, 0);
709 calStatus
= cal_normal
;
714 calStatus
= cal_toDo
;
715 break; /* just for the safety */
721 /* -----------------------------------------------------------------------
722 Handles all two operands calculations
723 ----------------------------------------------------------------------- */
724 void twoOperands(void)
728 doAdd(&operand
, &operandPower
, -result
, power
);
731 doAdd(&operand
, &operandPower
, result
, power
);
734 doMultiple(&operand
, &operandPower
, result
, power
);
737 if ( ABS(result
) > MINIMUM
){
738 doMultiple(&operand
, &operandPower
, 1/result
, -power
);
741 calStatus
= cal_error
;
744 switchOperands(); /* counter switchOperands() below */
750 /* -----------------------------------------------------------------------
752 Invert display new button, invert back previous button
753 ----------------------------------------------------------------------- */
754 void moveButton(void){
757 case BUTTON_LEFT
| BUTTON_REPEAT
:
765 case BUTTON_RIGHT
| BUTTON_REPEAT
:
773 case CALCULATOR_UP
| BUTTON_REPEAT
:
780 case CALCULATOR_DOWN
:
781 case CALCULATOR_DOWN
| BUTTON_REPEAT
:
789 rb
->lcd_set_drawmode(DRMODE_COMPLEMENT
);
790 rb
->lcd_fillrect( X_0_POS
+ prev_n
*REC_WIDTH
+ 1,
791 Y_1_POS
+ prev_m
*REC_HEIGHT
+ 1,
792 REC_WIDTH
- 1, REC_HEIGHT
- 1);
794 rb
->lcd_fillrect( X_0_POS
+ n
*REC_WIDTH
+ 1,
795 Y_1_POS
+ m
*REC_HEIGHT
+ 1,
796 REC_WIDTH
- 1, REC_HEIGHT
- 1);
797 rb
->lcd_set_drawmode(DRMODE_SOLID
);
799 rb
->lcd_update_rect( X_0_POS
+ prev_n
*REC_WIDTH
+ 1,
800 Y_1_POS
+ prev_m
*REC_HEIGHT
+ 1,
801 REC_WIDTH
- 1, REC_HEIGHT
- 1);
803 rb
->lcd_update_rect( X_0_POS
+ n
*REC_WIDTH
+ 1,
804 Y_1_POS
+ m
*REC_HEIGHT
+ 1,
805 REC_WIDTH
- 1, REC_HEIGHT
- 1);
810 /* -----------------------------------------------------------------------
811 Print buttons when switching 1st and 2nd
812 int group = {basicButtons, sciButtons}
813 ----------------------------------------------------------------------- */
814 void printButtonGroups(int group
)
817 for (i
= 0; i
< 5; i
++){
818 for (j
= 3; j
<= 4; j
++){
819 rb
->lcd_getstringsize( buttonChar
[group
][i
][j
],&w
,&h
);
820 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
821 rb
->lcd_fillrect( X_0_POS
+ j
*REC_WIDTH
+ 1,
822 Y_1_POS
+ i
*REC_HEIGHT
+ 1,
823 REC_WIDTH
- 1, REC_HEIGHT
- 1);
824 rb
->lcd_set_drawmode(DRMODE_SOLID
);
825 rb
->lcd_putsxy( X_0_POS
+ j
*REC_WIDTH
+ (REC_WIDTH
- w
)/2,
826 TEXT_2_POS
+ i
*REC_HEIGHT
,
827 buttonChar
[group
][i
][j
] );
830 for (i
= 0; i
<= 0; i
++){
831 for (j
= 0; j
<= 2; j
++){
832 rb
->lcd_getstringsize( buttonChar
[group
][i
][j
],&w
,&h
);
833 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
834 rb
->lcd_fillrect( X_0_POS
+ j
*REC_WIDTH
+ 1,
835 Y_1_POS
+ i
*REC_HEIGHT
+ 1,
836 REC_WIDTH
- 1, REC_HEIGHT
- 1);
837 rb
->lcd_set_drawmode(DRMODE_SOLID
);
838 rb
->lcd_putsxy( X_0_POS
+ j
*REC_WIDTH
+ (REC_WIDTH
- w
)/2,
839 TEXT_2_POS
+ i
*REC_HEIGHT
,
840 buttonChar
[group
][i
][j
] );
843 rb
->lcd_set_drawmode(DRMODE_COMPLEMENT
);
844 rb
->lcd_fillrect( X_0_POS
+ 2*REC_WIDTH
+ 1,
845 Y_1_POS
+ 0*REC_HEIGHT
+ 1,
846 REC_WIDTH
- 1, REC_HEIGHT
- 1);
847 rb
->lcd_set_drawmode(DRMODE_SOLID
);
848 rb
->lcd_update_rect( X_0_POS
, Y_1_POS
,
849 REC_WIDTH
*5, REC_HEIGHT
*5);
851 /* -----------------------------------------------------------------------
852 flash the button pressed
853 ----------------------------------------------------------------------- */
854 void flashButton(int b
)
856 int i
= b
/5; int j
= b
- i
*5;
858 rb
->lcd_set_drawmode(DRMODE_COMPLEMENT
);
860 rb
->lcd_fillrect( X_0_POS
+ j
*REC_WIDTH
+ 1,
861 Y_1_POS
+ i
*REC_HEIGHT
+ 1,
862 REC_WIDTH
- 1, REC_HEIGHT
- 1);
863 rb
->lcd_update_rect( X_0_POS
+ j
*REC_WIDTH
+ 1,
864 Y_1_POS
+ i
*REC_HEIGHT
+ 1,
865 REC_WIDTH
- 1, REC_HEIGHT
- 1);
871 rb
->lcd_set_drawmode(DRMODE_SOLID
);
874 /* -----------------------------------------------------------------------
875 pos is the position that needs animation. pos = [1~18]
876 ----------------------------------------------------------------------- */
877 void deleteAnimation(int pos
)
880 if (pos
<1 || pos
>18)
883 rb
->lcd_fillrect(1+pos
*6, TEXT_1_POS
, 6, 8);
884 rb
->lcd_update_rect(1+pos
*6, TEXT_1_POS
, 6, 8);
888 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
889 rb
->lcd_fillrect(1+pos
*6, TEXT_1_POS
, 6, 8);
890 rb
->lcd_set_drawmode(DRMODE_SOLID
);
891 rb
->lcd_fillrect(1+pos
*6+1+k
, TEXT_1_POS
+k
,
892 (5-2*k
)>0?(5-2*k
):1, (7-2*k
)>0?(7-2*k
):1 );
893 rb
->lcd_update_rect(1+pos
*6, TEXT_1_POS
, 6, 8);
898 /* -----------------------------------------------------------------------
899 result may be one of these formats:
905 formatResult() change result to standard format: 0.xxxx
906 if result is close to 0, let it be 0;
907 if result is close to 1, let it be 0.1 and power++;
908 ----------------------------------------------------------------------- */
909 void formatResult(void)
911 int resultsign
= SIGN(result
);
912 result
= ABS(result
);
913 if(result
> MINIMUM
){ /* doesn't check power, might have problem
915 + - * / of two formatted number wouldn't.
916 only a calculation that makes a formatted
917 number (0.xxxx) less than MINIMUM in only
921 while( (int)(result
*10) == 0 ){
927 else{ /* result >= 1 */
928 while( (int)result
!= 0 ){
935 if (result
> (1-MINIMUM
)){
940 result
*= resultsign
;
949 /* -----------------------------------------------------------------------
950 result2typingbuf() outputs standard format result to typingbuf.
951 case SCIENTIFIC_FORMAT, let temppower = 1;
952 case temppower > 0: print '.' in the middle
953 case temppower <= 0: print '.' in the begining
954 ----------------------------------------------------------------------- */
955 void result2typingbuf(void)
957 bool haveDot
= false;
960 double tempresult
= ABS(result
); /* positive num makes things simple */
963 double tempmodifier
= 1;
966 if(SCIENTIFIC_FORMAT
)
967 temppower
= 1; /* output x.xxxx format */
973 if(tempresult
< MINIMUM
){ /* if 0,faster display and avoid complication*/
977 else{ /* tempresult > 0 */
978 typingbuf
[0] = (SIGN(result
)<0)?'-':' ';
980 typingbufPointer
= typingbuf
;
982 for (k
= 0; k
<DIGITLEN
+1 ; k
++){
984 if(temppower
|| *(typingbufPointer
-1) == '.'){
986 tempmodifier
= tempmodifier
/10;
987 while( (tempresult
-tempmodifier
*count
) >
988 (tempmodifier
-MINIMUM
)){
991 tempresult
-= tempmodifier
*count
;
992 tempresult
= ABS(tempresult
);
994 *typingbufPointer
= count
+ '0';
996 else{ /* temppower == 0 */
997 *typingbufPointer
= '.';
1004 typingbufPointer
++; *typingbufPointer
= '0';
1005 typingbufPointer
++; *typingbufPointer
= '.';
1006 for (k
= 2; k
<DIGITLEN
+1 ; k
++){
1009 if ( (-temppower
) < (k
-1)){
1010 tempmodifier
= tempmodifier
/10;
1011 while((tempresult
-tempmodifier
*count
)>(tempmodifier
-MINIMUM
)){
1015 tempresult
-= tempmodifier
*count
;
1016 tempresult
= ABS(tempresult
);
1019 *typingbufPointer
= count
+ '0';
1022 /* now, typingbufPointer = typingbuf + 16 */
1023 /* backward strip off 0 and '.' */
1025 while( (*typingbufPointer
== '0') || (*typingbufPointer
== '.')){
1026 tempchar
= *typingbufPointer
;
1027 *typingbufPointer
= 0;
1029 if (tempchar
== '.') break;
1032 typingbuf
[DIGITLEN
+1] = 0;
1033 } /* else tempresult > 0 */
1036 /* -----------------------------------------------------------------------
1037 printResult() generates LCD display.
1038 ----------------------------------------------------------------------- */
1039 void printResult(void)
1046 rb
->lcd_clear_display();
1047 rb
->splash(HZ
/3, "Bye now!");
1051 rb
->snprintf(buf
, 19, "%18s","Error");
1055 rb
->snprintf(buf
, 19, "%18s","Coming soon ^_* ");
1061 if( power
> 1000 ){ /* power -1 > 999 */
1062 calStatus
= cal_error
;
1065 if (power
< -998 ) /* power -1 < -999 */
1066 clearResult(); /* too small, let it be 0 */
1072 buf
[1] = ( ABS(memTemp
) > MINIMUM
)?'M':' ';
1075 if(SCIENTIFIC_FORMAT
){
1076 /* output format: X.XXXX eXXX */
1077 if(power
> -98){ /* power-1 >= -99, eXXX or e-XX */
1078 rb
->snprintf(buf
+3, 12, "%11s",typingbuf
);
1079 for(k
=14;k
<=17;k
++) buf
[k
] = ' ';
1081 rb
->snprintf(typingbuf
, 5, "e%d",power
-1);
1082 rb
->snprintf(buf
+14, 5, "%4s",typingbuf
);
1084 else{ /* power-1 <= -100, e-XXX */
1085 rb
->snprintf(buf
+2, 12, "%11s",typingbuf
);
1086 rb
->snprintf(buf
+13, 6, "e%d",power
-1);
1090 rb
->snprintf(buf
+7, 12, "%11s",typingbuf
);
1091 } /* if SCIENTIFIC_FORMAT */
1097 buf
[1] = ( ABS(memTemp
) > MINIMUM
)?'M':' ';
1100 rb
->snprintf(buf
+7, 12, "%11s",typingbuf
);
1105 rb
->lcd_putsxy(1, TEXT_1_POS
,buf
);
1106 rb
->lcd_update_rect(1, TEXT_1_POS
, 6*18, 8);
1109 /* -----------------------------------------------------------------------
1110 Process typing buttons: 1-9, '.', sign
1111 main operand "result" and typingbuf are processed seperately here.
1112 ----------------------------------------------------------------------- */
1113 void typingProcess(void){
1114 switch( CAL_BUTTON
){
1116 if (calStatus
== cal_typing
||
1117 calStatus
== cal_dotted
)
1118 typingbuf
[0] = (typingbuf
[0]=='-')?' ':'-';
1122 operInputted
= false;
1126 *typingbufPointer
= '0';
1129 calStatus
= cal_dotted
;
1130 *typingbufPointer
= '.';
1131 if (typingbufPointer
!= typingbuf
+DIGITLEN
+1)
1134 default: /* cal_dotted */
1139 operInputted
= false;
1140 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1143 if(CAL_BUTTON
== btn_0
)
1144 break; /* first input is 0, ignore */
1146 /*no operator means start a new calculation*/
1149 calStatus
= cal_typing
;
1150 /* go on typing, no break */
1155 *typingbufPointer
= '0';
1158 *typingbufPointer
=(7+n
-3*(m
-1))+ '0';
1161 if (typingbufPointer
!=typingbuf
+DIGITLEN
+1){
1164 {/* result processing */
1165 if (calStatus
== cal_typing
) power
++;
1166 if (CAL_BUTTON
!= btn_0
)
1169 (7+n
-3*(m
-1))*modifier
;
1173 else /* last byte always '\0' */
1174 *typingbufPointer
= 0;
1176 default: /* cal_error, cal_exit */
1179 break; /* default, 0-9 */
1180 } /* switch( CAL_BUTTON ) */
1183 /* -----------------------------------------------------------------------
1184 Handle delete operation
1185 main operand "result" and typingbuf are processed seperately here.
1186 ----------------------------------------------------------------------- */
1187 void doDelete(void){
1188 deleteAnimation(18);
1191 if (*(typingbufPointer
-1) == '.'){
1192 /* if dotted and deleting '.',
1193 change status and delete '.' below */
1194 calStatus
= cal_typing
;
1196 else{ /* if dotted and not deleting '.',
1198 power
++; /* counter "power--;" below */
1203 {/* result processing */ /* 0-9, '.' */
1204 /* if deleting '.', do nothing */
1205 if ( *typingbufPointer
!= '.'){
1208 result
= result
- SIGN(result
)*
1209 ((*typingbufPointer
)- '0')*modifier
;
1213 *typingbufPointer
= 0;
1215 /* if (only one digit left and it's 0)
1216 or no digit left, change status*/
1217 if ( typingbufPointer
== typingbuf
+1 ||
1218 ( typingbufPointer
== typingbuf
+2 &&
1219 *(typingbufPointer
-1) == '0' ))
1220 calStatus
= cal_normal
;
1222 default: /* normal, error, exit */
1226 /* -----------------------------------------------------------------------
1227 Handle buttons on basic screen
1228 ----------------------------------------------------------------------- */
1229 void basicButtonsProcess(void){
1231 case CALCULATOR_INPUT
:
1232 if (calStatus
== cal_error
&& (CAL_BUTTON
!= btn_C
) ) break;
1233 flashButton(CAL_BUTTON
);
1234 switch( CAL_BUTTON
){
1236 operInputted
= false;
1237 result
= memTemp
; power
= memTempPower
;
1238 calStatus
= cal_normal
;
1242 if (memTemp
> MINIMUM
)
1243 doAdd(&memTemp
, &memTempPower
, result
, power
);
1245 /* if result is too small and memTemp = 0,
1246 doAdd will not add */
1248 memTempPower
= power
;
1250 calStatus
= cal_normal
;
1253 case btn_C
: clearMem(); break;
1254 case btn_CE
: clearInput(); break;
1257 buttonGroup
= sciButtons
;
1258 printButtonGroups(buttonGroup
);
1261 /* one operand calculation, may be changed to
1262 like sin, cos, log, etc */
1266 formatResult(); /* not necessary, just for safty */
1270 case_btn_equal
: /* F3 shortkey entrance */
1273 calStatus
= cal_normal
;
1274 operInputted
= false;
1275 if (oper
!= ' ') twoOperands();
1282 if(!operInputted
) {twoOperands(); operInputted
= true;}
1283 oper
= buttonChar
[basicButtons
][m
][n
][0];
1284 #ifdef CALCULATOR_OPERATORS
1285 case_cycle_operators
: /* F2 shortkey entrance */
1287 calStatus
= cal_normal
;
1290 operandPower
= power
;
1299 } /* switch (CAL_BUTTON) */
1302 #ifdef CALCULATOR_OPERATORS
1303 case CALCULATOR_OPERATORS
:
1304 if (calStatus
== cal_error
) break;
1305 if (!operInputted
) {twoOperands(); operInputted
= true;}
1308 case '/': oper
= '+'; flashButton(btn_add
); break;
1309 case '+': oper
= '-'; flashButton(btn_minus
); break;
1310 case '-': oper
= '*'; flashButton(btn_time
); break;
1311 case '*': oper
= '/'; flashButton(btn_div
); break;
1313 goto case_cycle_operators
;
1317 case CALCULATOR_CALC
:
1318 if (calStatus
== cal_error
) break;
1319 flashButton(btn_equal
);
1320 goto case_btn_equal
;
1327 /* -----------------------------------------------------------------------
1328 Handle buttons on scientific screen
1329 ----------------------------------------------------------------------- */
1330 void sciButtonsProcess(void){
1332 case CALCULATOR_INPUT
:
1333 if (calStatus
== cal_error
&& (CAL_BUTTON
!= sci_sci
) ) break;
1334 flashButton(CAL_BUTTON
);
1335 switch( CAL_BUTTON
){
1338 result
= M_PI
; power
= 0;
1339 calStatus
= cal_normal
;
1345 buttonGroup
= basicButtons
;
1346 printButtonGroups(basicButtons
);
1359 formatResult(); /* not necessary, just for safty */
1368 } /* switch (CAL_BUTTON) */
1371 #ifdef CALCULATOR_OPERATORS
1372 case CALCULATOR_OPERATORS
:
1373 if (calStatus
== cal_error
) break;
1374 if (!operInputted
) {twoOperands(); operInputted
= true;}
1376 case ' ': oper
= '+'; break;
1377 case '/': oper
= '+'; deleteAnimation(1); break;
1378 case '+': oper
= '-'; deleteAnimation(1); break;
1379 case '-': oper
= '*'; deleteAnimation(1); break;
1380 case '*': oper
= '/'; deleteAnimation(1); break;
1382 calStatus
= cal_normal
;
1385 operandPower
= power
;
1389 case CALCULATOR_CALC
:
1390 if (calStatus
== cal_error
) break;
1392 calStatus
= cal_normal
;
1393 operInputted
= false;
1394 if (oper
!= ' ') twoOperands();
1401 /* -----------------------------------------------------------------------
1403 ----------------------------------------------------------------------- */
1404 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
1409 /* now go ahead and have fun! */
1413 while (calStatus
!= cal_exit
) {
1414 btn
= rb
->button_get_w_tmo(HZ
/2);
1416 case CALCULATOR_INPUT
:
1417 case CALCULATOR_CALC
:
1418 #ifdef CALCULATOR_INPUT_CALC_PRE
1419 if (lastbtn
!= CALCULATOR_INPUT_CALC_PRE
)
1421 /* no unconditional break; here! */
1423 #ifdef CALCULATOR_OPERATORS
1424 case CALCULATOR_OPERATORS
:
1426 switch(buttonGroup
){
1428 basicButtonsProcess();
1431 sciButtonsProcess();
1436 #ifdef CALCULATOR_CLEAR
1437 case CALCULATOR_CLEAR
:
1443 default: /* cal_normal, cal_error, cal_exit */
1452 case BUTTON_LEFT
| BUTTON_REPEAT
:
1454 case BUTTON_RIGHT
| BUTTON_REPEAT
:
1456 case CALCULATOR_UP
| BUTTON_REPEAT
:
1457 case CALCULATOR_DOWN
:
1458 case CALCULATOR_DOWN
| BUTTON_REPEAT
:
1461 #ifdef CALCULATOR_RC_QUIT
1462 case CALCULATOR_RC_QUIT
:
1464 case CALCULATOR_QUIT
:
1465 calStatus
= cal_exit
;
1469 if(rb
->default_event_handler(btn
) == SYS_USB_CONNECTED
)
1470 return PLUGIN_USB_CONNECTED
;
1472 } /* switch (btn) */
1473 if (btn
!= BUTTON_NONE
)
1475 } /* while (calStatus != cal_exit ) */
1477 /* rb->splash(HZ*2, "Hello world!"); */
1478 rb
->button_clear_queue();
1482 #endif /* #ifdef HAVE_LCD_BITMAP */