fix some problems with the menu code:
[kugel-rb.git] / apps / plugins / calculator.c
blob0a91d94619c2dcfd958c99ea6286fad3d330ad58
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 Pengxuan Liu (Isaac)
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 00 01 21 22 23 43 44 45 65 66 67 87 88 89 109110111
24 00 |-----------|-----------|-----------|-----------|-----------|
25 01 | | | | | |
26 |***********|***********|***********|***********|***********|
27 |***********|***********|***********|***********|***********|
28 11 | | | | | |
29 12 |-----------|-----------|-----------|-----------|-----------|
30 13 |-----------|-----------|-----------|-----------|-----------| y1
31 14 | | | | | |
32 | | | | | |
33 22 | | | | | |
34 23 |-----------|-----------|-----------|-----------|-----------| y2
35 24 | | | | | |
36 | | | | | |
37 32 | | | | | |
38 33 |-----------|-----------|-----------|-----------|-----------| y3
39 34 | | | | | |
40 | | | | | |
41 42 | | | | | |
42 43 |-----------|-----------|-----------|-----------|-----------| y4
43 44 | | | | | |
44 | | | | | |
45 52 | | | | | |
46 53 |-----------|-----------|-----------|-----------|-----------| y5
47 54 | | | | | |
48 | | | | | |
49 62 | | | | | |
50 63 |-----------|-----------|-----------|-----------|-----------| y6
51 x0 x1 x2 x3 x4 x5
54 /*---------------------------------------------------------------------------
55 Features:
56 - Scientific number format core code. Support range 10^-999 ~ 10^999
57 - Number of significant figures up to 10
59 Limitations:
60 - Right now, only accept "num, operator (+,-,*,/), num, =" input sequence.
61 Input "3, +, 5, -, 2, =", the calculator will only do 5-2 and result = 3
62 You have to input "3, +, 5, =, -, 2, =" to get 3+5-2 = 6
64 - "*,/" have no priority. Actually you can't input 3+5*2 yet.
66 User Instructions:
67 use arrow button to move cursor, "play" button to select, "off" button to exit
68 F1: if typing numbers, it's equal to "Del"; otherwise, equal to "C"
69 F2: circle input "+, -, *, /"
70 F3: equal to "="
72 "MR" : load temp memory
73 "M+" : add currently display to temp memory
74 "C" : reset calculator
75 ---------------------------------------------------------------------------*/
77 #include "plugin.h"
78 #ifdef HAVE_LCD_BITMAP
79 #include "math.h"
81 PLUGIN_HEADER
83 #define BUTTON_ROWS 5
84 #define BUTTON_COLS 5
86 #define REC_HEIGHT (int)(LCD_HEIGHT / (BUTTON_ROWS + 1))
87 #define REC_WIDTH (int)(LCD_WIDTH / BUTTON_COLS)
89 #define Y_6_POS (LCD_HEIGHT) /* Leave room for the border */
90 #define Y_5_POS (Y_6_POS - REC_HEIGHT) /* y5 = 53 */
91 #define Y_4_POS (Y_5_POS - REC_HEIGHT) /* y4 = 43 */
92 #define Y_3_POS (Y_4_POS - REC_HEIGHT) /* y3 = 33 */
93 #define Y_2_POS (Y_3_POS - REC_HEIGHT) /* y2 = 23 */
94 #define Y_1_POS (Y_2_POS - REC_HEIGHT) /* y1 = 13 */
95 #define Y_0_POS 0 /* y0 = 0 */
97 #define X_0_POS 0 /* x0 = 0 */
98 #define X_1_POS (X_0_POS + REC_WIDTH) /* x1 = 22 */
99 #define X_2_POS (X_1_POS + REC_WIDTH) /* x2 = 44 */
100 #define X_3_POS (X_2_POS + REC_WIDTH) /* x3 = 66 */
101 #define X_4_POS (X_3_POS + REC_WIDTH) /* x4 = 88 */
102 #define X_5_POS (X_4_POS + REC_WIDTH) /* x5 = 110, column 111 left blank */
104 #define TEXT_1_POS (Y_1_POS-10) /* y1 = 2 */ /* blank height = 12 */
105 #define TEXT_2_POS (Y_2_POS-8) /* y2 = 15 */ /* blank height = 9 */
106 #define TEXT_3_POS (Y_3_POS-8) /* y3 = 25 */
107 #define TEXT_4_POS (Y_4_POS-8) /* y4 = 35 */
108 #define TEXT_5_POS (Y_5_POS-8) /* y5 = 45 */
109 #define TEXT_6_POS (Y_6_POS-8) /* y6 = 55 */
111 #define SIGN(x) ((x)<0?-1:1)
112 #define ABS(x) ((x)<0?-(x):(x))
114 /* variable button definitions */
115 #if CONFIG_KEYPAD == RECORDER_PAD
116 #define CALCULATOR_LEFT BUTTON_LEFT
117 #define CALCULATOR_RIGHT BUTTON_RIGHT
118 #define CALCULATOR_UP BUTTON_UP
119 #define CALCULATOR_DOWN BUTTON_DOWN
120 #define CALCULATOR_QUIT BUTTON_OFF
121 #define CALCULATOR_INPUT BUTTON_PLAY
122 #define CALCULATOR_CALC BUTTON_F3
123 #define CALCULATOR_OPERATORS BUTTON_F2
124 #define CALCULATOR_CLEAR BUTTON_F1
126 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
127 #define CALCULATOR_LEFT BUTTON_LEFT
128 #define CALCULATOR_RIGHT BUTTON_RIGHT
129 #define CALCULATOR_UP BUTTON_UP
130 #define CALCULATOR_DOWN BUTTON_DOWN
131 #define CALCULATOR_QUIT BUTTON_OFF
132 #define CALCULATOR_INPUT BUTTON_SELECT
133 #define CALCULATOR_CALC BUTTON_F3
134 #define CALCULATOR_OPERATORS BUTTON_F2
135 #define CALCULATOR_CLEAR BUTTON_F1
137 #elif CONFIG_KEYPAD == ONDIO_PAD
138 #define CALCULATOR_LEFT BUTTON_LEFT
139 #define CALCULATOR_RIGHT BUTTON_RIGHT
140 #define CALCULATOR_UP BUTTON_UP
141 #define CALCULATOR_DOWN BUTTON_DOWN
142 #define CALCULATOR_QUIT BUTTON_OFF
143 #define CALCULATOR_INPUT_CALC_PRE BUTTON_MENU
144 #define CALCULATOR_INPUT (BUTTON_MENU | BUTTON_REL)
145 #define CALCULATOR_CALC (BUTTON_MENU | BUTTON_REPEAT)
147 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
148 (CONFIG_KEYPAD == IRIVER_H300_PAD)
149 #define CALCULATOR_LEFT BUTTON_LEFT
150 #define CALCULATOR_RIGHT BUTTON_RIGHT
151 #define CALCULATOR_UP BUTTON_UP
152 #define CALCULATOR_DOWN BUTTON_DOWN
153 #define CALCULATOR_QUIT BUTTON_OFF
154 #define CALCULATOR_INPUT BUTTON_SELECT
155 #define CALCULATOR_CALC BUTTON_ON
156 #define CALCULATOR_OPERATORS BUTTON_MODE
157 #define CALCULATOR_CLEAR BUTTON_REC
159 #define CALCULATOR_RC_QUIT BUTTON_RC_STOP
161 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
162 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
163 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
165 #define CALCULATOR_LEFT BUTTON_LEFT
166 #define CALCULATOR_RIGHT BUTTON_RIGHT
167 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
168 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
169 #define CALCULATOR_QUIT BUTTON_MENU
170 #define CALCULATOR_INPUT BUTTON_SELECT
171 #define CALCULATOR_CALC BUTTON_PLAY
173 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
175 #define CALCULATOR_LEFT BUTTON_LEFT
176 #define CALCULATOR_RIGHT BUTTON_RIGHT
177 #define CALCULATOR_UP BUTTON_UP
178 #define CALCULATOR_DOWN BUTTON_DOWN
179 #define CALCULATOR_QUIT BUTTON_POWER
180 #define CALCULATOR_INPUT BUTTON_SELECT
181 #define CALCULATOR_CALC BUTTON_PLAY
182 #define CALCULATOR_CLEAR BUTTON_REC
184 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
186 #define CALCULATOR_LEFT BUTTON_LEFT
187 #define CALCULATOR_RIGHT BUTTON_RIGHT
188 #define CALCULATOR_UP BUTTON_UP
189 #define CALCULATOR_DOWN BUTTON_DOWN
190 #define CALCULATOR_QUIT BUTTON_POWER
191 #define CALCULATOR_INPUT BUTTON_SELECT
192 #define CALCULATOR_CALC BUTTON_MENU
193 #define CALCULATOR_CLEAR BUTTON_A
195 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
196 (CONFIG_KEYPAD == SANSA_C200_PAD)
197 #define CALCULATOR_LEFT BUTTON_LEFT
198 #define CALCULATOR_RIGHT BUTTON_RIGHT
199 #define CALCULATOR_UP BUTTON_UP
200 #define CALCULATOR_DOWN BUTTON_DOWN
201 #if CONFIG_KEYPAD == SANSA_E200_PAD
202 /* c200 does not have a scroll wheel */
203 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
204 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
205 #endif
206 #define CALCULATOR_QUIT BUTTON_POWER
207 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
208 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
209 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
210 #define CALCULATOR_CLEAR BUTTON_REC
212 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
214 #define CALCULATOR_LEFT BUTTON_LEFT
215 #define CALCULATOR_RIGHT BUTTON_RIGHT
216 #define CALCULATOR_UP BUTTON_SCROLL_UP
217 #define CALCULATOR_DOWN BUTTON_SCROLL_DOWN
218 #define CALCULATOR_QUIT BUTTON_POWER
219 #define CALCULATOR_INPUT_CALC_PRE BUTTON_PLAY
220 #define CALCULATOR_INPUT (BUTTON_PLAY | BUTTON_REL)
221 #define CALCULATOR_CALC (BUTTON_PLAY | BUTTON_REPEAT)
222 #define CALCULATOR_CLEAR BUTTON_REW
224 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
226 #define CALCULATOR_LEFT BUTTON_LEFT
227 #define CALCULATOR_RIGHT BUTTON_RIGHT
228 #define CALCULATOR_UP BUTTON_UP
229 #define CALCULATOR_DOWN BUTTON_DOWN
230 #define CALCULATOR_QUIT BUTTON_BACK
231 #define CALCULATOR_INPUT BUTTON_SELECT
232 #define CALCULATOR_CALC BUTTON_MENU
233 #define CALCULATOR_CLEAR BUTTON_PLAY
235 #elif (CONFIG_KEYPAD == MROBE100_PAD)
237 #define CALCULATOR_LEFT BUTTON_LEFT
238 #define CALCULATOR_RIGHT BUTTON_RIGHT
239 #define CALCULATOR_UP BUTTON_UP
240 #define CALCULATOR_DOWN BUTTON_DOWN
241 #define CALCULATOR_QUIT BUTTON_POWER
242 #define CALCULATOR_INPUT BUTTON_SELECT
243 #define CALCULATOR_CALC BUTTON_MENU
244 #define CALCULATOR_CLEAR BUTTON_DISPLAY
246 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
248 #define CALCULATOR_LEFT BUTTON_RC_REW
249 #define CALCULATOR_RIGHT BUTTON_RC_FF
250 #define CALCULATOR_UP BUTTON_RC_VOL_UP
251 #define CALCULATOR_DOWN BUTTON_RC_VOL_DOWN
252 #define CALCULATOR_QUIT BUTTON_RC_REC
253 #define CALCULATOR_INPUT BUTTON_RC_PLAY
254 #define CALCULATOR_CALC BUTTON_RC_MODE
255 #define CALCULATOR_CLEAR BUTTON_RC_MENU
257 #define CALCULATOR_RC_QUIT BUTTON_REC
259 #elif (CONFIG_KEYPAD == COWOND2_PAD)
261 #define CALCULATOR_QUIT BUTTON_POWER
262 #define CALCULATOR_CLEAR BUTTON_MENU
264 #elif CONFIG_KEYPAD == IAUDIO67_PAD
266 #define CALCULATOR_LEFT BUTTON_LEFT
267 #define CALCULATOR_RIGHT BUTTON_RIGHT
268 #define CALCULATOR_UP BUTTON_VOLUP
269 #define CALCULATOR_DOWN BUTTON_VOLDOWN
270 #define CALCULATOR_QUIT BUTTON_POWER
271 #define CALCULATOR_INPUT BUTTON_PLAY
272 #define CALCULATOR_CALC BUTTON_MENU
273 #define CALCULATOR_CLEAR BUTTON_STOP
275 #define CALCULATOR_RC_QUIT (BUTTON_MENU|BUTTON_PLAY)
277 #else
278 #error No keymap defined!
279 #endif
281 #ifdef HAVE_TOUCHSCREEN
282 #ifndef CALCULATOR_LEFT
283 #define CALCULATOR_LEFT BUTTON_MIDLEFT
284 #endif
285 #ifndef CALCULATOR_RIGHT
286 #define CALCULATOR_RIGHT BUTTON_MIDRIGHT
287 #endif
288 #ifndef CALCULATOR_UP
289 #define CALCULATOR_UP BUTTON_TOPMIDDLE
290 #endif
291 #ifndef CALCULATOR_DOWN
292 #define CALCULATOR_DOWN BUTTON_BOTTOMMIDDLE
293 #endif
294 #ifndef CALCULATOR_CALC
295 #define CALCULATOR_CALC BUTTON_BOTTOMRIGHT
296 #endif
297 #ifndef CALCULATOR_INPUT
298 #define CALCULATOR_INPUT BUTTON_CENTER
299 #endif
300 #ifndef CALCULATOR_CLEAR
301 #define CALCULATOR_CLEAR BUTTON_TOPRIGHT
302 #endif
304 #include "lib/touchscreen.h"
305 static struct ts_raster calc_raster = { X_0_POS, Y_1_POS, BUTTON_COLS*REC_WIDTH, BUTTON_ROWS*REC_HEIGHT, REC_WIDTH, REC_HEIGHT };
306 #endif
308 static const struct plugin_api* rb;
309 MEM_FUNCTION_WRAPPERS(rb);
311 enum {
312 basicButtons,
313 sciButtons
314 } buttonGroup;
316 unsigned char* buttonChar[2][5][5] = {
317 { { "MR" , "M+" , "2nd" , "CE" , "C" },
318 { "7" , "8" , "9" , "/" , "sqr" },
319 { "4" , "5" , "6" , "*" , "x^2" },
320 { "1" , "2" , "3" , "-" , "1/x" },
321 { "0" , "+/-", "." , "+" , "=" } },
323 { { "n!" , "PI" , "1st" , "sin" , "asi" },
324 { "7" , "8" , "9" , "cos" , "aco" },
325 { "4" , "5" , "6" , "tan" , "ata" },
326 { "1" , "2" , "3" , "ln" , "e^x" },
327 { "0" , "+/-", "." , "log" , "x^y" } }
330 enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C ,
331 btn_7 , btn_8 , btn_9 , btn_div , btn_sqr ,
332 btn_4 , btn_5 , btn_6 , btn_time , btn_square ,
333 btn_1 , btn_2 , btn_3 , btn_minus , btn_rec ,
334 btn_0 , btn_sign , btn_dot , btn_add , btn_equal
337 enum { sci_fac, sci_pi , sci_sci , sci_sin , sci_asin ,
338 sci_7 , sci_8 , sci_9 , sci_cos , sci_acos ,
339 sci_4 , sci_5 , sci_6 , sci_tan , sci_atan ,
340 sci_1 , sci_2 , sci_3 , sci_ln , sci_exp ,
341 sci_0 , sci_sign , sci_dot , sci_log , sci_xy
344 #define MINIMUM 0.000000000001 /* e-12 */
345 /* ^ ^ ^ ^ */
346 /* 123456789abcdef */
348 #define DIGITLEN 10 /* must <= 10 */
349 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
350 /* 0.000 00000 0001 */
351 /* ^ ^ ^ ^ ^ ^ */
352 /* DIGITLEN 12345 6789a bcdef */
353 /* power 12 34567 89abc def */
354 /* 10^- 123 45678 9abcd ef */
356 unsigned char buf[19];/* 18 bytes of output line,
357 buf[0] is operator
358 buf[1] = 'M' if memTemp is not 0
359 buf[2] = ' '
361 if SCIENTIFIC_FORMAT
362 buf[2]-buf[12] or buf[3]-buf[13] = result;
363 format X.XXXXXXXX
364 buf[13] or buf[14] -buf[17] = power;
365 format eXXX or e-XXX
366 else
367 buf[3]-buf[6] = ' ';
368 buf[7]-buf[17] = result;
370 buf[18] = '\0' */
372 unsigned char typingbuf[DIGITLEN+2];/* byte 0 is sign or ' ',
373 byte 1~DIGITLEN are num and '.'
374 byte (DIGITLEN+1) is '\0' */
375 unsigned char* typingbufPointer = typingbuf;
377 double result = 0; /* main operand, format 0.xxxxx */
378 int power = 0; /* 10^power */
379 double modifier = 0.1; /* position of next input */
380 double operand = 0; /* second operand, format 0.xxxxx */
381 int operandPower = 0; /* 10^power of second operand */
382 char oper = ' '; /* operators: + - * / */
383 bool operInputted = false; /* false: do calculation first and
384 replace current oper
385 true: just replace current oper */
387 double memTemp = 0; /* temp memory */
388 int memTempPower = 0; /* 10^^power of memTemp */
390 int btn_row, btn_col; /* current position index for button */
391 int prev_btn_row, prev_btn_col; /* previous cursor position */
392 #define CAL_BUTTON (btn_row*5+btn_col)
394 int btn = BUTTON_NONE;
395 int lastbtn = BUTTON_NONE;
397 /* Status of calculator */
398 enum {cal_normal, /* 0, normal status, display result */
399 cal_typing, /* 1, currently typing, dot hasn't been typed */
400 cal_dotted, /* 2, currently typing, dot already has been typed. */
401 cal_error,
402 cal_exit,
403 cal_toDo
404 } calStatus;
406 /* constant table for CORDIC algorithm */
407 double cordicTable[51][2]= {
408 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
409 {1e+00, 7.853981633974483e-01},
410 {5e-01, 4.636476090008061e-01},
411 {2.5e-01, 2.449786631268641e-01},
412 {1.25e-01, 1.243549945467614e-01},
413 {6.25e-02, 6.241880999595735e-02},
414 {3.125e-02, 3.123983343026828e-02},
415 {1.5625e-02, 1.562372862047683e-02},
416 {7.8125e-03, 7.812341060101111e-03},
417 {3.90625e-03, 3.906230131966972e-03},
418 {1.953125e-03, 1.953122516478819e-03},
419 {9.765625e-04, 9.765621895593195e-04},
420 {4.8828125e-04, 4.882812111948983e-04},
421 {2.44140625e-04, 2.441406201493618e-04},
422 {1.220703125e-04, 1.220703118936702e-04},
423 {6.103515625e-05, 6.103515617420877e-05},
424 {3.0517578125e-05, 3.051757811552610e-05},
425 {1.52587890625e-05, 1.525878906131576e-05},
426 {7.62939453125e-06, 7.629394531101970e-06},
427 {3.814697265625e-06, 3.814697265606496e-06},
428 {1.9073486328125e-06, 1.907348632810187e-06},
429 {9.5367431640625e-07, 9.536743164059608e-07},
430 {4.76837158203125e-07, 4.768371582030888e-07},
431 {2.384185791015625e-07, 2.384185791015580e-07},
432 {1.1920928955078125e-07, 1.192092895507807e-07},
433 {5.9604644775390625e-08, 5.960464477539055e-08},
434 {2.98023223876953125e-08, 2.980232238769530e-08},
435 {1.490116119384765625e-08, 1.490116119384765e-08},
436 {7.450580596923828125e-09, 7.450580596923828e-09},
437 {3.7252902984619140625e-09, 3.725290298461914e-09},
438 {1.86264514923095703125e-09, 1.862645149230957e-09},
439 {9.31322574615478515625e-10, 9.313225746154785e-10},
440 {4.656612873077392578125e-10, 4.656612873077393e-10},
441 {2.3283064365386962890625e-10, 2.328306436538696e-10},
442 {1.16415321826934814453125e-10, 1.164153218269348e-10},
443 {5.82076609134674072265625e-11, 5.820766091346741e-11},
444 {2.910383045673370361328125e-11, 2.910383045673370e-11},
445 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
446 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
447 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
448 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
449 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
450 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
451 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
452 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
453 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
454 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
455 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
456 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
457 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
458 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
459 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
462 void doMultiple(double* operandOne, int* powerOne,
463 double operandTwo, int powerTwo);
464 void doAdd (double* operandOne, int* powerOne,
465 double operandTwo, int powerTwo);
466 void printResult(void);
467 void formatResult(void);
468 void oneOperand(void);
470 void drawLines(void);
471 void drawButtons(int group);
473 /* -----------------------------------------------------------------------
474 Handy funtions
475 ----------------------------------------------------------------------- */
476 void cleartypingbuf(void)
478 int k;
479 for( k=1; k<=(DIGITLEN+1); k++)
480 typingbuf[k] = 0;
481 typingbuf[0] = ' ';
482 typingbufPointer = typingbuf+1;
484 void clearbuf(void)
486 int k;
487 for(k=0;k<18;k++)
488 buf[k]=' ';
489 buf[18] = 0;
491 void clearResult(void)
493 result = 0;
494 power = 0;
495 modifier = 0.1;
498 void clearInput(void)
500 calStatus = cal_normal;
501 clearResult();
502 cleartypingbuf();
503 rb->lcd_clear_display();
504 drawButtons(buttonGroup);
505 drawLines();
508 void clearOperand(void)
510 operand = 0;
511 operandPower = 0;
514 void clearMemTemp(void)
516 memTemp = 0;
517 memTempPower = 0;
520 void clearOper(void)
522 oper = ' ';
523 operInputted = false;
526 void clearMem(void)
528 clearInput();
529 clearMemTemp();
530 clearOperand();
531 clearOper();
532 btn = BUTTON_NONE;
535 void switchOperands(void)
537 double tempr = operand;
538 int tempp = operandPower;
539 operand = result;
540 operandPower = power;
541 result = tempr;
542 power = tempp;
545 void drawLines(void)
547 int i;
548 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS-1);
549 for (i = 0; i < 5 ; i++)
550 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS+i*REC_HEIGHT);
551 for (i = 0; i < 4 ; i++)
552 rb->lcd_vline(X_1_POS+i*REC_WIDTH, Y_1_POS, LCD_HEIGHT);
555 void drawButtons(int group)
557 int i, j, w, h;
558 for (i = 0; i <= 4; i++){
559 for (j = 0; j <= 4; j++){
560 rb->lcd_getstringsize( buttonChar[group][i][j],&w,&h);
561 if (i == btn_row && j == btn_col) /* selected item */
562 rb->lcd_set_drawmode(DRMODE_SOLID);
563 else
564 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
565 rb->lcd_fillrect( X_0_POS + j*REC_WIDTH,
566 Y_1_POS + i*REC_HEIGHT,
567 REC_WIDTH, REC_HEIGHT+1);
568 if (i == btn_row && j == btn_col) /* selected item */
569 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
570 else
571 rb->lcd_set_drawmode(DRMODE_SOLID);
572 rb->lcd_putsxy( X_0_POS + j*REC_WIDTH + (REC_WIDTH - w)/2,
573 Y_1_POS + i*REC_HEIGHT + (REC_HEIGHT - h)/2 + 1,
574 buttonChar[group][i][j] );
577 rb->lcd_set_drawmode(DRMODE_SOLID);
580 /* -----------------------------------------------------------------------
581 Initiate calculator
582 ----------------------------------------------------------------------- */
583 void cal_initial (void)
585 int w,h;
587 rb->lcd_getstringsize("A",&w,&h);
588 if (h >= REC_HEIGHT)
589 rb->lcd_setfont(FONT_SYSFIXED);
591 rb->lcd_clear_display();
593 #ifdef CALCULATOR_OPERATORS
594 /* basic operators are available through separate button */
595 buttonGroup = sciButtons;
596 #else
597 buttonGroup = basicButtons;
598 #endif
600 /* initially, invert button "5" */
601 btn_row = 2;
602 btn_col = 1;
603 prev_btn_row = btn_row;
604 prev_btn_col = btn_col;
605 drawButtons(buttonGroup);
606 drawLines();
607 rb->lcd_update();
609 /* initial mem and output display*/
610 clearMem();
611 printResult();
613 /* clear button queue */
614 rb->button_clear_queue();
617 /* -----------------------------------------------------------------------
618 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
619 in it's private case for sqrt.
620 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
621 ----------------------------------------------------------------------- */
622 double mySqrt(double square)
624 int k = 0;
625 double temp = 0;
626 double root= ABS(square+1)/2;
628 while( ABS(root - temp) > MINIMUM ){
629 temp = root;
630 root = (square/temp + temp)/2;
631 k++;
632 if (k>10000) return 0;
635 return root;
637 /* -----------------------------------------------------------------------
638 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
639 transcendFunc can do sin,cos,log,exp
640 input parameter is angle
641 ----------------------------------------------------------------------- */
642 void transcendFunc(char* func, double* tt, int* ttPower)
644 double t = (*tt)*M_PI/180; int tPower = *ttPower;
645 int sign = 1;
646 int n = 50; /* n <=50, tables are all <= 50 */
647 int j;
648 double x,y,z,xt,yt,zt;
650 if (tPower < -998) {
651 calStatus = cal_normal;
652 return;
654 if (tPower > 8) {
655 calStatus = cal_error;
656 return;
658 *ttPower = 0;
659 calStatus = cal_normal;
661 if( func[0] =='s' || func[0] =='S'|| func[0] =='t' || func[0] =='T')
662 sign = SIGN(t);
663 else {
664 /* if( func[0] =='c' || func[0] =='C') */
665 sign = 1;
667 t = ABS(t);
669 while (tPower > 0){
670 t *= 10;
671 tPower--;
673 while (tPower < 0) {
674 t /= 10;
675 tPower++;
677 j = 0;
678 while (t > j*M_TWOPI) {j++;}
679 t -= (j-1)*M_TWOPI;
680 if (M_PI_2 < t && t < 3*M_PI_2){
681 t = M_PI - t;
682 if (func[0] =='c' || func[0] =='C')
683 sign = -1;
684 else if (func[0] =='t' || func[0] =='T')
685 t*=-1;
687 else if ( 3*M_PI_2 <= t && t <= M_TWOPI)
688 t -= M_TWOPI;
690 x = 0.60725293500888; y = 0; z = t;
691 for (j=1;j<n+2;j++){
692 xt = x - SIGN(z) * y*cordicTable[j-1][0];
693 yt = y + SIGN(z) * x*cordicTable[j-1][0];
694 zt = z - SIGN(z) * cordicTable[j-1][1];
695 x = xt;
696 y=yt;
697 z=zt;
699 if( func[0] =='s' || func[0] =='S') {
700 *tt = sign*y;
701 return;
703 else if( func[0] =='c' || func[0] =='C') {
704 *tt = sign*x;
705 return;
707 else /*if( func[0] =='t' || func[0] =='T')*/ {
708 if(t==M_PI_2||t==-M_PI_2){
709 calStatus = cal_error;
710 return;
712 else{
713 *tt = sign*(y/x);
714 return;
719 /* -----------------------------------------------------------------------
720 add in scientific number format
721 ----------------------------------------------------------------------- */
722 void doAdd (double* operandOne, int* powerOne,
723 double operandTwo, int powerTwo)
725 if ( *powerOne >= powerTwo ){
726 if (*powerOne - powerTwo <= DIGITLEN+1){
727 while (powerTwo < *powerOne){
728 operandTwo /=10;
729 powerTwo++;
731 *operandOne += operandTwo;
733 /*do nothing if operandTwo is too small*/
735 else{
736 if (powerTwo - *powerOne <= DIGITLEN+1){
737 while(powerTwo > *powerOne){
738 *operandOne /=10;
739 (*powerOne)++;
741 (*operandOne) += operandTwo;
743 else{/* simply copy operandTwo if operandOne is too small */
744 *operandOne = operandTwo;
745 *powerOne = powerTwo;
749 /* -----------------------------------------------------------------------
750 multiple in scientific number format
751 ----------------------------------------------------------------------- */
752 void doMultiple(double* operandOne, int* powerOne,
753 double operandTwo, int powerTwo)
755 (*operandOne) *= operandTwo;
756 (*powerOne) += powerTwo;
759 /* -----------------------------------------------------------------------
760 Handles all one operand calculations
761 ----------------------------------------------------------------------- */
762 void oneOperand(void)
764 int k = 0;
765 if (buttonGroup == basicButtons){
766 switch(CAL_BUTTON){
767 case btn_sqr:
768 if (result<0)
769 calStatus = cal_error;
770 else{
771 if (power%2 == 1){
772 result = (mySqrt(result*10))/10;
773 power = (power+1) / 2;
775 else{
776 result = mySqrt(result);
777 power = power / 2;
779 calStatus = cal_normal;
781 break;
782 case btn_square:
783 power *= 2;
784 result *= result;
785 calStatus = cal_normal;
786 break;
788 case btn_rec:
789 if (result==0)
790 calStatus = cal_error;
791 else{
792 power = -power;
793 result = 1/result;
794 calStatus = cal_normal;
796 break;
797 default:
798 calStatus = cal_toDo;
799 break; /* just for the safety */
802 else{ /* sciButtons */
803 switch(CAL_BUTTON){
804 case sci_sin:
805 transcendFunc("sin", &result, &power);
806 break;
807 case sci_cos:
808 transcendFunc("cos", &result, &power);
809 break;
810 case sci_tan:
811 transcendFunc("tan", &result, &power);
812 break;
813 case sci_fac:
814 if (power<0 || power>8 || result<0 )
815 calStatus = cal_error;
816 else if(result == 0) {
817 result = 1;
818 power = 0;
820 else{
821 while(power > 0) {
822 result *= 10;
823 power--;
825 if ( ( result - (int)result) > MINIMUM )
826 calStatus = cal_error;
827 else {
828 k = result; result = 1;
829 while (k > 1){
830 doMultiple(&result, &power, k, 0);
831 formatResult();
832 k--;
834 calStatus = cal_normal;
837 break;
838 default:
839 calStatus = cal_toDo;
840 break; /* just for the safety */
846 /* -----------------------------------------------------------------------
847 Handles all two operands calculations
848 ----------------------------------------------------------------------- */
849 void twoOperands(void)
851 switch(oper){
852 case '-':
853 doAdd(&operand, &operandPower, -result, power);
854 break;
855 case '+':
856 doAdd(&operand, &operandPower, result, power);
857 break;
858 case '*':
859 doMultiple(&operand, &operandPower, result, power);
860 break;
861 case '/':
862 if ( ABS(result) > MINIMUM ){
863 doMultiple(&operand, &operandPower, 1/result, -power);
865 else
866 calStatus = cal_error;
867 break;
868 default: /* ' ' */
869 switchOperands(); /* counter switchOperands() below */
870 break;
871 } /* switch(oper) */
872 switchOperands();
873 clearOper();
876 /* First, increases *dimen1 by dimen1_delta modulo dimen1_modulo.
877 If dimen1 wraps, increases *dimen2 by dimen2_delta modulo dimen2_modulo.
879 static void move_with_wrap_and_shift(
880 int *dimen1, int dimen1_delta, int dimen1_modulo,
881 int *dimen2, int dimen2_delta, int dimen2_modulo)
883 bool wrapped = false;
885 *dimen1 += dimen1_delta;
886 if (*dimen1 < 0)
888 *dimen1 = dimen1_modulo - 1;
889 wrapped = true;
891 else if (*dimen1 >= dimen1_modulo)
893 *dimen1 = 0;
894 wrapped = true;
897 if (wrapped)
899 /* Make the dividend always positive to be sure about the result.
900 Adding dimen2_modulo does not change it since we do it modulo. */
901 *dimen2 = (*dimen2 + dimen2_modulo + dimen2_delta) % dimen2_modulo;
905 /* -----------------------------------------------------------------------
906 Print buttons when switching 1st and 2nd
907 int group = {basicButtons, sciButtons}
908 ----------------------------------------------------------------------- */
909 void printButtonGroups(int group)
911 drawButtons(group);
912 drawLines();
913 rb->lcd_update();
915 /* -----------------------------------------------------------------------
916 flash the currently marked button
917 ----------------------------------------------------------------------- */
918 void flashButton(void)
920 int k, w, h;
921 for (k=2;k>0;k--)
923 rb->lcd_getstringsize( buttonChar[buttonGroup][btn_row][btn_col],&w,&h);
924 rb->lcd_set_drawmode(DRMODE_SOLID|(k==1) ? 0 : DRMODE_INVERSEVID);
925 rb->lcd_fillrect( X_0_POS + btn_col*REC_WIDTH + 1,
926 Y_1_POS + btn_row*REC_HEIGHT + 1,
927 REC_WIDTH - 1, REC_HEIGHT - 1);
928 rb->lcd_putsxy( X_0_POS + btn_col*REC_WIDTH + (REC_WIDTH - w)/2,
929 Y_1_POS + btn_row*REC_HEIGHT + (REC_HEIGHT - h)/2 +1,
930 buttonChar[buttonGroup][btn_row][btn_col] );
931 rb->lcd_update_rect( X_0_POS + btn_col*REC_WIDTH + 1,
932 Y_1_POS + btn_row*REC_HEIGHT + 1,
933 REC_WIDTH - 1, REC_HEIGHT - 1);
935 if (k!= 1)
936 rb->sleep(HZ/22);
941 /* -----------------------------------------------------------------------
942 pos is the position that needs animation. pos = [1~18]
943 ----------------------------------------------------------------------- */
944 void deleteAnimation(int pos)
946 int k;
947 if (pos<1 || pos >18)
948 return;
949 pos--;
950 rb->lcd_fillrect(1+pos*6, TEXT_1_POS, 6, 8);
951 rb->lcd_update_rect(1+pos*6, TEXT_1_POS, 6, 8);
953 for (k=1;k<=4;k++){
954 rb->sleep(HZ/32);
955 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
956 rb->lcd_fillrect(1+pos*6, TEXT_1_POS, 6, 8);
957 rb->lcd_set_drawmode(DRMODE_SOLID);
958 rb->lcd_fillrect(1+pos*6+1+k, TEXT_1_POS+k,
959 (5-2*k)>0?(5-2*k):1, (7-2*k)>0?(7-2*k):1 );
960 rb->lcd_update_rect(1+pos*6, TEXT_1_POS, 6, 8);
965 /* -----------------------------------------------------------------------
966 result may be one of these formats:
968 xxxx.xxxx
969 0.xxxx
970 0.0000xxxx
972 formatResult() change result to standard format: 0.xxxx
973 if result is close to 0, let it be 0;
974 if result is close to 1, let it be 0.1 and power++;
975 ----------------------------------------------------------------------- */
976 void formatResult(void)
978 int resultsign = SIGN(result);
979 result = ABS(result);
980 if(result > MINIMUM ){ /* doesn't check power, might have problem
981 input wouldn't,
982 + - * / of two formatted number wouldn't.
983 only a calculation that makes a formatted
984 number (0.xxxx) less than MINIMUM in only
985 one operation */
987 if (result<1){
988 while( (int)(result*10) == 0 ){
989 result *= 10;
990 power--;
991 modifier *= 10;
994 else{ /* result >= 1 */
995 while( (int)result != 0 ){
996 result /= 10;
997 power++;
998 modifier /= 10;
1000 } /* if result<1 */
1002 if (result > (1-MINIMUM)){
1003 result = 0.1;
1004 power++;
1005 modifier /= 10;
1007 result *= resultsign;
1009 else {
1010 result = 0;
1011 power = 0;
1012 modifier = 0.1;
1016 /* -----------------------------------------------------------------------
1017 result2typingbuf() outputs standard format result to typingbuf.
1018 case SCIENTIFIC_FORMAT, let temppower = 1;
1019 case temppower > 0: print '.' in the middle
1020 case temppower <= 0: print '.' in the begining
1021 ----------------------------------------------------------------------- */
1022 void result2typingbuf(void)
1024 bool haveDot = false;
1025 char tempchar = 0;
1026 int k;
1027 double tempresult = ABS(result); /* positive num makes things simple */
1029 int temppower;
1030 double tempmodifier = 1;
1031 int count;
1033 if(SCIENTIFIC_FORMAT)
1034 temppower = 1; /* output x.xxxx format */
1035 else
1036 temppower = power;
1038 cleartypingbuf();
1040 if(tempresult < MINIMUM){ /* if 0,faster display and avoid complication*/
1041 typingbuf[0] = ' ';
1042 typingbuf[1] = '0';
1044 else{ /* tempresult > 0 */
1045 typingbuf[0] = (SIGN(result)<0)?'-':' ';
1047 typingbufPointer = typingbuf;
1048 if(temppower > 0){
1049 for (k = 0; k<DIGITLEN+1 ; k++){
1050 typingbufPointer++;
1051 if(temppower || *(typingbufPointer-1) == '.'){
1052 count = 0;
1053 tempmodifier = tempmodifier/10;
1054 while( (tempresult-tempmodifier*count) >
1055 (tempmodifier-MINIMUM)){
1056 count++;
1058 tempresult -= tempmodifier*count;
1059 tempresult = ABS(tempresult);
1060 temppower-- ;
1061 *typingbufPointer = count + '0';
1063 else{ /* temppower == 0 */
1064 *typingbufPointer = '.';
1065 haveDot = true;
1067 } /* for */
1069 else{
1070 haveDot = true;
1071 typingbufPointer++; *typingbufPointer = '0';
1072 typingbufPointer++; *typingbufPointer = '.';
1073 for (k = 2; k<DIGITLEN+1 ; k++){
1074 typingbufPointer++;
1075 count = 0;
1076 if ( (-temppower) < (k-1)){
1077 tempmodifier = tempmodifier/10;
1078 while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){
1079 count++;
1082 tempresult -= tempmodifier*count;
1083 tempresult = ABS(tempresult);
1084 temppower-- ;
1086 *typingbufPointer = count + '0';
1089 /* now, typingbufPointer = typingbuf + 16 */
1090 /* backward strip off 0 and '.' */
1091 if (haveDot){
1092 while( (*typingbufPointer == '0') || (*typingbufPointer == '.')){
1093 tempchar = *typingbufPointer;
1094 *typingbufPointer = 0;
1095 typingbufPointer--;
1096 if (tempchar == '.') break;
1099 typingbuf[DIGITLEN+1] = 0;
1100 } /* else tempresult > 0 */
1103 /* -----------------------------------------------------------------------
1104 printResult() generates LCD display.
1105 ----------------------------------------------------------------------- */
1106 void printResult(void)
1108 int k, w, h;
1110 char operbuf[3] = {0, 0, 0};
1112 switch_Status:
1113 switch(calStatus){
1114 case cal_exit:
1115 rb->lcd_clear_display();
1116 rb->splash(HZ/3, "Bye now!");
1117 break;
1118 case cal_error:
1119 clearbuf();
1120 rb->snprintf(buf, 19, "%18s","Error");
1121 break;
1122 case cal_toDo:
1123 clearbuf();
1124 rb->snprintf(buf, 19, "%18s","Coming soon ^_* ");
1125 break;
1127 case cal_normal:
1128 formatResult();
1130 if( power > 1000 ){ /* power -1 > 999 */
1131 calStatus = cal_error;
1132 goto switch_Status;
1134 if (power < -998 ) /* power -1 < -999 */
1135 clearResult(); /* too small, let it be 0 */
1137 result2typingbuf();
1138 clearbuf();
1140 operbuf[0] = oper;
1141 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1142 operbuf[2] = '\0';
1144 if(SCIENTIFIC_FORMAT){
1145 /* output format: X.XXXX eXXX */
1146 if(power > -98){ /* power-1 >= -99, eXXX or e-XX */
1147 rb->snprintf(buf, 12, "%11s",typingbuf);
1148 for(k=11;k<=14;k++) buf[k] = ' ';
1149 cleartypingbuf();
1150 rb->snprintf(typingbuf, 5, "e%d",power-1);
1151 rb->snprintf(buf+11, 5, "%4s",typingbuf);
1153 else{ /* power-1 <= -100, e-XXX */
1154 rb->snprintf(buf, 12, "%11s",typingbuf);
1155 rb->snprintf(buf+11, 6, "e%d",power-1);
1158 else{
1159 rb->snprintf(buf, 12, "%11s",typingbuf);
1160 } /* if SCIENTIFIC_FORMAT */
1161 break;
1162 case cal_typing:
1163 case cal_dotted:
1164 clearbuf();
1165 operbuf[0] = oper;
1166 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1167 rb->snprintf(buf, 12, "%11s",typingbuf);
1168 break;
1172 rb->lcd_getstringsize(buf, &w, &h);
1173 rb->screen_clear_area(rb->screens[0], 0, 0, LCD_WIDTH, REC_HEIGHT-1);
1174 rb->lcd_putsxy(4, Y_1_POS - h -1, operbuf);
1175 rb->lcd_putsxy(LCD_WIDTH - w - 4, Y_1_POS - h -1, buf);
1176 rb->lcd_update_rect(0, 1, LCD_WIDTH, Y_1_POS);
1179 /* -----------------------------------------------------------------------
1180 Process typing buttons: 1-9, '.', sign
1181 main operand "result" and typingbuf are processed seperately here.
1182 ----------------------------------------------------------------------- */
1183 void typingProcess(void){
1184 switch( CAL_BUTTON ){
1185 case btn_sign:
1186 if (calStatus == cal_typing ||
1187 calStatus == cal_dotted)
1188 typingbuf[0] = (typingbuf[0]=='-')?' ':'-';
1189 result = -result;
1190 break;
1191 case btn_dot:
1192 operInputted = false;
1193 switch(calStatus){
1194 case cal_normal:
1195 clearInput();
1196 *typingbufPointer = '0';
1197 typingbufPointer++;
1198 case cal_typing:
1199 calStatus = cal_dotted;
1200 *typingbufPointer = '.';
1201 if (typingbufPointer != typingbuf+DIGITLEN+1)
1202 typingbufPointer++;
1203 break;
1204 default: /* cal_dotted */
1205 break;
1207 break;
1208 default: /* 0-9 */
1209 operInputted = false;
1210 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1211 switch(calStatus){
1212 case cal_normal:
1213 if(CAL_BUTTON == btn_0 )
1214 break; /* first input is 0, ignore */
1215 clearInput();
1216 /*no operator means start a new calculation*/
1217 if (oper ==' ')
1218 clearOperand();
1219 calStatus = cal_typing;
1220 /* go on typing, no break */
1221 case cal_typing:
1222 case cal_dotted:
1223 switch(CAL_BUTTON){
1224 case btn_0:
1225 *typingbufPointer = '0';
1226 break;
1227 default:
1228 *typingbufPointer=(7+btn_col-3*(btn_row-1))+ '0';
1229 break;
1231 if (typingbufPointer!=typingbuf+DIGITLEN+1){
1232 typingbufPointer++;
1234 {/* result processing */
1235 if (calStatus == cal_typing) power++;
1236 if (CAL_BUTTON != btn_0)
1237 result= result +
1238 SIGN(result)*
1239 (7+btn_col-3*(btn_row-1))*modifier;
1240 modifier /= 10;
1243 else /* last byte always '\0' */
1244 *typingbufPointer = 0;
1245 break;
1246 default: /* cal_error, cal_exit */
1247 break;
1249 break; /* default, 0-9 */
1250 } /* switch( CAL_BUTTON ) */
1253 /* -----------------------------------------------------------------------
1254 Handle delete operation
1255 main operand "result" and typingbuf are processed seperately here.
1256 ----------------------------------------------------------------------- */
1257 void doDelete(void){
1258 deleteAnimation(18);
1259 switch(calStatus){
1260 case cal_dotted:
1261 if (*(typingbufPointer-1) == '.'){
1262 /* if dotted and deleting '.',
1263 change status and delete '.' below */
1264 calStatus = cal_typing;
1266 else{ /* if dotted and not deleting '.',
1267 power stays */
1268 power++; /* counter "power--;" below */
1270 case cal_typing:
1271 typingbufPointer--;
1273 {/* result processing */ /* 0-9, '.' */
1274 /* if deleting '.', do nothing */
1275 if ( *typingbufPointer != '.'){
1276 power--;
1277 modifier *= 10;
1278 result = result - SIGN(result)*
1279 ((*typingbufPointer)- '0')*modifier;
1283 *typingbufPointer = 0;
1285 /* if (only one digit left and it's 0)
1286 or no digit left, change status*/
1287 if ( typingbufPointer == typingbuf+1 ||
1288 ( typingbufPointer == typingbuf+2 &&
1289 *(typingbufPointer-1) == '0' ))
1290 calStatus = cal_normal;
1291 break;
1292 default: /* normal, error, exit */
1293 break;
1296 /* -----------------------------------------------------------------------
1297 Handle buttons on basic screen
1298 ----------------------------------------------------------------------- */
1299 void basicButtonsProcess(void){
1300 switch (btn) {
1301 case CALCULATOR_INPUT:
1302 if (calStatus == cal_error && (CAL_BUTTON != btn_C) ) break;
1303 flashButton();
1304 switch( CAL_BUTTON ){
1305 case btn_MR:
1306 operInputted = false;
1307 result = memTemp; power = memTempPower;
1308 calStatus = cal_normal;
1309 break;
1310 case btn_M:
1311 formatResult();
1312 if (memTemp > MINIMUM)
1313 doAdd(&memTemp, &memTempPower, result, power);
1314 else {
1315 /* if result is too small and memTemp = 0,
1316 doAdd will not add */
1317 memTemp = result;
1318 memTempPower = power;
1320 calStatus = cal_normal;
1321 break;
1323 case btn_C: clearMem(); break;
1324 case btn_CE: clearInput(); break;
1326 case btn_bas:
1327 buttonGroup = sciButtons;
1328 printButtonGroups(buttonGroup);
1329 break;
1331 /* one operand calculation, may be changed to
1332 like sin, cos, log, etc */
1333 case btn_sqr:
1334 case btn_square:
1335 case btn_rec:
1336 formatResult(); /* not necessary, just for safty */
1337 oneOperand();
1338 break;
1340 case_btn_equal: /* F3 shortkey entrance */
1341 case btn_equal:
1342 formatResult();
1343 calStatus = cal_normal;
1344 operInputted = false;
1345 if (oper != ' ') twoOperands();
1346 break;
1348 case btn_div:
1349 case btn_time:
1350 case btn_minus:
1351 case btn_add:
1352 if(!operInputted) {twoOperands(); operInputted = true;}
1353 oper = buttonChar[basicButtons][btn_row][btn_col][0];
1354 #ifdef CALCULATOR_OPERATORS
1355 case_cycle_operators: /* F2 shortkey entrance */
1356 #endif
1357 calStatus = cal_normal;
1358 formatResult();
1359 operand = result;
1360 operandPower = power;
1362 break;
1364 case btn_sign:
1365 case btn_dot:
1366 default: /* 0-9 */
1367 typingProcess();
1368 break;
1369 } /* switch (CAL_BUTTON) */
1370 break;
1372 #ifdef CALCULATOR_OPERATORS
1373 case CALCULATOR_OPERATORS:
1374 if (calStatus == cal_error) break;
1375 if (!operInputted) {twoOperands(); operInputted = true;}
1376 switch (oper){
1377 case ' ':
1378 case '/': oper = '+'; flashButton(); break;
1379 case '+': oper = '-'; flashButton(); break;
1380 case '-': oper = '*'; flashButton(); break;
1381 case '*': oper = '/'; flashButton(); break;
1383 goto case_cycle_operators;
1384 break;
1385 #endif
1387 case CALCULATOR_CALC:
1388 if (calStatus == cal_error) break;
1389 flashButton();
1390 goto case_btn_equal;
1391 break;
1392 default: break;
1394 printResult();
1397 /* -----------------------------------------------------------------------
1398 Handle buttons on scientific screen
1399 ----------------------------------------------------------------------- */
1400 void sciButtonsProcess(void){
1401 switch (btn) {
1402 case CALCULATOR_INPUT:
1403 if (calStatus == cal_error && (CAL_BUTTON != sci_sci) ) break;
1404 flashButton();
1405 switch( CAL_BUTTON ){
1407 case sci_pi:
1408 result = M_PI; power = 0;
1409 calStatus = cal_normal;
1410 break;
1412 case sci_xy: break;
1414 case sci_sci:
1415 buttonGroup = basicButtons;
1416 printButtonGroups(basicButtons);
1417 break;
1419 case sci_fac:
1420 case sci_sin:
1421 case sci_asin:
1422 case sci_cos:
1423 case sci_acos:
1424 case sci_tan:
1425 case sci_atan:
1426 case sci_ln:
1427 case sci_exp:
1428 case sci_log:
1429 formatResult(); /* not necessary, just for safty */
1430 oneOperand();
1431 break;
1433 case btn_sign:
1434 case btn_dot:
1435 default: /* 0-9 */
1436 typingProcess();
1437 break;
1438 } /* switch (CAL_BUTTON) */
1439 break;
1441 #ifdef CALCULATOR_OPERATORS
1442 case CALCULATOR_OPERATORS:
1443 if (calStatus == cal_error) break;
1444 if (!operInputted) {twoOperands(); operInputted = true;}
1445 switch (oper){
1446 case ' ': oper = '+'; break;
1447 case '/': oper = '+'; deleteAnimation(1); break;
1448 case '+': oper = '-'; deleteAnimation(1); break;
1449 case '-': oper = '*'; deleteAnimation(1); break;
1450 case '*': oper = '/'; deleteAnimation(1); break;
1452 calStatus = cal_normal;
1453 formatResult();
1454 operand = result;
1455 operandPower = power;
1456 break;
1457 #endif
1459 case CALCULATOR_CALC:
1460 if (calStatus == cal_error) break;
1461 formatResult();
1462 calStatus = cal_normal;
1463 operInputted = false;
1464 if (oper != ' ') twoOperands();
1465 break;
1466 default: break;
1468 printResult();
1471 /* -----------------------------------------------------------------------
1472 move button index
1473 Invert display new button, invert back previous button
1474 ----------------------------------------------------------------------- */
1475 int handleButton(int button){
1476 switch(button)
1478 case CALCULATOR_INPUT:
1479 case CALCULATOR_CALC:
1480 #ifdef CALCULATOR_INPUT_CALC_PRE
1481 if (lastbtn != CALCULATOR_INPUT_CALC_PRE)
1482 break;
1483 /* no unconditional break; here! */
1484 #endif
1485 #ifdef CALCULATOR_OPERATORS
1486 case CALCULATOR_OPERATORS:
1487 #endif
1488 switch(buttonGroup){
1489 case basicButtons:
1490 basicButtonsProcess();
1491 break;
1492 case sciButtons:
1493 sciButtonsProcess();
1494 break;
1496 break;
1498 #ifdef CALCULATOR_CLEAR
1499 case CALCULATOR_CLEAR:
1500 switch(calStatus){
1501 case cal_typing:
1502 case cal_dotted:
1503 doDelete();
1504 break;
1505 default: /* cal_normal, cal_error, cal_exit */
1506 clearMem();
1507 break;
1509 printResult();
1510 break;
1511 #endif
1512 case CALCULATOR_LEFT:
1513 case CALCULATOR_LEFT | BUTTON_REPEAT:
1514 move_with_wrap_and_shift(
1515 &btn_col, -1, BUTTON_COLS,
1516 &btn_row, 0, BUTTON_ROWS);
1517 break;
1519 case CALCULATOR_RIGHT:
1520 case CALCULATOR_RIGHT | BUTTON_REPEAT:
1521 move_with_wrap_and_shift(
1522 &btn_col, 1, BUTTON_COLS,
1523 &btn_row, 0, BUTTON_ROWS);
1524 break;
1526 #ifdef CALCULATOR_UP
1527 case CALCULATOR_UP:
1528 case CALCULATOR_UP | BUTTON_REPEAT:
1529 move_with_wrap_and_shift(
1530 &btn_row, -1, BUTTON_ROWS,
1531 &btn_col, 0, BUTTON_COLS);
1532 break;
1533 #endif
1534 #ifdef CALCULATOR_DOWN
1535 case CALCULATOR_DOWN:
1536 case CALCULATOR_DOWN | BUTTON_REPEAT:
1537 move_with_wrap_and_shift(
1538 &btn_row, 1, BUTTON_ROWS,
1539 &btn_col, 0, BUTTON_COLS);
1540 break;
1541 #endif
1543 #ifdef CALCULATOR_UP_W_SHIFT
1544 case CALCULATOR_UP_W_SHIFT:
1545 case CALCULATOR_UP_W_SHIFT | BUTTON_REPEAT:
1546 move_with_wrap_and_shift(
1547 &btn_row, -1, BUTTON_ROWS,
1548 &btn_col, -1, BUTTON_COLS);
1549 break;
1550 #endif
1551 #ifdef CALCULATOR_DOWN_W_SHIFT
1552 case CALCULATOR_DOWN_W_SHIFT:
1553 case CALCULATOR_DOWN_W_SHIFT | BUTTON_REPEAT:
1554 move_with_wrap_and_shift(
1555 &btn_row, 1, BUTTON_ROWS,
1556 &btn_col, 1, BUTTON_COLS);
1557 break;
1558 #endif
1559 #ifdef CALCULATOR_LEFT_W_SHIFT
1560 case CALCULATOR_LEFT_W_SHIFT:
1561 case CALCULATOR_LEFT_W_SHIFT | BUTTON_REPEAT:
1562 move_with_wrap_and_shift(
1563 &btn_col, -1, BUTTON_COLS,
1564 &btn_row, -1, BUTTON_ROWS);
1565 break;
1566 #endif
1567 #ifdef CALCULATOR_RIGHT_W_SHIFT
1568 case CALCULATOR_RIGHT_W_SHIFT:
1569 case CALCULATOR_RIGHT_W_SHIFT | BUTTON_REPEAT:
1570 move_with_wrap_and_shift(
1571 &btn_col, 1, BUTTON_COLS,
1572 &btn_row, 1, BUTTON_ROWS);
1573 break;
1574 #endif
1575 #ifdef CALCULATOR_RC_QUIT
1576 case CALCULATOR_RC_QUIT:
1577 #endif
1578 case CALCULATOR_QUIT:
1579 return -1;
1582 return 0;
1584 prev_btn_row = btn_row;
1585 prev_btn_col = btn_col;
1588 /* -----------------------------------------------------------------------
1589 Main();
1590 ----------------------------------------------------------------------- */
1591 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
1593 (void)parameter;
1594 rb = api;
1596 /* now go ahead and have fun! */
1598 cal_initial();
1600 while (calStatus != cal_exit ) {
1601 btn = rb->button_get_w_tmo(HZ/2);
1602 #ifdef HAVE_TOUCHSCREEN
1603 if(btn & BUTTON_TOUCHSCREEN)
1605 struct ts_raster_result res;
1606 if(touchscreen_map_raster(&calc_raster, rb->button_get_data() >> 16,
1607 rb->button_get_data() & 0xffff, &res) == 1)
1609 btn_row = res.y;
1610 btn_col = res.x;
1611 drawButtons(buttonGroup);
1612 drawLines();
1614 rb->lcd_update();
1616 prev_btn_row = btn_row;
1617 prev_btn_col = btn_col;
1618 if(btn & BUTTON_REL)
1620 btn = CALCULATOR_INPUT;
1621 switch(buttonGroup){
1622 case basicButtons:
1623 basicButtonsProcess();
1624 break;
1625 case sciButtons:
1626 sciButtonsProcess();
1627 break;
1629 btn = BUTTON_TOUCHSCREEN;
1633 #endif
1634 if (handleButton(btn) == -1)
1636 calStatus = cal_exit;
1637 printResult();
1639 else
1641 drawButtons(buttonGroup);
1642 drawLines();
1645 rb->lcd_update();
1647 if(rb->default_event_handler(btn) == SYS_USB_CONNECTED)
1648 return PLUGIN_USB_CONNECTED;
1650 if (btn != BUTTON_NONE)
1651 lastbtn = btn;
1652 } /* while (calStatus != cal_exit ) */
1654 rb->button_clear_queue();
1655 return PLUGIN_OK;
1658 #endif /* #ifdef HAVE_LCD_BITMAP */