Don't rebuild the dependency file on 'make reconf'. Build type and target won't chang...
[kugel-rb.git] / apps / plugins / calculator.c
blob1795bca7a798250086353b37e60e4ac29393bedf
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 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
214 #define CALCULATOR_CLEAR BUTTON_HOME
215 #endif
218 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
219 #define CALCULATOR_LEFT BUTTON_LEFT
220 #define CALCULATOR_RIGHT BUTTON_RIGHT
221 #define CALCULATOR_UP BUTTON_UP
222 #define CALCULATOR_DOWN BUTTON_DOWN
223 #define CALCULATOR_QUIT BUTTON_POWER
224 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
225 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
226 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
227 #define CALCULATOR_CLEAR BUTTON_HOME
229 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
230 #define CALCULATOR_LEFT BUTTON_LEFT
231 #define CALCULATOR_RIGHT BUTTON_RIGHT
232 #define CALCULATOR_UP BUTTON_UP
233 #define CALCULATOR_DOWN BUTTON_DOWN
234 #define CALCULATOR_QUIT BUTTON_POWER
235 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
236 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
237 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
238 #define CALCULATOR_CLEAR (BUTTON_SELECT|BUTTON_UP)
240 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
242 #define CALCULATOR_LEFT BUTTON_LEFT
243 #define CALCULATOR_RIGHT BUTTON_RIGHT
244 #define CALCULATOR_UP BUTTON_SCROLL_UP
245 #define CALCULATOR_DOWN BUTTON_SCROLL_DOWN
246 #define CALCULATOR_QUIT BUTTON_POWER
247 #define CALCULATOR_INPUT_CALC_PRE BUTTON_PLAY
248 #define CALCULATOR_INPUT (BUTTON_PLAY | BUTTON_REL)
249 #define CALCULATOR_CALC (BUTTON_PLAY | BUTTON_REPEAT)
250 #define CALCULATOR_CLEAR BUTTON_REW
252 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
254 #define CALCULATOR_LEFT BUTTON_LEFT
255 #define CALCULATOR_RIGHT BUTTON_RIGHT
256 #define CALCULATOR_UP BUTTON_UP
257 #define CALCULATOR_DOWN BUTTON_DOWN
258 #define CALCULATOR_QUIT BUTTON_BACK
259 #define CALCULATOR_INPUT BUTTON_SELECT
260 #define CALCULATOR_CALC BUTTON_MENU
261 #define CALCULATOR_CLEAR BUTTON_PLAY
263 #elif (CONFIG_KEYPAD == MROBE100_PAD)
265 #define CALCULATOR_LEFT BUTTON_LEFT
266 #define CALCULATOR_RIGHT BUTTON_RIGHT
267 #define CALCULATOR_UP BUTTON_UP
268 #define CALCULATOR_DOWN BUTTON_DOWN
269 #define CALCULATOR_QUIT BUTTON_POWER
270 #define CALCULATOR_INPUT BUTTON_SELECT
271 #define CALCULATOR_CALC BUTTON_MENU
272 #define CALCULATOR_CLEAR BUTTON_DISPLAY
274 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
276 #define CALCULATOR_LEFT BUTTON_RC_REW
277 #define CALCULATOR_RIGHT BUTTON_RC_FF
278 #define CALCULATOR_UP BUTTON_RC_VOL_UP
279 #define CALCULATOR_DOWN BUTTON_RC_VOL_DOWN
280 #define CALCULATOR_QUIT BUTTON_RC_REC
281 #define CALCULATOR_INPUT BUTTON_RC_PLAY
282 #define CALCULATOR_CALC BUTTON_RC_MODE
283 #define CALCULATOR_CLEAR BUTTON_RC_MENU
285 #define CALCULATOR_RC_QUIT BUTTON_REC
287 #elif (CONFIG_KEYPAD == COWOND2_PAD)
289 #define CALCULATOR_QUIT BUTTON_POWER
290 #define CALCULATOR_CLEAR BUTTON_MENU
292 #elif CONFIG_KEYPAD == IAUDIO67_PAD
294 #define CALCULATOR_LEFT BUTTON_LEFT
295 #define CALCULATOR_RIGHT BUTTON_RIGHT
296 #define CALCULATOR_UP BUTTON_VOLUP
297 #define CALCULATOR_DOWN BUTTON_VOLDOWN
298 #define CALCULATOR_QUIT BUTTON_POWER
299 #define CALCULATOR_INPUT BUTTON_PLAY
300 #define CALCULATOR_CALC BUTTON_MENU
301 #define CALCULATOR_CLEAR BUTTON_STOP
303 #define CALCULATOR_RC_QUIT (BUTTON_MENU|BUTTON_PLAY)
305 #elif (CONFIG_KEYPAD == CREATIVEZVM_PAD)
307 #define CALCULATOR_LEFT BUTTON_LEFT
308 #define CALCULATOR_RIGHT BUTTON_RIGHT
309 #define CALCULATOR_UP BUTTON_UP
310 #define CALCULATOR_DOWN BUTTON_DOWN
311 #define CALCULATOR_QUIT BUTTON_BACK
312 #define CALCULATOR_INPUT BUTTON_SELECT
313 #define CALCULATOR_CALC BUTTON_MENU
314 #define CALCULATOR_CLEAR BUTTON_PLAY
316 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
318 #define CALCULATOR_LEFT BUTTON_LEFT
319 #define CALCULATOR_RIGHT BUTTON_RIGHT
320 #define CALCULATOR_UP BUTTON_UP
321 #define CALCULATOR_DOWN BUTTON_DOWN
322 #define CALCULATOR_QUIT BUTTON_POWER
323 #define CALCULATOR_INPUT BUTTON_SELECT
324 #define CALCULATOR_CALC BUTTON_MENU
325 #define CALCULATOR_CLEAR BUTTON_VIEW
327 #else
328 #error No keymap defined!
329 #endif
331 #ifdef HAVE_TOUCHSCREEN
332 #ifndef CALCULATOR_LEFT
333 #define CALCULATOR_LEFT BUTTON_MIDLEFT
334 #endif
335 #ifndef CALCULATOR_RIGHT
336 #define CALCULATOR_RIGHT BUTTON_MIDRIGHT
337 #endif
338 #ifndef CALCULATOR_UP
339 #define CALCULATOR_UP BUTTON_TOPMIDDLE
340 #endif
341 #ifndef CALCULATOR_DOWN
342 #define CALCULATOR_DOWN BUTTON_BOTTOMMIDDLE
343 #endif
344 #ifndef CALCULATOR_CALC
345 #define CALCULATOR_CALC BUTTON_BOTTOMRIGHT
346 #endif
347 #ifndef CALCULATOR_INPUT
348 #define CALCULATOR_INPUT BUTTON_CENTER
349 #endif
350 #ifndef CALCULATOR_CLEAR
351 #define CALCULATOR_CLEAR BUTTON_TOPRIGHT
352 #endif
354 #include "lib/touchscreen.h"
355 static struct ts_raster calc_raster = { X_0_POS, Y_1_POS, BUTTON_COLS*REC_WIDTH, BUTTON_ROWS*REC_HEIGHT, REC_WIDTH, REC_HEIGHT };
356 #endif
358 enum {
359 basicButtons,
360 sciButtons
361 } buttonGroup;
363 unsigned char* buttonChar[2][5][5] = {
364 { { "MR" , "M+" , "2nd" , "CE" , "C" },
365 { "7" , "8" , "9" , "/" , "sqr" },
366 { "4" , "5" , "6" , "*" , "x^2" },
367 { "1" , "2" , "3" , "-" , "1/x" },
368 { "0" , "+/-", "." , "+" , "=" } },
370 { { "n!" , "PI" , "1st" , "sin" , "asi" },
371 { "7" , "8" , "9" , "cos" , "aco" },
372 { "4" , "5" , "6" , "tan" , "ata" },
373 { "1" , "2" , "3" , "ln" , "e^x" },
374 { "0" , "+/-", "." , "log" , "x^y" } }
377 enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C ,
378 btn_7 , btn_8 , btn_9 , btn_div , btn_sqr ,
379 btn_4 , btn_5 , btn_6 , btn_time , btn_square ,
380 btn_1 , btn_2 , btn_3 , btn_minus , btn_rec ,
381 btn_0 , btn_sign , btn_dot , btn_add , btn_equal
384 enum { sci_fac, sci_pi , sci_sci , sci_sin , sci_asin ,
385 sci_7 , sci_8 , sci_9 , sci_cos , sci_acos ,
386 sci_4 , sci_5 , sci_6 , sci_tan , sci_atan ,
387 sci_1 , sci_2 , sci_3 , sci_ln , sci_exp ,
388 sci_0 , sci_sign , sci_dot , sci_log , sci_xy
391 #define MINIMUM 0.000000000001 /* e-12 */
392 /* ^ ^ ^ ^ */
393 /* 123456789abcdef */
395 #define DIGITLEN 10 /* must <= 10 */
396 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
397 /* 0.000 00000 0001 */
398 /* ^ ^ ^ ^ ^ ^ */
399 /* DIGITLEN 12345 6789a bcdef */
400 /* power 12 34567 89abc def */
401 /* 10^- 123 45678 9abcd ef */
403 unsigned char buf[19];/* 18 bytes of output line,
404 buf[0] is operator
405 buf[1] = 'M' if memTemp is not 0
406 buf[2] = ' '
408 if SCIENTIFIC_FORMAT
409 buf[2]-buf[12] or buf[3]-buf[13] = result;
410 format X.XXXXXXXX
411 buf[13] or buf[14] -buf[17] = power;
412 format eXXX or e-XXX
413 else
414 buf[3]-buf[6] = ' ';
415 buf[7]-buf[17] = result;
417 buf[18] = '\0' */
419 unsigned char typingbuf[DIGITLEN+2];/* byte 0 is sign or ' ',
420 byte 1~DIGITLEN are num and '.'
421 byte (DIGITLEN+1) is '\0' */
422 unsigned char* typingbufPointer = typingbuf;
424 double result = 0; /* main operand, format 0.xxxxx */
425 int power = 0; /* 10^power */
426 double modifier = 0.1; /* position of next input */
427 double operand = 0; /* second operand, format 0.xxxxx */
428 int operandPower = 0; /* 10^power of second operand */
429 char oper = ' '; /* operators: + - * / */
430 bool operInputted = false; /* false: do calculation first and
431 replace current oper
432 true: just replace current oper */
434 double memTemp = 0; /* temp memory */
435 int memTempPower = 0; /* 10^^power of memTemp */
437 int btn_row, btn_col; /* current position index for button */
438 int prev_btn_row, prev_btn_col; /* previous cursor position */
439 #define CAL_BUTTON (btn_row*5+btn_col)
441 int btn = BUTTON_NONE;
442 int lastbtn = BUTTON_NONE;
444 /* Status of calculator */
445 enum {cal_normal, /* 0, normal status, display result */
446 cal_typing, /* 1, currently typing, dot hasn't been typed */
447 cal_dotted, /* 2, currently typing, dot already has been typed. */
448 cal_error,
449 cal_exit,
450 cal_toDo
451 } calStatus;
453 /* constant table for CORDIC algorithm */
454 double cordicTable[51][2]= {
455 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
456 {1e+00, 7.853981633974483e-01},
457 {5e-01, 4.636476090008061e-01},
458 {2.5e-01, 2.449786631268641e-01},
459 {1.25e-01, 1.243549945467614e-01},
460 {6.25e-02, 6.241880999595735e-02},
461 {3.125e-02, 3.123983343026828e-02},
462 {1.5625e-02, 1.562372862047683e-02},
463 {7.8125e-03, 7.812341060101111e-03},
464 {3.90625e-03, 3.906230131966972e-03},
465 {1.953125e-03, 1.953122516478819e-03},
466 {9.765625e-04, 9.765621895593195e-04},
467 {4.8828125e-04, 4.882812111948983e-04},
468 {2.44140625e-04, 2.441406201493618e-04},
469 {1.220703125e-04, 1.220703118936702e-04},
470 {6.103515625e-05, 6.103515617420877e-05},
471 {3.0517578125e-05, 3.051757811552610e-05},
472 {1.52587890625e-05, 1.525878906131576e-05},
473 {7.62939453125e-06, 7.629394531101970e-06},
474 {3.814697265625e-06, 3.814697265606496e-06},
475 {1.9073486328125e-06, 1.907348632810187e-06},
476 {9.5367431640625e-07, 9.536743164059608e-07},
477 {4.76837158203125e-07, 4.768371582030888e-07},
478 {2.384185791015625e-07, 2.384185791015580e-07},
479 {1.1920928955078125e-07, 1.192092895507807e-07},
480 {5.9604644775390625e-08, 5.960464477539055e-08},
481 {2.98023223876953125e-08, 2.980232238769530e-08},
482 {1.490116119384765625e-08, 1.490116119384765e-08},
483 {7.450580596923828125e-09, 7.450580596923828e-09},
484 {3.7252902984619140625e-09, 3.725290298461914e-09},
485 {1.86264514923095703125e-09, 1.862645149230957e-09},
486 {9.31322574615478515625e-10, 9.313225746154785e-10},
487 {4.656612873077392578125e-10, 4.656612873077393e-10},
488 {2.3283064365386962890625e-10, 2.328306436538696e-10},
489 {1.16415321826934814453125e-10, 1.164153218269348e-10},
490 {5.82076609134674072265625e-11, 5.820766091346741e-11},
491 {2.910383045673370361328125e-11, 2.910383045673370e-11},
492 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
493 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
494 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
495 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
496 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
497 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
498 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
499 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
500 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
501 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
502 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
503 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
504 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
505 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
506 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
509 void doMultiple(double* operandOne, int* powerOne,
510 double operandTwo, int powerTwo);
511 void doAdd (double* operandOne, int* powerOne,
512 double operandTwo, int powerTwo);
513 void printResult(void);
514 void formatResult(void);
515 void oneOperand(void);
517 void drawLines(void);
518 void drawButtons(int group);
520 /* -----------------------------------------------------------------------
521 Handy funtions
522 ----------------------------------------------------------------------- */
523 void cleartypingbuf(void)
525 int k;
526 for( k=1; k<=(DIGITLEN+1); k++)
527 typingbuf[k] = 0;
528 typingbuf[0] = ' ';
529 typingbufPointer = typingbuf+1;
531 void clearbuf(void)
533 int k;
534 for(k=0;k<18;k++)
535 buf[k]=' ';
536 buf[18] = 0;
538 void clearResult(void)
540 result = 0;
541 power = 0;
542 modifier = 0.1;
545 void clearInput(void)
547 calStatus = cal_normal;
548 clearResult();
549 cleartypingbuf();
550 rb->lcd_clear_display();
551 drawButtons(buttonGroup);
552 drawLines();
555 void clearOperand(void)
557 operand = 0;
558 operandPower = 0;
561 void clearMemTemp(void)
563 memTemp = 0;
564 memTempPower = 0;
567 void clearOper(void)
569 oper = ' ';
570 operInputted = false;
573 void clearMem(void)
575 clearInput();
576 clearMemTemp();
577 clearOperand();
578 clearOper();
579 btn = BUTTON_NONE;
582 void switchOperands(void)
584 double tempr = operand;
585 int tempp = operandPower;
586 operand = result;
587 operandPower = power;
588 result = tempr;
589 power = tempp;
592 void drawLines(void)
594 int i;
595 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS-1);
596 for (i = 0; i < 5 ; i++)
597 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS+i*REC_HEIGHT);
598 for (i = 0; i < 4 ; i++)
599 rb->lcd_vline(X_1_POS+i*REC_WIDTH, Y_1_POS, LCD_HEIGHT);
602 void drawButtons(int group)
604 int i, j, w, h;
605 for (i = 0; i <= 4; i++){
606 for (j = 0; j <= 4; j++){
607 rb->lcd_getstringsize( buttonChar[group][i][j],&w,&h);
608 if (i == btn_row && j == btn_col) /* selected item */
609 rb->lcd_set_drawmode(DRMODE_SOLID);
610 else
611 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
612 rb->lcd_fillrect( X_0_POS + j*REC_WIDTH,
613 Y_1_POS + i*REC_HEIGHT,
614 REC_WIDTH, REC_HEIGHT+1);
615 if (i == btn_row && j == btn_col) /* selected item */
616 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
617 else
618 rb->lcd_set_drawmode(DRMODE_SOLID);
619 rb->lcd_putsxy( X_0_POS + j*REC_WIDTH + (REC_WIDTH - w)/2,
620 Y_1_POS + i*REC_HEIGHT + (REC_HEIGHT - h)/2 + 1,
621 buttonChar[group][i][j] );
624 rb->lcd_set_drawmode(DRMODE_SOLID);
627 /* -----------------------------------------------------------------------
628 Initiate calculator
629 ----------------------------------------------------------------------- */
630 void cal_initial (void)
632 int w,h;
634 rb->lcd_getstringsize("A",&w,&h);
635 if (h >= REC_HEIGHT)
636 rb->lcd_setfont(FONT_SYSFIXED);
638 rb->lcd_clear_display();
640 #ifdef CALCULATOR_OPERATORS
641 /* basic operators are available through separate button */
642 buttonGroup = sciButtons;
643 #else
644 buttonGroup = basicButtons;
645 #endif
647 /* initially, invert button "5" */
648 btn_row = 2;
649 btn_col = 1;
650 prev_btn_row = btn_row;
651 prev_btn_col = btn_col;
652 drawButtons(buttonGroup);
653 drawLines();
654 rb->lcd_update();
656 /* initial mem and output display*/
657 clearMem();
658 printResult();
660 /* clear button queue */
661 rb->button_clear_queue();
664 /* -----------------------------------------------------------------------
665 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
666 in it's private case for sqrt.
667 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
668 ----------------------------------------------------------------------- */
669 double mySqrt(double square)
671 int k = 0;
672 double temp = 0;
673 double root= ABS(square+1)/2;
675 while( ABS(root - temp) > MINIMUM ){
676 temp = root;
677 root = (square/temp + temp)/2;
678 k++;
679 if (k>10000) return 0;
682 return root;
684 /* -----------------------------------------------------------------------
685 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
686 transcendFunc can do sin,cos,log,exp
687 input parameter is angle
688 ----------------------------------------------------------------------- */
689 void transcendFunc(char* func, double* tt, int* ttPower)
691 double t = (*tt)*M_PI/180; int tPower = *ttPower;
692 int sign = 1;
693 int n = 50; /* n <=50, tables are all <= 50 */
694 int j;
695 double x,y,z,xt,yt,zt;
697 if (tPower < -998) {
698 calStatus = cal_normal;
699 return;
701 if (tPower > 8) {
702 calStatus = cal_error;
703 return;
705 *ttPower = 0;
706 calStatus = cal_normal;
708 if( func[0] =='s' || func[0] =='S'|| func[0] =='t' || func[0] =='T')
709 sign = SIGN(t);
710 else {
711 /* if( func[0] =='c' || func[0] =='C') */
712 sign = 1;
714 t = ABS(t);
716 while (tPower > 0){
717 t *= 10;
718 tPower--;
720 while (tPower < 0) {
721 t /= 10;
722 tPower++;
724 j = 0;
725 while (t > j*M_TWOPI) {j++;}
726 t -= (j-1)*M_TWOPI;
727 if (M_PI_2 < t && t < 3*M_PI_2){
728 t = M_PI - t;
729 if (func[0] =='c' || func[0] =='C')
730 sign = -1;
731 else if (func[0] =='t' || func[0] =='T')
732 t*=-1;
734 else if ( 3*M_PI_2 <= t && t <= M_TWOPI)
735 t -= M_TWOPI;
737 x = 0.60725293500888; y = 0; z = t;
738 for (j=1;j<n+2;j++){
739 xt = x - SIGN(z) * y*cordicTable[j-1][0];
740 yt = y + SIGN(z) * x*cordicTable[j-1][0];
741 zt = z - SIGN(z) * cordicTable[j-1][1];
742 x = xt;
743 y=yt;
744 z=zt;
746 if( func[0] =='s' || func[0] =='S') {
747 *tt = sign*y;
748 return;
750 else if( func[0] =='c' || func[0] =='C') {
751 *tt = sign*x;
752 return;
754 else /*if( func[0] =='t' || func[0] =='T')*/ {
755 if(t==M_PI_2||t==-M_PI_2){
756 calStatus = cal_error;
757 return;
759 else{
760 *tt = sign*(y/x);
761 return;
766 /* -----------------------------------------------------------------------
767 add in scientific number format
768 ----------------------------------------------------------------------- */
769 void doAdd (double* operandOne, int* powerOne,
770 double operandTwo, int powerTwo)
772 if ( *powerOne >= powerTwo ){
773 if (*powerOne - powerTwo <= DIGITLEN+1){
774 while (powerTwo < *powerOne){
775 operandTwo /=10;
776 powerTwo++;
778 *operandOne += operandTwo;
780 /*do nothing if operandTwo is too small*/
782 else{
783 if (powerTwo - *powerOne <= DIGITLEN+1){
784 while(powerTwo > *powerOne){
785 *operandOne /=10;
786 (*powerOne)++;
788 (*operandOne) += operandTwo;
790 else{/* simply copy operandTwo if operandOne is too small */
791 *operandOne = operandTwo;
792 *powerOne = powerTwo;
796 /* -----------------------------------------------------------------------
797 multiple in scientific number format
798 ----------------------------------------------------------------------- */
799 void doMultiple(double* operandOne, int* powerOne,
800 double operandTwo, int powerTwo)
802 (*operandOne) *= operandTwo;
803 (*powerOne) += powerTwo;
806 /* -----------------------------------------------------------------------
807 Handles all one operand calculations
808 ----------------------------------------------------------------------- */
809 void oneOperand(void)
811 int k = 0;
812 if (buttonGroup == basicButtons){
813 switch(CAL_BUTTON){
814 case btn_sqr:
815 if (result<0)
816 calStatus = cal_error;
817 else{
818 if (power%2 == 1){
819 result = (mySqrt(result*10))/10;
820 power = (power+1) / 2;
822 else{
823 result = mySqrt(result);
824 power = power / 2;
826 calStatus = cal_normal;
828 break;
829 case btn_square:
830 power *= 2;
831 result *= result;
832 calStatus = cal_normal;
833 break;
835 case btn_rec:
836 if (result==0)
837 calStatus = cal_error;
838 else{
839 power = -power;
840 result = 1/result;
841 calStatus = cal_normal;
843 break;
844 default:
845 calStatus = cal_toDo;
846 break; /* just for the safety */
849 else{ /* sciButtons */
850 switch(CAL_BUTTON){
851 case sci_sin:
852 transcendFunc("sin", &result, &power);
853 break;
854 case sci_cos:
855 transcendFunc("cos", &result, &power);
856 break;
857 case sci_tan:
858 transcendFunc("tan", &result, &power);
859 break;
860 case sci_fac:
861 if (power<0 || power>8 || result<0 )
862 calStatus = cal_error;
863 else if(result == 0) {
864 result = 1;
865 power = 0;
867 else{
868 while(power > 0) {
869 result *= 10;
870 power--;
872 if ( ( result - (int)result) > MINIMUM )
873 calStatus = cal_error;
874 else {
875 k = result; result = 1;
876 while (k > 1){
877 doMultiple(&result, &power, k, 0);
878 formatResult();
879 k--;
881 calStatus = cal_normal;
884 break;
885 default:
886 calStatus = cal_toDo;
887 break; /* just for the safety */
893 /* -----------------------------------------------------------------------
894 Handles all two operands calculations
895 ----------------------------------------------------------------------- */
896 void twoOperands(void)
898 switch(oper){
899 case '-':
900 doAdd(&operand, &operandPower, -result, power);
901 break;
902 case '+':
903 doAdd(&operand, &operandPower, result, power);
904 break;
905 case '*':
906 doMultiple(&operand, &operandPower, result, power);
907 break;
908 case '/':
909 if ( ABS(result) > MINIMUM ){
910 doMultiple(&operand, &operandPower, 1/result, -power);
912 else
913 calStatus = cal_error;
914 break;
915 default: /* ' ' */
916 switchOperands(); /* counter switchOperands() below */
917 break;
918 } /* switch(oper) */
919 switchOperands();
920 clearOper();
923 /* First, increases *dimen1 by dimen1_delta modulo dimen1_modulo.
924 If dimen1 wraps, increases *dimen2 by dimen2_delta modulo dimen2_modulo.
926 static void move_with_wrap_and_shift(
927 int *dimen1, int dimen1_delta, int dimen1_modulo,
928 int *dimen2, int dimen2_delta, int dimen2_modulo)
930 bool wrapped = false;
932 *dimen1 += dimen1_delta;
933 if (*dimen1 < 0)
935 *dimen1 = dimen1_modulo - 1;
936 wrapped = true;
938 else if (*dimen1 >= dimen1_modulo)
940 *dimen1 = 0;
941 wrapped = true;
944 if (wrapped)
946 /* Make the dividend always positive to be sure about the result.
947 Adding dimen2_modulo does not change it since we do it modulo. */
948 *dimen2 = (*dimen2 + dimen2_modulo + dimen2_delta) % dimen2_modulo;
952 /* -----------------------------------------------------------------------
953 Print buttons when switching 1st and 2nd
954 int group = {basicButtons, sciButtons}
955 ----------------------------------------------------------------------- */
956 void printButtonGroups(int group)
958 drawButtons(group);
959 drawLines();
960 rb->lcd_update();
962 /* -----------------------------------------------------------------------
963 flash the currently marked button
964 ----------------------------------------------------------------------- */
965 void flashButton(void)
967 int k, w, h;
968 for (k=2;k>0;k--)
970 rb->lcd_getstringsize( buttonChar[buttonGroup][btn_row][btn_col],&w,&h);
971 rb->lcd_set_drawmode(DRMODE_SOLID|(k==1) ? 0 : DRMODE_INVERSEVID);
972 rb->lcd_fillrect( X_0_POS + btn_col*REC_WIDTH + 1,
973 Y_1_POS + btn_row*REC_HEIGHT + 1,
974 REC_WIDTH - 1, REC_HEIGHT - 1);
975 rb->lcd_putsxy( X_0_POS + btn_col*REC_WIDTH + (REC_WIDTH - w)/2,
976 Y_1_POS + btn_row*REC_HEIGHT + (REC_HEIGHT - h)/2 +1,
977 buttonChar[buttonGroup][btn_row][btn_col] );
978 rb->lcd_update_rect( X_0_POS + btn_col*REC_WIDTH + 1,
979 Y_1_POS + btn_row*REC_HEIGHT + 1,
980 REC_WIDTH - 1, REC_HEIGHT - 1);
982 if (k!= 1)
983 rb->sleep(HZ/22);
988 /* -----------------------------------------------------------------------
989 pos is the position that needs animation. pos = [1~18]
990 ----------------------------------------------------------------------- */
991 void deleteAnimation(int pos)
993 int k;
994 if (pos<1 || pos >18)
995 return;
996 pos--;
997 rb->lcd_fillrect(1+pos*6, TEXT_1_POS, 6, 8);
998 rb->lcd_update_rect(1+pos*6, TEXT_1_POS, 6, 8);
1000 for (k=1;k<=4;k++){
1001 rb->sleep(HZ/32);
1002 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1003 rb->lcd_fillrect(1+pos*6, TEXT_1_POS, 6, 8);
1004 rb->lcd_set_drawmode(DRMODE_SOLID);
1005 rb->lcd_fillrect(1+pos*6+1+k, TEXT_1_POS+k,
1006 (5-2*k)>0?(5-2*k):1, (7-2*k)>0?(7-2*k):1 );
1007 rb->lcd_update_rect(1+pos*6, TEXT_1_POS, 6, 8);
1012 /* -----------------------------------------------------------------------
1013 result may be one of these formats:
1015 xxxx.xxxx
1016 0.xxxx
1017 0.0000xxxx
1019 formatResult() change result to standard format: 0.xxxx
1020 if result is close to 0, let it be 0;
1021 if result is close to 1, let it be 0.1 and power++;
1022 ----------------------------------------------------------------------- */
1023 void formatResult(void)
1025 int resultsign = SIGN(result);
1026 result = ABS(result);
1027 if(result > MINIMUM ){ /* doesn't check power, might have problem
1028 input wouldn't,
1029 + - * / of two formatted number wouldn't.
1030 only a calculation that makes a formatted
1031 number (0.xxxx) less than MINIMUM in only
1032 one operation */
1034 if (result<1){
1035 while( (int)(result*10) == 0 ){
1036 result *= 10;
1037 power--;
1038 modifier *= 10;
1041 else{ /* result >= 1 */
1042 while( (int)result != 0 ){
1043 result /= 10;
1044 power++;
1045 modifier /= 10;
1047 } /* if result<1 */
1049 if (result > (1-MINIMUM)){
1050 result = 0.1;
1051 power++;
1052 modifier /= 10;
1054 result *= resultsign;
1056 else {
1057 result = 0;
1058 power = 0;
1059 modifier = 0.1;
1063 /* -----------------------------------------------------------------------
1064 result2typingbuf() outputs standard format result to typingbuf.
1065 case SCIENTIFIC_FORMAT, let temppower = 1;
1066 case temppower > 0: print '.' in the middle
1067 case temppower <= 0: print '.' in the begining
1068 ----------------------------------------------------------------------- */
1069 void result2typingbuf(void)
1071 bool haveDot = false;
1072 char tempchar = 0;
1073 int k;
1074 double tempresult = ABS(result); /* positive num makes things simple */
1076 int temppower;
1077 double tempmodifier = 1;
1078 int count;
1080 if(SCIENTIFIC_FORMAT)
1081 temppower = 1; /* output x.xxxx format */
1082 else
1083 temppower = power;
1085 cleartypingbuf();
1087 if(tempresult < MINIMUM){ /* if 0,faster display and avoid complication*/
1088 typingbuf[0] = ' ';
1089 typingbuf[1] = '0';
1091 else{ /* tempresult > 0 */
1092 typingbuf[0] = (SIGN(result)<0)?'-':' ';
1094 typingbufPointer = typingbuf;
1095 if(temppower > 0){
1096 for (k = 0; k<DIGITLEN+1 ; k++){
1097 typingbufPointer++;
1098 if(temppower || *(typingbufPointer-1) == '.'){
1099 count = 0;
1100 tempmodifier = tempmodifier/10;
1101 while( (tempresult-tempmodifier*count) >
1102 (tempmodifier-MINIMUM)){
1103 count++;
1105 tempresult -= tempmodifier*count;
1106 tempresult = ABS(tempresult);
1107 temppower-- ;
1108 *typingbufPointer = count + '0';
1110 else{ /* temppower == 0 */
1111 *typingbufPointer = '.';
1112 haveDot = true;
1114 } /* for */
1116 else{
1117 haveDot = true;
1118 typingbufPointer++; *typingbufPointer = '0';
1119 typingbufPointer++; *typingbufPointer = '.';
1120 for (k = 2; k<DIGITLEN+1 ; k++){
1121 typingbufPointer++;
1122 count = 0;
1123 if ( (-temppower) < (k-1)){
1124 tempmodifier = tempmodifier/10;
1125 while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){
1126 count++;
1129 tempresult -= tempmodifier*count;
1130 tempresult = ABS(tempresult);
1131 temppower-- ;
1133 *typingbufPointer = count + '0';
1136 /* now, typingbufPointer = typingbuf + 16 */
1137 /* backward strip off 0 and '.' */
1138 if (haveDot){
1139 while( (*typingbufPointer == '0') || (*typingbufPointer == '.')){
1140 tempchar = *typingbufPointer;
1141 *typingbufPointer = 0;
1142 typingbufPointer--;
1143 if (tempchar == '.') break;
1146 typingbuf[DIGITLEN+1] = 0;
1147 } /* else tempresult > 0 */
1150 /* -----------------------------------------------------------------------
1151 printResult() generates LCD display.
1152 ----------------------------------------------------------------------- */
1153 void printResult(void)
1155 int k, w, h;
1157 char operbuf[3] = {0, 0, 0};
1159 switch_Status:
1160 switch(calStatus){
1161 case cal_exit:
1162 rb->lcd_clear_display();
1163 rb->splash(HZ/3, "Bye now!");
1164 break;
1165 case cal_error:
1166 clearbuf();
1167 rb->snprintf(buf, 19, "%18s","Error");
1168 break;
1169 case cal_toDo:
1170 clearbuf();
1171 rb->snprintf(buf, 19, "%18s","Coming soon ^_* ");
1172 break;
1174 case cal_normal:
1175 formatResult();
1177 if( power > 1000 ){ /* power -1 > 999 */
1178 calStatus = cal_error;
1179 goto switch_Status;
1181 if (power < -998 ) /* power -1 < -999 */
1182 clearResult(); /* too small, let it be 0 */
1184 result2typingbuf();
1185 clearbuf();
1187 operbuf[0] = oper;
1188 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1189 operbuf[2] = '\0';
1191 if(SCIENTIFIC_FORMAT){
1192 /* output format: X.XXXX eXXX */
1193 if(power > -98){ /* power-1 >= -99, eXXX or e-XX */
1194 rb->snprintf(buf, 12, "%11s",typingbuf);
1195 for(k=11;k<=14;k++) buf[k] = ' ';
1196 cleartypingbuf();
1197 rb->snprintf(typingbuf, 5, "e%d",power-1);
1198 rb->snprintf(buf+11, 5, "%4s",typingbuf);
1200 else{ /* power-1 <= -100, e-XXX */
1201 rb->snprintf(buf, 12, "%11s",typingbuf);
1202 rb->snprintf(buf+11, 6, "e%d",power-1);
1205 else{
1206 rb->snprintf(buf, 12, "%11s",typingbuf);
1207 } /* if SCIENTIFIC_FORMAT */
1208 break;
1209 case cal_typing:
1210 case cal_dotted:
1211 clearbuf();
1212 operbuf[0] = oper;
1213 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1214 rb->snprintf(buf, 12, "%11s",typingbuf);
1215 break;
1219 rb->lcd_getstringsize(buf, &w, &h);
1220 rb->screen_clear_area(rb->screens[0], 0, 0, LCD_WIDTH, REC_HEIGHT-1);
1221 rb->lcd_putsxy(4, Y_1_POS - h -1, operbuf);
1222 rb->lcd_putsxy(LCD_WIDTH - w - 4, Y_1_POS - h -1, buf);
1223 rb->lcd_update_rect(0, 1, LCD_WIDTH, Y_1_POS);
1226 /* -----------------------------------------------------------------------
1227 Process typing buttons: 1-9, '.', sign
1228 main operand "result" and typingbuf are processed seperately here.
1229 ----------------------------------------------------------------------- */
1230 void typingProcess(void){
1231 switch( CAL_BUTTON ){
1232 case btn_sign:
1233 if (calStatus == cal_typing ||
1234 calStatus == cal_dotted)
1235 typingbuf[0] = (typingbuf[0]=='-')?' ':'-';
1236 result = -result;
1237 break;
1238 case btn_dot:
1239 operInputted = false;
1240 switch(calStatus){
1241 case cal_normal:
1242 clearInput();
1243 *typingbufPointer = '0';
1244 typingbufPointer++;
1245 case cal_typing:
1246 calStatus = cal_dotted;
1247 *typingbufPointer = '.';
1248 if (typingbufPointer != typingbuf+DIGITLEN+1)
1249 typingbufPointer++;
1250 break;
1251 default: /* cal_dotted */
1252 break;
1254 break;
1255 default: /* 0-9 */
1256 operInputted = false;
1257 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1258 switch(calStatus){
1259 case cal_normal:
1260 if(CAL_BUTTON == btn_0 )
1261 break; /* first input is 0, ignore */
1262 clearInput();
1263 /*no operator means start a new calculation*/
1264 if (oper ==' ')
1265 clearOperand();
1266 calStatus = cal_typing;
1267 /* go on typing, no break */
1268 case cal_typing:
1269 case cal_dotted:
1270 switch(CAL_BUTTON){
1271 case btn_0:
1272 *typingbufPointer = '0';
1273 break;
1274 default:
1275 *typingbufPointer=(7+btn_col-3*(btn_row-1))+ '0';
1276 break;
1278 if (typingbufPointer!=typingbuf+DIGITLEN+1){
1279 typingbufPointer++;
1281 {/* result processing */
1282 if (calStatus == cal_typing) power++;
1283 if (CAL_BUTTON != btn_0)
1284 result= result +
1285 SIGN(result)*
1286 (7+btn_col-3*(btn_row-1))*modifier;
1287 modifier /= 10;
1290 else /* last byte always '\0' */
1291 *typingbufPointer = 0;
1292 break;
1293 default: /* cal_error, cal_exit */
1294 break;
1296 break; /* default, 0-9 */
1297 } /* switch( CAL_BUTTON ) */
1300 /* -----------------------------------------------------------------------
1301 Handle delete operation
1302 main operand "result" and typingbuf are processed seperately here.
1303 ----------------------------------------------------------------------- */
1304 void doDelete(void){
1305 deleteAnimation(18);
1306 switch(calStatus){
1307 case cal_dotted:
1308 if (*(typingbufPointer-1) == '.'){
1309 /* if dotted and deleting '.',
1310 change status and delete '.' below */
1311 calStatus = cal_typing;
1313 else{ /* if dotted and not deleting '.',
1314 power stays */
1315 power++; /* counter "power--;" below */
1317 case cal_typing:
1318 typingbufPointer--;
1320 {/* result processing */ /* 0-9, '.' */
1321 /* if deleting '.', do nothing */
1322 if ( *typingbufPointer != '.'){
1323 power--;
1324 modifier *= 10;
1325 result = result - SIGN(result)*
1326 ((*typingbufPointer)- '0')*modifier;
1330 *typingbufPointer = 0;
1332 /* if (only one digit left and it's 0)
1333 or no digit left, change status*/
1334 if ( typingbufPointer == typingbuf+1 ||
1335 ( typingbufPointer == typingbuf+2 &&
1336 *(typingbufPointer-1) == '0' ))
1337 calStatus = cal_normal;
1338 break;
1339 default: /* normal, error, exit */
1340 break;
1343 /* -----------------------------------------------------------------------
1344 Handle buttons on basic screen
1345 ----------------------------------------------------------------------- */
1346 void basicButtonsProcess(void){
1347 switch (btn) {
1348 case CALCULATOR_INPUT:
1349 if (calStatus == cal_error && (CAL_BUTTON != btn_C) ) break;
1350 flashButton();
1351 switch( CAL_BUTTON ){
1352 case btn_MR:
1353 operInputted = false;
1354 result = memTemp; power = memTempPower;
1355 calStatus = cal_normal;
1356 break;
1357 case btn_M:
1358 formatResult();
1359 if (memTemp > MINIMUM)
1360 doAdd(&memTemp, &memTempPower, result, power);
1361 else {
1362 /* if result is too small and memTemp = 0,
1363 doAdd will not add */
1364 memTemp = result;
1365 memTempPower = power;
1367 calStatus = cal_normal;
1368 break;
1370 case btn_C: clearMem(); break;
1371 case btn_CE: clearInput(); break;
1373 case btn_bas:
1374 buttonGroup = sciButtons;
1375 printButtonGroups(buttonGroup);
1376 break;
1378 /* one operand calculation, may be changed to
1379 like sin, cos, log, etc */
1380 case btn_sqr:
1381 case btn_square:
1382 case btn_rec:
1383 formatResult(); /* not necessary, just for safty */
1384 oneOperand();
1385 break;
1387 case_btn_equal: /* F3 shortkey entrance */
1388 case btn_equal:
1389 formatResult();
1390 calStatus = cal_normal;
1391 operInputted = false;
1392 if (oper != ' ') twoOperands();
1393 break;
1395 case btn_div:
1396 case btn_time:
1397 case btn_minus:
1398 case btn_add:
1399 if(!operInputted) {twoOperands(); operInputted = true;}
1400 oper = buttonChar[basicButtons][btn_row][btn_col][0];
1401 #ifdef CALCULATOR_OPERATORS
1402 case_cycle_operators: /* F2 shortkey entrance */
1403 #endif
1404 calStatus = cal_normal;
1405 formatResult();
1406 operand = result;
1407 operandPower = power;
1409 break;
1411 case btn_sign:
1412 case btn_dot:
1413 default: /* 0-9 */
1414 typingProcess();
1415 break;
1416 } /* switch (CAL_BUTTON) */
1417 break;
1419 #ifdef CALCULATOR_OPERATORS
1420 case CALCULATOR_OPERATORS:
1421 if (calStatus == cal_error) break;
1422 if (!operInputted) {twoOperands(); operInputted = true;}
1423 switch (oper){
1424 case ' ':
1425 case '/': oper = '+'; flashButton(); break;
1426 case '+': oper = '-'; flashButton(); break;
1427 case '-': oper = '*'; flashButton(); break;
1428 case '*': oper = '/'; flashButton(); break;
1430 goto case_cycle_operators;
1431 break;
1432 #endif
1434 case CALCULATOR_CALC:
1435 if (calStatus == cal_error) break;
1436 flashButton();
1437 goto case_btn_equal;
1438 break;
1439 default: break;
1441 printResult();
1444 /* -----------------------------------------------------------------------
1445 Handle buttons on scientific screen
1446 ----------------------------------------------------------------------- */
1447 void sciButtonsProcess(void){
1448 switch (btn) {
1449 case CALCULATOR_INPUT:
1450 if (calStatus == cal_error && (CAL_BUTTON != sci_sci) ) break;
1451 flashButton();
1452 switch( CAL_BUTTON ){
1454 case sci_pi:
1455 result = M_PI; power = 0;
1456 calStatus = cal_normal;
1457 break;
1459 case sci_xy: break;
1461 case sci_sci:
1462 buttonGroup = basicButtons;
1463 printButtonGroups(basicButtons);
1464 break;
1466 case sci_fac:
1467 case sci_sin:
1468 case sci_asin:
1469 case sci_cos:
1470 case sci_acos:
1471 case sci_tan:
1472 case sci_atan:
1473 case sci_ln:
1474 case sci_exp:
1475 case sci_log:
1476 formatResult(); /* not necessary, just for safty */
1477 oneOperand();
1478 break;
1480 case btn_sign:
1481 case btn_dot:
1482 default: /* 0-9 */
1483 typingProcess();
1484 break;
1485 } /* switch (CAL_BUTTON) */
1486 break;
1488 #ifdef CALCULATOR_OPERATORS
1489 case CALCULATOR_OPERATORS:
1490 if (calStatus == cal_error) break;
1491 if (!operInputted) {twoOperands(); operInputted = true;}
1492 switch (oper){
1493 case ' ': oper = '+'; break;
1494 case '/': oper = '+'; deleteAnimation(1); break;
1495 case '+': oper = '-'; deleteAnimation(1); break;
1496 case '-': oper = '*'; deleteAnimation(1); break;
1497 case '*': oper = '/'; deleteAnimation(1); break;
1499 calStatus = cal_normal;
1500 formatResult();
1501 operand = result;
1502 operandPower = power;
1503 break;
1504 #endif
1506 case CALCULATOR_CALC:
1507 if (calStatus == cal_error) break;
1508 formatResult();
1509 calStatus = cal_normal;
1510 operInputted = false;
1511 if (oper != ' ') twoOperands();
1512 break;
1513 default: break;
1515 printResult();
1518 /* -----------------------------------------------------------------------
1519 move button index
1520 Invert display new button, invert back previous button
1521 ----------------------------------------------------------------------- */
1522 int handleButton(int button){
1523 switch(button)
1525 case CALCULATOR_INPUT:
1526 case CALCULATOR_CALC:
1527 #ifdef CALCULATOR_INPUT_CALC_PRE
1528 if (lastbtn != CALCULATOR_INPUT_CALC_PRE)
1529 break;
1530 /* no unconditional break; here! */
1531 #endif
1532 #ifdef CALCULATOR_OPERATORS
1533 case CALCULATOR_OPERATORS:
1534 #endif
1535 switch(buttonGroup){
1536 case basicButtons:
1537 basicButtonsProcess();
1538 break;
1539 case sciButtons:
1540 sciButtonsProcess();
1541 break;
1543 break;
1545 #ifdef CALCULATOR_CLEAR
1546 case CALCULATOR_CLEAR:
1547 switch(calStatus){
1548 case cal_typing:
1549 case cal_dotted:
1550 doDelete();
1551 break;
1552 default: /* cal_normal, cal_error, cal_exit */
1553 clearMem();
1554 break;
1556 printResult();
1557 break;
1558 #endif
1559 case CALCULATOR_LEFT:
1560 case CALCULATOR_LEFT | BUTTON_REPEAT:
1561 move_with_wrap_and_shift(
1562 &btn_col, -1, BUTTON_COLS,
1563 &btn_row, 0, BUTTON_ROWS);
1564 break;
1566 case CALCULATOR_RIGHT:
1567 case CALCULATOR_RIGHT | BUTTON_REPEAT:
1568 move_with_wrap_and_shift(
1569 &btn_col, 1, BUTTON_COLS,
1570 &btn_row, 0, BUTTON_ROWS);
1571 break;
1573 #ifdef CALCULATOR_UP
1574 case CALCULATOR_UP:
1575 case CALCULATOR_UP | BUTTON_REPEAT:
1576 move_with_wrap_and_shift(
1577 &btn_row, -1, BUTTON_ROWS,
1578 &btn_col, 0, BUTTON_COLS);
1579 break;
1580 #endif
1581 #ifdef CALCULATOR_DOWN
1582 case CALCULATOR_DOWN:
1583 case CALCULATOR_DOWN | BUTTON_REPEAT:
1584 move_with_wrap_and_shift(
1585 &btn_row, 1, BUTTON_ROWS,
1586 &btn_col, 0, BUTTON_COLS);
1587 break;
1588 #endif
1590 #ifdef CALCULATOR_UP_W_SHIFT
1591 case CALCULATOR_UP_W_SHIFT:
1592 case CALCULATOR_UP_W_SHIFT | BUTTON_REPEAT:
1593 move_with_wrap_and_shift(
1594 &btn_row, -1, BUTTON_ROWS,
1595 &btn_col, -1, BUTTON_COLS);
1596 break;
1597 #endif
1598 #ifdef CALCULATOR_DOWN_W_SHIFT
1599 case CALCULATOR_DOWN_W_SHIFT:
1600 case CALCULATOR_DOWN_W_SHIFT | BUTTON_REPEAT:
1601 move_with_wrap_and_shift(
1602 &btn_row, 1, BUTTON_ROWS,
1603 &btn_col, 1, BUTTON_COLS);
1604 break;
1605 #endif
1606 #ifdef CALCULATOR_LEFT_W_SHIFT
1607 case CALCULATOR_LEFT_W_SHIFT:
1608 case CALCULATOR_LEFT_W_SHIFT | BUTTON_REPEAT:
1609 move_with_wrap_and_shift(
1610 &btn_col, -1, BUTTON_COLS,
1611 &btn_row, -1, BUTTON_ROWS);
1612 break;
1613 #endif
1614 #ifdef CALCULATOR_RIGHT_W_SHIFT
1615 case CALCULATOR_RIGHT_W_SHIFT:
1616 case CALCULATOR_RIGHT_W_SHIFT | BUTTON_REPEAT:
1617 move_with_wrap_and_shift(
1618 &btn_col, 1, BUTTON_COLS,
1619 &btn_row, 1, BUTTON_ROWS);
1620 break;
1621 #endif
1622 #ifdef CALCULATOR_RC_QUIT
1623 case CALCULATOR_RC_QUIT:
1624 #endif
1625 case CALCULATOR_QUIT:
1626 return -1;
1629 return 0;
1631 prev_btn_row = btn_row;
1632 prev_btn_col = btn_col;
1635 /* -----------------------------------------------------------------------
1636 Main();
1637 ----------------------------------------------------------------------- */
1638 enum plugin_status plugin_start(const void* parameter)
1640 (void)parameter;
1642 /* now go ahead and have fun! */
1644 cal_initial();
1646 while (calStatus != cal_exit ) {
1647 btn = rb->button_get_w_tmo(HZ/2);
1648 #ifdef HAVE_TOUCHSCREEN
1649 if(btn & BUTTON_TOUCHSCREEN)
1651 struct ts_raster_result res;
1652 if(touchscreen_map_raster(&calc_raster, rb->button_get_data() >> 16,
1653 rb->button_get_data() & 0xffff, &res) == 1)
1655 btn_row = res.y;
1656 btn_col = res.x;
1657 drawButtons(buttonGroup);
1658 drawLines();
1660 rb->lcd_update();
1662 prev_btn_row = btn_row;
1663 prev_btn_col = btn_col;
1664 if(btn & BUTTON_REL)
1666 btn = CALCULATOR_INPUT;
1667 switch(buttonGroup){
1668 case basicButtons:
1669 basicButtonsProcess();
1670 break;
1671 case sciButtons:
1672 sciButtonsProcess();
1673 break;
1675 btn = BUTTON_TOUCHSCREEN;
1679 #endif
1680 if (handleButton(btn) == -1)
1682 calStatus = cal_exit;
1683 printResult();
1685 else
1687 drawButtons(buttonGroup);
1688 drawLines();
1691 rb->lcd_update();
1693 if(rb->default_event_handler(btn) == SYS_USB_CONNECTED)
1694 return PLUGIN_USB_CONNECTED;
1696 if (btn != BUTTON_NONE)
1697 lastbtn = btn;
1698 } /* while (calStatus != cal_exit ) */
1700 rb->button_clear_queue();
1701 return PLUGIN_OK;
1704 #endif /* #ifdef HAVE_LCD_BITMAP */