Add 2008 to the copyright notice.
[Rockbox.git] / apps / plugins / calculator.c
blobb16094203c5e82943a84d335f70eaba3dee6f0ba
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 |-----------|-----------|-----------|-----------|-----------|
23 01 | | | | | |
24 |***********|***********|***********|***********|***********|
25 |***********|***********|***********|***********|***********|
26 11 | | | | | |
27 12 |-----------|-----------|-----------|-----------|-----------|
28 13 |-----------|-----------|-----------|-----------|-----------| y1
29 14 | | | | | |
30 | | | | | |
31 22 | | | | | |
32 23 |-----------|-----------|-----------|-----------|-----------| y2
33 24 | | | | | |
34 | | | | | |
35 32 | | | | | |
36 33 |-----------|-----------|-----------|-----------|-----------| y3
37 34 | | | | | |
38 | | | | | |
39 42 | | | | | |
40 43 |-----------|-----------|-----------|-----------|-----------| y4
41 44 | | | | | |
42 | | | | | |
43 52 | | | | | |
44 53 |-----------|-----------|-----------|-----------|-----------| y5
45 54 | | | | | |
46 | | | | | |
47 62 | | | | | |
48 63 |-----------|-----------|-----------|-----------|-----------| y6
49 x0 x1 x2 x3 x4 x5
52 /*---------------------------------------------------------------------------
53 Features:
54 - Scientific number format core code. Support range 10^-999 ~ 10^999
55 - Number of significant figures up to 10
57 Limitations:
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.
64 User Instructions:
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 "+, -, *, /"
68 F3: equal to "="
70 "MR" : load temp memory
71 "M+" : add currently display to temp memory
72 "C" : reset calculator
73 ---------------------------------------------------------------------------*/
75 #include "plugin.h"
76 #ifdef HAVE_LCD_BITMAP
77 #include "math.h"
79 PLUGIN_HEADER
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
209 #endif
211 static struct plugin_api* rb;
213 enum {
214 basicButtons,
215 sciButtons
216 } buttonGroup;
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 */
244 /* ^ ^ ^ ^ */
245 /* 123456789abcdef */
247 #define DIGITLEN 10 /* must <= 10 */
248 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
249 /* 0.000 00000 0001 */
250 /* ^ ^ ^ ^ ^ ^ */
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,
256 buf[0] is operator
257 buf[1] = 'M' if memTemp is not 0
258 buf[2] = ' '
260 if SCIENTIFIC_FORMAT
261 buf[2]-buf[12] or buf[3]-buf[13] = result;
262 format X.XXXXXXXX
263 buf[13] or buf[14] -buf[17] = power;
264 format eXXX or e-XXX
265 else
266 buf[3]-buf[6] = ' ';
267 buf[7]-buf[17] = result;
269 buf[18] = '\0' */
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
283 replace current oper
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. */
299 cal_error,
300 cal_exit,
301 cal_toDo
302 } calStatus;
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 /* -----------------------------------------------------------------------
369 Handy funtions
370 ----------------------------------------------------------------------- */
371 void cleartypingbuf(void)
373 int k;
374 for( k=1; k<=(DIGITLEN+1); k++)
375 typingbuf[k] = 0;
376 typingbuf[0] = ' ';
377 typingbufPointer = typingbuf+1;
379 void clearbuf(void)
381 int k;
382 for(k=0;k<18;k++)
383 buf[k]=' ';
384 buf[18] = 0;
386 void clearResult(void)
388 result = 0;
389 power = 0;
390 modifier = 0.1;
393 void clearInput(void)
395 calStatus = cal_normal;
396 clearResult();
397 cleartypingbuf();
400 void clearOperand(void)
402 operand = 0;
403 operandPower = 0;
406 void clearMemTemp(void)
408 memTemp = 0;
409 memTempPower = 0;
412 void clearOper(void)
414 oper = ' ';
415 operInputted = false;
418 void clearMem(void)
420 clearInput();
421 clearMemTemp();
422 clearOperand();
423 clearOper();
424 btn = BUTTON_NONE;
427 void switchOperands(void)
429 double tempr = operand;
430 int tempp = operandPower;
431 operand = result;
432 operandPower = power;
433 result = tempr;
434 power = tempp;
437 /* -----------------------------------------------------------------------
438 Initiate calculator
439 ----------------------------------------------------------------------- */
440 void cal_initial (void)
442 int i,j,w,h;
443 rb->lcd_setfont(FONT_SYSFIXED);
444 rb->lcd_clear_display();
446 /* draw lines */
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;
459 #else
460 buttonGroup = basicButtons;
461 #endif
462 /* draw buttons */
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" */
473 m = 2;
474 n = 1;
475 prev_m = m;
476 prev_n = n;
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);
482 rb->lcd_update();
484 /* initial mem and output display*/
485 clearMem();
486 printResult();
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)
499 int k = 0;
500 double temp = 0;
501 double root= ABS(square+1)/2;
503 while( ABS(root - temp) > MINIMUM ){
504 temp = root;
505 root = (square/temp + temp)/2;
506 k++;
507 if (k>10000) return 0;
510 return root;
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;
520 int sign = 1;
521 int n = 50; /* n <=50, tables are all <= 50 */
522 int j;
523 double x,y,z,xt,yt,zt;
525 if (tPower < -998) {
526 calStatus = cal_normal;
527 return;
529 if (tPower > 8) {
530 calStatus = cal_error;
531 return;
533 *ttPower = 0;
534 calStatus = cal_normal;
536 if( func[0] =='s' || func[0] =='S'|| func[0] =='t' || func[0] =='T')
537 sign = SIGN(t);
538 else {
539 /* if( func[0] =='c' || func[0] =='C') */
540 sign = 1;
542 t = ABS(t);
544 while (tPower > 0){
545 t *= 10;
546 tPower--;
548 while (tPower < 0) {
549 t /= 10;
550 tPower++;
552 j = 0;
553 while (t > j*M_TWOPI) {j++;}
554 t -= (j-1)*M_TWOPI;
555 if (M_PI_2 < t && t < 3*M_PI_2){
556 t = M_PI - t;
557 if (func[0] =='c' || func[0] =='C')
558 sign = -1;
559 else if (func[0] =='t' || func[0] =='T')
560 t*=-1;
562 else if ( 3*M_PI_2 <= t && t <= M_TWOPI)
563 t -= M_TWOPI;
565 x = 0.60725293500888; y = 0; z = t;
566 for (j=1;j<n+2;j++){
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];
570 x = xt;
571 y=yt;
572 z=zt;
574 if( func[0] =='s' || func[0] =='S') {
575 *tt = sign*y;
576 return;
578 else if( func[0] =='c' || func[0] =='C') {
579 *tt = sign*x;
580 return;
582 else /*if( func[0] =='t' || func[0] =='T')*/ {
583 if(t==M_PI_2||t==-M_PI_2){
584 calStatus = cal_error;
585 return;
587 else{
588 *tt = sign*(y/x);
589 return;
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){
603 operandTwo /=10;
604 powerTwo++;
606 *operandOne += operandTwo;
608 /*do nothing if operandTwo is too small*/
610 else{
611 if (powerTwo - *powerOne <= DIGITLEN+1){
612 while(powerTwo > *powerOne){
613 *operandOne /=10;
614 (*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)
639 int k = 0;
640 if (buttonGroup == basicButtons){
641 switch(CAL_BUTTON){
642 case btn_sqr:
643 if (result<0)
644 calStatus = cal_error;
645 else{
646 if (power%2 == 1){
647 result = (mySqrt(result*10))/10;
648 power = (power+1) / 2;
650 else{
651 result = mySqrt(result);
652 power = power / 2;
654 calStatus = cal_normal;
656 break;
657 case btn_square:
658 power *= 2;
659 result *= result;
660 calStatus = cal_normal;
661 break;
663 case btn_rec:
664 if (result==0)
665 calStatus = cal_error;
666 else{
667 power = -power;
668 result = 1/result;
669 calStatus = cal_normal;
671 break;
672 default:
673 calStatus = cal_toDo;
674 break; /* just for the safety */
677 else{ /* sciButtons */
678 switch(CAL_BUTTON){
679 case sci_sin:
680 transcendFunc("sin", &result, &power);
681 break;
682 case sci_cos:
683 transcendFunc("cos", &result, &power);
684 break;
685 case sci_tan:
686 transcendFunc("tan", &result, &power);
687 break;
688 case sci_fac:
689 if (power<0 || power>8 || result<0 )
690 calStatus = cal_error;
691 else if(result == 0) {
692 result = 1;
693 power = 0;
695 else{
696 while(power > 0) {
697 result *= 10;
698 power--;
700 if ( ( result - (int)result) > MINIMUM )
701 calStatus = cal_error;
702 else {
703 k = result; result = 1;
704 while (k > 1){
705 doMultiple(&result, &power, k, 0);
706 formatResult();
707 k--;
709 calStatus = cal_normal;
712 break;
713 default:
714 calStatus = cal_toDo;
715 break; /* just for the safety */
721 /* -----------------------------------------------------------------------
722 Handles all two operands calculations
723 ----------------------------------------------------------------------- */
724 void twoOperands(void)
726 switch(oper){
727 case '-':
728 doAdd(&operand, &operandPower, -result, power);
729 break;
730 case '+':
731 doAdd(&operand, &operandPower, result, power);
732 break;
733 case '*':
734 doMultiple(&operand, &operandPower, result, power);
735 break;
736 case '/':
737 if ( ABS(result) > MINIMUM ){
738 doMultiple(&operand, &operandPower, 1/result, -power);
740 else
741 calStatus = cal_error;
742 break;
743 default: /* ' ' */
744 switchOperands(); /* counter switchOperands() below */
745 break;
746 } /* switch(oper) */
747 switchOperands();
748 clearOper();
750 /* -----------------------------------------------------------------------
751 move button index
752 Invert display new button, invert back previous button
753 ----------------------------------------------------------------------- */
754 void moveButton(void){
755 switch(btn){
756 case BUTTON_LEFT:
757 case BUTTON_LEFT | BUTTON_REPEAT:
758 if (n == 0)
759 n = 4;
760 else
761 n--;
762 break;
764 case BUTTON_RIGHT:
765 case BUTTON_RIGHT | BUTTON_REPEAT:
766 if (n == 4)
767 n = 0;
768 else
769 n++;
770 break;
772 case CALCULATOR_UP:
773 case CALCULATOR_UP | BUTTON_REPEAT:
774 if (m == 0)
775 m = 4;
776 else
777 m--;
778 break;
780 case CALCULATOR_DOWN:
781 case CALCULATOR_DOWN | BUTTON_REPEAT:
782 if (m == 4)
783 m = 0;
784 else
785 m++;
786 break;
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);
807 prev_m = m;
808 prev_n = n;
810 /* -----------------------------------------------------------------------
811 Print buttons when switching 1st and 2nd
812 int group = {basicButtons, sciButtons}
813 ----------------------------------------------------------------------- */
814 void printButtonGroups(int group)
816 int i,j,w,h;
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;
857 int k;
858 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
859 for (k=1*2;k>0;k--){
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);
867 if (k!= 1)
868 rb->sleep(HZ/22);
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)
879 int k;
880 if (pos<1 || pos >18)
881 return;
882 pos--;
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);
886 for (k=1;k<=4;k++){
887 rb->sleep(HZ/32);
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:
901 xxxx.xxxx
902 0.xxxx
903 0.0000xxxx
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
914 input wouldn't,
915 + - * / of two formatted number wouldn't.
916 only a calculation that makes a formatted
917 number (0.xxxx) less than MINIMUM in only
918 one operation */
920 if (result<1){
921 while( (int)(result*10) == 0 ){
922 result *= 10;
923 power--;
924 modifier *= 10;
927 else{ /* result >= 1 */
928 while( (int)result != 0 ){
929 result /= 10;
930 power++;
931 modifier /= 10;
933 } /* if result<1 */
935 if (result > (1-MINIMUM)){
936 result = 0.1;
937 power++;
938 modifier /= 10;
940 result *= resultsign;
942 else {
943 result = 0;
944 power = 0;
945 modifier = 0.1;
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;
958 char tempchar = 0;
959 int k;
960 double tempresult = ABS(result); /* positive num makes things simple */
962 int temppower;
963 double tempmodifier = 1;
964 int count;
966 if(SCIENTIFIC_FORMAT)
967 temppower = 1; /* output x.xxxx format */
968 else
969 temppower = power;
971 cleartypingbuf();
973 if(tempresult < MINIMUM){ /* if 0,faster display and avoid complication*/
974 typingbuf[0] = ' ';
975 typingbuf[1] = '0';
977 else{ /* tempresult > 0 */
978 typingbuf[0] = (SIGN(result)<0)?'-':' ';
980 typingbufPointer = typingbuf;
981 if(temppower > 0){
982 for (k = 0; k<DIGITLEN+1 ; k++){
983 typingbufPointer++;
984 if(temppower || *(typingbufPointer-1) == '.'){
985 count = 0;
986 tempmodifier = tempmodifier/10;
987 while( (tempresult-tempmodifier*count) >
988 (tempmodifier-MINIMUM)){
989 count++;
991 tempresult -= tempmodifier*count;
992 tempresult = ABS(tempresult);
993 temppower-- ;
994 *typingbufPointer = count + '0';
996 else{ /* temppower == 0 */
997 *typingbufPointer = '.';
998 haveDot = true;
1000 } /* for */
1002 else{
1003 haveDot = true;
1004 typingbufPointer++; *typingbufPointer = '0';
1005 typingbufPointer++; *typingbufPointer = '.';
1006 for (k = 2; k<DIGITLEN+1 ; k++){
1007 typingbufPointer++;
1008 count = 0;
1009 if ( (-temppower) < (k-1)){
1010 tempmodifier = tempmodifier/10;
1011 while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){
1012 count++;
1015 tempresult -= tempmodifier*count;
1016 tempresult = ABS(tempresult);
1017 temppower-- ;
1019 *typingbufPointer = count + '0';
1022 /* now, typingbufPointer = typingbuf + 16 */
1023 /* backward strip off 0 and '.' */
1024 if (haveDot){
1025 while( (*typingbufPointer == '0') || (*typingbufPointer == '.')){
1026 tempchar = *typingbufPointer;
1027 *typingbufPointer = 0;
1028 typingbufPointer--;
1029 if (tempchar == '.') break;
1032 typingbuf[DIGITLEN+1] = 0;
1033 } /* else tempresult > 0 */
1036 /* -----------------------------------------------------------------------
1037 printResult() generates LCD display.
1038 ----------------------------------------------------------------------- */
1039 void printResult(void)
1041 int k;
1043 switch_Status:
1044 switch(calStatus){
1045 case cal_exit:
1046 rb->lcd_clear_display();
1047 rb->splash(HZ/3, "Bye now!");
1048 break;
1049 case cal_error:
1050 clearbuf();
1051 rb->snprintf(buf, 19, "%18s","Error");
1052 break;
1053 case cal_toDo:
1054 clearbuf();
1055 rb->snprintf(buf, 19, "%18s","Coming soon ^_* ");
1056 break;
1058 case cal_normal:
1059 formatResult();
1061 if( power > 1000 ){ /* power -1 > 999 */
1062 calStatus = cal_error;
1063 goto switch_Status;
1065 if (power < -998 ) /* power -1 < -999 */
1066 clearResult(); /* too small, let it be 0 */
1068 result2typingbuf();
1069 clearbuf();
1071 buf[0] = oper;
1072 buf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1073 buf[2] = ' ';
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] = ' ';
1080 cleartypingbuf();
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);
1089 else{
1090 rb->snprintf(buf+7, 12, "%11s",typingbuf);
1091 } /* if SCIENTIFIC_FORMAT */
1092 break;
1093 case cal_typing:
1094 case cal_dotted:
1095 clearbuf();
1096 buf[0] = oper;
1097 buf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1098 for(k=2;k<=6;k++)
1099 buf[k] = ' ';
1100 rb->snprintf(buf+7, 12, "%11s",typingbuf);
1101 break;
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 ){
1115 case btn_sign:
1116 if (calStatus == cal_typing ||
1117 calStatus == cal_dotted)
1118 typingbuf[0] = (typingbuf[0]=='-')?' ':'-';
1119 result = -result;
1120 break;
1121 case btn_dot:
1122 operInputted = false;
1123 switch(calStatus){
1124 case cal_normal:
1125 clearInput();
1126 *typingbufPointer = '0';
1127 typingbufPointer++;
1128 case cal_typing:
1129 calStatus = cal_dotted;
1130 *typingbufPointer = '.';
1131 if (typingbufPointer != typingbuf+DIGITLEN+1)
1132 typingbufPointer++;
1133 break;
1134 default: /* cal_dotted */
1135 break;
1137 break;
1138 default: /* 0-9 */
1139 operInputted = false;
1140 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1141 switch(calStatus){
1142 case cal_normal:
1143 if(CAL_BUTTON == btn_0 )
1144 break; /* first input is 0, ignore */
1145 clearInput();
1146 /*no operator means start a new calculation*/
1147 if (oper ==' ')
1148 clearOperand();
1149 calStatus = cal_typing;
1150 /* go on typing, no break */
1151 case cal_typing:
1152 case cal_dotted:
1153 switch(CAL_BUTTON){
1154 case btn_0:
1155 *typingbufPointer = '0';
1156 break;
1157 default:
1158 *typingbufPointer=(7+n-3*(m-1))+ '0';
1159 break;
1161 if (typingbufPointer!=typingbuf+DIGITLEN+1){
1162 typingbufPointer++;
1164 {/* result processing */
1165 if (calStatus == cal_typing) power++;
1166 if (CAL_BUTTON != btn_0)
1167 result= result +
1168 SIGN(result)*
1169 (7+n-3*(m-1))*modifier;
1170 modifier /= 10;
1173 else /* last byte always '\0' */
1174 *typingbufPointer = 0;
1175 break;
1176 default: /* cal_error, cal_exit */
1177 break;
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);
1189 switch(calStatus){
1190 case cal_dotted:
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 '.',
1197 power stays */
1198 power++; /* counter "power--;" below */
1200 case cal_typing:
1201 typingbufPointer--;
1203 {/* result processing */ /* 0-9, '.' */
1204 /* if deleting '.', do nothing */
1205 if ( *typingbufPointer != '.'){
1206 power--;
1207 modifier *= 10;
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;
1221 break;
1222 default: /* normal, error, exit */
1223 break;
1226 /* -----------------------------------------------------------------------
1227 Handle buttons on basic screen
1228 ----------------------------------------------------------------------- */
1229 void basicButtonsProcess(void){
1230 switch (btn) {
1231 case CALCULATOR_INPUT:
1232 if (calStatus == cal_error && (CAL_BUTTON != btn_C) ) break;
1233 flashButton(CAL_BUTTON);
1234 switch( CAL_BUTTON ){
1235 case btn_MR:
1236 operInputted = false;
1237 result = memTemp; power = memTempPower;
1238 calStatus = cal_normal;
1239 break;
1240 case btn_M:
1241 formatResult();
1242 if (memTemp > MINIMUM)
1243 doAdd(&memTemp, &memTempPower, result, power);
1244 else {
1245 /* if result is too small and memTemp = 0,
1246 doAdd will not add */
1247 memTemp = result;
1248 memTempPower = power;
1250 calStatus = cal_normal;
1251 break;
1253 case btn_C: clearMem(); break;
1254 case btn_CE: clearInput(); break;
1256 case btn_bas:
1257 buttonGroup = sciButtons;
1258 printButtonGroups(buttonGroup);
1259 break;
1261 /* one operand calculation, may be changed to
1262 like sin, cos, log, etc */
1263 case btn_sqr:
1264 case btn_square:
1265 case btn_rec:
1266 formatResult(); /* not necessary, just for safty */
1267 oneOperand();
1268 break;
1270 case_btn_equal: /* F3 shortkey entrance */
1271 case btn_equal:
1272 formatResult();
1273 calStatus = cal_normal;
1274 operInputted = false;
1275 if (oper != ' ') twoOperands();
1276 break;
1278 case btn_div:
1279 case btn_time:
1280 case btn_minus:
1281 case btn_add:
1282 if(!operInputted) {twoOperands(); operInputted = true;}
1283 oper = buttonChar[basicButtons][m][n][0];
1284 #ifdef CALCULATOR_OPERATORS
1285 case_cycle_operators: /* F2 shortkey entrance */
1286 #endif
1287 calStatus = cal_normal;
1288 formatResult();
1289 operand = result;
1290 operandPower = power;
1292 break;
1294 case btn_sign:
1295 case btn_dot:
1296 default: /* 0-9 */
1297 typingProcess();
1298 break;
1299 } /* switch (CAL_BUTTON) */
1300 break;
1302 #ifdef CALCULATOR_OPERATORS
1303 case CALCULATOR_OPERATORS:
1304 if (calStatus == cal_error) break;
1305 if (!operInputted) {twoOperands(); operInputted = true;}
1306 switch (oper){
1307 case ' ':
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;
1314 break;
1315 #endif
1317 case CALCULATOR_CALC:
1318 if (calStatus == cal_error) break;
1319 flashButton(btn_equal);
1320 goto case_btn_equal;
1321 break;
1322 default: break;
1324 printResult();
1327 /* -----------------------------------------------------------------------
1328 Handle buttons on scientific screen
1329 ----------------------------------------------------------------------- */
1330 void sciButtonsProcess(void){
1331 switch (btn) {
1332 case CALCULATOR_INPUT:
1333 if (calStatus == cal_error && (CAL_BUTTON != sci_sci) ) break;
1334 flashButton(CAL_BUTTON);
1335 switch( CAL_BUTTON ){
1337 case sci_pi:
1338 result = M_PI; power = 0;
1339 calStatus = cal_normal;
1340 break;
1342 case sci_xy: break;
1344 case sci_sci:
1345 buttonGroup = basicButtons;
1346 printButtonGroups(basicButtons);
1347 break;
1349 case sci_fac:
1350 case sci_sin:
1351 case sci_asin:
1352 case sci_cos:
1353 case sci_acos:
1354 case sci_tan:
1355 case sci_atan:
1356 case sci_ln:
1357 case sci_exp:
1358 case sci_log:
1359 formatResult(); /* not necessary, just for safty */
1360 oneOperand();
1361 break;
1363 case btn_sign:
1364 case btn_dot:
1365 default: /* 0-9 */
1366 typingProcess();
1367 break;
1368 } /* switch (CAL_BUTTON) */
1369 break;
1371 #ifdef CALCULATOR_OPERATORS
1372 case CALCULATOR_OPERATORS:
1373 if (calStatus == cal_error) break;
1374 if (!operInputted) {twoOperands(); operInputted = true;}
1375 switch (oper){
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;
1383 formatResult();
1384 operand = result;
1385 operandPower = power;
1386 break;
1387 #endif
1389 case CALCULATOR_CALC:
1390 if (calStatus == cal_error) break;
1391 formatResult();
1392 calStatus = cal_normal;
1393 operInputted = false;
1394 if (oper != ' ') twoOperands();
1395 break;
1396 default: break;
1398 printResult();
1401 /* -----------------------------------------------------------------------
1402 Main();
1403 ----------------------------------------------------------------------- */
1404 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
1406 (void)parameter;
1407 rb = api;
1409 /* now go ahead and have fun! */
1411 cal_initial();
1413 while (calStatus != cal_exit ) {
1414 btn = rb->button_get_w_tmo(HZ/2);
1415 switch (btn) {
1416 case CALCULATOR_INPUT:
1417 case CALCULATOR_CALC:
1418 #ifdef CALCULATOR_INPUT_CALC_PRE
1419 if (lastbtn != CALCULATOR_INPUT_CALC_PRE)
1420 break;
1421 /* no unconditional break; here! */
1422 #endif
1423 #ifdef CALCULATOR_OPERATORS
1424 case CALCULATOR_OPERATORS:
1425 #endif
1426 switch(buttonGroup){
1427 case basicButtons:
1428 basicButtonsProcess();
1429 break;
1430 case sciButtons:
1431 sciButtonsProcess();
1432 break;
1434 break;
1436 #ifdef CALCULATOR_CLEAR
1437 case CALCULATOR_CLEAR:
1438 switch(calStatus){
1439 case cal_typing:
1440 case cal_dotted:
1441 doDelete();
1442 break;
1443 default: /* cal_normal, cal_error, cal_exit */
1444 clearMem();
1445 break;
1447 printResult();
1448 break;
1449 #endif
1451 case BUTTON_LEFT:
1452 case BUTTON_LEFT | BUTTON_REPEAT:
1453 case BUTTON_RIGHT:
1454 case BUTTON_RIGHT | BUTTON_REPEAT:
1455 case CALCULATOR_UP:
1456 case CALCULATOR_UP | BUTTON_REPEAT:
1457 case CALCULATOR_DOWN:
1458 case CALCULATOR_DOWN | BUTTON_REPEAT:
1459 moveButton();
1460 break;
1461 #ifdef CALCULATOR_RC_QUIT
1462 case CALCULATOR_RC_QUIT:
1463 #endif
1464 case CALCULATOR_QUIT:
1465 calStatus = cal_exit;
1466 printResult();
1467 break;
1468 default:
1469 if(rb->default_event_handler(btn) == SYS_USB_CONNECTED)
1470 return PLUGIN_USB_CONNECTED;
1471 break;
1472 } /* switch (btn) */
1473 if (btn != BUTTON_NONE)
1474 lastbtn = btn;
1475 } /* while (calStatus != cal_exit ) */
1477 /* rb->splash(HZ*2, "Hello world!"); */
1478 rb->button_clear_queue();
1479 return PLUGIN_OK;
1482 #endif /* #ifdef HAVE_LCD_BITMAP */