Reverting parts of r19760 that was mistakenly committed.
[kugel-rb.git] / apps / plugins / calculator.c
blob7e6eb92b0eecd665ade15f03ca22d7f000d6346f
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 (CONFIG_KEYPAD == SANSA_FUZE_PAD)
198 #define CALCULATOR_LEFT BUTTON_LEFT
199 #define CALCULATOR_RIGHT BUTTON_RIGHT
200 #define CALCULATOR_UP BUTTON_UP
201 #define CALCULATOR_DOWN BUTTON_DOWN
202 #if defined(HAVE_SCROLLWHEEL)
203 /* c200 does not have a scroll wheel */
204 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
205 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
206 #endif
207 #define CALCULATOR_QUIT BUTTON_POWER
208 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
209 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
210 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
211 #if (CONFIG_KEYPAD == SANSA_E200_PAD)
212 #define CALCULATOR_CLEAR BUTTON_REC
213 /* FIXME: define as soon as HOME works
214 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
215 #define CALCULATOR_CLEAR BUTTON_HOME */
216 #endif
219 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
220 #define CALCULATOR_LEFT BUTTON_LEFT
221 #define CALCULATOR_RIGHT BUTTON_RIGHT
222 #define CALCULATOR_UP BUTTON_UP
223 #define CALCULATOR_DOWN BUTTON_DOWN
224 #define CALCULATOR_QUIT BUTTON_POWER
225 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
226 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
227 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
228 #define CALCULATOR_CLEAR BUTTON_HOME
230 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
231 #define CALCULATOR_LEFT BUTTON_LEFT
232 #define CALCULATOR_RIGHT BUTTON_RIGHT
233 #define CALCULATOR_UP BUTTON_UP
234 #define CALCULATOR_DOWN BUTTON_DOWN
235 #define CALCULATOR_QUIT BUTTON_POWER
236 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
237 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
238 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
239 #define CALCULATOR_CLEAR (BUTTON_SELECT|BUTTON_UP)
241 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
243 #define CALCULATOR_LEFT BUTTON_LEFT
244 #define CALCULATOR_RIGHT BUTTON_RIGHT
245 #define CALCULATOR_UP BUTTON_SCROLL_UP
246 #define CALCULATOR_DOWN BUTTON_SCROLL_DOWN
247 #define CALCULATOR_QUIT BUTTON_POWER
248 #define CALCULATOR_INPUT_CALC_PRE BUTTON_PLAY
249 #define CALCULATOR_INPUT (BUTTON_PLAY | BUTTON_REL)
250 #define CALCULATOR_CALC (BUTTON_PLAY | BUTTON_REPEAT)
251 #define CALCULATOR_CLEAR BUTTON_REW
253 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
255 #define CALCULATOR_LEFT BUTTON_LEFT
256 #define CALCULATOR_RIGHT BUTTON_RIGHT
257 #define CALCULATOR_UP BUTTON_UP
258 #define CALCULATOR_DOWN BUTTON_DOWN
259 #define CALCULATOR_QUIT BUTTON_BACK
260 #define CALCULATOR_INPUT BUTTON_SELECT
261 #define CALCULATOR_CALC BUTTON_MENU
262 #define CALCULATOR_CLEAR BUTTON_PLAY
264 #elif (CONFIG_KEYPAD == MROBE100_PAD)
266 #define CALCULATOR_LEFT BUTTON_LEFT
267 #define CALCULATOR_RIGHT BUTTON_RIGHT
268 #define CALCULATOR_UP BUTTON_UP
269 #define CALCULATOR_DOWN BUTTON_DOWN
270 #define CALCULATOR_QUIT BUTTON_POWER
271 #define CALCULATOR_INPUT BUTTON_SELECT
272 #define CALCULATOR_CALC BUTTON_MENU
273 #define CALCULATOR_CLEAR BUTTON_DISPLAY
275 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
277 #define CALCULATOR_LEFT BUTTON_RC_REW
278 #define CALCULATOR_RIGHT BUTTON_RC_FF
279 #define CALCULATOR_UP BUTTON_RC_VOL_UP
280 #define CALCULATOR_DOWN BUTTON_RC_VOL_DOWN
281 #define CALCULATOR_QUIT BUTTON_RC_REC
282 #define CALCULATOR_INPUT BUTTON_RC_PLAY
283 #define CALCULATOR_CALC BUTTON_RC_MODE
284 #define CALCULATOR_CLEAR BUTTON_RC_MENU
286 #define CALCULATOR_RC_QUIT BUTTON_REC
288 #elif (CONFIG_KEYPAD == COWOND2_PAD)
290 #define CALCULATOR_QUIT BUTTON_POWER
291 #define CALCULATOR_CLEAR BUTTON_MENU
293 #elif CONFIG_KEYPAD == IAUDIO67_PAD
295 #define CALCULATOR_LEFT BUTTON_LEFT
296 #define CALCULATOR_RIGHT BUTTON_RIGHT
297 #define CALCULATOR_UP BUTTON_VOLUP
298 #define CALCULATOR_DOWN BUTTON_VOLDOWN
299 #define CALCULATOR_QUIT BUTTON_POWER
300 #define CALCULATOR_INPUT BUTTON_PLAY
301 #define CALCULATOR_CALC BUTTON_MENU
302 #define CALCULATOR_CLEAR BUTTON_STOP
304 #define CALCULATOR_RC_QUIT (BUTTON_MENU|BUTTON_PLAY)
306 #elif (CONFIG_KEYPAD == CREATIVEZVM_PAD)
308 #define CALCULATOR_LEFT BUTTON_LEFT
309 #define CALCULATOR_RIGHT BUTTON_RIGHT
310 #define CALCULATOR_UP BUTTON_UP
311 #define CALCULATOR_DOWN BUTTON_DOWN
312 #define CALCULATOR_QUIT BUTTON_BACK
313 #define CALCULATOR_INPUT BUTTON_SELECT
314 #define CALCULATOR_CALC BUTTON_MENU
315 #define CALCULATOR_CLEAR BUTTON_PLAY
317 #else
318 #error No keymap defined!
319 #endif
321 #ifdef HAVE_TOUCHSCREEN
322 #ifndef CALCULATOR_LEFT
323 #define CALCULATOR_LEFT BUTTON_MIDLEFT
324 #endif
325 #ifndef CALCULATOR_RIGHT
326 #define CALCULATOR_RIGHT BUTTON_MIDRIGHT
327 #endif
328 #ifndef CALCULATOR_UP
329 #define CALCULATOR_UP BUTTON_TOPMIDDLE
330 #endif
331 #ifndef CALCULATOR_DOWN
332 #define CALCULATOR_DOWN BUTTON_BOTTOMMIDDLE
333 #endif
334 #ifndef CALCULATOR_CALC
335 #define CALCULATOR_CALC BUTTON_BOTTOMRIGHT
336 #endif
337 #ifndef CALCULATOR_INPUT
338 #define CALCULATOR_INPUT BUTTON_CENTER
339 #endif
340 #ifndef CALCULATOR_CLEAR
341 #define CALCULATOR_CLEAR BUTTON_TOPRIGHT
342 #endif
344 #include "lib/touchscreen.h"
345 static struct ts_raster calc_raster = { X_0_POS, Y_1_POS, BUTTON_COLS*REC_WIDTH, BUTTON_ROWS*REC_HEIGHT, REC_WIDTH, REC_HEIGHT };
346 #endif
348 static const struct plugin_api* rb;
349 MEM_FUNCTION_WRAPPERS(rb);
351 enum {
352 basicButtons,
353 sciButtons
354 } buttonGroup;
356 unsigned char* buttonChar[2][5][5] = {
357 { { "MR" , "M+" , "2nd" , "CE" , "C" },
358 { "7" , "8" , "9" , "/" , "sqr" },
359 { "4" , "5" , "6" , "*" , "x^2" },
360 { "1" , "2" , "3" , "-" , "1/x" },
361 { "0" , "+/-", "." , "+" , "=" } },
363 { { "n!" , "PI" , "1st" , "sin" , "asi" },
364 { "7" , "8" , "9" , "cos" , "aco" },
365 { "4" , "5" , "6" , "tan" , "ata" },
366 { "1" , "2" , "3" , "ln" , "e^x" },
367 { "0" , "+/-", "." , "log" , "x^y" } }
370 enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C ,
371 btn_7 , btn_8 , btn_9 , btn_div , btn_sqr ,
372 btn_4 , btn_5 , btn_6 , btn_time , btn_square ,
373 btn_1 , btn_2 , btn_3 , btn_minus , btn_rec ,
374 btn_0 , btn_sign , btn_dot , btn_add , btn_equal
377 enum { sci_fac, sci_pi , sci_sci , sci_sin , sci_asin ,
378 sci_7 , sci_8 , sci_9 , sci_cos , sci_acos ,
379 sci_4 , sci_5 , sci_6 , sci_tan , sci_atan ,
380 sci_1 , sci_2 , sci_3 , sci_ln , sci_exp ,
381 sci_0 , sci_sign , sci_dot , sci_log , sci_xy
384 #define MINIMUM 0.000000000001 /* e-12 */
385 /* ^ ^ ^ ^ */
386 /* 123456789abcdef */
388 #define DIGITLEN 10 /* must <= 10 */
389 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
390 /* 0.000 00000 0001 */
391 /* ^ ^ ^ ^ ^ ^ */
392 /* DIGITLEN 12345 6789a bcdef */
393 /* power 12 34567 89abc def */
394 /* 10^- 123 45678 9abcd ef */
396 unsigned char buf[19];/* 18 bytes of output line,
397 buf[0] is operator
398 buf[1] = 'M' if memTemp is not 0
399 buf[2] = ' '
401 if SCIENTIFIC_FORMAT
402 buf[2]-buf[12] or buf[3]-buf[13] = result;
403 format X.XXXXXXXX
404 buf[13] or buf[14] -buf[17] = power;
405 format eXXX or e-XXX
406 else
407 buf[3]-buf[6] = ' ';
408 buf[7]-buf[17] = result;
410 buf[18] = '\0' */
412 unsigned char typingbuf[DIGITLEN+2];/* byte 0 is sign or ' ',
413 byte 1~DIGITLEN are num and '.'
414 byte (DIGITLEN+1) is '\0' */
415 unsigned char* typingbufPointer = typingbuf;
417 double result = 0; /* main operand, format 0.xxxxx */
418 int power = 0; /* 10^power */
419 double modifier = 0.1; /* position of next input */
420 double operand = 0; /* second operand, format 0.xxxxx */
421 int operandPower = 0; /* 10^power of second operand */
422 char oper = ' '; /* operators: + - * / */
423 bool operInputted = false; /* false: do calculation first and
424 replace current oper
425 true: just replace current oper */
427 double memTemp = 0; /* temp memory */
428 int memTempPower = 0; /* 10^^power of memTemp */
430 int btn_row, btn_col; /* current position index for button */
431 int prev_btn_row, prev_btn_col; /* previous cursor position */
432 #define CAL_BUTTON (btn_row*5+btn_col)
434 int btn = BUTTON_NONE;
435 int lastbtn = BUTTON_NONE;
437 /* Status of calculator */
438 enum {cal_normal, /* 0, normal status, display result */
439 cal_typing, /* 1, currently typing, dot hasn't been typed */
440 cal_dotted, /* 2, currently typing, dot already has been typed. */
441 cal_error,
442 cal_exit,
443 cal_toDo
444 } calStatus;
446 /* constant table for CORDIC algorithm */
447 double cordicTable[51][2]= {
448 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
449 {1e+00, 7.853981633974483e-01},
450 {5e-01, 4.636476090008061e-01},
451 {2.5e-01, 2.449786631268641e-01},
452 {1.25e-01, 1.243549945467614e-01},
453 {6.25e-02, 6.241880999595735e-02},
454 {3.125e-02, 3.123983343026828e-02},
455 {1.5625e-02, 1.562372862047683e-02},
456 {7.8125e-03, 7.812341060101111e-03},
457 {3.90625e-03, 3.906230131966972e-03},
458 {1.953125e-03, 1.953122516478819e-03},
459 {9.765625e-04, 9.765621895593195e-04},
460 {4.8828125e-04, 4.882812111948983e-04},
461 {2.44140625e-04, 2.441406201493618e-04},
462 {1.220703125e-04, 1.220703118936702e-04},
463 {6.103515625e-05, 6.103515617420877e-05},
464 {3.0517578125e-05, 3.051757811552610e-05},
465 {1.52587890625e-05, 1.525878906131576e-05},
466 {7.62939453125e-06, 7.629394531101970e-06},
467 {3.814697265625e-06, 3.814697265606496e-06},
468 {1.9073486328125e-06, 1.907348632810187e-06},
469 {9.5367431640625e-07, 9.536743164059608e-07},
470 {4.76837158203125e-07, 4.768371582030888e-07},
471 {2.384185791015625e-07, 2.384185791015580e-07},
472 {1.1920928955078125e-07, 1.192092895507807e-07},
473 {5.9604644775390625e-08, 5.960464477539055e-08},
474 {2.98023223876953125e-08, 2.980232238769530e-08},
475 {1.490116119384765625e-08, 1.490116119384765e-08},
476 {7.450580596923828125e-09, 7.450580596923828e-09},
477 {3.7252902984619140625e-09, 3.725290298461914e-09},
478 {1.86264514923095703125e-09, 1.862645149230957e-09},
479 {9.31322574615478515625e-10, 9.313225746154785e-10},
480 {4.656612873077392578125e-10, 4.656612873077393e-10},
481 {2.3283064365386962890625e-10, 2.328306436538696e-10},
482 {1.16415321826934814453125e-10, 1.164153218269348e-10},
483 {5.82076609134674072265625e-11, 5.820766091346741e-11},
484 {2.910383045673370361328125e-11, 2.910383045673370e-11},
485 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
486 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
487 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
488 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
489 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
490 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
491 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
492 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
493 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
494 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
495 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
496 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
497 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
498 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
499 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
502 void doMultiple(double* operandOne, int* powerOne,
503 double operandTwo, int powerTwo);
504 void doAdd (double* operandOne, int* powerOne,
505 double operandTwo, int powerTwo);
506 void printResult(void);
507 void formatResult(void);
508 void oneOperand(void);
510 void drawLines(void);
511 void drawButtons(int group);
513 /* -----------------------------------------------------------------------
514 Handy funtions
515 ----------------------------------------------------------------------- */
516 void cleartypingbuf(void)
518 int k;
519 for( k=1; k<=(DIGITLEN+1); k++)
520 typingbuf[k] = 0;
521 typingbuf[0] = ' ';
522 typingbufPointer = typingbuf+1;
524 void clearbuf(void)
526 int k;
527 for(k=0;k<18;k++)
528 buf[k]=' ';
529 buf[18] = 0;
531 void clearResult(void)
533 result = 0;
534 power = 0;
535 modifier = 0.1;
538 void clearInput(void)
540 calStatus = cal_normal;
541 clearResult();
542 cleartypingbuf();
543 rb->lcd_clear_display();
544 drawButtons(buttonGroup);
545 drawLines();
548 void clearOperand(void)
550 operand = 0;
551 operandPower = 0;
554 void clearMemTemp(void)
556 memTemp = 0;
557 memTempPower = 0;
560 void clearOper(void)
562 oper = ' ';
563 operInputted = false;
566 void clearMem(void)
568 clearInput();
569 clearMemTemp();
570 clearOperand();
571 clearOper();
572 btn = BUTTON_NONE;
575 void switchOperands(void)
577 double tempr = operand;
578 int tempp = operandPower;
579 operand = result;
580 operandPower = power;
581 result = tempr;
582 power = tempp;
585 void drawLines(void)
587 int i;
588 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS-1);
589 for (i = 0; i < 5 ; i++)
590 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS+i*REC_HEIGHT);
591 for (i = 0; i < 4 ; i++)
592 rb->lcd_vline(X_1_POS+i*REC_WIDTH, Y_1_POS, LCD_HEIGHT);
595 void drawButtons(int group)
597 int i, j, w, h;
598 for (i = 0; i <= 4; i++){
599 for (j = 0; j <= 4; j++){
600 rb->lcd_getstringsize( buttonChar[group][i][j],&w,&h);
601 if (i == btn_row && j == btn_col) /* selected item */
602 rb->lcd_set_drawmode(DRMODE_SOLID);
603 else
604 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
605 rb->lcd_fillrect( X_0_POS + j*REC_WIDTH,
606 Y_1_POS + i*REC_HEIGHT,
607 REC_WIDTH, REC_HEIGHT+1);
608 if (i == btn_row && j == btn_col) /* selected item */
609 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
610 else
611 rb->lcd_set_drawmode(DRMODE_SOLID);
612 rb->lcd_putsxy( X_0_POS + j*REC_WIDTH + (REC_WIDTH - w)/2,
613 Y_1_POS + i*REC_HEIGHT + (REC_HEIGHT - h)/2 + 1,
614 buttonChar[group][i][j] );
617 rb->lcd_set_drawmode(DRMODE_SOLID);
620 /* -----------------------------------------------------------------------
621 Initiate calculator
622 ----------------------------------------------------------------------- */
623 void cal_initial (void)
625 int w,h;
627 rb->lcd_getstringsize("A",&w,&h);
628 if (h >= REC_HEIGHT)
629 rb->lcd_setfont(FONT_SYSFIXED);
631 rb->lcd_clear_display();
633 #ifdef CALCULATOR_OPERATORS
634 /* basic operators are available through separate button */
635 buttonGroup = sciButtons;
636 #else
637 buttonGroup = basicButtons;
638 #endif
640 /* initially, invert button "5" */
641 btn_row = 2;
642 btn_col = 1;
643 prev_btn_row = btn_row;
644 prev_btn_col = btn_col;
645 drawButtons(buttonGroup);
646 drawLines();
647 rb->lcd_update();
649 /* initial mem and output display*/
650 clearMem();
651 printResult();
653 /* clear button queue */
654 rb->button_clear_queue();
657 /* -----------------------------------------------------------------------
658 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
659 in it's private case for sqrt.
660 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
661 ----------------------------------------------------------------------- */
662 double mySqrt(double square)
664 int k = 0;
665 double temp = 0;
666 double root= ABS(square+1)/2;
668 while( ABS(root - temp) > MINIMUM ){
669 temp = root;
670 root = (square/temp + temp)/2;
671 k++;
672 if (k>10000) return 0;
675 return root;
677 /* -----------------------------------------------------------------------
678 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
679 transcendFunc can do sin,cos,log,exp
680 input parameter is angle
681 ----------------------------------------------------------------------- */
682 void transcendFunc(char* func, double* tt, int* ttPower)
684 double t = (*tt)*M_PI/180; int tPower = *ttPower;
685 int sign = 1;
686 int n = 50; /* n <=50, tables are all <= 50 */
687 int j;
688 double x,y,z,xt,yt,zt;
690 if (tPower < -998) {
691 calStatus = cal_normal;
692 return;
694 if (tPower > 8) {
695 calStatus = cal_error;
696 return;
698 *ttPower = 0;
699 calStatus = cal_normal;
701 if( func[0] =='s' || func[0] =='S'|| func[0] =='t' || func[0] =='T')
702 sign = SIGN(t);
703 else {
704 /* if( func[0] =='c' || func[0] =='C') */
705 sign = 1;
707 t = ABS(t);
709 while (tPower > 0){
710 t *= 10;
711 tPower--;
713 while (tPower < 0) {
714 t /= 10;
715 tPower++;
717 j = 0;
718 while (t > j*M_TWOPI) {j++;}
719 t -= (j-1)*M_TWOPI;
720 if (M_PI_2 < t && t < 3*M_PI_2){
721 t = M_PI - t;
722 if (func[0] =='c' || func[0] =='C')
723 sign = -1;
724 else if (func[0] =='t' || func[0] =='T')
725 t*=-1;
727 else if ( 3*M_PI_2 <= t && t <= M_TWOPI)
728 t -= M_TWOPI;
730 x = 0.60725293500888; y = 0; z = t;
731 for (j=1;j<n+2;j++){
732 xt = x - SIGN(z) * y*cordicTable[j-1][0];
733 yt = y + SIGN(z) * x*cordicTable[j-1][0];
734 zt = z - SIGN(z) * cordicTable[j-1][1];
735 x = xt;
736 y=yt;
737 z=zt;
739 if( func[0] =='s' || func[0] =='S') {
740 *tt = sign*y;
741 return;
743 else if( func[0] =='c' || func[0] =='C') {
744 *tt = sign*x;
745 return;
747 else /*if( func[0] =='t' || func[0] =='T')*/ {
748 if(t==M_PI_2||t==-M_PI_2){
749 calStatus = cal_error;
750 return;
752 else{
753 *tt = sign*(y/x);
754 return;
759 /* -----------------------------------------------------------------------
760 add in scientific number format
761 ----------------------------------------------------------------------- */
762 void doAdd (double* operandOne, int* powerOne,
763 double operandTwo, int powerTwo)
765 if ( *powerOne >= powerTwo ){
766 if (*powerOne - powerTwo <= DIGITLEN+1){
767 while (powerTwo < *powerOne){
768 operandTwo /=10;
769 powerTwo++;
771 *operandOne += operandTwo;
773 /*do nothing if operandTwo is too small*/
775 else{
776 if (powerTwo - *powerOne <= DIGITLEN+1){
777 while(powerTwo > *powerOne){
778 *operandOne /=10;
779 (*powerOne)++;
781 (*operandOne) += operandTwo;
783 else{/* simply copy operandTwo if operandOne is too small */
784 *operandOne = operandTwo;
785 *powerOne = powerTwo;
789 /* -----------------------------------------------------------------------
790 multiple in scientific number format
791 ----------------------------------------------------------------------- */
792 void doMultiple(double* operandOne, int* powerOne,
793 double operandTwo, int powerTwo)
795 (*operandOne) *= operandTwo;
796 (*powerOne) += powerTwo;
799 /* -----------------------------------------------------------------------
800 Handles all one operand calculations
801 ----------------------------------------------------------------------- */
802 void oneOperand(void)
804 int k = 0;
805 if (buttonGroup == basicButtons){
806 switch(CAL_BUTTON){
807 case btn_sqr:
808 if (result<0)
809 calStatus = cal_error;
810 else{
811 if (power%2 == 1){
812 result = (mySqrt(result*10))/10;
813 power = (power+1) / 2;
815 else{
816 result = mySqrt(result);
817 power = power / 2;
819 calStatus = cal_normal;
821 break;
822 case btn_square:
823 power *= 2;
824 result *= result;
825 calStatus = cal_normal;
826 break;
828 case btn_rec:
829 if (result==0)
830 calStatus = cal_error;
831 else{
832 power = -power;
833 result = 1/result;
834 calStatus = cal_normal;
836 break;
837 default:
838 calStatus = cal_toDo;
839 break; /* just for the safety */
842 else{ /* sciButtons */
843 switch(CAL_BUTTON){
844 case sci_sin:
845 transcendFunc("sin", &result, &power);
846 break;
847 case sci_cos:
848 transcendFunc("cos", &result, &power);
849 break;
850 case sci_tan:
851 transcendFunc("tan", &result, &power);
852 break;
853 case sci_fac:
854 if (power<0 || power>8 || result<0 )
855 calStatus = cal_error;
856 else if(result == 0) {
857 result = 1;
858 power = 0;
860 else{
861 while(power > 0) {
862 result *= 10;
863 power--;
865 if ( ( result - (int)result) > MINIMUM )
866 calStatus = cal_error;
867 else {
868 k = result; result = 1;
869 while (k > 1){
870 doMultiple(&result, &power, k, 0);
871 formatResult();
872 k--;
874 calStatus = cal_normal;
877 break;
878 default:
879 calStatus = cal_toDo;
880 break; /* just for the safety */
886 /* -----------------------------------------------------------------------
887 Handles all two operands calculations
888 ----------------------------------------------------------------------- */
889 void twoOperands(void)
891 switch(oper){
892 case '-':
893 doAdd(&operand, &operandPower, -result, power);
894 break;
895 case '+':
896 doAdd(&operand, &operandPower, result, power);
897 break;
898 case '*':
899 doMultiple(&operand, &operandPower, result, power);
900 break;
901 case '/':
902 if ( ABS(result) > MINIMUM ){
903 doMultiple(&operand, &operandPower, 1/result, -power);
905 else
906 calStatus = cal_error;
907 break;
908 default: /* ' ' */
909 switchOperands(); /* counter switchOperands() below */
910 break;
911 } /* switch(oper) */
912 switchOperands();
913 clearOper();
916 /* First, increases *dimen1 by dimen1_delta modulo dimen1_modulo.
917 If dimen1 wraps, increases *dimen2 by dimen2_delta modulo dimen2_modulo.
919 static void move_with_wrap_and_shift(
920 int *dimen1, int dimen1_delta, int dimen1_modulo,
921 int *dimen2, int dimen2_delta, int dimen2_modulo)
923 bool wrapped = false;
925 *dimen1 += dimen1_delta;
926 if (*dimen1 < 0)
928 *dimen1 = dimen1_modulo - 1;
929 wrapped = true;
931 else if (*dimen1 >= dimen1_modulo)
933 *dimen1 = 0;
934 wrapped = true;
937 if (wrapped)
939 /* Make the dividend always positive to be sure about the result.
940 Adding dimen2_modulo does not change it since we do it modulo. */
941 *dimen2 = (*dimen2 + dimen2_modulo + dimen2_delta) % dimen2_modulo;
945 /* -----------------------------------------------------------------------
946 Print buttons when switching 1st and 2nd
947 int group = {basicButtons, sciButtons}
948 ----------------------------------------------------------------------- */
949 void printButtonGroups(int group)
951 drawButtons(group);
952 drawLines();
953 rb->lcd_update();
955 /* -----------------------------------------------------------------------
956 flash the currently marked button
957 ----------------------------------------------------------------------- */
958 void flashButton(void)
960 int k, w, h;
961 for (k=2;k>0;k--)
963 rb->lcd_getstringsize( buttonChar[buttonGroup][btn_row][btn_col],&w,&h);
964 rb->lcd_set_drawmode(DRMODE_SOLID|(k==1) ? 0 : DRMODE_INVERSEVID);
965 rb->lcd_fillrect( X_0_POS + btn_col*REC_WIDTH + 1,
966 Y_1_POS + btn_row*REC_HEIGHT + 1,
967 REC_WIDTH - 1, REC_HEIGHT - 1);
968 rb->lcd_putsxy( X_0_POS + btn_col*REC_WIDTH + (REC_WIDTH - w)/2,
969 Y_1_POS + btn_row*REC_HEIGHT + (REC_HEIGHT - h)/2 +1,
970 buttonChar[buttonGroup][btn_row][btn_col] );
971 rb->lcd_update_rect( X_0_POS + btn_col*REC_WIDTH + 1,
972 Y_1_POS + btn_row*REC_HEIGHT + 1,
973 REC_WIDTH - 1, REC_HEIGHT - 1);
975 if (k!= 1)
976 rb->sleep(HZ/22);
981 /* -----------------------------------------------------------------------
982 pos is the position that needs animation. pos = [1~18]
983 ----------------------------------------------------------------------- */
984 void deleteAnimation(int pos)
986 int k;
987 if (pos<1 || pos >18)
988 return;
989 pos--;
990 rb->lcd_fillrect(1+pos*6, TEXT_1_POS, 6, 8);
991 rb->lcd_update_rect(1+pos*6, TEXT_1_POS, 6, 8);
993 for (k=1;k<=4;k++){
994 rb->sleep(HZ/32);
995 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
996 rb->lcd_fillrect(1+pos*6, TEXT_1_POS, 6, 8);
997 rb->lcd_set_drawmode(DRMODE_SOLID);
998 rb->lcd_fillrect(1+pos*6+1+k, TEXT_1_POS+k,
999 (5-2*k)>0?(5-2*k):1, (7-2*k)>0?(7-2*k):1 );
1000 rb->lcd_update_rect(1+pos*6, TEXT_1_POS, 6, 8);
1005 /* -----------------------------------------------------------------------
1006 result may be one of these formats:
1008 xxxx.xxxx
1009 0.xxxx
1010 0.0000xxxx
1012 formatResult() change result to standard format: 0.xxxx
1013 if result is close to 0, let it be 0;
1014 if result is close to 1, let it be 0.1 and power++;
1015 ----------------------------------------------------------------------- */
1016 void formatResult(void)
1018 int resultsign = SIGN(result);
1019 result = ABS(result);
1020 if(result > MINIMUM ){ /* doesn't check power, might have problem
1021 input wouldn't,
1022 + - * / of two formatted number wouldn't.
1023 only a calculation that makes a formatted
1024 number (0.xxxx) less than MINIMUM in only
1025 one operation */
1027 if (result<1){
1028 while( (int)(result*10) == 0 ){
1029 result *= 10;
1030 power--;
1031 modifier *= 10;
1034 else{ /* result >= 1 */
1035 while( (int)result != 0 ){
1036 result /= 10;
1037 power++;
1038 modifier /= 10;
1040 } /* if result<1 */
1042 if (result > (1-MINIMUM)){
1043 result = 0.1;
1044 power++;
1045 modifier /= 10;
1047 result *= resultsign;
1049 else {
1050 result = 0;
1051 power = 0;
1052 modifier = 0.1;
1056 /* -----------------------------------------------------------------------
1057 result2typingbuf() outputs standard format result to typingbuf.
1058 case SCIENTIFIC_FORMAT, let temppower = 1;
1059 case temppower > 0: print '.' in the middle
1060 case temppower <= 0: print '.' in the begining
1061 ----------------------------------------------------------------------- */
1062 void result2typingbuf(void)
1064 bool haveDot = false;
1065 char tempchar = 0;
1066 int k;
1067 double tempresult = ABS(result); /* positive num makes things simple */
1069 int temppower;
1070 double tempmodifier = 1;
1071 int count;
1073 if(SCIENTIFIC_FORMAT)
1074 temppower = 1; /* output x.xxxx format */
1075 else
1076 temppower = power;
1078 cleartypingbuf();
1080 if(tempresult < MINIMUM){ /* if 0,faster display and avoid complication*/
1081 typingbuf[0] = ' ';
1082 typingbuf[1] = '0';
1084 else{ /* tempresult > 0 */
1085 typingbuf[0] = (SIGN(result)<0)?'-':' ';
1087 typingbufPointer = typingbuf;
1088 if(temppower > 0){
1089 for (k = 0; k<DIGITLEN+1 ; k++){
1090 typingbufPointer++;
1091 if(temppower || *(typingbufPointer-1) == '.'){
1092 count = 0;
1093 tempmodifier = tempmodifier/10;
1094 while( (tempresult-tempmodifier*count) >
1095 (tempmodifier-MINIMUM)){
1096 count++;
1098 tempresult -= tempmodifier*count;
1099 tempresult = ABS(tempresult);
1100 temppower-- ;
1101 *typingbufPointer = count + '0';
1103 else{ /* temppower == 0 */
1104 *typingbufPointer = '.';
1105 haveDot = true;
1107 } /* for */
1109 else{
1110 haveDot = true;
1111 typingbufPointer++; *typingbufPointer = '0';
1112 typingbufPointer++; *typingbufPointer = '.';
1113 for (k = 2; k<DIGITLEN+1 ; k++){
1114 typingbufPointer++;
1115 count = 0;
1116 if ( (-temppower) < (k-1)){
1117 tempmodifier = tempmodifier/10;
1118 while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){
1119 count++;
1122 tempresult -= tempmodifier*count;
1123 tempresult = ABS(tempresult);
1124 temppower-- ;
1126 *typingbufPointer = count + '0';
1129 /* now, typingbufPointer = typingbuf + 16 */
1130 /* backward strip off 0 and '.' */
1131 if (haveDot){
1132 while( (*typingbufPointer == '0') || (*typingbufPointer == '.')){
1133 tempchar = *typingbufPointer;
1134 *typingbufPointer = 0;
1135 typingbufPointer--;
1136 if (tempchar == '.') break;
1139 typingbuf[DIGITLEN+1] = 0;
1140 } /* else tempresult > 0 */
1143 /* -----------------------------------------------------------------------
1144 printResult() generates LCD display.
1145 ----------------------------------------------------------------------- */
1146 void printResult(void)
1148 int k, w, h;
1150 char operbuf[3] = {0, 0, 0};
1152 switch_Status:
1153 switch(calStatus){
1154 case cal_exit:
1155 rb->lcd_clear_display();
1156 rb->splash(HZ/3, "Bye now!");
1157 break;
1158 case cal_error:
1159 clearbuf();
1160 rb->snprintf(buf, 19, "%18s","Error");
1161 break;
1162 case cal_toDo:
1163 clearbuf();
1164 rb->snprintf(buf, 19, "%18s","Coming soon ^_* ");
1165 break;
1167 case cal_normal:
1168 formatResult();
1170 if( power > 1000 ){ /* power -1 > 999 */
1171 calStatus = cal_error;
1172 goto switch_Status;
1174 if (power < -998 ) /* power -1 < -999 */
1175 clearResult(); /* too small, let it be 0 */
1177 result2typingbuf();
1178 clearbuf();
1180 operbuf[0] = oper;
1181 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1182 operbuf[2] = '\0';
1184 if(SCIENTIFIC_FORMAT){
1185 /* output format: X.XXXX eXXX */
1186 if(power > -98){ /* power-1 >= -99, eXXX or e-XX */
1187 rb->snprintf(buf, 12, "%11s",typingbuf);
1188 for(k=11;k<=14;k++) buf[k] = ' ';
1189 cleartypingbuf();
1190 rb->snprintf(typingbuf, 5, "e%d",power-1);
1191 rb->snprintf(buf+11, 5, "%4s",typingbuf);
1193 else{ /* power-1 <= -100, e-XXX */
1194 rb->snprintf(buf, 12, "%11s",typingbuf);
1195 rb->snprintf(buf+11, 6, "e%d",power-1);
1198 else{
1199 rb->snprintf(buf, 12, "%11s",typingbuf);
1200 } /* if SCIENTIFIC_FORMAT */
1201 break;
1202 case cal_typing:
1203 case cal_dotted:
1204 clearbuf();
1205 operbuf[0] = oper;
1206 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1207 rb->snprintf(buf, 12, "%11s",typingbuf);
1208 break;
1212 rb->lcd_getstringsize(buf, &w, &h);
1213 rb->screen_clear_area(rb->screens[0], 0, 0, LCD_WIDTH, REC_HEIGHT-1);
1214 rb->lcd_putsxy(4, Y_1_POS - h -1, operbuf);
1215 rb->lcd_putsxy(LCD_WIDTH - w - 4, Y_1_POS - h -1, buf);
1216 rb->lcd_update_rect(0, 1, LCD_WIDTH, Y_1_POS);
1219 /* -----------------------------------------------------------------------
1220 Process typing buttons: 1-9, '.', sign
1221 main operand "result" and typingbuf are processed seperately here.
1222 ----------------------------------------------------------------------- */
1223 void typingProcess(void){
1224 switch( CAL_BUTTON ){
1225 case btn_sign:
1226 if (calStatus == cal_typing ||
1227 calStatus == cal_dotted)
1228 typingbuf[0] = (typingbuf[0]=='-')?' ':'-';
1229 result = -result;
1230 break;
1231 case btn_dot:
1232 operInputted = false;
1233 switch(calStatus){
1234 case cal_normal:
1235 clearInput();
1236 *typingbufPointer = '0';
1237 typingbufPointer++;
1238 case cal_typing:
1239 calStatus = cal_dotted;
1240 *typingbufPointer = '.';
1241 if (typingbufPointer != typingbuf+DIGITLEN+1)
1242 typingbufPointer++;
1243 break;
1244 default: /* cal_dotted */
1245 break;
1247 break;
1248 default: /* 0-9 */
1249 operInputted = false;
1250 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1251 switch(calStatus){
1252 case cal_normal:
1253 if(CAL_BUTTON == btn_0 )
1254 break; /* first input is 0, ignore */
1255 clearInput();
1256 /*no operator means start a new calculation*/
1257 if (oper ==' ')
1258 clearOperand();
1259 calStatus = cal_typing;
1260 /* go on typing, no break */
1261 case cal_typing:
1262 case cal_dotted:
1263 switch(CAL_BUTTON){
1264 case btn_0:
1265 *typingbufPointer = '0';
1266 break;
1267 default:
1268 *typingbufPointer=(7+btn_col-3*(btn_row-1))+ '0';
1269 break;
1271 if (typingbufPointer!=typingbuf+DIGITLEN+1){
1272 typingbufPointer++;
1274 {/* result processing */
1275 if (calStatus == cal_typing) power++;
1276 if (CAL_BUTTON != btn_0)
1277 result= result +
1278 SIGN(result)*
1279 (7+btn_col-3*(btn_row-1))*modifier;
1280 modifier /= 10;
1283 else /* last byte always '\0' */
1284 *typingbufPointer = 0;
1285 break;
1286 default: /* cal_error, cal_exit */
1287 break;
1289 break; /* default, 0-9 */
1290 } /* switch( CAL_BUTTON ) */
1293 /* -----------------------------------------------------------------------
1294 Handle delete operation
1295 main operand "result" and typingbuf are processed seperately here.
1296 ----------------------------------------------------------------------- */
1297 void doDelete(void){
1298 deleteAnimation(18);
1299 switch(calStatus){
1300 case cal_dotted:
1301 if (*(typingbufPointer-1) == '.'){
1302 /* if dotted and deleting '.',
1303 change status and delete '.' below */
1304 calStatus = cal_typing;
1306 else{ /* if dotted and not deleting '.',
1307 power stays */
1308 power++; /* counter "power--;" below */
1310 case cal_typing:
1311 typingbufPointer--;
1313 {/* result processing */ /* 0-9, '.' */
1314 /* if deleting '.', do nothing */
1315 if ( *typingbufPointer != '.'){
1316 power--;
1317 modifier *= 10;
1318 result = result - SIGN(result)*
1319 ((*typingbufPointer)- '0')*modifier;
1323 *typingbufPointer = 0;
1325 /* if (only one digit left and it's 0)
1326 or no digit left, change status*/
1327 if ( typingbufPointer == typingbuf+1 ||
1328 ( typingbufPointer == typingbuf+2 &&
1329 *(typingbufPointer-1) == '0' ))
1330 calStatus = cal_normal;
1331 break;
1332 default: /* normal, error, exit */
1333 break;
1336 /* -----------------------------------------------------------------------
1337 Handle buttons on basic screen
1338 ----------------------------------------------------------------------- */
1339 void basicButtonsProcess(void){
1340 switch (btn) {
1341 case CALCULATOR_INPUT:
1342 if (calStatus == cal_error && (CAL_BUTTON != btn_C) ) break;
1343 flashButton();
1344 switch( CAL_BUTTON ){
1345 case btn_MR:
1346 operInputted = false;
1347 result = memTemp; power = memTempPower;
1348 calStatus = cal_normal;
1349 break;
1350 case btn_M:
1351 formatResult();
1352 if (memTemp > MINIMUM)
1353 doAdd(&memTemp, &memTempPower, result, power);
1354 else {
1355 /* if result is too small and memTemp = 0,
1356 doAdd will not add */
1357 memTemp = result;
1358 memTempPower = power;
1360 calStatus = cal_normal;
1361 break;
1363 case btn_C: clearMem(); break;
1364 case btn_CE: clearInput(); break;
1366 case btn_bas:
1367 buttonGroup = sciButtons;
1368 printButtonGroups(buttonGroup);
1369 break;
1371 /* one operand calculation, may be changed to
1372 like sin, cos, log, etc */
1373 case btn_sqr:
1374 case btn_square:
1375 case btn_rec:
1376 formatResult(); /* not necessary, just for safty */
1377 oneOperand();
1378 break;
1380 case_btn_equal: /* F3 shortkey entrance */
1381 case btn_equal:
1382 formatResult();
1383 calStatus = cal_normal;
1384 operInputted = false;
1385 if (oper != ' ') twoOperands();
1386 break;
1388 case btn_div:
1389 case btn_time:
1390 case btn_minus:
1391 case btn_add:
1392 if(!operInputted) {twoOperands(); operInputted = true;}
1393 oper = buttonChar[basicButtons][btn_row][btn_col][0];
1394 #ifdef CALCULATOR_OPERATORS
1395 case_cycle_operators: /* F2 shortkey entrance */
1396 #endif
1397 calStatus = cal_normal;
1398 formatResult();
1399 operand = result;
1400 operandPower = power;
1402 break;
1404 case btn_sign:
1405 case btn_dot:
1406 default: /* 0-9 */
1407 typingProcess();
1408 break;
1409 } /* switch (CAL_BUTTON) */
1410 break;
1412 #ifdef CALCULATOR_OPERATORS
1413 case CALCULATOR_OPERATORS:
1414 if (calStatus == cal_error) break;
1415 if (!operInputted) {twoOperands(); operInputted = true;}
1416 switch (oper){
1417 case ' ':
1418 case '/': oper = '+'; flashButton(); break;
1419 case '+': oper = '-'; flashButton(); break;
1420 case '-': oper = '*'; flashButton(); break;
1421 case '*': oper = '/'; flashButton(); break;
1423 goto case_cycle_operators;
1424 break;
1425 #endif
1427 case CALCULATOR_CALC:
1428 if (calStatus == cal_error) break;
1429 flashButton();
1430 goto case_btn_equal;
1431 break;
1432 default: break;
1434 printResult();
1437 /* -----------------------------------------------------------------------
1438 Handle buttons on scientific screen
1439 ----------------------------------------------------------------------- */
1440 void sciButtonsProcess(void){
1441 switch (btn) {
1442 case CALCULATOR_INPUT:
1443 if (calStatus == cal_error && (CAL_BUTTON != sci_sci) ) break;
1444 flashButton();
1445 switch( CAL_BUTTON ){
1447 case sci_pi:
1448 result = M_PI; power = 0;
1449 calStatus = cal_normal;
1450 break;
1452 case sci_xy: break;
1454 case sci_sci:
1455 buttonGroup = basicButtons;
1456 printButtonGroups(basicButtons);
1457 break;
1459 case sci_fac:
1460 case sci_sin:
1461 case sci_asin:
1462 case sci_cos:
1463 case sci_acos:
1464 case sci_tan:
1465 case sci_atan:
1466 case sci_ln:
1467 case sci_exp:
1468 case sci_log:
1469 formatResult(); /* not necessary, just for safty */
1470 oneOperand();
1471 break;
1473 case btn_sign:
1474 case btn_dot:
1475 default: /* 0-9 */
1476 typingProcess();
1477 break;
1478 } /* switch (CAL_BUTTON) */
1479 break;
1481 #ifdef CALCULATOR_OPERATORS
1482 case CALCULATOR_OPERATORS:
1483 if (calStatus == cal_error) break;
1484 if (!operInputted) {twoOperands(); operInputted = true;}
1485 switch (oper){
1486 case ' ': oper = '+'; break;
1487 case '/': oper = '+'; deleteAnimation(1); break;
1488 case '+': oper = '-'; deleteAnimation(1); break;
1489 case '-': oper = '*'; deleteAnimation(1); break;
1490 case '*': oper = '/'; deleteAnimation(1); break;
1492 calStatus = cal_normal;
1493 formatResult();
1494 operand = result;
1495 operandPower = power;
1496 break;
1497 #endif
1499 case CALCULATOR_CALC:
1500 if (calStatus == cal_error) break;
1501 formatResult();
1502 calStatus = cal_normal;
1503 operInputted = false;
1504 if (oper != ' ') twoOperands();
1505 break;
1506 default: break;
1508 printResult();
1511 /* -----------------------------------------------------------------------
1512 move button index
1513 Invert display new button, invert back previous button
1514 ----------------------------------------------------------------------- */
1515 int handleButton(int button){
1516 switch(button)
1518 case CALCULATOR_INPUT:
1519 case CALCULATOR_CALC:
1520 #ifdef CALCULATOR_INPUT_CALC_PRE
1521 if (lastbtn != CALCULATOR_INPUT_CALC_PRE)
1522 break;
1523 /* no unconditional break; here! */
1524 #endif
1525 #ifdef CALCULATOR_OPERATORS
1526 case CALCULATOR_OPERATORS:
1527 #endif
1528 switch(buttonGroup){
1529 case basicButtons:
1530 basicButtonsProcess();
1531 break;
1532 case sciButtons:
1533 sciButtonsProcess();
1534 break;
1536 break;
1538 #ifdef CALCULATOR_CLEAR
1539 case CALCULATOR_CLEAR:
1540 switch(calStatus){
1541 case cal_typing:
1542 case cal_dotted:
1543 doDelete();
1544 break;
1545 default: /* cal_normal, cal_error, cal_exit */
1546 clearMem();
1547 break;
1549 printResult();
1550 break;
1551 #endif
1552 case CALCULATOR_LEFT:
1553 case CALCULATOR_LEFT | BUTTON_REPEAT:
1554 move_with_wrap_and_shift(
1555 &btn_col, -1, BUTTON_COLS,
1556 &btn_row, 0, BUTTON_ROWS);
1557 break;
1559 case CALCULATOR_RIGHT:
1560 case CALCULATOR_RIGHT | BUTTON_REPEAT:
1561 move_with_wrap_and_shift(
1562 &btn_col, 1, BUTTON_COLS,
1563 &btn_row, 0, BUTTON_ROWS);
1564 break;
1566 #ifdef CALCULATOR_UP
1567 case CALCULATOR_UP:
1568 case CALCULATOR_UP | BUTTON_REPEAT:
1569 move_with_wrap_and_shift(
1570 &btn_row, -1, BUTTON_ROWS,
1571 &btn_col, 0, BUTTON_COLS);
1572 break;
1573 #endif
1574 #ifdef CALCULATOR_DOWN
1575 case CALCULATOR_DOWN:
1576 case CALCULATOR_DOWN | BUTTON_REPEAT:
1577 move_with_wrap_and_shift(
1578 &btn_row, 1, BUTTON_ROWS,
1579 &btn_col, 0, BUTTON_COLS);
1580 break;
1581 #endif
1583 #ifdef CALCULATOR_UP_W_SHIFT
1584 case CALCULATOR_UP_W_SHIFT:
1585 case CALCULATOR_UP_W_SHIFT | BUTTON_REPEAT:
1586 move_with_wrap_and_shift(
1587 &btn_row, -1, BUTTON_ROWS,
1588 &btn_col, -1, BUTTON_COLS);
1589 break;
1590 #endif
1591 #ifdef CALCULATOR_DOWN_W_SHIFT
1592 case CALCULATOR_DOWN_W_SHIFT:
1593 case CALCULATOR_DOWN_W_SHIFT | BUTTON_REPEAT:
1594 move_with_wrap_and_shift(
1595 &btn_row, 1, BUTTON_ROWS,
1596 &btn_col, 1, BUTTON_COLS);
1597 break;
1598 #endif
1599 #ifdef CALCULATOR_LEFT_W_SHIFT
1600 case CALCULATOR_LEFT_W_SHIFT:
1601 case CALCULATOR_LEFT_W_SHIFT | BUTTON_REPEAT:
1602 move_with_wrap_and_shift(
1603 &btn_col, -1, BUTTON_COLS,
1604 &btn_row, -1, BUTTON_ROWS);
1605 break;
1606 #endif
1607 #ifdef CALCULATOR_RIGHT_W_SHIFT
1608 case CALCULATOR_RIGHT_W_SHIFT:
1609 case CALCULATOR_RIGHT_W_SHIFT | BUTTON_REPEAT:
1610 move_with_wrap_and_shift(
1611 &btn_col, 1, BUTTON_COLS,
1612 &btn_row, 1, BUTTON_ROWS);
1613 break;
1614 #endif
1615 #ifdef CALCULATOR_RC_QUIT
1616 case CALCULATOR_RC_QUIT:
1617 #endif
1618 case CALCULATOR_QUIT:
1619 return -1;
1622 return 0;
1624 prev_btn_row = btn_row;
1625 prev_btn_col = btn_col;
1628 /* -----------------------------------------------------------------------
1629 Main();
1630 ----------------------------------------------------------------------- */
1631 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
1633 (void)parameter;
1634 rb = api;
1636 /* now go ahead and have fun! */
1638 cal_initial();
1640 while (calStatus != cal_exit ) {
1641 btn = rb->button_get_w_tmo(HZ/2);
1642 #ifdef HAVE_TOUCHSCREEN
1643 if(btn & BUTTON_TOUCHSCREEN)
1645 struct ts_raster_result res;
1646 if(touchscreen_map_raster(&calc_raster, rb->button_get_data() >> 16,
1647 rb->button_get_data() & 0xffff, &res) == 1)
1649 btn_row = res.y;
1650 btn_col = res.x;
1651 drawButtons(buttonGroup);
1652 drawLines();
1654 rb->lcd_update();
1656 prev_btn_row = btn_row;
1657 prev_btn_col = btn_col;
1658 if(btn & BUTTON_REL)
1660 btn = CALCULATOR_INPUT;
1661 switch(buttonGroup){
1662 case basicButtons:
1663 basicButtonsProcess();
1664 break;
1665 case sciButtons:
1666 sciButtonsProcess();
1667 break;
1669 btn = BUTTON_TOUCHSCREEN;
1673 #endif
1674 if (handleButton(btn) == -1)
1676 calStatus = cal_exit;
1677 printResult();
1679 else
1681 drawButtons(buttonGroup);
1682 drawLines();
1685 rb->lcd_update();
1687 if(rb->default_event_handler(btn) == SYS_USB_CONNECTED)
1688 return PLUGIN_USB_CONNECTED;
1690 if (btn != BUTTON_NONE)
1691 lastbtn = btn;
1692 } /* while (calStatus != cal_exit ) */
1694 rb->button_clear_queue();
1695 return PLUGIN_OK;
1698 #endif /* #ifdef HAVE_LCD_BITMAP */