Fix FS#10486 - "fuze screen not drawn correctly on backlight turn-on if playlist...
[kugel-rb/myfork.git] / apps / plugins / calculator.c
blob33235fccb51f088c4ccc50964c9fa26a2eb80f72
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 SIGN(x) ((x)<0?-1:1)
106 /* variable button definitions */
107 #if CONFIG_KEYPAD == RECORDER_PAD
108 #define CALCULATOR_LEFT BUTTON_LEFT
109 #define CALCULATOR_RIGHT BUTTON_RIGHT
110 #define CALCULATOR_UP BUTTON_UP
111 #define CALCULATOR_DOWN BUTTON_DOWN
112 #define CALCULATOR_QUIT BUTTON_OFF
113 #define CALCULATOR_INPUT BUTTON_PLAY
114 #define CALCULATOR_CALC BUTTON_F3
115 #define CALCULATOR_OPERATORS BUTTON_F2
116 #define CALCULATOR_CLEAR BUTTON_F1
118 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
119 #define CALCULATOR_LEFT BUTTON_LEFT
120 #define CALCULATOR_RIGHT BUTTON_RIGHT
121 #define CALCULATOR_UP BUTTON_UP
122 #define CALCULATOR_DOWN BUTTON_DOWN
123 #define CALCULATOR_QUIT BUTTON_OFF
124 #define CALCULATOR_INPUT BUTTON_SELECT
125 #define CALCULATOR_CALC BUTTON_F3
126 #define CALCULATOR_OPERATORS BUTTON_F2
127 #define CALCULATOR_CLEAR BUTTON_F1
129 #elif CONFIG_KEYPAD == ONDIO_PAD
130 #define CALCULATOR_LEFT BUTTON_LEFT
131 #define CALCULATOR_RIGHT BUTTON_RIGHT
132 #define CALCULATOR_UP BUTTON_UP
133 #define CALCULATOR_DOWN BUTTON_DOWN
134 #define CALCULATOR_QUIT BUTTON_OFF
135 #define CALCULATOR_INPUT_CALC_PRE BUTTON_MENU
136 #define CALCULATOR_INPUT (BUTTON_MENU | BUTTON_REL)
137 #define CALCULATOR_CALC (BUTTON_MENU | BUTTON_REPEAT)
139 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
140 (CONFIG_KEYPAD == IRIVER_H300_PAD)
141 #define CALCULATOR_LEFT BUTTON_LEFT
142 #define CALCULATOR_RIGHT BUTTON_RIGHT
143 #define CALCULATOR_UP BUTTON_UP
144 #define CALCULATOR_DOWN BUTTON_DOWN
145 #define CALCULATOR_QUIT BUTTON_OFF
146 #define CALCULATOR_INPUT BUTTON_SELECT
147 #define CALCULATOR_CALC BUTTON_ON
148 #define CALCULATOR_OPERATORS BUTTON_MODE
149 #define CALCULATOR_CLEAR BUTTON_REC
151 #define CALCULATOR_RC_QUIT BUTTON_RC_STOP
153 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
154 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
155 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
157 #define CALCULATOR_LEFT BUTTON_LEFT
158 #define CALCULATOR_RIGHT BUTTON_RIGHT
159 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
160 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
161 #define CALCULATOR_QUIT BUTTON_MENU
162 #define CALCULATOR_INPUT BUTTON_SELECT
163 #define CALCULATOR_CALC BUTTON_PLAY
165 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
167 #define CALCULATOR_LEFT BUTTON_LEFT
168 #define CALCULATOR_RIGHT BUTTON_RIGHT
169 #define CALCULATOR_UP BUTTON_UP
170 #define CALCULATOR_DOWN BUTTON_DOWN
171 #define CALCULATOR_QUIT BUTTON_POWER
172 #define CALCULATOR_INPUT BUTTON_SELECT
173 #define CALCULATOR_CALC BUTTON_PLAY
174 #define CALCULATOR_CLEAR BUTTON_REC
176 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
178 #define CALCULATOR_LEFT BUTTON_LEFT
179 #define CALCULATOR_RIGHT BUTTON_RIGHT
180 #define CALCULATOR_UP BUTTON_UP
181 #define CALCULATOR_DOWN BUTTON_DOWN
182 #define CALCULATOR_QUIT BUTTON_POWER
183 #define CALCULATOR_INPUT BUTTON_SELECT
184 #define CALCULATOR_CALC BUTTON_MENU
185 #define CALCULATOR_CLEAR BUTTON_A
187 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
188 #define CALCULATOR_LEFT BUTTON_LEFT
189 #define CALCULATOR_RIGHT BUTTON_RIGHT
190 #define CALCULATOR_UP BUTTON_UP
191 #define CALCULATOR_DOWN BUTTON_DOWN
192 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
193 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
194 #define CALCULATOR_QUIT BUTTON_POWER
195 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
196 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
197 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
198 #define CALCULATOR_CLEAR BUTTON_REC
200 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
201 #define CALCULATOR_LEFT BUTTON_LEFT
202 #define CALCULATOR_RIGHT BUTTON_RIGHT
203 #define CALCULATOR_UP BUTTON_UP
204 #define CALCULATOR_DOWN BUTTON_DOWN
205 #define CALCULATOR_QUIT BUTTON_POWER
206 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
207 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
208 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
210 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
211 #define CALCULATOR_LEFT BUTTON_LEFT
212 #define CALCULATOR_RIGHT BUTTON_RIGHT
213 #define CALCULATOR_UP BUTTON_UP
214 #define CALCULATOR_DOWN BUTTON_DOWN
215 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
216 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
217 #define CALCULATOR_QUIT (BUTTON_HOME|BUTTON_REPEAT)
218 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
219 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
220 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
223 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
224 #define CALCULATOR_LEFT BUTTON_LEFT
225 #define CALCULATOR_RIGHT BUTTON_RIGHT
226 #define CALCULATOR_UP BUTTON_UP
227 #define CALCULATOR_DOWN BUTTON_DOWN
228 #define CALCULATOR_QUIT BUTTON_POWER
229 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
230 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
231 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
232 #define CALCULATOR_CLEAR BUTTON_HOME
234 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
235 #define CALCULATOR_LEFT BUTTON_LEFT
236 #define CALCULATOR_RIGHT BUTTON_RIGHT
237 #define CALCULATOR_UP BUTTON_UP
238 #define CALCULATOR_DOWN BUTTON_DOWN
239 #define CALCULATOR_QUIT BUTTON_POWER
240 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
241 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
242 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
243 #define CALCULATOR_CLEAR (BUTTON_SELECT|BUTTON_UP)
245 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
247 #define CALCULATOR_LEFT BUTTON_LEFT
248 #define CALCULATOR_RIGHT BUTTON_RIGHT
249 #define CALCULATOR_UP BUTTON_SCROLL_UP
250 #define CALCULATOR_DOWN BUTTON_SCROLL_DOWN
251 #define CALCULATOR_QUIT BUTTON_POWER
252 #define CALCULATOR_INPUT_CALC_PRE BUTTON_PLAY
253 #define CALCULATOR_INPUT (BUTTON_PLAY | BUTTON_REL)
254 #define CALCULATOR_CALC (BUTTON_PLAY | BUTTON_REPEAT)
255 #define CALCULATOR_CLEAR BUTTON_REW
257 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
259 #define CALCULATOR_LEFT BUTTON_LEFT
260 #define CALCULATOR_RIGHT BUTTON_RIGHT
261 #define CALCULATOR_UP BUTTON_UP
262 #define CALCULATOR_DOWN BUTTON_DOWN
263 #define CALCULATOR_QUIT BUTTON_BACK
264 #define CALCULATOR_INPUT BUTTON_SELECT
265 #define CALCULATOR_CALC BUTTON_MENU
266 #define CALCULATOR_CLEAR BUTTON_PLAY
268 #elif (CONFIG_KEYPAD == MROBE100_PAD)
270 #define CALCULATOR_LEFT BUTTON_LEFT
271 #define CALCULATOR_RIGHT BUTTON_RIGHT
272 #define CALCULATOR_UP BUTTON_UP
273 #define CALCULATOR_DOWN BUTTON_DOWN
274 #define CALCULATOR_QUIT BUTTON_POWER
275 #define CALCULATOR_INPUT BUTTON_SELECT
276 #define CALCULATOR_CALC BUTTON_MENU
277 #define CALCULATOR_CLEAR BUTTON_DISPLAY
279 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
281 #define CALCULATOR_LEFT BUTTON_RC_REW
282 #define CALCULATOR_RIGHT BUTTON_RC_FF
283 #define CALCULATOR_UP BUTTON_RC_VOL_UP
284 #define CALCULATOR_DOWN BUTTON_RC_VOL_DOWN
285 #define CALCULATOR_QUIT BUTTON_RC_REC
286 #define CALCULATOR_INPUT BUTTON_RC_PLAY
287 #define CALCULATOR_CALC BUTTON_RC_MODE
288 #define CALCULATOR_CLEAR BUTTON_RC_MENU
290 #define CALCULATOR_RC_QUIT BUTTON_REC
292 #elif (CONFIG_KEYPAD == COWOND2_PAD)
294 #define CALCULATOR_QUIT BUTTON_POWER
295 #define CALCULATOR_CLEAR BUTTON_MENU
297 #elif CONFIG_KEYPAD == IAUDIO67_PAD
299 #define CALCULATOR_LEFT BUTTON_LEFT
300 #define CALCULATOR_RIGHT BUTTON_RIGHT
301 #define CALCULATOR_UP BUTTON_VOLUP
302 #define CALCULATOR_DOWN BUTTON_VOLDOWN
303 #define CALCULATOR_QUIT BUTTON_POWER
304 #define CALCULATOR_INPUT BUTTON_PLAY
305 #define CALCULATOR_CALC BUTTON_MENU
306 #define CALCULATOR_CLEAR BUTTON_STOP
308 #define CALCULATOR_RC_QUIT (BUTTON_MENU|BUTTON_PLAY)
310 #elif (CONFIG_KEYPAD == CREATIVEZVM_PAD)
312 #define CALCULATOR_LEFT BUTTON_LEFT
313 #define CALCULATOR_RIGHT BUTTON_RIGHT
314 #define CALCULATOR_UP BUTTON_UP
315 #define CALCULATOR_DOWN BUTTON_DOWN
316 #define CALCULATOR_QUIT BUTTON_BACK
317 #define CALCULATOR_INPUT BUTTON_SELECT
318 #define CALCULATOR_CALC BUTTON_MENU
319 #define CALCULATOR_CLEAR BUTTON_PLAY
321 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
323 #define CALCULATOR_LEFT BUTTON_LEFT
324 #define CALCULATOR_RIGHT BUTTON_RIGHT
325 #define CALCULATOR_UP BUTTON_UP
326 #define CALCULATOR_DOWN BUTTON_DOWN
327 #define CALCULATOR_QUIT BUTTON_POWER
328 #define CALCULATOR_INPUT BUTTON_SELECT
329 #define CALCULATOR_CALC BUTTON_MENU
330 #define CALCULATOR_CLEAR BUTTON_VIEW
332 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
334 #define CALCULATOR_QUIT BUTTON_POWER
335 #define CALCULATOR_CLEAR BUTTON_MENU
337 #elif CONFIG_KEYPAD == MROBE500_PAD
338 #define CALCULATOR_QUIT BUTTON_POWER
340 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
342 #define CALCULATOR_LEFT BUTTON_LEFT
343 #define CALCULATOR_RIGHT BUTTON_RIGHT
344 #define CALCULATOR_UP BUTTON_UP
345 #define CALCULATOR_DOWN BUTTON_DOWN
346 #define CALCULATOR_QUIT BUTTON_REC
347 #define CALCULATOR_INPUT BUTTON_PLAY
348 #define CALCULATOR_CALC BUTTON_FFWD
349 #define CALCULATOR_CLEAR BUTTON_REW
351 #else
352 #error No keymap defined!
353 #endif
355 #ifdef HAVE_TOUCHSCREEN
356 #ifndef CALCULATOR_LEFT
357 #define CALCULATOR_LEFT BUTTON_MIDLEFT
358 #endif
359 #ifndef CALCULATOR_RIGHT
360 #define CALCULATOR_RIGHT BUTTON_MIDRIGHT
361 #endif
362 #ifndef CALCULATOR_UP
363 #define CALCULATOR_UP BUTTON_TOPMIDDLE
364 #endif
365 #ifndef CALCULATOR_DOWN
366 #define CALCULATOR_DOWN BUTTON_BOTTOMMIDDLE
367 #endif
368 #ifndef CALCULATOR_CALC
369 #define CALCULATOR_CALC BUTTON_BOTTOMRIGHT
370 #endif
371 #ifndef CALCULATOR_INPUT
372 #define CALCULATOR_INPUT BUTTON_CENTER
373 #endif
374 #ifndef CALCULATOR_CLEAR
375 #define CALCULATOR_CLEAR BUTTON_TOPRIGHT
376 #endif
378 #include "lib/pluginlib_touchscreen.h"
379 static struct ts_raster calc_raster = { X_0_POS, Y_1_POS,
380 BUTTON_COLS*REC_WIDTH, BUTTON_ROWS*REC_HEIGHT, REC_WIDTH, REC_HEIGHT };
381 #endif
383 enum {
384 basicButtons,
385 sciButtons
386 } buttonGroup;
388 unsigned char* buttonChar[2][5][5] = {
389 { { "MR" , "M+" , "2nd" , "CE" , "C" },
390 { "7" , "8" , "9" , "/" , "sqr" },
391 { "4" , "5" , "6" , "*" , "x^2" },
392 { "1" , "2" , "3" , "-" , "1/x" },
393 { "0" , "+/-", "." , "+" , "=" } },
395 { { "n!" , "PI" , "1st" , "sin" , "asi" },
396 { "7" , "8" , "9" , "cos" , "aco" },
397 { "4" , "5" , "6" , "tan" , "ata" },
398 { "1" , "2" , "3" , "ln" , "e^x" },
399 { "0" , "+/-", "." , "log" , "x^y" } }
402 enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C ,
403 btn_7 , btn_8 , btn_9 , btn_div , btn_sqr ,
404 btn_4 , btn_5 , btn_6 , btn_time , btn_square ,
405 btn_1 , btn_2 , btn_3 , btn_minus , btn_rec ,
406 btn_0 , btn_sign , btn_dot , btn_add , btn_equal
409 enum { sci_fac, sci_pi , sci_sci , sci_sin , sci_asin ,
410 sci_7 , sci_8 , sci_9 , sci_cos , sci_acos ,
411 sci_4 , sci_5 , sci_6 , sci_tan , sci_atan ,
412 sci_1 , sci_2 , sci_3 , sci_ln , sci_exp ,
413 sci_0 , sci_sign , sci_dot , sci_log , sci_xy
416 #define MINIMUM 0.000000000001 /* e-12 */
417 /* ^ ^ ^ ^ */
418 /* 123456789abcdef */
420 #define DIGITLEN 10 /* must <= 10 */
421 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
422 /* 0.000 00000 0001 */
423 /* ^ ^ ^ ^ ^ ^ */
424 /* DIGITLEN 12345 6789a bcdef */
425 /* power 12 34567 89abc def */
426 /* 10^- 123 45678 9abcd ef */
428 unsigned char buf[19];/* 18 bytes of output line,
429 buf[0] is operator
430 buf[1] = 'M' if memTemp is not 0
431 buf[2] = ' '
433 if SCIENTIFIC_FORMAT
434 buf[2]-buf[12] or buf[3]-buf[13] = result;
435 format X.XXXXXXXX
436 buf[13] or buf[14] -buf[17] = power;
437 format eXXX or e-XXX
438 else
439 buf[3]-buf[6] = ' ';
440 buf[7]-buf[17] = result;
442 buf[18] = '\0' */
444 unsigned char typingbuf[DIGITLEN+2];/* byte 0 is sign or ' ',
445 byte 1~DIGITLEN are num and '.'
446 byte (DIGITLEN+1) is '\0' */
447 unsigned char* typingbufPointer = typingbuf;
449 double result = 0; /* main operand, format 0.xxxxx */
450 int power = 0; /* 10^power */
451 double modifier = 0.1; /* position of next input */
452 double operand = 0; /* second operand, format 0.xxxxx */
453 int operandPower = 0; /* 10^power of second operand */
454 char oper = ' '; /* operators: + - * / */
455 bool operInputted = false; /* false: do calculation first and
456 replace current oper
457 true: just replace current oper */
459 double memTemp = 0; /* temp memory */
460 int memTempPower = 0; /* 10^^power of memTemp */
462 int btn_row, btn_col; /* current position index for button */
463 int prev_btn_row, prev_btn_col; /* previous cursor position */
464 #define CAL_BUTTON (btn_row*5+btn_col)
466 int btn = BUTTON_NONE;
467 int lastbtn = BUTTON_NONE;
469 /* Status of calculator */
470 enum {cal_normal, /* 0, normal status, display result */
471 cal_typing, /* 1, currently typing, dot hasn't been typed */
472 cal_dotted, /* 2, currently typing, dot already has been typed. */
473 cal_error,
474 cal_exit,
475 cal_toDo
476 } calStatus;
478 /* constant table for CORDIC algorithm */
479 double cordicTable[51][2]= {
480 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
481 {1e+00, 7.853981633974483e-01},
482 {5e-01, 4.636476090008061e-01},
483 {2.5e-01, 2.449786631268641e-01},
484 {1.25e-01, 1.243549945467614e-01},
485 {6.25e-02, 6.241880999595735e-02},
486 {3.125e-02, 3.123983343026828e-02},
487 {1.5625e-02, 1.562372862047683e-02},
488 {7.8125e-03, 7.812341060101111e-03},
489 {3.90625e-03, 3.906230131966972e-03},
490 {1.953125e-03, 1.953122516478819e-03},
491 {9.765625e-04, 9.765621895593195e-04},
492 {4.8828125e-04, 4.882812111948983e-04},
493 {2.44140625e-04, 2.441406201493618e-04},
494 {1.220703125e-04, 1.220703118936702e-04},
495 {6.103515625e-05, 6.103515617420877e-05},
496 {3.0517578125e-05, 3.051757811552610e-05},
497 {1.52587890625e-05, 1.525878906131576e-05},
498 {7.62939453125e-06, 7.629394531101970e-06},
499 {3.814697265625e-06, 3.814697265606496e-06},
500 {1.9073486328125e-06, 1.907348632810187e-06},
501 {9.5367431640625e-07, 9.536743164059608e-07},
502 {4.76837158203125e-07, 4.768371582030888e-07},
503 {2.384185791015625e-07, 2.384185791015580e-07},
504 {1.1920928955078125e-07, 1.192092895507807e-07},
505 {5.9604644775390625e-08, 5.960464477539055e-08},
506 {2.98023223876953125e-08, 2.980232238769530e-08},
507 {1.490116119384765625e-08, 1.490116119384765e-08},
508 {7.450580596923828125e-09, 7.450580596923828e-09},
509 {3.7252902984619140625e-09, 3.725290298461914e-09},
510 {1.86264514923095703125e-09, 1.862645149230957e-09},
511 {9.31322574615478515625e-10, 9.313225746154785e-10},
512 {4.656612873077392578125e-10, 4.656612873077393e-10},
513 {2.3283064365386962890625e-10, 2.328306436538696e-10},
514 {1.16415321826934814453125e-10, 1.164153218269348e-10},
515 {5.82076609134674072265625e-11, 5.820766091346741e-11},
516 {2.910383045673370361328125e-11, 2.910383045673370e-11},
517 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
518 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
519 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
520 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
521 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
522 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
523 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
524 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
525 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
526 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
527 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
528 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
529 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
530 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
531 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
534 void doMultiple(double* operandOne, int* powerOne,
535 double operandTwo, int powerTwo);
536 void doAdd (double* operandOne, int* powerOne,
537 double operandTwo, int powerTwo);
538 void printResult(void);
539 void formatResult(void);
540 void oneOperand(void);
542 void drawLines(void);
543 void drawButtons(int group);
545 /* -----------------------------------------------------------------------
546 Handy funtions
547 ----------------------------------------------------------------------- */
548 void cleartypingbuf(void)
550 int k;
551 for( k=1; k<=(DIGITLEN+1); k++)
552 typingbuf[k] = 0;
553 typingbuf[0] = ' ';
554 typingbufPointer = typingbuf+1;
556 void clearbuf(void)
558 int k;
559 for(k=0;k<18;k++)
560 buf[k]=' ';
561 buf[18] = 0;
563 void clearResult(void)
565 result = 0;
566 power = 0;
567 modifier = 0.1;
570 void clearInput(void)
572 calStatus = cal_normal;
573 clearResult();
574 cleartypingbuf();
575 rb->lcd_clear_display();
576 drawButtons(buttonGroup);
577 drawLines();
580 void clearOperand(void)
582 operand = 0;
583 operandPower = 0;
586 void clearMemTemp(void)
588 memTemp = 0;
589 memTempPower = 0;
592 void clearOper(void)
594 oper = ' ';
595 operInputted = false;
598 void clearMem(void)
600 clearInput();
601 clearMemTemp();
602 clearOperand();
603 clearOper();
604 btn = BUTTON_NONE;
607 void switchOperands(void)
609 double tempr = operand;
610 int tempp = operandPower;
611 operand = result;
612 operandPower = power;
613 result = tempr;
614 power = tempp;
617 void drawLines(void)
619 int i;
620 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS-1);
621 for (i = 0; i < 5 ; i++)
622 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS+i*REC_HEIGHT);
623 for (i = 0; i < 4 ; i++)
624 rb->lcd_vline(X_1_POS+i*REC_WIDTH, Y_1_POS, LCD_HEIGHT);
627 void drawButtons(int group)
629 int i, j, w, h;
630 for (i = 0; i <= 4; i++){
631 for (j = 0; j <= 4; j++){
632 rb->lcd_getstringsize( buttonChar[group][i][j],&w,&h);
633 if (i == btn_row && j == btn_col) /* selected item */
634 rb->lcd_set_drawmode(DRMODE_SOLID);
635 else
636 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
637 rb->lcd_fillrect( X_0_POS + j*REC_WIDTH,
638 Y_1_POS + i*REC_HEIGHT,
639 REC_WIDTH, REC_HEIGHT+1);
640 if (i == btn_row && j == btn_col) /* selected item */
641 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
642 else
643 rb->lcd_set_drawmode(DRMODE_SOLID);
644 rb->lcd_putsxy( X_0_POS + j*REC_WIDTH + (REC_WIDTH - w)/2,
645 Y_1_POS + i*REC_HEIGHT + (REC_HEIGHT - h)/2 + 1,
646 buttonChar[group][i][j] );
649 rb->lcd_set_drawmode(DRMODE_SOLID);
652 /* -----------------------------------------------------------------------
653 Initiate calculator
654 ----------------------------------------------------------------------- */
655 void cal_initial (void)
657 int w,h;
659 rb->lcd_getstringsize("2nd",&w,&h);
660 if (w > REC_WIDTH || h > REC_HEIGHT)
661 rb->lcd_setfont(FONT_SYSFIXED);
663 rb->lcd_clear_display();
665 #ifdef CALCULATOR_OPERATORS
666 /* basic operators are available through separate button */
667 buttonGroup = sciButtons;
668 #else
669 buttonGroup = basicButtons;
670 #endif
672 /* initially, invert button "5" */
673 btn_row = 2;
674 btn_col = 1;
675 prev_btn_row = btn_row;
676 prev_btn_col = btn_col;
677 drawButtons(buttonGroup);
678 drawLines();
679 rb->lcd_update();
681 /* initial mem and output display*/
682 clearMem();
683 printResult();
685 /* clear button queue */
686 rb->button_clear_queue();
689 /* -----------------------------------------------------------------------
690 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
691 in it's private case for sqrt.
692 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
693 ----------------------------------------------------------------------- */
694 double mySqrt(double square)
696 int k = 0;
697 double temp = 0;
698 double root= ABS(square+1)/2;
700 while( ABS(root - temp) > MINIMUM ){
701 temp = root;
702 root = (square/temp + temp)/2;
703 k++;
704 if (k>10000) return 0;
707 return root;
709 /* -----------------------------------------------------------------------
710 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
711 transcendFunc can do sin,cos,log,exp
712 input parameter is angle
713 ----------------------------------------------------------------------- */
714 void transcendFunc(char* func, double* tt, int* ttPower)
716 double t = (*tt)*M_PI/180; int tPower = *ttPower;
717 int sign = 1;
718 int n = 50; /* n <=50, tables are all <= 50 */
719 int j;
720 double x,y,z,xt,yt,zt;
722 if (tPower < -998) {
723 calStatus = cal_normal;
724 return;
726 if (tPower > 8) {
727 calStatus = cal_error;
728 return;
730 *ttPower = 0;
731 calStatus = cal_normal;
733 if( func[0] =='s' || func[0] =='S'|| func[0] =='t' || func[0] =='T')
734 sign = SIGN(t);
735 else {
736 /* if( func[0] =='c' || func[0] =='C') */
737 sign = 1;
739 t = ABS(t);
741 while (tPower > 0){
742 t *= 10;
743 tPower--;
745 while (tPower < 0) {
746 t /= 10;
747 tPower++;
749 j = 0;
750 while (t > j*M_TWOPI) {j++;}
751 t -= (j-1)*M_TWOPI;
752 if (M_PI_2 < t && t < 3*M_PI_2){
753 t = M_PI - t;
754 if (func[0] =='c' || func[0] =='C')
755 sign = -1;
756 else if (func[0] =='t' || func[0] =='T')
757 t*=-1;
759 else if ( 3*M_PI_2 <= t && t <= M_TWOPI)
760 t -= M_TWOPI;
762 x = 0.60725293500888; y = 0; z = t;
763 for (j=1;j<n+2;j++){
764 xt = x - SIGN(z) * y*cordicTable[j-1][0];
765 yt = y + SIGN(z) * x*cordicTable[j-1][0];
766 zt = z - SIGN(z) * cordicTable[j-1][1];
767 x = xt;
768 y=yt;
769 z=zt;
771 if( func[0] =='s' || func[0] =='S') {
772 *tt = sign*y;
773 return;
775 else if( func[0] =='c' || func[0] =='C') {
776 *tt = sign*x;
777 return;
779 else /*if( func[0] =='t' || func[0] =='T')*/ {
780 if(t==M_PI_2||t==-M_PI_2){
781 calStatus = cal_error;
782 return;
784 else{
785 *tt = sign*(y/x);
786 return;
791 /* -----------------------------------------------------------------------
792 add in scientific number format
793 ----------------------------------------------------------------------- */
794 void doAdd (double* operandOne, int* powerOne,
795 double operandTwo, int powerTwo)
797 if ( *powerOne >= powerTwo ){
798 if (*powerOne - powerTwo <= DIGITLEN+1){
799 while (powerTwo < *powerOne){
800 operandTwo /=10;
801 powerTwo++;
803 *operandOne += operandTwo;
805 /*do nothing if operandTwo is too small*/
807 else{
808 if (powerTwo - *powerOne <= DIGITLEN+1){
809 while(powerTwo > *powerOne){
810 *operandOne /=10;
811 (*powerOne)++;
813 (*operandOne) += operandTwo;
815 else{/* simply copy operandTwo if operandOne is too small */
816 *operandOne = operandTwo;
817 *powerOne = powerTwo;
821 /* -----------------------------------------------------------------------
822 multiple in scientific number format
823 ----------------------------------------------------------------------- */
824 void doMultiple(double* operandOne, int* powerOne,
825 double operandTwo, int powerTwo)
827 (*operandOne) *= operandTwo;
828 (*powerOne) += powerTwo;
831 /* -----------------------------------------------------------------------
832 Handles all one operand calculations
833 ----------------------------------------------------------------------- */
834 void oneOperand(void)
836 int k = 0;
837 if (buttonGroup == basicButtons){
838 switch(CAL_BUTTON){
839 case btn_sqr:
840 if (result<0)
841 calStatus = cal_error;
842 else{
843 if (power%2 == 1){
844 result = (mySqrt(result*10))/10;
845 power = (power+1) / 2;
847 else{
848 result = mySqrt(result);
849 power = power / 2;
851 calStatus = cal_normal;
853 break;
854 case btn_square:
855 power *= 2;
856 result *= result;
857 calStatus = cal_normal;
858 break;
860 case btn_rec:
861 if (result==0)
862 calStatus = cal_error;
863 else{
864 power = -power;
865 result = 1/result;
866 calStatus = cal_normal;
868 break;
869 default:
870 calStatus = cal_toDo;
871 break; /* just for the safety */
874 else{ /* sciButtons */
875 switch(CAL_BUTTON){
876 case sci_sin:
877 transcendFunc("sin", &result, &power);
878 break;
879 case sci_cos:
880 transcendFunc("cos", &result, &power);
881 break;
882 case sci_tan:
883 transcendFunc("tan", &result, &power);
884 break;
885 case sci_fac:
886 if (power<0 || power>8 || result<0 )
887 calStatus = cal_error;
888 else if(result == 0) {
889 result = 1;
890 power = 0;
892 else{
893 while(power > 0) {
894 result *= 10;
895 power--;
897 if ( ( result - (int)result) > MINIMUM )
898 calStatus = cal_error;
899 else {
900 k = result; result = 1;
901 while (k > 1){
902 doMultiple(&result, &power, k, 0);
903 formatResult();
904 k--;
906 calStatus = cal_normal;
909 break;
910 default:
911 calStatus = cal_toDo;
912 break; /* just for the safety */
918 /* -----------------------------------------------------------------------
919 Handles all two operands calculations
920 ----------------------------------------------------------------------- */
921 void twoOperands(void)
923 switch(oper){
924 case '-':
925 doAdd(&operand, &operandPower, -result, power);
926 break;
927 case '+':
928 doAdd(&operand, &operandPower, result, power);
929 break;
930 case '*':
931 doMultiple(&operand, &operandPower, result, power);
932 break;
933 case '/':
934 if ( ABS(result) > MINIMUM ){
935 doMultiple(&operand, &operandPower, 1/result, -power);
937 else
938 calStatus = cal_error;
939 break;
940 default: /* ' ' */
941 switchOperands(); /* counter switchOperands() below */
942 break;
943 } /* switch(oper) */
944 switchOperands();
945 clearOper();
948 /* First, increases *dimen1 by dimen1_delta modulo dimen1_modulo.
949 If dimen1 wraps, increases *dimen2 by dimen2_delta modulo dimen2_modulo.
951 static void move_with_wrap_and_shift(
952 int *dimen1, int dimen1_delta, int dimen1_modulo,
953 int *dimen2, int dimen2_delta, int dimen2_modulo)
955 bool wrapped = false;
957 *dimen1 += dimen1_delta;
958 if (*dimen1 < 0)
960 *dimen1 = dimen1_modulo - 1;
961 wrapped = true;
963 else if (*dimen1 >= dimen1_modulo)
965 *dimen1 = 0;
966 wrapped = true;
969 if (wrapped)
971 /* Make the dividend always positive to be sure about the result.
972 Adding dimen2_modulo does not change it since we do it modulo. */
973 *dimen2 = (*dimen2 + dimen2_modulo + dimen2_delta) % dimen2_modulo;
977 /* -----------------------------------------------------------------------
978 Print buttons when switching 1st and 2nd
979 int group = {basicButtons, sciButtons}
980 ----------------------------------------------------------------------- */
981 void printButtonGroups(int group)
983 drawButtons(group);
984 drawLines();
985 rb->lcd_update();
987 /* -----------------------------------------------------------------------
988 flash the currently marked button
989 ----------------------------------------------------------------------- */
990 void flashButton(void)
992 int k, w, h;
993 for (k=2;k>0;k--)
995 rb->lcd_getstringsize( buttonChar[buttonGroup][btn_row][btn_col],&w,&h);
996 rb->lcd_set_drawmode(DRMODE_SOLID|(k==1) ? 0 : DRMODE_INVERSEVID);
997 rb->lcd_fillrect( X_0_POS + btn_col*REC_WIDTH + 1,
998 Y_1_POS + btn_row*REC_HEIGHT + 1,
999 REC_WIDTH - 1, REC_HEIGHT - 1);
1000 rb->lcd_putsxy( X_0_POS + btn_col*REC_WIDTH + (REC_WIDTH - w)/2,
1001 Y_1_POS + btn_row*REC_HEIGHT + (REC_HEIGHT - h)/2 +1,
1002 buttonChar[buttonGroup][btn_row][btn_col] );
1003 rb->lcd_update_rect( X_0_POS + btn_col*REC_WIDTH + 1,
1004 Y_1_POS + btn_row*REC_HEIGHT + 1,
1005 REC_WIDTH - 1, REC_HEIGHT - 1);
1007 if (k!= 1)
1008 rb->sleep(HZ/22);
1013 /* -----------------------------------------------------------------------
1014 pos is the position that needs animation. pos = [1~18]
1015 ----------------------------------------------------------------------- */
1016 void deleteAnimation(int pos)
1018 int k, w, h, x;
1019 if (pos<1 || pos >18)
1020 return;
1022 rb->lcd_getstringsize("0", &w, &h);
1023 x = (pos==1? 4: LCD_WIDTH - 4 - w);
1025 for (k=0;k<4;k++){
1026 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1027 rb->lcd_fillrect(x, Y_1_POS - h -1, w, h);
1028 rb->lcd_set_drawmode(DRMODE_SOLID);
1029 rb->lcd_fillrect(x + (w*k)/8, Y_1_POS - h -1 + (h*k)/8,
1030 (w*(4-k))/4, (h*(4-k))/4);
1031 rb->lcd_update_rect(x, Y_1_POS - h -1, w, h);
1032 rb->sleep(HZ/32);
1036 /* -----------------------------------------------------------------------
1037 result may be one of these formats:
1039 xxxx.xxxx
1040 0.xxxx
1041 0.0000xxxx
1043 formatResult() change result to standard format: 0.xxxx
1044 if result is close to 0, let it be 0;
1045 if result is close to 1, let it be 0.1 and power++;
1046 ----------------------------------------------------------------------- */
1047 void formatResult(void)
1049 int resultsign = SIGN(result);
1050 result = ABS(result);
1051 if(result > MINIMUM ){ /* doesn't check power, might have problem
1052 input wouldn't,
1053 + - * / of two formatted number wouldn't.
1054 only a calculation that makes a formatted
1055 number (0.xxxx) less than MINIMUM in only
1056 one operation */
1058 if (result<1){
1059 while( (int)(result*10) == 0 ){
1060 result *= 10;
1061 power--;
1062 modifier *= 10;
1065 else{ /* result >= 1 */
1066 while( (int)result != 0 ){
1067 result /= 10;
1068 power++;
1069 modifier /= 10;
1071 } /* if result<1 */
1073 if (result > (1-MINIMUM)){
1074 result = 0.1;
1075 power++;
1076 modifier /= 10;
1078 result *= resultsign;
1080 else {
1081 result = 0;
1082 power = 0;
1083 modifier = 0.1;
1087 /* -----------------------------------------------------------------------
1088 result2typingbuf() outputs standard format result to typingbuf.
1089 case SCIENTIFIC_FORMAT, let temppower = 1;
1090 case temppower > 0: print '.' in the middle
1091 case temppower <= 0: print '.' in the begining
1092 ----------------------------------------------------------------------- */
1093 void result2typingbuf(void)
1095 bool haveDot = false;
1096 char tempchar = 0;
1097 int k;
1098 double tempresult = ABS(result); /* positive num makes things simple */
1100 int temppower;
1101 double tempmodifier = 1;
1102 int count;
1104 if(SCIENTIFIC_FORMAT)
1105 temppower = 1; /* output x.xxxx format */
1106 else
1107 temppower = power;
1109 cleartypingbuf();
1111 if(tempresult < MINIMUM){ /* if 0,faster display and avoid complication*/
1112 typingbuf[0] = ' ';
1113 typingbuf[1] = '0';
1115 else{ /* tempresult > 0 */
1116 typingbuf[0] = (SIGN(result)<0)?'-':' ';
1118 typingbufPointer = typingbuf;
1119 if(temppower > 0){
1120 for (k = 0; k<DIGITLEN+1 ; k++){
1121 typingbufPointer++;
1122 if(temppower || *(typingbufPointer-1) == '.'){
1123 count = 0;
1124 tempmodifier = tempmodifier/10;
1125 while( (tempresult-tempmodifier*count) >
1126 (tempmodifier-MINIMUM)){
1127 count++;
1129 tempresult -= tempmodifier*count;
1130 tempresult = ABS(tempresult);
1131 temppower-- ;
1132 *typingbufPointer = count + '0';
1134 else{ /* temppower == 0 */
1135 *typingbufPointer = '.';
1136 haveDot = true;
1138 } /* for */
1140 else{
1141 haveDot = true;
1142 typingbufPointer++; *typingbufPointer = '0';
1143 typingbufPointer++; *typingbufPointer = '.';
1144 for (k = 2; k<DIGITLEN+1 ; k++){
1145 typingbufPointer++;
1146 count = 0;
1147 if ( (-temppower) < (k-1)){
1148 tempmodifier = tempmodifier/10;
1149 while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){
1150 count++;
1153 tempresult -= tempmodifier*count;
1154 tempresult = ABS(tempresult);
1155 temppower-- ;
1157 *typingbufPointer = count + '0';
1160 /* now, typingbufPointer = typingbuf + 16 */
1161 /* backward strip off 0 and '.' */
1162 if (haveDot){
1163 while( (*typingbufPointer == '0') || (*typingbufPointer == '.')){
1164 tempchar = *typingbufPointer;
1165 *typingbufPointer = 0;
1166 typingbufPointer--;
1167 if (tempchar == '.') break;
1170 typingbuf[DIGITLEN+1] = 0;
1171 } /* else tempresult > 0 */
1174 /* -----------------------------------------------------------------------
1175 printResult() generates LCD display.
1176 ----------------------------------------------------------------------- */
1177 void printResult(void)
1179 int k, w, h;
1181 char operbuf[3] = {0, 0, 0};
1183 switch_Status:
1184 switch(calStatus){
1185 case cal_exit:
1186 rb->lcd_clear_display();
1187 rb->splash(HZ/3, "Bye now!");
1188 break;
1189 case cal_error:
1190 clearbuf();
1191 rb->snprintf(buf, 19, "%18s","Error");
1192 break;
1193 case cal_toDo:
1194 clearbuf();
1195 rb->snprintf(buf, 19, "%18s","Coming soon ^_* ");
1196 break;
1198 case cal_normal:
1199 formatResult();
1201 if( power > 1000 ){ /* power -1 > 999 */
1202 calStatus = cal_error;
1203 goto switch_Status;
1205 if (power < -998 ) /* power -1 < -999 */
1206 clearResult(); /* too small, let it be 0 */
1208 result2typingbuf();
1209 clearbuf();
1211 operbuf[0] = oper;
1212 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1213 operbuf[2] = '\0';
1215 if(SCIENTIFIC_FORMAT){
1216 /* output format: X.XXXX eXXX */
1217 if(power > -98){ /* power-1 >= -99, eXXX or e-XX */
1218 rb->snprintf(buf, 12, "%11s",typingbuf);
1219 for(k=11;k<=14;k++) buf[k] = ' ';
1220 cleartypingbuf();
1221 rb->snprintf(typingbuf, 5, "e%d",power-1);
1222 rb->snprintf(buf+11, 5, "%4s",typingbuf);
1224 else{ /* power-1 <= -100, e-XXX */
1225 rb->snprintf(buf, 12, "%11s",typingbuf);
1226 rb->snprintf(buf+11, 6, "e%d",power-1);
1229 else{
1230 rb->snprintf(buf, 12, "%11s",typingbuf);
1231 } /* if SCIENTIFIC_FORMAT */
1232 break;
1233 case cal_typing:
1234 case cal_dotted:
1235 clearbuf();
1236 operbuf[0] = oper;
1237 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1238 rb->snprintf(buf, 12, "%11s",typingbuf);
1239 break;
1243 rb->lcd_getstringsize(buf, &w, &h);
1244 rb->screen_clear_area(rb->screens[0], 0, 0, LCD_WIDTH, Y_1_POS - 1);
1245 rb->lcd_putsxy(4, Y_1_POS - h -1, operbuf);
1246 rb->lcd_putsxy(LCD_WIDTH - w - 4, Y_1_POS - h -1, buf);
1247 rb->lcd_update_rect(0, 1, LCD_WIDTH, Y_1_POS);
1250 /* -----------------------------------------------------------------------
1251 Process typing buttons: 1-9, '.', sign
1252 main operand "result" and typingbuf are processed seperately here.
1253 ----------------------------------------------------------------------- */
1254 void typingProcess(void){
1255 switch( CAL_BUTTON ){
1256 case btn_sign:
1257 if (calStatus == cal_typing ||
1258 calStatus == cal_dotted)
1259 typingbuf[0] = (typingbuf[0]=='-')?' ':'-';
1260 result = -result;
1261 break;
1262 case btn_dot:
1263 operInputted = false;
1264 switch(calStatus){
1265 case cal_normal:
1266 clearInput();
1267 *typingbufPointer = '0';
1268 typingbufPointer++;
1269 case cal_typing:
1270 calStatus = cal_dotted;
1271 *typingbufPointer = '.';
1272 if (typingbufPointer != typingbuf+DIGITLEN+1)
1273 typingbufPointer++;
1274 break;
1275 default: /* cal_dotted */
1276 break;
1278 break;
1279 default: /* 0-9 */
1280 operInputted = false;
1281 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1282 switch(calStatus){
1283 case cal_normal:
1284 if(CAL_BUTTON == btn_0 )
1285 break; /* first input is 0, ignore */
1286 clearInput();
1287 /*no operator means start a new calculation*/
1288 if (oper ==' ')
1289 clearOperand();
1290 calStatus = cal_typing;
1291 /* go on typing, no break */
1292 case cal_typing:
1293 case cal_dotted:
1294 switch(CAL_BUTTON){
1295 case btn_0:
1296 *typingbufPointer = '0';
1297 break;
1298 default:
1299 *typingbufPointer=(7+btn_col-3*(btn_row-1))+ '0';
1300 break;
1302 if (typingbufPointer!=typingbuf+DIGITLEN+1){
1303 typingbufPointer++;
1305 {/* result processing */
1306 if (calStatus == cal_typing) power++;
1307 if (CAL_BUTTON != btn_0)
1308 result= result +
1309 SIGN(result)*
1310 (7+btn_col-3*(btn_row-1))*modifier;
1311 modifier /= 10;
1314 else /* last byte always '\0' */
1315 *typingbufPointer = 0;
1316 break;
1317 default: /* cal_error, cal_exit */
1318 break;
1320 break; /* default, 0-9 */
1321 } /* switch( CAL_BUTTON ) */
1324 /* -----------------------------------------------------------------------
1325 Handle delete operation
1326 main operand "result" and typingbuf are processed seperately here.
1327 ----------------------------------------------------------------------- */
1328 void doDelete(void){
1329 deleteAnimation(18);
1330 switch(calStatus){
1331 case cal_dotted:
1332 if (*(typingbufPointer-1) == '.'){
1333 /* if dotted and deleting '.',
1334 change status and delete '.' below */
1335 calStatus = cal_typing;
1337 else{ /* if dotted and not deleting '.',
1338 power stays */
1339 power++; /* counter "power--;" below */
1341 case cal_typing:
1342 typingbufPointer--;
1344 {/* result processing */ /* 0-9, '.' */
1345 /* if deleting '.', do nothing */
1346 if ( *typingbufPointer != '.'){
1347 power--;
1348 modifier *= 10;
1349 result = result - SIGN(result)*
1350 ((*typingbufPointer)- '0')*modifier;
1354 *typingbufPointer = 0;
1356 /* if (only one digit left and it's 0)
1357 or no digit left, change status*/
1358 if ( typingbufPointer == typingbuf+1 ||
1359 ( typingbufPointer == typingbuf+2 &&
1360 *(typingbufPointer-1) == '0' ))
1361 calStatus = cal_normal;
1362 break;
1363 default: /* normal, error, exit */
1364 break;
1367 /* -----------------------------------------------------------------------
1368 Handle buttons on basic screen
1369 ----------------------------------------------------------------------- */
1370 void basicButtonsProcess(void){
1371 switch (btn) {
1372 case CALCULATOR_INPUT:
1373 if (calStatus == cal_error && (CAL_BUTTON != btn_C) ) break;
1374 flashButton();
1375 switch( CAL_BUTTON ){
1376 case btn_MR:
1377 operInputted = false;
1378 result = memTemp; power = memTempPower;
1379 calStatus = cal_normal;
1380 break;
1381 case btn_M:
1382 formatResult();
1383 if (memTemp > MINIMUM)
1384 doAdd(&memTemp, &memTempPower, result, power);
1385 else {
1386 /* if result is too small and memTemp = 0,
1387 doAdd will not add */
1388 memTemp = result;
1389 memTempPower = power;
1391 calStatus = cal_normal;
1392 break;
1394 case btn_C: clearMem(); break;
1395 case btn_CE: clearInput(); break;
1397 case btn_bas:
1398 buttonGroup = sciButtons;
1399 printButtonGroups(buttonGroup);
1400 break;
1402 /* one operand calculation, may be changed to
1403 like sin, cos, log, etc */
1404 case btn_sqr:
1405 case btn_square:
1406 case btn_rec:
1407 formatResult(); /* not necessary, just for safty */
1408 oneOperand();
1409 break;
1411 case_btn_equal: /* F3 shortkey entrance */
1412 case btn_equal:
1413 formatResult();
1414 calStatus = cal_normal;
1415 operInputted = false;
1416 if (oper != ' ') twoOperands();
1417 break;
1419 case btn_div:
1420 case btn_time:
1421 case btn_minus:
1422 case btn_add:
1423 if(!operInputted) {twoOperands(); operInputted = true;}
1424 oper = buttonChar[basicButtons][btn_row][btn_col][0];
1425 #ifdef CALCULATOR_OPERATORS
1426 case_cycle_operators: /* F2 shortkey entrance */
1427 #endif
1428 calStatus = cal_normal;
1429 formatResult();
1430 operand = result;
1431 operandPower = power;
1433 break;
1435 case btn_sign:
1436 case btn_dot:
1437 default: /* 0-9 */
1438 typingProcess();
1439 break;
1440 } /* switch (CAL_BUTTON) */
1441 break;
1443 #ifdef CALCULATOR_OPERATORS
1444 case CALCULATOR_OPERATORS:
1445 if (calStatus == cal_error) break;
1446 if (!operInputted) {twoOperands(); operInputted = true;}
1447 switch (oper){
1448 case ' ':
1449 case '/': oper = '+'; flashButton(); break;
1450 case '+': oper = '-'; flashButton(); break;
1451 case '-': oper = '*'; flashButton(); break;
1452 case '*': oper = '/'; flashButton(); break;
1454 goto case_cycle_operators;
1455 break;
1456 #endif
1458 case CALCULATOR_CALC:
1459 if (calStatus == cal_error) break;
1460 flashButton();
1461 goto case_btn_equal;
1462 break;
1463 default: break;
1465 printResult();
1468 /* -----------------------------------------------------------------------
1469 Handle buttons on scientific screen
1470 ----------------------------------------------------------------------- */
1471 void sciButtonsProcess(void){
1472 switch (btn) {
1473 case CALCULATOR_INPUT:
1474 if (calStatus == cal_error && (CAL_BUTTON != sci_sci) ) break;
1475 flashButton();
1476 switch( CAL_BUTTON ){
1478 case sci_pi:
1479 result = M_PI; power = 0;
1480 calStatus = cal_normal;
1481 break;
1483 case sci_xy: break;
1485 case sci_sci:
1486 buttonGroup = basicButtons;
1487 printButtonGroups(basicButtons);
1488 break;
1490 case sci_fac:
1491 case sci_sin:
1492 case sci_asin:
1493 case sci_cos:
1494 case sci_acos:
1495 case sci_tan:
1496 case sci_atan:
1497 case sci_ln:
1498 case sci_exp:
1499 case sci_log:
1500 formatResult(); /* not necessary, just for safty */
1501 oneOperand();
1502 break;
1504 case btn_sign:
1505 case btn_dot:
1506 default: /* 0-9 */
1507 typingProcess();
1508 break;
1509 } /* switch (CAL_BUTTON) */
1510 break;
1512 #ifdef CALCULATOR_OPERATORS
1513 case CALCULATOR_OPERATORS:
1514 if (calStatus == cal_error) break;
1515 if (!operInputted) {twoOperands(); operInputted = true;}
1516 switch (oper){
1517 case ' ': oper = '+'; break;
1518 case '/': oper = '+'; deleteAnimation(1); break;
1519 case '+': oper = '-'; deleteAnimation(1); break;
1520 case '-': oper = '*'; deleteAnimation(1); break;
1521 case '*': oper = '/'; deleteAnimation(1); break;
1523 calStatus = cal_normal;
1524 formatResult();
1525 operand = result;
1526 operandPower = power;
1527 break;
1528 #endif
1530 case CALCULATOR_CALC:
1531 if (calStatus == cal_error) break;
1532 formatResult();
1533 calStatus = cal_normal;
1534 operInputted = false;
1535 if (oper != ' ') twoOperands();
1536 break;
1537 default: break;
1539 printResult();
1542 /* -----------------------------------------------------------------------
1543 move button index
1544 Invert display new button, invert back previous button
1545 ----------------------------------------------------------------------- */
1546 int handleButton(int button){
1547 switch(button)
1549 case CALCULATOR_INPUT:
1550 case CALCULATOR_CALC:
1551 #ifdef CALCULATOR_INPUT_CALC_PRE
1552 if (lastbtn != CALCULATOR_INPUT_CALC_PRE)
1553 break;
1554 /* no unconditional break; here! */
1555 #endif
1556 #ifdef CALCULATOR_OPERATORS
1557 case CALCULATOR_OPERATORS:
1558 #endif
1559 switch(buttonGroup){
1560 case basicButtons:
1561 basicButtonsProcess();
1562 break;
1563 case sciButtons:
1564 sciButtonsProcess();
1565 break;
1567 break;
1569 #ifdef CALCULATOR_CLEAR
1570 case CALCULATOR_CLEAR:
1571 switch(calStatus){
1572 case cal_typing:
1573 case cal_dotted:
1574 doDelete();
1575 break;
1576 default: /* cal_normal, cal_error, cal_exit */
1577 clearMem();
1578 break;
1580 printResult();
1581 break;
1582 #endif
1583 case CALCULATOR_LEFT:
1584 case CALCULATOR_LEFT | BUTTON_REPEAT:
1585 move_with_wrap_and_shift(
1586 &btn_col, -1, BUTTON_COLS,
1587 &btn_row, 0, BUTTON_ROWS);
1588 break;
1590 case CALCULATOR_RIGHT:
1591 case CALCULATOR_RIGHT | BUTTON_REPEAT:
1592 move_with_wrap_and_shift(
1593 &btn_col, 1, BUTTON_COLS,
1594 &btn_row, 0, BUTTON_ROWS);
1595 break;
1597 #ifdef CALCULATOR_UP
1598 case CALCULATOR_UP:
1599 case CALCULATOR_UP | BUTTON_REPEAT:
1600 move_with_wrap_and_shift(
1601 &btn_row, -1, BUTTON_ROWS,
1602 &btn_col, 0, BUTTON_COLS);
1603 break;
1604 #endif
1605 #ifdef CALCULATOR_DOWN
1606 case CALCULATOR_DOWN:
1607 case CALCULATOR_DOWN | BUTTON_REPEAT:
1608 move_with_wrap_and_shift(
1609 &btn_row, 1, BUTTON_ROWS,
1610 &btn_col, 0, BUTTON_COLS);
1611 break;
1612 #endif
1614 #ifdef CALCULATOR_UP_W_SHIFT
1615 case CALCULATOR_UP_W_SHIFT:
1616 case CALCULATOR_UP_W_SHIFT | BUTTON_REPEAT:
1617 move_with_wrap_and_shift(
1618 &btn_row, -1, BUTTON_ROWS,
1619 &btn_col, -1, BUTTON_COLS);
1620 break;
1621 #endif
1622 #ifdef CALCULATOR_DOWN_W_SHIFT
1623 case CALCULATOR_DOWN_W_SHIFT:
1624 case CALCULATOR_DOWN_W_SHIFT | BUTTON_REPEAT:
1625 move_with_wrap_and_shift(
1626 &btn_row, 1, BUTTON_ROWS,
1627 &btn_col, 1, BUTTON_COLS);
1628 break;
1629 #endif
1630 #ifdef CALCULATOR_LEFT_W_SHIFT
1631 case CALCULATOR_LEFT_W_SHIFT:
1632 case CALCULATOR_LEFT_W_SHIFT | BUTTON_REPEAT:
1633 move_with_wrap_and_shift(
1634 &btn_col, -1, BUTTON_COLS,
1635 &btn_row, -1, BUTTON_ROWS);
1636 break;
1637 #endif
1638 #ifdef CALCULATOR_RIGHT_W_SHIFT
1639 case CALCULATOR_RIGHT_W_SHIFT:
1640 case CALCULATOR_RIGHT_W_SHIFT | BUTTON_REPEAT:
1641 move_with_wrap_and_shift(
1642 &btn_col, 1, BUTTON_COLS,
1643 &btn_row, 1, BUTTON_ROWS);
1644 break;
1645 #endif
1646 #ifdef CALCULATOR_RC_QUIT
1647 case CALCULATOR_RC_QUIT:
1648 #endif
1649 case CALCULATOR_QUIT:
1650 return -1;
1653 return 0;
1655 prev_btn_row = btn_row;
1656 prev_btn_col = btn_col;
1659 /* -----------------------------------------------------------------------
1660 Main();
1661 ----------------------------------------------------------------------- */
1662 enum plugin_status plugin_start(const void* parameter)
1664 (void)parameter;
1666 /* now go ahead and have fun! */
1668 #ifdef HAVE_TOUCHSCREEN
1669 rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
1670 #endif
1672 cal_initial();
1674 while (calStatus != cal_exit ) {
1675 btn = rb->button_get_w_tmo(HZ/2);
1676 #ifdef HAVE_TOUCHSCREEN
1677 if(btn & BUTTON_TOUCHSCREEN)
1679 struct ts_raster_result res;
1680 if(touchscreen_map_raster(&calc_raster, rb->button_get_data() >> 16,
1681 rb->button_get_data() & 0xffff, &res) == 1)
1683 btn_row = res.y;
1684 btn_col = res.x;
1685 drawButtons(buttonGroup);
1686 drawLines();
1688 rb->lcd_update();
1690 prev_btn_row = btn_row;
1691 prev_btn_col = btn_col;
1692 if(btn & BUTTON_REL)
1694 btn = CALCULATOR_INPUT;
1695 switch(buttonGroup){
1696 case basicButtons:
1697 basicButtonsProcess();
1698 break;
1699 case sciButtons:
1700 sciButtonsProcess();
1701 break;
1703 btn = BUTTON_TOUCHSCREEN;
1707 #endif
1708 if (handleButton(btn) == -1)
1710 calStatus = cal_exit;
1711 printResult();
1713 else
1715 drawButtons(buttonGroup);
1716 drawLines();
1719 rb->lcd_update();
1721 if(rb->default_event_handler(btn) == SYS_USB_CONNECTED)
1722 return PLUGIN_USB_CONNECTED;
1724 if (btn != BUTTON_NONE)
1725 lastbtn = btn;
1726 } /* while (calStatus != cal_exit ) */
1728 rb->button_clear_queue();
1729 return PLUGIN_OK;
1732 #endif /* #ifdef HAVE_LCD_BITMAP */