Accept patch in FS#9637 by Keith Perri, fixing a crash when loading a cfg which speci...
[kugel-rb.git] / apps / plugins / calculator.c
blob214932e0a9345c427406a41335b82e33cdadc3bf
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 == SANSA_CLIP_PAD)
213 #define CALCULATOR_LEFT BUTTON_LEFT
214 #define CALCULATOR_RIGHT BUTTON_RIGHT
215 #define CALCULATOR_UP BUTTON_UP
216 #define CALCULATOR_DOWN BUTTON_DOWN
217 #define CALCULATOR_QUIT BUTTON_POWER
218 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
219 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
220 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
221 #define CALCULATOR_CLEAR BUTTON_HOME
223 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
224 #define CALCULATOR_LEFT BUTTON_LEFT
225 #define CALCULATOR_RIGHT BUTTON_RIGHT
226 #define CALCULATOR_UP BUTTON_UP
227 #define CALCULATOR_DOWN BUTTON_DOWN
228 #define CALCULATOR_QUIT BUTTON_POWER
229 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
230 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
231 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
232 #define CALCULATOR_CLEAR (BUTTON_SELECT|BUTTON_UP)
234 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
236 #define CALCULATOR_LEFT BUTTON_LEFT
237 #define CALCULATOR_RIGHT BUTTON_RIGHT
238 #define CALCULATOR_UP BUTTON_SCROLL_UP
239 #define CALCULATOR_DOWN BUTTON_SCROLL_DOWN
240 #define CALCULATOR_QUIT BUTTON_POWER
241 #define CALCULATOR_INPUT_CALC_PRE BUTTON_PLAY
242 #define CALCULATOR_INPUT (BUTTON_PLAY | BUTTON_REL)
243 #define CALCULATOR_CALC (BUTTON_PLAY | BUTTON_REPEAT)
244 #define CALCULATOR_CLEAR BUTTON_REW
246 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
248 #define CALCULATOR_LEFT BUTTON_LEFT
249 #define CALCULATOR_RIGHT BUTTON_RIGHT
250 #define CALCULATOR_UP BUTTON_UP
251 #define CALCULATOR_DOWN BUTTON_DOWN
252 #define CALCULATOR_QUIT BUTTON_BACK
253 #define CALCULATOR_INPUT BUTTON_SELECT
254 #define CALCULATOR_CALC BUTTON_MENU
255 #define CALCULATOR_CLEAR BUTTON_PLAY
257 #elif (CONFIG_KEYPAD == MROBE100_PAD)
259 #define CALCULATOR_LEFT BUTTON_LEFT
260 #define CALCULATOR_RIGHT BUTTON_RIGHT
261 #define CALCULATOR_UP BUTTON_UP
262 #define CALCULATOR_DOWN BUTTON_DOWN
263 #define CALCULATOR_QUIT BUTTON_POWER
264 #define CALCULATOR_INPUT BUTTON_SELECT
265 #define CALCULATOR_CALC BUTTON_MENU
266 #define CALCULATOR_CLEAR BUTTON_DISPLAY
268 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
270 #define CALCULATOR_LEFT BUTTON_RC_REW
271 #define CALCULATOR_RIGHT BUTTON_RC_FF
272 #define CALCULATOR_UP BUTTON_RC_VOL_UP
273 #define CALCULATOR_DOWN BUTTON_RC_VOL_DOWN
274 #define CALCULATOR_QUIT BUTTON_RC_REC
275 #define CALCULATOR_INPUT BUTTON_RC_PLAY
276 #define CALCULATOR_CALC BUTTON_RC_MODE
277 #define CALCULATOR_CLEAR BUTTON_RC_MENU
279 #define CALCULATOR_RC_QUIT BUTTON_REC
281 #elif (CONFIG_KEYPAD == COWOND2_PAD)
283 #define CALCULATOR_QUIT BUTTON_POWER
284 #define CALCULATOR_CLEAR BUTTON_MENU
286 #elif CONFIG_KEYPAD == IAUDIO67_PAD
288 #define CALCULATOR_LEFT BUTTON_LEFT
289 #define CALCULATOR_RIGHT BUTTON_RIGHT
290 #define CALCULATOR_UP BUTTON_VOLUP
291 #define CALCULATOR_DOWN BUTTON_VOLDOWN
292 #define CALCULATOR_QUIT BUTTON_POWER
293 #define CALCULATOR_INPUT BUTTON_PLAY
294 #define CALCULATOR_CALC BUTTON_MENU
295 #define CALCULATOR_CLEAR BUTTON_STOP
297 #define CALCULATOR_RC_QUIT (BUTTON_MENU|BUTTON_PLAY)
299 #elif (CONFIG_KEYPAD == CREATIVEZVM_PAD)
301 #define CALCULATOR_LEFT BUTTON_LEFT
302 #define CALCULATOR_RIGHT BUTTON_RIGHT
303 #define CALCULATOR_UP BUTTON_UP
304 #define CALCULATOR_DOWN BUTTON_DOWN
305 #define CALCULATOR_QUIT BUTTON_BACK
306 #define CALCULATOR_INPUT BUTTON_SELECT
307 #define CALCULATOR_CALC BUTTON_MENU
308 #define CALCULATOR_CLEAR BUTTON_PLAY
310 #else
311 #error No keymap defined!
312 #endif
314 #ifdef HAVE_TOUCHSCREEN
315 #ifndef CALCULATOR_LEFT
316 #define CALCULATOR_LEFT BUTTON_MIDLEFT
317 #endif
318 #ifndef CALCULATOR_RIGHT
319 #define CALCULATOR_RIGHT BUTTON_MIDRIGHT
320 #endif
321 #ifndef CALCULATOR_UP
322 #define CALCULATOR_UP BUTTON_TOPMIDDLE
323 #endif
324 #ifndef CALCULATOR_DOWN
325 #define CALCULATOR_DOWN BUTTON_BOTTOMMIDDLE
326 #endif
327 #ifndef CALCULATOR_CALC
328 #define CALCULATOR_CALC BUTTON_BOTTOMRIGHT
329 #endif
330 #ifndef CALCULATOR_INPUT
331 #define CALCULATOR_INPUT BUTTON_CENTER
332 #endif
333 #ifndef CALCULATOR_CLEAR
334 #define CALCULATOR_CLEAR BUTTON_TOPRIGHT
335 #endif
337 #include "lib/touchscreen.h"
338 static struct ts_raster calc_raster = { X_0_POS, Y_1_POS, BUTTON_COLS*REC_WIDTH, BUTTON_ROWS*REC_HEIGHT, REC_WIDTH, REC_HEIGHT };
339 #endif
341 static const struct plugin_api* rb;
342 MEM_FUNCTION_WRAPPERS(rb);
344 enum {
345 basicButtons,
346 sciButtons
347 } buttonGroup;
349 unsigned char* buttonChar[2][5][5] = {
350 { { "MR" , "M+" , "2nd" , "CE" , "C" },
351 { "7" , "8" , "9" , "/" , "sqr" },
352 { "4" , "5" , "6" , "*" , "x^2" },
353 { "1" , "2" , "3" , "-" , "1/x" },
354 { "0" , "+/-", "." , "+" , "=" } },
356 { { "n!" , "PI" , "1st" , "sin" , "asi" },
357 { "7" , "8" , "9" , "cos" , "aco" },
358 { "4" , "5" , "6" , "tan" , "ata" },
359 { "1" , "2" , "3" , "ln" , "e^x" },
360 { "0" , "+/-", "." , "log" , "x^y" } }
363 enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C ,
364 btn_7 , btn_8 , btn_9 , btn_div , btn_sqr ,
365 btn_4 , btn_5 , btn_6 , btn_time , btn_square ,
366 btn_1 , btn_2 , btn_3 , btn_minus , btn_rec ,
367 btn_0 , btn_sign , btn_dot , btn_add , btn_equal
370 enum { sci_fac, sci_pi , sci_sci , sci_sin , sci_asin ,
371 sci_7 , sci_8 , sci_9 , sci_cos , sci_acos ,
372 sci_4 , sci_5 , sci_6 , sci_tan , sci_atan ,
373 sci_1 , sci_2 , sci_3 , sci_ln , sci_exp ,
374 sci_0 , sci_sign , sci_dot , sci_log , sci_xy
377 #define MINIMUM 0.000000000001 /* e-12 */
378 /* ^ ^ ^ ^ */
379 /* 123456789abcdef */
381 #define DIGITLEN 10 /* must <= 10 */
382 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
383 /* 0.000 00000 0001 */
384 /* ^ ^ ^ ^ ^ ^ */
385 /* DIGITLEN 12345 6789a bcdef */
386 /* power 12 34567 89abc def */
387 /* 10^- 123 45678 9abcd ef */
389 unsigned char buf[19];/* 18 bytes of output line,
390 buf[0] is operator
391 buf[1] = 'M' if memTemp is not 0
392 buf[2] = ' '
394 if SCIENTIFIC_FORMAT
395 buf[2]-buf[12] or buf[3]-buf[13] = result;
396 format X.XXXXXXXX
397 buf[13] or buf[14] -buf[17] = power;
398 format eXXX or e-XXX
399 else
400 buf[3]-buf[6] = ' ';
401 buf[7]-buf[17] = result;
403 buf[18] = '\0' */
405 unsigned char typingbuf[DIGITLEN+2];/* byte 0 is sign or ' ',
406 byte 1~DIGITLEN are num and '.'
407 byte (DIGITLEN+1) is '\0' */
408 unsigned char* typingbufPointer = typingbuf;
410 double result = 0; /* main operand, format 0.xxxxx */
411 int power = 0; /* 10^power */
412 double modifier = 0.1; /* position of next input */
413 double operand = 0; /* second operand, format 0.xxxxx */
414 int operandPower = 0; /* 10^power of second operand */
415 char oper = ' '; /* operators: + - * / */
416 bool operInputted = false; /* false: do calculation first and
417 replace current oper
418 true: just replace current oper */
420 double memTemp = 0; /* temp memory */
421 int memTempPower = 0; /* 10^^power of memTemp */
423 int btn_row, btn_col; /* current position index for button */
424 int prev_btn_row, prev_btn_col; /* previous cursor position */
425 #define CAL_BUTTON (btn_row*5+btn_col)
427 int btn = BUTTON_NONE;
428 int lastbtn = BUTTON_NONE;
430 /* Status of calculator */
431 enum {cal_normal, /* 0, normal status, display result */
432 cal_typing, /* 1, currently typing, dot hasn't been typed */
433 cal_dotted, /* 2, currently typing, dot already has been typed. */
434 cal_error,
435 cal_exit,
436 cal_toDo
437 } calStatus;
439 /* constant table for CORDIC algorithm */
440 double cordicTable[51][2]= {
441 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
442 {1e+00, 7.853981633974483e-01},
443 {5e-01, 4.636476090008061e-01},
444 {2.5e-01, 2.449786631268641e-01},
445 {1.25e-01, 1.243549945467614e-01},
446 {6.25e-02, 6.241880999595735e-02},
447 {3.125e-02, 3.123983343026828e-02},
448 {1.5625e-02, 1.562372862047683e-02},
449 {7.8125e-03, 7.812341060101111e-03},
450 {3.90625e-03, 3.906230131966972e-03},
451 {1.953125e-03, 1.953122516478819e-03},
452 {9.765625e-04, 9.765621895593195e-04},
453 {4.8828125e-04, 4.882812111948983e-04},
454 {2.44140625e-04, 2.441406201493618e-04},
455 {1.220703125e-04, 1.220703118936702e-04},
456 {6.103515625e-05, 6.103515617420877e-05},
457 {3.0517578125e-05, 3.051757811552610e-05},
458 {1.52587890625e-05, 1.525878906131576e-05},
459 {7.62939453125e-06, 7.629394531101970e-06},
460 {3.814697265625e-06, 3.814697265606496e-06},
461 {1.9073486328125e-06, 1.907348632810187e-06},
462 {9.5367431640625e-07, 9.536743164059608e-07},
463 {4.76837158203125e-07, 4.768371582030888e-07},
464 {2.384185791015625e-07, 2.384185791015580e-07},
465 {1.1920928955078125e-07, 1.192092895507807e-07},
466 {5.9604644775390625e-08, 5.960464477539055e-08},
467 {2.98023223876953125e-08, 2.980232238769530e-08},
468 {1.490116119384765625e-08, 1.490116119384765e-08},
469 {7.450580596923828125e-09, 7.450580596923828e-09},
470 {3.7252902984619140625e-09, 3.725290298461914e-09},
471 {1.86264514923095703125e-09, 1.862645149230957e-09},
472 {9.31322574615478515625e-10, 9.313225746154785e-10},
473 {4.656612873077392578125e-10, 4.656612873077393e-10},
474 {2.3283064365386962890625e-10, 2.328306436538696e-10},
475 {1.16415321826934814453125e-10, 1.164153218269348e-10},
476 {5.82076609134674072265625e-11, 5.820766091346741e-11},
477 {2.910383045673370361328125e-11, 2.910383045673370e-11},
478 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
479 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
480 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
481 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
482 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
483 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
484 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
485 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
486 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
487 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
488 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
489 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
490 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
491 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
492 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
495 void doMultiple(double* operandOne, int* powerOne,
496 double operandTwo, int powerTwo);
497 void doAdd (double* operandOne, int* powerOne,
498 double operandTwo, int powerTwo);
499 void printResult(void);
500 void formatResult(void);
501 void oneOperand(void);
503 void drawLines(void);
504 void drawButtons(int group);
506 /* -----------------------------------------------------------------------
507 Handy funtions
508 ----------------------------------------------------------------------- */
509 void cleartypingbuf(void)
511 int k;
512 for( k=1; k<=(DIGITLEN+1); k++)
513 typingbuf[k] = 0;
514 typingbuf[0] = ' ';
515 typingbufPointer = typingbuf+1;
517 void clearbuf(void)
519 int k;
520 for(k=0;k<18;k++)
521 buf[k]=' ';
522 buf[18] = 0;
524 void clearResult(void)
526 result = 0;
527 power = 0;
528 modifier = 0.1;
531 void clearInput(void)
533 calStatus = cal_normal;
534 clearResult();
535 cleartypingbuf();
536 rb->lcd_clear_display();
537 drawButtons(buttonGroup);
538 drawLines();
541 void clearOperand(void)
543 operand = 0;
544 operandPower = 0;
547 void clearMemTemp(void)
549 memTemp = 0;
550 memTempPower = 0;
553 void clearOper(void)
555 oper = ' ';
556 operInputted = false;
559 void clearMem(void)
561 clearInput();
562 clearMemTemp();
563 clearOperand();
564 clearOper();
565 btn = BUTTON_NONE;
568 void switchOperands(void)
570 double tempr = operand;
571 int tempp = operandPower;
572 operand = result;
573 operandPower = power;
574 result = tempr;
575 power = tempp;
578 void drawLines(void)
580 int i;
581 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS-1);
582 for (i = 0; i < 5 ; i++)
583 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS+i*REC_HEIGHT);
584 for (i = 0; i < 4 ; i++)
585 rb->lcd_vline(X_1_POS+i*REC_WIDTH, Y_1_POS, LCD_HEIGHT);
588 void drawButtons(int group)
590 int i, j, w, h;
591 for (i = 0; i <= 4; i++){
592 for (j = 0; j <= 4; j++){
593 rb->lcd_getstringsize( buttonChar[group][i][j],&w,&h);
594 if (i == btn_row && j == btn_col) /* selected item */
595 rb->lcd_set_drawmode(DRMODE_SOLID);
596 else
597 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
598 rb->lcd_fillrect( X_0_POS + j*REC_WIDTH,
599 Y_1_POS + i*REC_HEIGHT,
600 REC_WIDTH, REC_HEIGHT+1);
601 if (i == btn_row && j == btn_col) /* selected item */
602 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
603 else
604 rb->lcd_set_drawmode(DRMODE_SOLID);
605 rb->lcd_putsxy( X_0_POS + j*REC_WIDTH + (REC_WIDTH - w)/2,
606 Y_1_POS + i*REC_HEIGHT + (REC_HEIGHT - h)/2 + 1,
607 buttonChar[group][i][j] );
610 rb->lcd_set_drawmode(DRMODE_SOLID);
613 /* -----------------------------------------------------------------------
614 Initiate calculator
615 ----------------------------------------------------------------------- */
616 void cal_initial (void)
618 int w,h;
620 rb->lcd_getstringsize("A",&w,&h);
621 if (h >= REC_HEIGHT)
622 rb->lcd_setfont(FONT_SYSFIXED);
624 rb->lcd_clear_display();
626 #ifdef CALCULATOR_OPERATORS
627 /* basic operators are available through separate button */
628 buttonGroup = sciButtons;
629 #else
630 buttonGroup = basicButtons;
631 #endif
633 /* initially, invert button "5" */
634 btn_row = 2;
635 btn_col = 1;
636 prev_btn_row = btn_row;
637 prev_btn_col = btn_col;
638 drawButtons(buttonGroup);
639 drawLines();
640 rb->lcd_update();
642 /* initial mem and output display*/
643 clearMem();
644 printResult();
646 /* clear button queue */
647 rb->button_clear_queue();
650 /* -----------------------------------------------------------------------
651 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
652 in it's private case for sqrt.
653 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
654 ----------------------------------------------------------------------- */
655 double mySqrt(double square)
657 int k = 0;
658 double temp = 0;
659 double root= ABS(square+1)/2;
661 while( ABS(root - temp) > MINIMUM ){
662 temp = root;
663 root = (square/temp + temp)/2;
664 k++;
665 if (k>10000) return 0;
668 return root;
670 /* -----------------------------------------------------------------------
671 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
672 transcendFunc can do sin,cos,log,exp
673 input parameter is angle
674 ----------------------------------------------------------------------- */
675 void transcendFunc(char* func, double* tt, int* ttPower)
677 double t = (*tt)*M_PI/180; int tPower = *ttPower;
678 int sign = 1;
679 int n = 50; /* n <=50, tables are all <= 50 */
680 int j;
681 double x,y,z,xt,yt,zt;
683 if (tPower < -998) {
684 calStatus = cal_normal;
685 return;
687 if (tPower > 8) {
688 calStatus = cal_error;
689 return;
691 *ttPower = 0;
692 calStatus = cal_normal;
694 if( func[0] =='s' || func[0] =='S'|| func[0] =='t' || func[0] =='T')
695 sign = SIGN(t);
696 else {
697 /* if( func[0] =='c' || func[0] =='C') */
698 sign = 1;
700 t = ABS(t);
702 while (tPower > 0){
703 t *= 10;
704 tPower--;
706 while (tPower < 0) {
707 t /= 10;
708 tPower++;
710 j = 0;
711 while (t > j*M_TWOPI) {j++;}
712 t -= (j-1)*M_TWOPI;
713 if (M_PI_2 < t && t < 3*M_PI_2){
714 t = M_PI - t;
715 if (func[0] =='c' || func[0] =='C')
716 sign = -1;
717 else if (func[0] =='t' || func[0] =='T')
718 t*=-1;
720 else if ( 3*M_PI_2 <= t && t <= M_TWOPI)
721 t -= M_TWOPI;
723 x = 0.60725293500888; y = 0; z = t;
724 for (j=1;j<n+2;j++){
725 xt = x - SIGN(z) * y*cordicTable[j-1][0];
726 yt = y + SIGN(z) * x*cordicTable[j-1][0];
727 zt = z - SIGN(z) * cordicTable[j-1][1];
728 x = xt;
729 y=yt;
730 z=zt;
732 if( func[0] =='s' || func[0] =='S') {
733 *tt = sign*y;
734 return;
736 else if( func[0] =='c' || func[0] =='C') {
737 *tt = sign*x;
738 return;
740 else /*if( func[0] =='t' || func[0] =='T')*/ {
741 if(t==M_PI_2||t==-M_PI_2){
742 calStatus = cal_error;
743 return;
745 else{
746 *tt = sign*(y/x);
747 return;
752 /* -----------------------------------------------------------------------
753 add in scientific number format
754 ----------------------------------------------------------------------- */
755 void doAdd (double* operandOne, int* powerOne,
756 double operandTwo, int powerTwo)
758 if ( *powerOne >= powerTwo ){
759 if (*powerOne - powerTwo <= DIGITLEN+1){
760 while (powerTwo < *powerOne){
761 operandTwo /=10;
762 powerTwo++;
764 *operandOne += operandTwo;
766 /*do nothing if operandTwo is too small*/
768 else{
769 if (powerTwo - *powerOne <= DIGITLEN+1){
770 while(powerTwo > *powerOne){
771 *operandOne /=10;
772 (*powerOne)++;
774 (*operandOne) += operandTwo;
776 else{/* simply copy operandTwo if operandOne is too small */
777 *operandOne = operandTwo;
778 *powerOne = powerTwo;
782 /* -----------------------------------------------------------------------
783 multiple in scientific number format
784 ----------------------------------------------------------------------- */
785 void doMultiple(double* operandOne, int* powerOne,
786 double operandTwo, int powerTwo)
788 (*operandOne) *= operandTwo;
789 (*powerOne) += powerTwo;
792 /* -----------------------------------------------------------------------
793 Handles all one operand calculations
794 ----------------------------------------------------------------------- */
795 void oneOperand(void)
797 int k = 0;
798 if (buttonGroup == basicButtons){
799 switch(CAL_BUTTON){
800 case btn_sqr:
801 if (result<0)
802 calStatus = cal_error;
803 else{
804 if (power%2 == 1){
805 result = (mySqrt(result*10))/10;
806 power = (power+1) / 2;
808 else{
809 result = mySqrt(result);
810 power = power / 2;
812 calStatus = cal_normal;
814 break;
815 case btn_square:
816 power *= 2;
817 result *= result;
818 calStatus = cal_normal;
819 break;
821 case btn_rec:
822 if (result==0)
823 calStatus = cal_error;
824 else{
825 power = -power;
826 result = 1/result;
827 calStatus = cal_normal;
829 break;
830 default:
831 calStatus = cal_toDo;
832 break; /* just for the safety */
835 else{ /* sciButtons */
836 switch(CAL_BUTTON){
837 case sci_sin:
838 transcendFunc("sin", &result, &power);
839 break;
840 case sci_cos:
841 transcendFunc("cos", &result, &power);
842 break;
843 case sci_tan:
844 transcendFunc("tan", &result, &power);
845 break;
846 case sci_fac:
847 if (power<0 || power>8 || result<0 )
848 calStatus = cal_error;
849 else if(result == 0) {
850 result = 1;
851 power = 0;
853 else{
854 while(power > 0) {
855 result *= 10;
856 power--;
858 if ( ( result - (int)result) > MINIMUM )
859 calStatus = cal_error;
860 else {
861 k = result; result = 1;
862 while (k > 1){
863 doMultiple(&result, &power, k, 0);
864 formatResult();
865 k--;
867 calStatus = cal_normal;
870 break;
871 default:
872 calStatus = cal_toDo;
873 break; /* just for the safety */
879 /* -----------------------------------------------------------------------
880 Handles all two operands calculations
881 ----------------------------------------------------------------------- */
882 void twoOperands(void)
884 switch(oper){
885 case '-':
886 doAdd(&operand, &operandPower, -result, power);
887 break;
888 case '+':
889 doAdd(&operand, &operandPower, result, power);
890 break;
891 case '*':
892 doMultiple(&operand, &operandPower, result, power);
893 break;
894 case '/':
895 if ( ABS(result) > MINIMUM ){
896 doMultiple(&operand, &operandPower, 1/result, -power);
898 else
899 calStatus = cal_error;
900 break;
901 default: /* ' ' */
902 switchOperands(); /* counter switchOperands() below */
903 break;
904 } /* switch(oper) */
905 switchOperands();
906 clearOper();
909 /* First, increases *dimen1 by dimen1_delta modulo dimen1_modulo.
910 If dimen1 wraps, increases *dimen2 by dimen2_delta modulo dimen2_modulo.
912 static void move_with_wrap_and_shift(
913 int *dimen1, int dimen1_delta, int dimen1_modulo,
914 int *dimen2, int dimen2_delta, int dimen2_modulo)
916 bool wrapped = false;
918 *dimen1 += dimen1_delta;
919 if (*dimen1 < 0)
921 *dimen1 = dimen1_modulo - 1;
922 wrapped = true;
924 else if (*dimen1 >= dimen1_modulo)
926 *dimen1 = 0;
927 wrapped = true;
930 if (wrapped)
932 /* Make the dividend always positive to be sure about the result.
933 Adding dimen2_modulo does not change it since we do it modulo. */
934 *dimen2 = (*dimen2 + dimen2_modulo + dimen2_delta) % dimen2_modulo;
938 /* -----------------------------------------------------------------------
939 Print buttons when switching 1st and 2nd
940 int group = {basicButtons, sciButtons}
941 ----------------------------------------------------------------------- */
942 void printButtonGroups(int group)
944 drawButtons(group);
945 drawLines();
946 rb->lcd_update();
948 /* -----------------------------------------------------------------------
949 flash the currently marked button
950 ----------------------------------------------------------------------- */
951 void flashButton(void)
953 int k, w, h;
954 for (k=2;k>0;k--)
956 rb->lcd_getstringsize( buttonChar[buttonGroup][btn_row][btn_col],&w,&h);
957 rb->lcd_set_drawmode(DRMODE_SOLID|(k==1) ? 0 : DRMODE_INVERSEVID);
958 rb->lcd_fillrect( X_0_POS + btn_col*REC_WIDTH + 1,
959 Y_1_POS + btn_row*REC_HEIGHT + 1,
960 REC_WIDTH - 1, REC_HEIGHT - 1);
961 rb->lcd_putsxy( X_0_POS + btn_col*REC_WIDTH + (REC_WIDTH - w)/2,
962 Y_1_POS + btn_row*REC_HEIGHT + (REC_HEIGHT - h)/2 +1,
963 buttonChar[buttonGroup][btn_row][btn_col] );
964 rb->lcd_update_rect( X_0_POS + btn_col*REC_WIDTH + 1,
965 Y_1_POS + btn_row*REC_HEIGHT + 1,
966 REC_WIDTH - 1, REC_HEIGHT - 1);
968 if (k!= 1)
969 rb->sleep(HZ/22);
974 /* -----------------------------------------------------------------------
975 pos is the position that needs animation. pos = [1~18]
976 ----------------------------------------------------------------------- */
977 void deleteAnimation(int pos)
979 int k;
980 if (pos<1 || pos >18)
981 return;
982 pos--;
983 rb->lcd_fillrect(1+pos*6, TEXT_1_POS, 6, 8);
984 rb->lcd_update_rect(1+pos*6, TEXT_1_POS, 6, 8);
986 for (k=1;k<=4;k++){
987 rb->sleep(HZ/32);
988 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
989 rb->lcd_fillrect(1+pos*6, TEXT_1_POS, 6, 8);
990 rb->lcd_set_drawmode(DRMODE_SOLID);
991 rb->lcd_fillrect(1+pos*6+1+k, TEXT_1_POS+k,
992 (5-2*k)>0?(5-2*k):1, (7-2*k)>0?(7-2*k):1 );
993 rb->lcd_update_rect(1+pos*6, TEXT_1_POS, 6, 8);
998 /* -----------------------------------------------------------------------
999 result may be one of these formats:
1001 xxxx.xxxx
1002 0.xxxx
1003 0.0000xxxx
1005 formatResult() change result to standard format: 0.xxxx
1006 if result is close to 0, let it be 0;
1007 if result is close to 1, let it be 0.1 and power++;
1008 ----------------------------------------------------------------------- */
1009 void formatResult(void)
1011 int resultsign = SIGN(result);
1012 result = ABS(result);
1013 if(result > MINIMUM ){ /* doesn't check power, might have problem
1014 input wouldn't,
1015 + - * / of two formatted number wouldn't.
1016 only a calculation that makes a formatted
1017 number (0.xxxx) less than MINIMUM in only
1018 one operation */
1020 if (result<1){
1021 while( (int)(result*10) == 0 ){
1022 result *= 10;
1023 power--;
1024 modifier *= 10;
1027 else{ /* result >= 1 */
1028 while( (int)result != 0 ){
1029 result /= 10;
1030 power++;
1031 modifier /= 10;
1033 } /* if result<1 */
1035 if (result > (1-MINIMUM)){
1036 result = 0.1;
1037 power++;
1038 modifier /= 10;
1040 result *= resultsign;
1042 else {
1043 result = 0;
1044 power = 0;
1045 modifier = 0.1;
1049 /* -----------------------------------------------------------------------
1050 result2typingbuf() outputs standard format result to typingbuf.
1051 case SCIENTIFIC_FORMAT, let temppower = 1;
1052 case temppower > 0: print '.' in the middle
1053 case temppower <= 0: print '.' in the begining
1054 ----------------------------------------------------------------------- */
1055 void result2typingbuf(void)
1057 bool haveDot = false;
1058 char tempchar = 0;
1059 int k;
1060 double tempresult = ABS(result); /* positive num makes things simple */
1062 int temppower;
1063 double tempmodifier = 1;
1064 int count;
1066 if(SCIENTIFIC_FORMAT)
1067 temppower = 1; /* output x.xxxx format */
1068 else
1069 temppower = power;
1071 cleartypingbuf();
1073 if(tempresult < MINIMUM){ /* if 0,faster display and avoid complication*/
1074 typingbuf[0] = ' ';
1075 typingbuf[1] = '0';
1077 else{ /* tempresult > 0 */
1078 typingbuf[0] = (SIGN(result)<0)?'-':' ';
1080 typingbufPointer = typingbuf;
1081 if(temppower > 0){
1082 for (k = 0; k<DIGITLEN+1 ; k++){
1083 typingbufPointer++;
1084 if(temppower || *(typingbufPointer-1) == '.'){
1085 count = 0;
1086 tempmodifier = tempmodifier/10;
1087 while( (tempresult-tempmodifier*count) >
1088 (tempmodifier-MINIMUM)){
1089 count++;
1091 tempresult -= tempmodifier*count;
1092 tempresult = ABS(tempresult);
1093 temppower-- ;
1094 *typingbufPointer = count + '0';
1096 else{ /* temppower == 0 */
1097 *typingbufPointer = '.';
1098 haveDot = true;
1100 } /* for */
1102 else{
1103 haveDot = true;
1104 typingbufPointer++; *typingbufPointer = '0';
1105 typingbufPointer++; *typingbufPointer = '.';
1106 for (k = 2; k<DIGITLEN+1 ; k++){
1107 typingbufPointer++;
1108 count = 0;
1109 if ( (-temppower) < (k-1)){
1110 tempmodifier = tempmodifier/10;
1111 while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){
1112 count++;
1115 tempresult -= tempmodifier*count;
1116 tempresult = ABS(tempresult);
1117 temppower-- ;
1119 *typingbufPointer = count + '0';
1122 /* now, typingbufPointer = typingbuf + 16 */
1123 /* backward strip off 0 and '.' */
1124 if (haveDot){
1125 while( (*typingbufPointer == '0') || (*typingbufPointer == '.')){
1126 tempchar = *typingbufPointer;
1127 *typingbufPointer = 0;
1128 typingbufPointer--;
1129 if (tempchar == '.') break;
1132 typingbuf[DIGITLEN+1] = 0;
1133 } /* else tempresult > 0 */
1136 /* -----------------------------------------------------------------------
1137 printResult() generates LCD display.
1138 ----------------------------------------------------------------------- */
1139 void printResult(void)
1141 int k, w, h;
1143 char operbuf[3] = {0, 0, 0};
1145 switch_Status:
1146 switch(calStatus){
1147 case cal_exit:
1148 rb->lcd_clear_display();
1149 rb->splash(HZ/3, "Bye now!");
1150 break;
1151 case cal_error:
1152 clearbuf();
1153 rb->snprintf(buf, 19, "%18s","Error");
1154 break;
1155 case cal_toDo:
1156 clearbuf();
1157 rb->snprintf(buf, 19, "%18s","Coming soon ^_* ");
1158 break;
1160 case cal_normal:
1161 formatResult();
1163 if( power > 1000 ){ /* power -1 > 999 */
1164 calStatus = cal_error;
1165 goto switch_Status;
1167 if (power < -998 ) /* power -1 < -999 */
1168 clearResult(); /* too small, let it be 0 */
1170 result2typingbuf();
1171 clearbuf();
1173 operbuf[0] = oper;
1174 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1175 operbuf[2] = '\0';
1177 if(SCIENTIFIC_FORMAT){
1178 /* output format: X.XXXX eXXX */
1179 if(power > -98){ /* power-1 >= -99, eXXX or e-XX */
1180 rb->snprintf(buf, 12, "%11s",typingbuf);
1181 for(k=11;k<=14;k++) buf[k] = ' ';
1182 cleartypingbuf();
1183 rb->snprintf(typingbuf, 5, "e%d",power-1);
1184 rb->snprintf(buf+11, 5, "%4s",typingbuf);
1186 else{ /* power-1 <= -100, e-XXX */
1187 rb->snprintf(buf, 12, "%11s",typingbuf);
1188 rb->snprintf(buf+11, 6, "e%d",power-1);
1191 else{
1192 rb->snprintf(buf, 12, "%11s",typingbuf);
1193 } /* if SCIENTIFIC_FORMAT */
1194 break;
1195 case cal_typing:
1196 case cal_dotted:
1197 clearbuf();
1198 operbuf[0] = oper;
1199 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1200 rb->snprintf(buf, 12, "%11s",typingbuf);
1201 break;
1205 rb->lcd_getstringsize(buf, &w, &h);
1206 rb->screen_clear_area(rb->screens[0], 0, 0, LCD_WIDTH, REC_HEIGHT-1);
1207 rb->lcd_putsxy(4, Y_1_POS - h -1, operbuf);
1208 rb->lcd_putsxy(LCD_WIDTH - w - 4, Y_1_POS - h -1, buf);
1209 rb->lcd_update_rect(0, 1, LCD_WIDTH, Y_1_POS);
1212 /* -----------------------------------------------------------------------
1213 Process typing buttons: 1-9, '.', sign
1214 main operand "result" and typingbuf are processed seperately here.
1215 ----------------------------------------------------------------------- */
1216 void typingProcess(void){
1217 switch( CAL_BUTTON ){
1218 case btn_sign:
1219 if (calStatus == cal_typing ||
1220 calStatus == cal_dotted)
1221 typingbuf[0] = (typingbuf[0]=='-')?' ':'-';
1222 result = -result;
1223 break;
1224 case btn_dot:
1225 operInputted = false;
1226 switch(calStatus){
1227 case cal_normal:
1228 clearInput();
1229 *typingbufPointer = '0';
1230 typingbufPointer++;
1231 case cal_typing:
1232 calStatus = cal_dotted;
1233 *typingbufPointer = '.';
1234 if (typingbufPointer != typingbuf+DIGITLEN+1)
1235 typingbufPointer++;
1236 break;
1237 default: /* cal_dotted */
1238 break;
1240 break;
1241 default: /* 0-9 */
1242 operInputted = false;
1243 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1244 switch(calStatus){
1245 case cal_normal:
1246 if(CAL_BUTTON == btn_0 )
1247 break; /* first input is 0, ignore */
1248 clearInput();
1249 /*no operator means start a new calculation*/
1250 if (oper ==' ')
1251 clearOperand();
1252 calStatus = cal_typing;
1253 /* go on typing, no break */
1254 case cal_typing:
1255 case cal_dotted:
1256 switch(CAL_BUTTON){
1257 case btn_0:
1258 *typingbufPointer = '0';
1259 break;
1260 default:
1261 *typingbufPointer=(7+btn_col-3*(btn_row-1))+ '0';
1262 break;
1264 if (typingbufPointer!=typingbuf+DIGITLEN+1){
1265 typingbufPointer++;
1267 {/* result processing */
1268 if (calStatus == cal_typing) power++;
1269 if (CAL_BUTTON != btn_0)
1270 result= result +
1271 SIGN(result)*
1272 (7+btn_col-3*(btn_row-1))*modifier;
1273 modifier /= 10;
1276 else /* last byte always '\0' */
1277 *typingbufPointer = 0;
1278 break;
1279 default: /* cal_error, cal_exit */
1280 break;
1282 break; /* default, 0-9 */
1283 } /* switch( CAL_BUTTON ) */
1286 /* -----------------------------------------------------------------------
1287 Handle delete operation
1288 main operand "result" and typingbuf are processed seperately here.
1289 ----------------------------------------------------------------------- */
1290 void doDelete(void){
1291 deleteAnimation(18);
1292 switch(calStatus){
1293 case cal_dotted:
1294 if (*(typingbufPointer-1) == '.'){
1295 /* if dotted and deleting '.',
1296 change status and delete '.' below */
1297 calStatus = cal_typing;
1299 else{ /* if dotted and not deleting '.',
1300 power stays */
1301 power++; /* counter "power--;" below */
1303 case cal_typing:
1304 typingbufPointer--;
1306 {/* result processing */ /* 0-9, '.' */
1307 /* if deleting '.', do nothing */
1308 if ( *typingbufPointer != '.'){
1309 power--;
1310 modifier *= 10;
1311 result = result - SIGN(result)*
1312 ((*typingbufPointer)- '0')*modifier;
1316 *typingbufPointer = 0;
1318 /* if (only one digit left and it's 0)
1319 or no digit left, change status*/
1320 if ( typingbufPointer == typingbuf+1 ||
1321 ( typingbufPointer == typingbuf+2 &&
1322 *(typingbufPointer-1) == '0' ))
1323 calStatus = cal_normal;
1324 break;
1325 default: /* normal, error, exit */
1326 break;
1329 /* -----------------------------------------------------------------------
1330 Handle buttons on basic screen
1331 ----------------------------------------------------------------------- */
1332 void basicButtonsProcess(void){
1333 switch (btn) {
1334 case CALCULATOR_INPUT:
1335 if (calStatus == cal_error && (CAL_BUTTON != btn_C) ) break;
1336 flashButton();
1337 switch( CAL_BUTTON ){
1338 case btn_MR:
1339 operInputted = false;
1340 result = memTemp; power = memTempPower;
1341 calStatus = cal_normal;
1342 break;
1343 case btn_M:
1344 formatResult();
1345 if (memTemp > MINIMUM)
1346 doAdd(&memTemp, &memTempPower, result, power);
1347 else {
1348 /* if result is too small and memTemp = 0,
1349 doAdd will not add */
1350 memTemp = result;
1351 memTempPower = power;
1353 calStatus = cal_normal;
1354 break;
1356 case btn_C: clearMem(); break;
1357 case btn_CE: clearInput(); break;
1359 case btn_bas:
1360 buttonGroup = sciButtons;
1361 printButtonGroups(buttonGroup);
1362 break;
1364 /* one operand calculation, may be changed to
1365 like sin, cos, log, etc */
1366 case btn_sqr:
1367 case btn_square:
1368 case btn_rec:
1369 formatResult(); /* not necessary, just for safty */
1370 oneOperand();
1371 break;
1373 case_btn_equal: /* F3 shortkey entrance */
1374 case btn_equal:
1375 formatResult();
1376 calStatus = cal_normal;
1377 operInputted = false;
1378 if (oper != ' ') twoOperands();
1379 break;
1381 case btn_div:
1382 case btn_time:
1383 case btn_minus:
1384 case btn_add:
1385 if(!operInputted) {twoOperands(); operInputted = true;}
1386 oper = buttonChar[basicButtons][btn_row][btn_col][0];
1387 #ifdef CALCULATOR_OPERATORS
1388 case_cycle_operators: /* F2 shortkey entrance */
1389 #endif
1390 calStatus = cal_normal;
1391 formatResult();
1392 operand = result;
1393 operandPower = power;
1395 break;
1397 case btn_sign:
1398 case btn_dot:
1399 default: /* 0-9 */
1400 typingProcess();
1401 break;
1402 } /* switch (CAL_BUTTON) */
1403 break;
1405 #ifdef CALCULATOR_OPERATORS
1406 case CALCULATOR_OPERATORS:
1407 if (calStatus == cal_error) break;
1408 if (!operInputted) {twoOperands(); operInputted = true;}
1409 switch (oper){
1410 case ' ':
1411 case '/': oper = '+'; flashButton(); break;
1412 case '+': oper = '-'; flashButton(); break;
1413 case '-': oper = '*'; flashButton(); break;
1414 case '*': oper = '/'; flashButton(); break;
1416 goto case_cycle_operators;
1417 break;
1418 #endif
1420 case CALCULATOR_CALC:
1421 if (calStatus == cal_error) break;
1422 flashButton();
1423 goto case_btn_equal;
1424 break;
1425 default: break;
1427 printResult();
1430 /* -----------------------------------------------------------------------
1431 Handle buttons on scientific screen
1432 ----------------------------------------------------------------------- */
1433 void sciButtonsProcess(void){
1434 switch (btn) {
1435 case CALCULATOR_INPUT:
1436 if (calStatus == cal_error && (CAL_BUTTON != sci_sci) ) break;
1437 flashButton();
1438 switch( CAL_BUTTON ){
1440 case sci_pi:
1441 result = M_PI; power = 0;
1442 calStatus = cal_normal;
1443 break;
1445 case sci_xy: break;
1447 case sci_sci:
1448 buttonGroup = basicButtons;
1449 printButtonGroups(basicButtons);
1450 break;
1452 case sci_fac:
1453 case sci_sin:
1454 case sci_asin:
1455 case sci_cos:
1456 case sci_acos:
1457 case sci_tan:
1458 case sci_atan:
1459 case sci_ln:
1460 case sci_exp:
1461 case sci_log:
1462 formatResult(); /* not necessary, just for safty */
1463 oneOperand();
1464 break;
1466 case btn_sign:
1467 case btn_dot:
1468 default: /* 0-9 */
1469 typingProcess();
1470 break;
1471 } /* switch (CAL_BUTTON) */
1472 break;
1474 #ifdef CALCULATOR_OPERATORS
1475 case CALCULATOR_OPERATORS:
1476 if (calStatus == cal_error) break;
1477 if (!operInputted) {twoOperands(); operInputted = true;}
1478 switch (oper){
1479 case ' ': oper = '+'; break;
1480 case '/': oper = '+'; deleteAnimation(1); break;
1481 case '+': oper = '-'; deleteAnimation(1); break;
1482 case '-': oper = '*'; deleteAnimation(1); break;
1483 case '*': oper = '/'; deleteAnimation(1); break;
1485 calStatus = cal_normal;
1486 formatResult();
1487 operand = result;
1488 operandPower = power;
1489 break;
1490 #endif
1492 case CALCULATOR_CALC:
1493 if (calStatus == cal_error) break;
1494 formatResult();
1495 calStatus = cal_normal;
1496 operInputted = false;
1497 if (oper != ' ') twoOperands();
1498 break;
1499 default: break;
1501 printResult();
1504 /* -----------------------------------------------------------------------
1505 move button index
1506 Invert display new button, invert back previous button
1507 ----------------------------------------------------------------------- */
1508 int handleButton(int button){
1509 switch(button)
1511 case CALCULATOR_INPUT:
1512 case CALCULATOR_CALC:
1513 #ifdef CALCULATOR_INPUT_CALC_PRE
1514 if (lastbtn != CALCULATOR_INPUT_CALC_PRE)
1515 break;
1516 /* no unconditional break; here! */
1517 #endif
1518 #ifdef CALCULATOR_OPERATORS
1519 case CALCULATOR_OPERATORS:
1520 #endif
1521 switch(buttonGroup){
1522 case basicButtons:
1523 basicButtonsProcess();
1524 break;
1525 case sciButtons:
1526 sciButtonsProcess();
1527 break;
1529 break;
1531 #ifdef CALCULATOR_CLEAR
1532 case CALCULATOR_CLEAR:
1533 switch(calStatus){
1534 case cal_typing:
1535 case cal_dotted:
1536 doDelete();
1537 break;
1538 default: /* cal_normal, cal_error, cal_exit */
1539 clearMem();
1540 break;
1542 printResult();
1543 break;
1544 #endif
1545 case CALCULATOR_LEFT:
1546 case CALCULATOR_LEFT | BUTTON_REPEAT:
1547 move_with_wrap_and_shift(
1548 &btn_col, -1, BUTTON_COLS,
1549 &btn_row, 0, BUTTON_ROWS);
1550 break;
1552 case CALCULATOR_RIGHT:
1553 case CALCULATOR_RIGHT | BUTTON_REPEAT:
1554 move_with_wrap_and_shift(
1555 &btn_col, 1, BUTTON_COLS,
1556 &btn_row, 0, BUTTON_ROWS);
1557 break;
1559 #ifdef CALCULATOR_UP
1560 case CALCULATOR_UP:
1561 case CALCULATOR_UP | BUTTON_REPEAT:
1562 move_with_wrap_and_shift(
1563 &btn_row, -1, BUTTON_ROWS,
1564 &btn_col, 0, BUTTON_COLS);
1565 break;
1566 #endif
1567 #ifdef CALCULATOR_DOWN
1568 case CALCULATOR_DOWN:
1569 case CALCULATOR_DOWN | BUTTON_REPEAT:
1570 move_with_wrap_and_shift(
1571 &btn_row, 1, BUTTON_ROWS,
1572 &btn_col, 0, BUTTON_COLS);
1573 break;
1574 #endif
1576 #ifdef CALCULATOR_UP_W_SHIFT
1577 case CALCULATOR_UP_W_SHIFT:
1578 case CALCULATOR_UP_W_SHIFT | BUTTON_REPEAT:
1579 move_with_wrap_and_shift(
1580 &btn_row, -1, BUTTON_ROWS,
1581 &btn_col, -1, BUTTON_COLS);
1582 break;
1583 #endif
1584 #ifdef CALCULATOR_DOWN_W_SHIFT
1585 case CALCULATOR_DOWN_W_SHIFT:
1586 case CALCULATOR_DOWN_W_SHIFT | BUTTON_REPEAT:
1587 move_with_wrap_and_shift(
1588 &btn_row, 1, BUTTON_ROWS,
1589 &btn_col, 1, BUTTON_COLS);
1590 break;
1591 #endif
1592 #ifdef CALCULATOR_LEFT_W_SHIFT
1593 case CALCULATOR_LEFT_W_SHIFT:
1594 case CALCULATOR_LEFT_W_SHIFT | BUTTON_REPEAT:
1595 move_with_wrap_and_shift(
1596 &btn_col, -1, BUTTON_COLS,
1597 &btn_row, -1, BUTTON_ROWS);
1598 break;
1599 #endif
1600 #ifdef CALCULATOR_RIGHT_W_SHIFT
1601 case CALCULATOR_RIGHT_W_SHIFT:
1602 case CALCULATOR_RIGHT_W_SHIFT | BUTTON_REPEAT:
1603 move_with_wrap_and_shift(
1604 &btn_col, 1, BUTTON_COLS,
1605 &btn_row, 1, BUTTON_ROWS);
1606 break;
1607 #endif
1608 #ifdef CALCULATOR_RC_QUIT
1609 case CALCULATOR_RC_QUIT:
1610 #endif
1611 case CALCULATOR_QUIT:
1612 return -1;
1615 return 0;
1617 prev_btn_row = btn_row;
1618 prev_btn_col = btn_col;
1621 /* -----------------------------------------------------------------------
1622 Main();
1623 ----------------------------------------------------------------------- */
1624 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
1626 (void)parameter;
1627 rb = api;
1629 /* now go ahead and have fun! */
1631 cal_initial();
1633 while (calStatus != cal_exit ) {
1634 btn = rb->button_get_w_tmo(HZ/2);
1635 #ifdef HAVE_TOUCHSCREEN
1636 if(btn & BUTTON_TOUCHSCREEN)
1638 struct ts_raster_result res;
1639 if(touchscreen_map_raster(&calc_raster, rb->button_get_data() >> 16,
1640 rb->button_get_data() & 0xffff, &res) == 1)
1642 btn_row = res.y;
1643 btn_col = res.x;
1644 drawButtons(buttonGroup);
1645 drawLines();
1647 rb->lcd_update();
1649 prev_btn_row = btn_row;
1650 prev_btn_col = btn_col;
1651 if(btn & BUTTON_REL)
1653 btn = CALCULATOR_INPUT;
1654 switch(buttonGroup){
1655 case basicButtons:
1656 basicButtonsProcess();
1657 break;
1658 case sciButtons:
1659 sciButtonsProcess();
1660 break;
1662 btn = BUTTON_TOUCHSCREEN;
1666 #endif
1667 if (handleButton(btn) == -1)
1669 calStatus = cal_exit;
1670 printResult();
1672 else
1674 drawButtons(buttonGroup);
1675 drawLines();
1678 rb->lcd_update();
1680 if(rb->default_event_handler(btn) == SYS_USB_CONNECTED)
1681 return PLUGIN_USB_CONNECTED;
1683 if (btn != BUTTON_NONE)
1684 lastbtn = btn;
1685 } /* while (calStatus != cal_exit ) */
1687 rb->button_clear_queue();
1688 return PLUGIN_OK;
1691 #endif /* #ifdef HAVE_LCD_BITMAP */