Theme Editor: Put together a simple GUI to test going back and forth between a tree...
[kugel-rb.git] / apps / plugins / calculator.c
blob95b35e903c00e18d5b6e36c931f391af4795df10
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
84 #define M_TWOPI (M_PI * 2.0)
86 #define BUTTON_ROWS 5
87 #define BUTTON_COLS 5
89 #define REC_HEIGHT (int)(LCD_HEIGHT / (BUTTON_ROWS + 1))
90 #define REC_WIDTH (int)(LCD_WIDTH / BUTTON_COLS)
92 #define Y_6_POS (LCD_HEIGHT) /* Leave room for the border */
93 #define Y_5_POS (Y_6_POS - REC_HEIGHT) /* y5 = 53 */
94 #define Y_4_POS (Y_5_POS - REC_HEIGHT) /* y4 = 43 */
95 #define Y_3_POS (Y_4_POS - REC_HEIGHT) /* y3 = 33 */
96 #define Y_2_POS (Y_3_POS - REC_HEIGHT) /* y2 = 23 */
97 #define Y_1_POS (Y_2_POS - REC_HEIGHT) /* y1 = 13 */
98 #define Y_0_POS 0 /* y0 = 0 */
100 #define X_0_POS 0 /* x0 = 0 */
101 #define X_1_POS (X_0_POS + REC_WIDTH) /* x1 = 22 */
102 #define X_2_POS (X_1_POS + REC_WIDTH) /* x2 = 44 */
103 #define X_3_POS (X_2_POS + REC_WIDTH) /* x3 = 66 */
104 #define X_4_POS (X_3_POS + REC_WIDTH) /* x4 = 88 */
105 #define X_5_POS (X_4_POS + REC_WIDTH) /* x5 = 110, column 111 left blank */
107 #define SIGN(x) ((x)<0?-1:1)
108 #ifndef ABS
109 #define ABS(a) (((a) < 0) ? -(a) : (a))
110 #endif
112 /* variable button definitions */
113 #if CONFIG_KEYPAD == RECORDER_PAD
114 #define CALCULATOR_LEFT BUTTON_LEFT
115 #define CALCULATOR_RIGHT BUTTON_RIGHT
116 #define CALCULATOR_UP BUTTON_UP
117 #define CALCULATOR_DOWN BUTTON_DOWN
118 #define CALCULATOR_QUIT BUTTON_OFF
119 #define CALCULATOR_INPUT BUTTON_PLAY
120 #define CALCULATOR_CALC BUTTON_F3
121 #define CALCULATOR_OPERATORS BUTTON_F2
122 #define CALCULATOR_CLEAR BUTTON_F1
124 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
125 #define CALCULATOR_LEFT BUTTON_LEFT
126 #define CALCULATOR_RIGHT BUTTON_RIGHT
127 #define CALCULATOR_UP BUTTON_UP
128 #define CALCULATOR_DOWN BUTTON_DOWN
129 #define CALCULATOR_QUIT BUTTON_OFF
130 #define CALCULATOR_INPUT BUTTON_SELECT
131 #define CALCULATOR_CALC BUTTON_F3
132 #define CALCULATOR_OPERATORS BUTTON_F2
133 #define CALCULATOR_CLEAR BUTTON_F1
135 #elif CONFIG_KEYPAD == ONDIO_PAD
136 #define CALCULATOR_LEFT BUTTON_LEFT
137 #define CALCULATOR_RIGHT BUTTON_RIGHT
138 #define CALCULATOR_UP BUTTON_UP
139 #define CALCULATOR_DOWN BUTTON_DOWN
140 #define CALCULATOR_QUIT BUTTON_OFF
141 #define CALCULATOR_INPUT_CALC_PRE BUTTON_MENU
142 #define CALCULATOR_INPUT (BUTTON_MENU | BUTTON_REL)
143 #define CALCULATOR_CALC (BUTTON_MENU | BUTTON_REPEAT)
145 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
146 (CONFIG_KEYPAD == IRIVER_H300_PAD)
147 #define CALCULATOR_LEFT BUTTON_LEFT
148 #define CALCULATOR_RIGHT BUTTON_RIGHT
149 #define CALCULATOR_UP BUTTON_UP
150 #define CALCULATOR_DOWN BUTTON_DOWN
151 #define CALCULATOR_QUIT BUTTON_OFF
152 #define CALCULATOR_INPUT BUTTON_SELECT
153 #define CALCULATOR_CALC BUTTON_ON
154 #define CALCULATOR_OPERATORS BUTTON_MODE
155 #define CALCULATOR_CLEAR BUTTON_REC
157 #define CALCULATOR_RC_QUIT BUTTON_RC_STOP
159 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
160 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
161 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
163 #define CALCULATOR_LEFT BUTTON_LEFT
164 #define CALCULATOR_RIGHT BUTTON_RIGHT
165 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
166 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
167 #define CALCULATOR_QUIT BUTTON_MENU
168 #define CALCULATOR_INPUT BUTTON_SELECT
169 #define CALCULATOR_CALC BUTTON_PLAY
171 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
173 #define CALCULATOR_LEFT BUTTON_LEFT
174 #define CALCULATOR_RIGHT BUTTON_RIGHT
175 #define CALCULATOR_UP BUTTON_UP
176 #define CALCULATOR_DOWN BUTTON_DOWN
177 #define CALCULATOR_QUIT BUTTON_POWER
178 #define CALCULATOR_INPUT BUTTON_SELECT
179 #define CALCULATOR_CALC BUTTON_PLAY
180 #define CALCULATOR_CLEAR BUTTON_REC
182 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
184 #define CALCULATOR_LEFT BUTTON_LEFT
185 #define CALCULATOR_RIGHT BUTTON_RIGHT
186 #define CALCULATOR_UP BUTTON_UP
187 #define CALCULATOR_DOWN BUTTON_DOWN
188 #define CALCULATOR_QUIT BUTTON_POWER
189 #define CALCULATOR_INPUT BUTTON_SELECT
190 #define CALCULATOR_CALC BUTTON_MENU
191 #define CALCULATOR_CLEAR BUTTON_A
193 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
194 #define CALCULATOR_LEFT BUTTON_LEFT
195 #define CALCULATOR_RIGHT BUTTON_RIGHT
196 #define CALCULATOR_UP BUTTON_UP
197 #define CALCULATOR_DOWN BUTTON_DOWN
198 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
199 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
200 #define CALCULATOR_QUIT BUTTON_POWER
201 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
202 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
203 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
204 #define CALCULATOR_CLEAR BUTTON_REC
206 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
207 #define CALCULATOR_LEFT BUTTON_LEFT
208 #define CALCULATOR_RIGHT BUTTON_RIGHT
209 #define CALCULATOR_UP BUTTON_UP
210 #define CALCULATOR_DOWN BUTTON_DOWN
211 #define CALCULATOR_QUIT BUTTON_POWER
212 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
213 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
214 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
216 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
217 #define CALCULATOR_LEFT BUTTON_LEFT
218 #define CALCULATOR_RIGHT BUTTON_RIGHT
219 #define CALCULATOR_UP BUTTON_UP
220 #define CALCULATOR_DOWN BUTTON_DOWN
221 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
222 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
223 #define CALCULATOR_QUIT (BUTTON_HOME|BUTTON_REPEAT)
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
230 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
231 #define CALCULATOR_LEFT BUTTON_LEFT
232 #define CALCULATOR_RIGHT BUTTON_RIGHT
233 #define CALCULATOR_UP BUTTON_UP
234 #define CALCULATOR_DOWN BUTTON_DOWN
235 #define CALCULATOR_QUIT BUTTON_POWER
236 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
237 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
238 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
239 #define CALCULATOR_CLEAR BUTTON_HOME
241 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
242 #define CALCULATOR_LEFT BUTTON_LEFT
243 #define CALCULATOR_RIGHT BUTTON_RIGHT
244 #define CALCULATOR_UP BUTTON_UP
245 #define CALCULATOR_DOWN BUTTON_DOWN
246 #define CALCULATOR_QUIT BUTTON_POWER
247 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
248 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
249 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
250 #define CALCULATOR_CLEAR (BUTTON_SELECT|BUTTON_UP)
252 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
254 #define CALCULATOR_LEFT BUTTON_LEFT
255 #define CALCULATOR_RIGHT BUTTON_RIGHT
256 #define CALCULATOR_UP BUTTON_SCROLL_UP
257 #define CALCULATOR_DOWN BUTTON_SCROLL_DOWN
258 #define CALCULATOR_QUIT BUTTON_POWER
259 #define CALCULATOR_INPUT_CALC_PRE BUTTON_PLAY
260 #define CALCULATOR_INPUT (BUTTON_PLAY | BUTTON_REL)
261 #define CALCULATOR_CALC (BUTTON_PLAY | BUTTON_REPEAT)
262 #define CALCULATOR_CLEAR BUTTON_REW
264 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
266 #define CALCULATOR_LEFT BUTTON_LEFT
267 #define CALCULATOR_RIGHT BUTTON_RIGHT
268 #define CALCULATOR_UP BUTTON_UP
269 #define CALCULATOR_DOWN BUTTON_DOWN
270 #define CALCULATOR_QUIT BUTTON_BACK
271 #define CALCULATOR_INPUT BUTTON_SELECT
272 #define CALCULATOR_CALC BUTTON_MENU
273 #define CALCULATOR_CLEAR BUTTON_PLAY
275 #elif (CONFIG_KEYPAD == MROBE100_PAD)
277 #define CALCULATOR_LEFT BUTTON_LEFT
278 #define CALCULATOR_RIGHT BUTTON_RIGHT
279 #define CALCULATOR_UP BUTTON_UP
280 #define CALCULATOR_DOWN BUTTON_DOWN
281 #define CALCULATOR_QUIT BUTTON_POWER
282 #define CALCULATOR_INPUT BUTTON_SELECT
283 #define CALCULATOR_CALC BUTTON_MENU
284 #define CALCULATOR_CLEAR BUTTON_DISPLAY
286 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
288 #define CALCULATOR_LEFT BUTTON_RC_REW
289 #define CALCULATOR_RIGHT BUTTON_RC_FF
290 #define CALCULATOR_UP BUTTON_RC_VOL_UP
291 #define CALCULATOR_DOWN BUTTON_RC_VOL_DOWN
292 #define CALCULATOR_QUIT BUTTON_RC_REC
293 #define CALCULATOR_INPUT BUTTON_RC_PLAY
294 #define CALCULATOR_CALC BUTTON_RC_MODE
295 #define CALCULATOR_CLEAR BUTTON_RC_MENU
297 #define CALCULATOR_RC_QUIT BUTTON_REC
299 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
301 #define CALCULATOR_QUIT BUTTON_POWER
302 #define CALCULATOR_CLEAR BUTTON_MENU
304 #elif CONFIG_KEYPAD == IAUDIO67_PAD
306 #define CALCULATOR_LEFT BUTTON_LEFT
307 #define CALCULATOR_RIGHT BUTTON_RIGHT
308 #define CALCULATOR_UP BUTTON_VOLUP
309 #define CALCULATOR_DOWN BUTTON_VOLDOWN
310 #define CALCULATOR_QUIT BUTTON_POWER
311 #define CALCULATOR_INPUT BUTTON_PLAY
312 #define CALCULATOR_CALC BUTTON_MENU
313 #define CALCULATOR_CLEAR BUTTON_STOP
315 #define CALCULATOR_RC_QUIT (BUTTON_MENU|BUTTON_PLAY)
317 #elif (CONFIG_KEYPAD == CREATIVEZVM_PAD)
319 #define CALCULATOR_LEFT BUTTON_LEFT
320 #define CALCULATOR_RIGHT BUTTON_RIGHT
321 #define CALCULATOR_UP BUTTON_UP
322 #define CALCULATOR_DOWN BUTTON_DOWN
323 #define CALCULATOR_QUIT BUTTON_BACK
324 #define CALCULATOR_INPUT BUTTON_SELECT
325 #define CALCULATOR_CALC BUTTON_MENU
326 #define CALCULATOR_CLEAR BUTTON_PLAY
328 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
330 #define CALCULATOR_LEFT BUTTON_LEFT
331 #define CALCULATOR_RIGHT BUTTON_RIGHT
332 #define CALCULATOR_UP BUTTON_UP
333 #define CALCULATOR_DOWN BUTTON_DOWN
334 #define CALCULATOR_QUIT BUTTON_POWER
335 #define CALCULATOR_INPUT BUTTON_SELECT
336 #define CALCULATOR_CALC BUTTON_MENU
337 #define CALCULATOR_CLEAR BUTTON_VIEW
339 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
341 #define CALCULATOR_LEFT BUTTON_PREV
342 #define CALCULATOR_RIGHT BUTTON_NEXT
343 #define CALCULATOR_UP BUTTON_UP
344 #define CALCULATOR_DOWN BUTTON_DOWN
345 #define CALCULATOR_QUIT BUTTON_POWER
346 #define CALCULATOR_INPUT BUTTON_PLAY
347 #define CALCULATOR_CALC BUTTON_MENU
348 #define CALCULATOR_CLEAR BUTTON_RIGHT
350 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
352 #define CALCULATOR_QUIT BUTTON_POWER
353 #define CALCULATOR_CLEAR BUTTON_MENU
355 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
356 #define CALCULATOR_QUIT BUTTON_POWER
358 #elif CONFIG_KEYPAD == MROBE500_PAD
359 #define CALCULATOR_QUIT BUTTON_POWER
361 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
363 #define CALCULATOR_LEFT BUTTON_LEFT
364 #define CALCULATOR_RIGHT BUTTON_RIGHT
365 #define CALCULATOR_UP BUTTON_UP
366 #define CALCULATOR_DOWN BUTTON_DOWN
367 #define CALCULATOR_QUIT BUTTON_REC
368 #define CALCULATOR_INPUT BUTTON_PLAY
369 #define CALCULATOR_CALC BUTTON_FFWD
370 #define CALCULATOR_CLEAR BUTTON_REW
372 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
374 #define CALCULATOR_LEFT BUTTON_PREV
375 #define CALCULATOR_RIGHT BUTTON_NEXT
376 #define CALCULATOR_UP BUTTON_UP
377 #define CALCULATOR_DOWN BUTTON_DOWN
378 #define CALCULATOR_QUIT BUTTON_REC
379 #define CALCULATOR_INPUT BUTTON_OK
380 #define CALCULATOR_CALC BUTTON_PLAY
381 #define CALCULATOR_CLEAR BUTTON_CANCEL
383 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
384 #define CALCULATOR_LEFT BUTTON_PREV
385 #define CALCULATOR_RIGHT BUTTON_NEXT
386 #define CALCULATOR_QUIT (BUTTON_REC|BUTTON_PLAY)
387 #define CALCULATOR_INPUT BUTTON_SELECT
388 #define CALCULATOR_CALC BUTTON_PLAY
390 #else
391 #error No keymap defined!
392 #endif
394 #ifdef HAVE_TOUCHSCREEN
395 #ifndef CALCULATOR_LEFT
396 #define CALCULATOR_LEFT BUTTON_MIDLEFT
397 #endif
398 #ifndef CALCULATOR_RIGHT
399 #define CALCULATOR_RIGHT BUTTON_MIDRIGHT
400 #endif
401 #ifndef CALCULATOR_UP
402 #define CALCULATOR_UP BUTTON_TOPMIDDLE
403 #endif
404 #ifndef CALCULATOR_DOWN
405 #define CALCULATOR_DOWN BUTTON_BOTTOMMIDDLE
406 #endif
407 #ifndef CALCULATOR_CALC
408 #define CALCULATOR_CALC BUTTON_BOTTOMRIGHT
409 #endif
410 #ifndef CALCULATOR_INPUT
411 #define CALCULATOR_INPUT BUTTON_CENTER
412 #endif
413 #ifndef CALCULATOR_CLEAR
414 #define CALCULATOR_CLEAR BUTTON_TOPRIGHT
415 #endif
417 #include "lib/pluginlib_touchscreen.h"
418 static struct ts_raster calc_raster = { X_0_POS, Y_1_POS,
419 BUTTON_COLS*REC_WIDTH, BUTTON_ROWS*REC_HEIGHT, REC_WIDTH, REC_HEIGHT };
420 #endif
422 enum {
423 basicButtons,
424 sciButtons
425 } buttonGroup;
427 unsigned char* buttonChar[2][5][5] = {
428 { { "MR" , "M+" , "2nd" , "CE" , "C" },
429 { "7" , "8" , "9" , "/" , "sqr" },
430 { "4" , "5" , "6" , "*" , "x^2" },
431 { "1" , "2" , "3" , "-" , "1/x" },
432 { "0" , "+/-", "." , "+" , "=" } },
434 { { "n!" , "PI" , "1st" , "sin" , "asi" },
435 { "7" , "8" , "9" , "cos" , "aco" },
436 { "4" , "5" , "6" , "tan" , "ata" },
437 { "1" , "2" , "3" , "ln" , "e^x" },
438 { "0" , "+/-", "." , "log" , "x^y" } }
441 enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C ,
442 btn_7 , btn_8 , btn_9 , btn_div , btn_sqr ,
443 btn_4 , btn_5 , btn_6 , btn_time , btn_square ,
444 btn_1 , btn_2 , btn_3 , btn_minus , btn_rec ,
445 btn_0 , btn_sign , btn_dot , btn_add , btn_equal
448 enum { sci_fac, sci_pi , sci_sci , sci_sin , sci_asin ,
449 sci_7 , sci_8 , sci_9 , sci_cos , sci_acos ,
450 sci_4 , sci_5 , sci_6 , sci_tan , sci_atan ,
451 sci_1 , sci_2 , sci_3 , sci_ln , sci_exp ,
452 sci_0 , sci_sign , sci_dot , sci_log , sci_xy
455 #define MINIMUM 0.000000000001 /* e-12 */
456 /* ^ ^ ^ ^ */
457 /* 123456789abcdef */
459 #define DIGITLEN 10 /* must <= 10 */
460 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
461 /* 0.000 00000 0001 */
462 /* ^ ^ ^ ^ ^ ^ */
463 /* DIGITLEN 12345 6789a bcdef */
464 /* power 12 34567 89abc def */
465 /* 10^- 123 45678 9abcd ef */
467 unsigned char buf[19];/* 18 bytes of output line,
468 buf[0] is operator
469 buf[1] = 'M' if memTemp is not 0
470 buf[2] = ' '
472 if SCIENTIFIC_FORMAT
473 buf[2]-buf[12] or buf[3]-buf[13] = result;
474 format X.XXXXXXXX
475 buf[13] or buf[14] -buf[17] = power;
476 format eXXX or e-XXX
477 else
478 buf[3]-buf[6] = ' ';
479 buf[7]-buf[17] = result;
481 buf[18] = '\0' */
483 unsigned char typingbuf[DIGITLEN+2];/* byte 0 is sign or ' ',
484 byte 1~DIGITLEN are num and '.'
485 byte (DIGITLEN+1) is '\0' */
486 unsigned char* typingbufPointer = typingbuf;
488 double result = 0; /* main operand, format 0.xxxxx */
489 int power = 0; /* 10^power */
490 double modifier = 0.1; /* position of next input */
491 double operand = 0; /* second operand, format 0.xxxxx */
492 int operandPower = 0; /* 10^power of second operand */
493 char oper = ' '; /* operators: + - * / */
494 bool operInputted = false; /* false: do calculation first and
495 replace current oper
496 true: just replace current oper */
498 double memTemp = 0; /* temp memory */
499 int memTempPower = 0; /* 10^^power of memTemp */
501 int btn_row, btn_col; /* current position index for button */
502 int prev_btn_row, prev_btn_col; /* previous cursor position */
503 #define CAL_BUTTON (btn_row*5+btn_col)
505 int btn = BUTTON_NONE;
506 int lastbtn = BUTTON_NONE;
508 /* Status of calculator */
509 enum {cal_normal, /* 0, normal status, display result */
510 cal_typing, /* 1, currently typing, dot hasn't been typed */
511 cal_dotted, /* 2, currently typing, dot already has been typed. */
512 cal_error,
513 cal_exit,
514 cal_toDo
515 } calStatus;
517 /* constant table for CORDIC algorithm */
518 double cordicTable[51][2]= {
519 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
520 {1e+00, 7.853981633974483e-01},
521 {5e-01, 4.636476090008061e-01},
522 {2.5e-01, 2.449786631268641e-01},
523 {1.25e-01, 1.243549945467614e-01},
524 {6.25e-02, 6.241880999595735e-02},
525 {3.125e-02, 3.123983343026828e-02},
526 {1.5625e-02, 1.562372862047683e-02},
527 {7.8125e-03, 7.812341060101111e-03},
528 {3.90625e-03, 3.906230131966972e-03},
529 {1.953125e-03, 1.953122516478819e-03},
530 {9.765625e-04, 9.765621895593195e-04},
531 {4.8828125e-04, 4.882812111948983e-04},
532 {2.44140625e-04, 2.441406201493618e-04},
533 {1.220703125e-04, 1.220703118936702e-04},
534 {6.103515625e-05, 6.103515617420877e-05},
535 {3.0517578125e-05, 3.051757811552610e-05},
536 {1.52587890625e-05, 1.525878906131576e-05},
537 {7.62939453125e-06, 7.629394531101970e-06},
538 {3.814697265625e-06, 3.814697265606496e-06},
539 {1.9073486328125e-06, 1.907348632810187e-06},
540 {9.5367431640625e-07, 9.536743164059608e-07},
541 {4.76837158203125e-07, 4.768371582030888e-07},
542 {2.384185791015625e-07, 2.384185791015580e-07},
543 {1.1920928955078125e-07, 1.192092895507807e-07},
544 {5.9604644775390625e-08, 5.960464477539055e-08},
545 {2.98023223876953125e-08, 2.980232238769530e-08},
546 {1.490116119384765625e-08, 1.490116119384765e-08},
547 {7.450580596923828125e-09, 7.450580596923828e-09},
548 {3.7252902984619140625e-09, 3.725290298461914e-09},
549 {1.86264514923095703125e-09, 1.862645149230957e-09},
550 {9.31322574615478515625e-10, 9.313225746154785e-10},
551 {4.656612873077392578125e-10, 4.656612873077393e-10},
552 {2.3283064365386962890625e-10, 2.328306436538696e-10},
553 {1.16415321826934814453125e-10, 1.164153218269348e-10},
554 {5.82076609134674072265625e-11, 5.820766091346741e-11},
555 {2.910383045673370361328125e-11, 2.910383045673370e-11},
556 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
557 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
558 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
559 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
560 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
561 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
562 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
563 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
564 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
565 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
566 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
567 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
568 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
569 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
570 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
573 void doMultiple(double* operandOne, int* powerOne,
574 double operandTwo, int powerTwo);
575 void doAdd (double* operandOne, int* powerOne,
576 double operandTwo, int powerTwo);
577 void printResult(void);
578 void formatResult(void);
579 void oneOperand(void);
581 void drawLines(void);
582 void drawButtons(int group);
584 /* -----------------------------------------------------------------------
585 Handy funtions
586 ----------------------------------------------------------------------- */
587 void cleartypingbuf(void)
589 int k;
590 for( k=1; k<=(DIGITLEN+1); k++)
591 typingbuf[k] = 0;
592 typingbuf[0] = ' ';
593 typingbufPointer = typingbuf+1;
595 void clearbuf(void)
597 int k;
598 for(k=0;k<18;k++)
599 buf[k]=' ';
600 buf[18] = 0;
602 void clearResult(void)
604 result = 0;
605 power = 0;
606 modifier = 0.1;
609 void clearInput(void)
611 calStatus = cal_normal;
612 clearResult();
613 cleartypingbuf();
614 rb->lcd_clear_display();
615 drawButtons(buttonGroup);
616 drawLines();
619 void clearOperand(void)
621 operand = 0;
622 operandPower = 0;
625 void clearMemTemp(void)
627 memTemp = 0;
628 memTempPower = 0;
631 void clearOper(void)
633 oper = ' ';
634 operInputted = false;
637 void clearMem(void)
639 clearInput();
640 clearMemTemp();
641 clearOperand();
642 clearOper();
643 btn = BUTTON_NONE;
646 void switchOperands(void)
648 double tempr = operand;
649 int tempp = operandPower;
650 operand = result;
651 operandPower = power;
652 result = tempr;
653 power = tempp;
656 void drawLines(void)
658 int i;
659 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS-1);
660 for (i = 0; i < 5 ; i++)
661 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS+i*REC_HEIGHT);
662 for (i = 0; i < 4 ; i++)
663 rb->lcd_vline(X_1_POS+i*REC_WIDTH, Y_1_POS, LCD_HEIGHT);
666 void drawButtons(int group)
668 int i, j, w, h;
669 for (i = 0; i <= 4; i++){
670 for (j = 0; j <= 4; j++){
671 rb->lcd_getstringsize( buttonChar[group][i][j],&w,&h);
672 if (i == btn_row && j == btn_col) /* selected item */
673 rb->lcd_set_drawmode(DRMODE_SOLID);
674 else
675 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
676 rb->lcd_fillrect( X_0_POS + j*REC_WIDTH,
677 Y_1_POS + i*REC_HEIGHT,
678 REC_WIDTH, REC_HEIGHT+1);
679 if (i == btn_row && j == btn_col) /* selected item */
680 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
681 else
682 rb->lcd_set_drawmode(DRMODE_SOLID);
683 rb->lcd_putsxy( X_0_POS + j*REC_WIDTH + (REC_WIDTH - w)/2,
684 Y_1_POS + i*REC_HEIGHT + (REC_HEIGHT - h)/2 + 1,
685 buttonChar[group][i][j] );
688 rb->lcd_set_drawmode(DRMODE_SOLID);
691 /* -----------------------------------------------------------------------
692 Initiate calculator
693 ----------------------------------------------------------------------- */
694 void cal_initial (void)
696 int w,h;
698 rb->lcd_getstringsize("2nd",&w,&h);
699 if (w > REC_WIDTH || h > REC_HEIGHT)
700 rb->lcd_setfont(FONT_SYSFIXED);
702 rb->lcd_clear_display();
704 #ifdef CALCULATOR_OPERATORS
705 /* basic operators are available through separate button */
706 buttonGroup = sciButtons;
707 #else
708 buttonGroup = basicButtons;
709 #endif
711 /* initially, invert button "5" */
712 btn_row = 2;
713 btn_col = 1;
714 prev_btn_row = btn_row;
715 prev_btn_col = btn_col;
716 drawButtons(buttonGroup);
717 drawLines();
718 rb->lcd_update();
720 /* initial mem and output display*/
721 clearMem();
722 printResult();
724 /* clear button queue */
725 rb->button_clear_queue();
728 /* -----------------------------------------------------------------------
729 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
730 in it's private case for sqrt.
731 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
732 ----------------------------------------------------------------------- */
733 double mySqrt(double square)
735 int k = 0;
736 double temp = 0;
737 double root= ABS(square+1)/2;
739 while( ABS(root - temp) > MINIMUM ){
740 temp = root;
741 root = (square/temp + temp)/2;
742 k++;
743 if (k>10000) return 0;
746 return root;
748 /* -----------------------------------------------------------------------
749 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
750 transcendFunc can do sin,cos,log,exp
751 input parameter is angle
752 ----------------------------------------------------------------------- */
753 void transcendFunc(char* func, double* tt, int* ttPower)
755 double t = (*tt)*M_PI/180; int tPower = *ttPower;
756 int sign = 1;
757 int n = 50; /* n <=50, tables are all <= 50 */
758 int j;
759 double x,y,z,xt,yt,zt;
761 if (tPower < -998) {
762 calStatus = cal_normal;
763 return;
765 if (tPower > 8) {
766 calStatus = cal_error;
767 return;
769 *ttPower = 0;
770 calStatus = cal_normal;
772 if( func[0] =='s' || func[0] =='S'|| func[0] =='t' || func[0] =='T')
773 sign = SIGN(t);
774 else {
775 /* if( func[0] =='c' || func[0] =='C') */
776 sign = 1;
778 t = ABS(t);
780 while (tPower > 0){
781 t *= 10;
782 tPower--;
784 while (tPower < 0) {
785 t /= 10;
786 tPower++;
788 j = 0;
789 while (t > j*M_TWOPI) {j++;}
790 t -= (j-1)*M_TWOPI;
791 if (M_PI_2 < t && t < 3*M_PI_2){
792 t = M_PI - t;
793 if (func[0] =='c' || func[0] =='C')
794 sign = -1;
795 else if (func[0] =='t' || func[0] =='T')
796 t*=-1;
798 else if ( 3*M_PI_2 <= t && t <= M_TWOPI)
799 t -= M_TWOPI;
801 x = 0.60725293500888; y = 0; z = t;
802 for (j=1;j<n+2;j++){
803 xt = x - SIGN(z) * y*cordicTable[j-1][0];
804 yt = y + SIGN(z) * x*cordicTable[j-1][0];
805 zt = z - SIGN(z) * cordicTable[j-1][1];
806 x = xt;
807 y=yt;
808 z=zt;
810 if( func[0] =='s' || func[0] =='S') {
811 *tt = sign*y;
812 return;
814 else if( func[0] =='c' || func[0] =='C') {
815 *tt = sign*x;
816 return;
818 else /*if( func[0] =='t' || func[0] =='T')*/ {
819 if(t==M_PI_2||t==-M_PI_2){
820 calStatus = cal_error;
821 return;
823 else{
824 *tt = sign*(y/x);
825 return;
830 /* -----------------------------------------------------------------------
831 add in scientific number format
832 ----------------------------------------------------------------------- */
833 void doAdd (double* operandOne, int* powerOne,
834 double operandTwo, int powerTwo)
836 if ( *powerOne >= powerTwo ){
837 if (*powerOne - powerTwo <= DIGITLEN+1){
838 while (powerTwo < *powerOne){
839 operandTwo /=10;
840 powerTwo++;
842 *operandOne += operandTwo;
844 /*do nothing if operandTwo is too small*/
846 else{
847 if (powerTwo - *powerOne <= DIGITLEN+1){
848 while(powerTwo > *powerOne){
849 *operandOne /=10;
850 (*powerOne)++;
852 (*operandOne) += operandTwo;
854 else{/* simply copy operandTwo if operandOne is too small */
855 *operandOne = operandTwo;
856 *powerOne = powerTwo;
860 /* -----------------------------------------------------------------------
861 multiple in scientific number format
862 ----------------------------------------------------------------------- */
863 void doMultiple(double* operandOne, int* powerOne,
864 double operandTwo, int powerTwo)
866 (*operandOne) *= operandTwo;
867 (*powerOne) += powerTwo;
870 /* -----------------------------------------------------------------------
871 Handles all one operand calculations
872 ----------------------------------------------------------------------- */
873 void oneOperand(void)
875 int k = 0;
876 if (buttonGroup == basicButtons){
877 switch(CAL_BUTTON){
878 case btn_sqr:
879 if (result<0)
880 calStatus = cal_error;
881 else{
882 if (power%2 == 1){
883 result = (mySqrt(result*10))/10;
884 power = (power+1) / 2;
886 else{
887 result = mySqrt(result);
888 power = power / 2;
890 calStatus = cal_normal;
892 break;
893 case btn_square:
894 power *= 2;
895 result *= result;
896 calStatus = cal_normal;
897 break;
899 case btn_rec:
900 if (result==0)
901 calStatus = cal_error;
902 else{
903 power = -power;
904 result = 1/result;
905 calStatus = cal_normal;
907 break;
908 default:
909 calStatus = cal_toDo;
910 break; /* just for the safety */
913 else{ /* sciButtons */
914 switch(CAL_BUTTON){
915 case sci_sin:
916 transcendFunc("sin", &result, &power);
917 break;
918 case sci_cos:
919 transcendFunc("cos", &result, &power);
920 break;
921 case sci_tan:
922 transcendFunc("tan", &result, &power);
923 break;
924 case sci_fac:
925 if (power<0 || power>8 || result<0 )
926 calStatus = cal_error;
927 else if(result == 0) {
928 result = 1;
929 power = 0;
931 else{
932 while(power > 0) {
933 result *= 10;
934 power--;
936 if ( ( result - (int)result) > MINIMUM )
937 calStatus = cal_error;
938 else {
939 k = result; result = 1;
940 while (k > 1){
941 doMultiple(&result, &power, k, 0);
942 formatResult();
943 k--;
945 calStatus = cal_normal;
948 break;
949 default:
950 calStatus = cal_toDo;
951 break; /* just for the safety */
957 /* -----------------------------------------------------------------------
958 Handles all two operands calculations
959 ----------------------------------------------------------------------- */
960 void twoOperands(void)
962 switch(oper){
963 case '-':
964 doAdd(&operand, &operandPower, -result, power);
965 break;
966 case '+':
967 doAdd(&operand, &operandPower, result, power);
968 break;
969 case '*':
970 doMultiple(&operand, &operandPower, result, power);
971 break;
972 case '/':
973 if ( ABS(result) > MINIMUM ){
974 doMultiple(&operand, &operandPower, 1/result, -power);
976 else
977 calStatus = cal_error;
978 break;
979 default: /* ' ' */
980 switchOperands(); /* counter switchOperands() below */
981 break;
982 } /* switch(oper) */
983 switchOperands();
984 clearOper();
987 /* First, increases *dimen1 by dimen1_delta modulo dimen1_modulo.
988 If dimen1 wraps, increases *dimen2 by dimen2_delta modulo dimen2_modulo.
990 static void move_with_wrap_and_shift(
991 int *dimen1, int dimen1_delta, int dimen1_modulo,
992 int *dimen2, int dimen2_delta, int dimen2_modulo)
994 bool wrapped = false;
996 *dimen1 += dimen1_delta;
997 if (*dimen1 < 0)
999 *dimen1 = dimen1_modulo - 1;
1000 wrapped = true;
1002 else if (*dimen1 >= dimen1_modulo)
1004 *dimen1 = 0;
1005 wrapped = true;
1008 if (wrapped)
1010 /* Make the dividend always positive to be sure about the result.
1011 Adding dimen2_modulo does not change it since we do it modulo. */
1012 *dimen2 = (*dimen2 + dimen2_modulo + dimen2_delta) % dimen2_modulo;
1016 /* -----------------------------------------------------------------------
1017 Print buttons when switching 1st and 2nd
1018 int group = {basicButtons, sciButtons}
1019 ----------------------------------------------------------------------- */
1020 void printButtonGroups(int group)
1022 drawButtons(group);
1023 drawLines();
1024 rb->lcd_update();
1026 /* -----------------------------------------------------------------------
1027 flash the currently marked button
1028 ----------------------------------------------------------------------- */
1029 void flashButton(void)
1031 int k, w, h;
1032 for (k=2;k>0;k--)
1034 rb->lcd_getstringsize( buttonChar[buttonGroup][btn_row][btn_col],&w,&h);
1035 rb->lcd_set_drawmode(DRMODE_SOLID|(k==1) ? 0 : DRMODE_INVERSEVID);
1036 rb->lcd_fillrect( X_0_POS + btn_col*REC_WIDTH + 1,
1037 Y_1_POS + btn_row*REC_HEIGHT + 1,
1038 REC_WIDTH - 1, REC_HEIGHT - 1);
1039 rb->lcd_putsxy( X_0_POS + btn_col*REC_WIDTH + (REC_WIDTH - w)/2,
1040 Y_1_POS + btn_row*REC_HEIGHT + (REC_HEIGHT - h)/2 +1,
1041 buttonChar[buttonGroup][btn_row][btn_col] );
1042 rb->lcd_update_rect( X_0_POS + btn_col*REC_WIDTH + 1,
1043 Y_1_POS + btn_row*REC_HEIGHT + 1,
1044 REC_WIDTH - 1, REC_HEIGHT - 1);
1046 if (k!= 1)
1047 rb->sleep(HZ/22);
1052 /* -----------------------------------------------------------------------
1053 pos is the position that needs animation. pos = [1~18]
1054 ----------------------------------------------------------------------- */
1055 void deleteAnimation(int pos)
1057 int k, w, h, x;
1058 if (pos<1 || pos >18)
1059 return;
1061 rb->lcd_getstringsize("0", &w, &h);
1062 x = (pos==1? 4: LCD_WIDTH - 4 - w);
1064 for (k=0;k<4;k++){
1065 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1066 rb->lcd_fillrect(x, Y_1_POS - h -1, w, h);
1067 rb->lcd_set_drawmode(DRMODE_SOLID);
1068 rb->lcd_fillrect(x + (w*k)/8, Y_1_POS - h -1 + (h*k)/8,
1069 (w*(4-k))/4, (h*(4-k))/4);
1070 rb->lcd_update_rect(x, Y_1_POS - h -1, w, h);
1071 rb->sleep(HZ/32);
1075 /* -----------------------------------------------------------------------
1076 result may be one of these formats:
1078 xxxx.xxxx
1079 0.xxxx
1080 0.0000xxxx
1082 formatResult() change result to standard format: 0.xxxx
1083 if result is close to 0, let it be 0;
1084 if result is close to 1, let it be 0.1 and power++;
1085 ----------------------------------------------------------------------- */
1086 void formatResult(void)
1088 int resultsign = SIGN(result);
1089 result = ABS(result);
1090 if(result > MINIMUM ){ /* doesn't check power, might have problem
1091 input wouldn't,
1092 + - * / of two formatted number wouldn't.
1093 only a calculation that makes a formatted
1094 number (0.xxxx) less than MINIMUM in only
1095 one operation */
1097 if (result<1){
1098 while( (int)(result*10) == 0 ){
1099 result *= 10;
1100 power--;
1101 modifier *= 10;
1104 else{ /* result >= 1 */
1105 while( (int)result != 0 ){
1106 result /= 10;
1107 power++;
1108 modifier /= 10;
1110 } /* if result<1 */
1112 if (result > (1-MINIMUM)){
1113 result = 0.1;
1114 power++;
1115 modifier /= 10;
1117 result *= resultsign;
1119 else {
1120 result = 0;
1121 power = 0;
1122 modifier = 0.1;
1126 /* -----------------------------------------------------------------------
1127 result2typingbuf() outputs standard format result to typingbuf.
1128 case SCIENTIFIC_FORMAT, let temppower = 1;
1129 case temppower > 0: print '.' in the middle
1130 case temppower <= 0: print '.' in the begining
1131 ----------------------------------------------------------------------- */
1132 void result2typingbuf(void)
1134 bool haveDot = false;
1135 char tempchar = 0;
1136 int k;
1137 double tempresult = ABS(result); /* positive num makes things simple */
1139 int temppower;
1140 double tempmodifier = 1;
1141 int count;
1143 if(SCIENTIFIC_FORMAT)
1144 temppower = 1; /* output x.xxxx format */
1145 else
1146 temppower = power;
1148 cleartypingbuf();
1150 if(tempresult < MINIMUM){ /* if 0,faster display and avoid complication*/
1151 typingbuf[0] = ' ';
1152 typingbuf[1] = '0';
1154 else{ /* tempresult > 0 */
1155 typingbuf[0] = (SIGN(result)<0)?'-':' ';
1157 typingbufPointer = typingbuf;
1158 if(temppower > 0){
1159 for (k = 0; k<DIGITLEN+1 ; k++){
1160 typingbufPointer++;
1161 if(temppower || *(typingbufPointer-1) == '.'){
1162 count = 0;
1163 tempmodifier = tempmodifier/10;
1164 while( (tempresult-tempmodifier*count) >
1165 (tempmodifier-MINIMUM)){
1166 count++;
1168 tempresult -= tempmodifier*count;
1169 tempresult = ABS(tempresult);
1170 temppower-- ;
1171 *typingbufPointer = count + '0';
1173 else{ /* temppower == 0 */
1174 *typingbufPointer = '.';
1175 haveDot = true;
1177 } /* for */
1179 else{
1180 haveDot = true;
1181 typingbufPointer++; *typingbufPointer = '0';
1182 typingbufPointer++; *typingbufPointer = '.';
1183 for (k = 2; k<DIGITLEN+1 ; k++){
1184 typingbufPointer++;
1185 count = 0;
1186 if ( (-temppower) < (k-1)){
1187 tempmodifier = tempmodifier/10;
1188 while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){
1189 count++;
1192 tempresult -= tempmodifier*count;
1193 tempresult = ABS(tempresult);
1194 temppower-- ;
1196 *typingbufPointer = count + '0';
1199 /* now, typingbufPointer = typingbuf + 16 */
1200 /* backward strip off 0 and '.' */
1201 if (haveDot){
1202 while( (*typingbufPointer == '0') || (*typingbufPointer == '.')){
1203 tempchar = *typingbufPointer;
1204 *typingbufPointer = 0;
1205 typingbufPointer--;
1206 if (tempchar == '.') break;
1209 typingbuf[DIGITLEN+1] = 0;
1210 } /* else tempresult > 0 */
1213 /* -----------------------------------------------------------------------
1214 printResult() generates LCD display.
1215 ----------------------------------------------------------------------- */
1216 void printResult(void)
1218 int k, w, h;
1220 char operbuf[3] = {0, 0, 0};
1222 switch_Status:
1223 switch(calStatus){
1224 case cal_exit:
1225 rb->lcd_clear_display();
1226 rb->splash(HZ/3, "Bye now!");
1227 break;
1228 case cal_error:
1229 clearbuf();
1230 rb->snprintf(buf, 19, "%18s","Error");
1231 break;
1232 case cal_toDo:
1233 clearbuf();
1234 rb->snprintf(buf, 19, "%18s","Coming soon ^_* ");
1235 break;
1237 case cal_normal:
1238 formatResult();
1240 if( power > 1000 ){ /* power -1 > 999 */
1241 calStatus = cal_error;
1242 goto switch_Status;
1244 if (power < -998 ) /* power -1 < -999 */
1245 clearResult(); /* too small, let it be 0 */
1247 result2typingbuf();
1248 clearbuf();
1250 operbuf[0] = oper;
1251 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1252 operbuf[2] = '\0';
1254 if(SCIENTIFIC_FORMAT){
1255 /* output format: X.XXXX eXXX */
1256 if(power > -98){ /* power-1 >= -99, eXXX or e-XX */
1257 rb->snprintf(buf, 12, "%11s",typingbuf);
1258 for(k=11;k<=14;k++) buf[k] = ' ';
1259 cleartypingbuf();
1260 rb->snprintf(typingbuf, 5, "e%d",power-1);
1261 rb->snprintf(buf+11, 5, "%4s",typingbuf);
1263 else{ /* power-1 <= -100, e-XXX */
1264 rb->snprintf(buf, 12, "%11s",typingbuf);
1265 rb->snprintf(buf+11, 6, "e%d",power-1);
1268 else{
1269 rb->snprintf(buf, 12, "%11s",typingbuf);
1270 } /* if SCIENTIFIC_FORMAT */
1271 break;
1272 case cal_typing:
1273 case cal_dotted:
1274 clearbuf();
1275 operbuf[0] = oper;
1276 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1277 rb->snprintf(buf, 12, "%11s",typingbuf);
1278 break;
1282 rb->lcd_getstringsize(buf, &w, &h);
1283 rb->screen_clear_area(rb->screens[0], 0, 0, LCD_WIDTH, Y_1_POS - 1);
1284 rb->lcd_putsxy(4, Y_1_POS - h -1, operbuf);
1285 rb->lcd_putsxy(LCD_WIDTH - w - 4, Y_1_POS - h -1, buf);
1286 rb->lcd_update_rect(0, 1, LCD_WIDTH, Y_1_POS);
1289 /* -----------------------------------------------------------------------
1290 Process typing buttons: 1-9, '.', sign
1291 main operand "result" and typingbuf are processed seperately here.
1292 ----------------------------------------------------------------------- */
1293 void typingProcess(void){
1294 switch( CAL_BUTTON ){
1295 case btn_sign:
1296 if (calStatus == cal_typing ||
1297 calStatus == cal_dotted)
1298 typingbuf[0] = (typingbuf[0]=='-')?' ':'-';
1299 result = -result;
1300 break;
1301 case btn_dot:
1302 operInputted = false;
1303 switch(calStatus){
1304 case cal_normal:
1305 clearInput();
1306 *typingbufPointer = '0';
1307 typingbufPointer++;
1308 case cal_typing:
1309 calStatus = cal_dotted;
1310 *typingbufPointer = '.';
1311 if (typingbufPointer != typingbuf+DIGITLEN+1)
1312 typingbufPointer++;
1313 break;
1314 default: /* cal_dotted */
1315 break;
1317 break;
1318 default: /* 0-9 */
1319 operInputted = false;
1320 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1321 switch(calStatus){
1322 case cal_normal:
1323 if(CAL_BUTTON == btn_0 )
1324 break; /* first input is 0, ignore */
1325 clearInput();
1326 /*no operator means start a new calculation*/
1327 if (oper ==' ')
1328 clearOperand();
1329 calStatus = cal_typing;
1330 /* go on typing, no break */
1331 case cal_typing:
1332 case cal_dotted:
1333 switch(CAL_BUTTON){
1334 case btn_0:
1335 *typingbufPointer = '0';
1336 break;
1337 default:
1338 *typingbufPointer=(7+btn_col-3*(btn_row-1))+ '0';
1339 break;
1341 if (typingbufPointer!=typingbuf+DIGITLEN+1){
1342 typingbufPointer++;
1344 {/* result processing */
1345 if (calStatus == cal_typing) power++;
1346 if (CAL_BUTTON != btn_0)
1347 result= result +
1348 SIGN(result)*
1349 (7+btn_col-3*(btn_row-1))*modifier;
1350 modifier /= 10;
1353 else /* last byte always '\0' */
1354 *typingbufPointer = 0;
1355 break;
1356 default: /* cal_error, cal_exit */
1357 break;
1359 break; /* default, 0-9 */
1360 } /* switch( CAL_BUTTON ) */
1363 /* -----------------------------------------------------------------------
1364 Handle delete operation
1365 main operand "result" and typingbuf are processed seperately here.
1366 ----------------------------------------------------------------------- */
1367 void doDelete(void){
1368 deleteAnimation(18);
1369 switch(calStatus){
1370 case cal_dotted:
1371 if (*(typingbufPointer-1) == '.'){
1372 /* if dotted and deleting '.',
1373 change status and delete '.' below */
1374 calStatus = cal_typing;
1376 else{ /* if dotted and not deleting '.',
1377 power stays */
1378 power++; /* counter "power--;" below */
1380 case cal_typing:
1381 typingbufPointer--;
1383 {/* result processing */ /* 0-9, '.' */
1384 /* if deleting '.', do nothing */
1385 if ( *typingbufPointer != '.'){
1386 power--;
1387 modifier *= 10;
1388 result = result - SIGN(result)*
1389 ((*typingbufPointer)- '0')*modifier;
1393 *typingbufPointer = 0;
1395 /* if (only one digit left and it's 0)
1396 or no digit left, change status*/
1397 if ( typingbufPointer == typingbuf+1 ||
1398 ( typingbufPointer == typingbuf+2 &&
1399 *(typingbufPointer-1) == '0' ))
1400 calStatus = cal_normal;
1401 break;
1402 default: /* normal, error, exit */
1403 break;
1406 /* -----------------------------------------------------------------------
1407 Handle buttons on basic screen
1408 ----------------------------------------------------------------------- */
1409 void basicButtonsProcess(void){
1410 switch (btn) {
1411 case CALCULATOR_INPUT:
1412 if (calStatus == cal_error && (CAL_BUTTON != btn_C) ) break;
1413 flashButton();
1414 switch( CAL_BUTTON ){
1415 case btn_MR:
1416 operInputted = false;
1417 result = memTemp; power = memTempPower;
1418 calStatus = cal_normal;
1419 break;
1420 case btn_M:
1421 formatResult();
1422 if (memTemp > MINIMUM)
1423 doAdd(&memTemp, &memTempPower, result, power);
1424 else {
1425 /* if result is too small and memTemp = 0,
1426 doAdd will not add */
1427 memTemp = result;
1428 memTempPower = power;
1430 calStatus = cal_normal;
1431 break;
1433 case btn_C: clearMem(); break;
1434 case btn_CE: clearInput(); break;
1436 case btn_bas:
1437 buttonGroup = sciButtons;
1438 printButtonGroups(buttonGroup);
1439 break;
1441 /* one operand calculation, may be changed to
1442 like sin, cos, log, etc */
1443 case btn_sqr:
1444 case btn_square:
1445 case btn_rec:
1446 formatResult(); /* not necessary, just for safty */
1447 oneOperand();
1448 break;
1450 case_btn_equal: /* F3 shortkey entrance */
1451 case btn_equal:
1452 formatResult();
1453 calStatus = cal_normal;
1454 operInputted = false;
1455 if (oper != ' ') twoOperands();
1456 break;
1458 case btn_div:
1459 case btn_time:
1460 case btn_minus:
1461 case btn_add:
1462 if(!operInputted) {twoOperands(); operInputted = true;}
1463 oper = buttonChar[basicButtons][btn_row][btn_col][0];
1464 #ifdef CALCULATOR_OPERATORS
1465 case_cycle_operators: /* F2 shortkey entrance */
1466 #endif
1467 calStatus = cal_normal;
1468 formatResult();
1469 operand = result;
1470 operandPower = power;
1472 break;
1474 case btn_sign:
1475 case btn_dot:
1476 default: /* 0-9 */
1477 typingProcess();
1478 break;
1479 } /* switch (CAL_BUTTON) */
1480 break;
1482 #ifdef CALCULATOR_OPERATORS
1483 case CALCULATOR_OPERATORS:
1484 if (calStatus == cal_error) break;
1485 if (!operInputted) {twoOperands(); operInputted = true;}
1486 switch (oper){
1487 case ' ':
1488 case '/': oper = '+'; flashButton(); break;
1489 case '+': oper = '-'; flashButton(); break;
1490 case '-': oper = '*'; flashButton(); break;
1491 case '*': oper = '/'; flashButton(); break;
1493 goto case_cycle_operators;
1494 break;
1495 #endif
1497 case CALCULATOR_CALC:
1498 if (calStatus == cal_error) break;
1499 flashButton();
1500 goto case_btn_equal;
1501 break;
1502 default: break;
1504 printResult();
1507 /* -----------------------------------------------------------------------
1508 Handle buttons on scientific screen
1509 ----------------------------------------------------------------------- */
1510 void sciButtonsProcess(void){
1511 switch (btn) {
1512 case CALCULATOR_INPUT:
1513 if (calStatus == cal_error && (CAL_BUTTON != sci_sci) ) break;
1514 flashButton();
1515 switch( CAL_BUTTON ){
1517 case sci_pi:
1518 result = M_PI; power = 0;
1519 calStatus = cal_normal;
1520 break;
1522 case sci_xy: break;
1524 case sci_sci:
1525 buttonGroup = basicButtons;
1526 printButtonGroups(basicButtons);
1527 break;
1529 case sci_fac:
1530 case sci_sin:
1531 case sci_asin:
1532 case sci_cos:
1533 case sci_acos:
1534 case sci_tan:
1535 case sci_atan:
1536 case sci_ln:
1537 case sci_exp:
1538 case sci_log:
1539 formatResult(); /* not necessary, just for safty */
1540 oneOperand();
1541 break;
1543 case btn_sign:
1544 case btn_dot:
1545 default: /* 0-9 */
1546 typingProcess();
1547 break;
1548 } /* switch (CAL_BUTTON) */
1549 break;
1551 #ifdef CALCULATOR_OPERATORS
1552 case CALCULATOR_OPERATORS:
1553 if (calStatus == cal_error) break;
1554 if (!operInputted) {twoOperands(); operInputted = true;}
1555 switch (oper){
1556 case ' ': oper = '+'; break;
1557 case '/': oper = '+'; deleteAnimation(1); break;
1558 case '+': oper = '-'; deleteAnimation(1); break;
1559 case '-': oper = '*'; deleteAnimation(1); break;
1560 case '*': oper = '/'; deleteAnimation(1); break;
1562 calStatus = cal_normal;
1563 formatResult();
1564 operand = result;
1565 operandPower = power;
1566 break;
1567 #endif
1569 case CALCULATOR_CALC:
1570 if (calStatus == cal_error) break;
1571 formatResult();
1572 calStatus = cal_normal;
1573 operInputted = false;
1574 if (oper != ' ') twoOperands();
1575 break;
1576 default: break;
1578 printResult();
1581 /* -----------------------------------------------------------------------
1582 move button index
1583 Invert display new button, invert back previous button
1584 ----------------------------------------------------------------------- */
1585 int handleButton(int button){
1586 switch(button)
1588 case CALCULATOR_INPUT:
1589 case CALCULATOR_CALC:
1590 #ifdef CALCULATOR_INPUT_CALC_PRE
1591 if (lastbtn != CALCULATOR_INPUT_CALC_PRE)
1592 break;
1593 /* no unconditional break; here! */
1594 #endif
1595 #ifdef CALCULATOR_OPERATORS
1596 case CALCULATOR_OPERATORS:
1597 #endif
1598 switch(buttonGroup){
1599 case basicButtons:
1600 basicButtonsProcess();
1601 break;
1602 case sciButtons:
1603 sciButtonsProcess();
1604 break;
1606 break;
1608 #ifdef CALCULATOR_CLEAR
1609 case CALCULATOR_CLEAR:
1610 switch(calStatus){
1611 case cal_typing:
1612 case cal_dotted:
1613 doDelete();
1614 break;
1615 default: /* cal_normal, cal_error, cal_exit */
1616 clearMem();
1617 break;
1619 printResult();
1620 break;
1621 #endif
1622 case CALCULATOR_LEFT:
1623 case CALCULATOR_LEFT | BUTTON_REPEAT:
1624 move_with_wrap_and_shift(
1625 &btn_col, -1, BUTTON_COLS,
1626 &btn_row, 0, BUTTON_ROWS);
1627 break;
1629 case CALCULATOR_RIGHT:
1630 case CALCULATOR_RIGHT | BUTTON_REPEAT:
1631 move_with_wrap_and_shift(
1632 &btn_col, 1, BUTTON_COLS,
1633 &btn_row, 0, BUTTON_ROWS);
1634 break;
1636 #ifdef CALCULATOR_UP
1637 case CALCULATOR_UP:
1638 case CALCULATOR_UP | BUTTON_REPEAT:
1639 move_with_wrap_and_shift(
1640 &btn_row, -1, BUTTON_ROWS,
1641 &btn_col, 0, BUTTON_COLS);
1642 break;
1643 #endif
1644 #ifdef CALCULATOR_DOWN
1645 case CALCULATOR_DOWN:
1646 case CALCULATOR_DOWN | BUTTON_REPEAT:
1647 move_with_wrap_and_shift(
1648 &btn_row, 1, BUTTON_ROWS,
1649 &btn_col, 0, BUTTON_COLS);
1650 break;
1651 #endif
1653 #ifdef CALCULATOR_UP_W_SHIFT
1654 case CALCULATOR_UP_W_SHIFT:
1655 case CALCULATOR_UP_W_SHIFT | BUTTON_REPEAT:
1656 move_with_wrap_and_shift(
1657 &btn_row, -1, BUTTON_ROWS,
1658 &btn_col, -1, BUTTON_COLS);
1659 break;
1660 #endif
1661 #ifdef CALCULATOR_DOWN_W_SHIFT
1662 case CALCULATOR_DOWN_W_SHIFT:
1663 case CALCULATOR_DOWN_W_SHIFT | BUTTON_REPEAT:
1664 move_with_wrap_and_shift(
1665 &btn_row, 1, BUTTON_ROWS,
1666 &btn_col, 1, BUTTON_COLS);
1667 break;
1668 #endif
1669 #ifdef CALCULATOR_LEFT_W_SHIFT
1670 case CALCULATOR_LEFT_W_SHIFT:
1671 case CALCULATOR_LEFT_W_SHIFT | BUTTON_REPEAT:
1672 move_with_wrap_and_shift(
1673 &btn_col, -1, BUTTON_COLS,
1674 &btn_row, -1, BUTTON_ROWS);
1675 break;
1676 #endif
1677 #ifdef CALCULATOR_RIGHT_W_SHIFT
1678 case CALCULATOR_RIGHT_W_SHIFT:
1679 case CALCULATOR_RIGHT_W_SHIFT | BUTTON_REPEAT:
1680 move_with_wrap_and_shift(
1681 &btn_col, 1, BUTTON_COLS,
1682 &btn_row, 1, BUTTON_ROWS);
1683 break;
1684 #endif
1685 #ifdef CALCULATOR_RC_QUIT
1686 case CALCULATOR_RC_QUIT:
1687 #endif
1688 case CALCULATOR_QUIT:
1689 return -1;
1692 return 0;
1694 prev_btn_row = btn_row;
1695 prev_btn_col = btn_col;
1698 /* -----------------------------------------------------------------------
1699 Main();
1700 ----------------------------------------------------------------------- */
1701 enum plugin_status plugin_start(const void* parameter)
1703 (void)parameter;
1705 /* now go ahead and have fun! */
1707 #ifdef HAVE_TOUCHSCREEN
1708 rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
1709 #endif
1711 cal_initial();
1713 while (calStatus != cal_exit ) {
1714 btn = rb->button_get_w_tmo(HZ/2);
1715 #ifdef HAVE_TOUCHSCREEN
1716 if(btn & BUTTON_TOUCHSCREEN)
1718 struct ts_raster_result res;
1719 if(touchscreen_map_raster(&calc_raster, rb->button_get_data() >> 16,
1720 rb->button_get_data() & 0xffff, &res) == 1)
1722 btn_row = res.y;
1723 btn_col = res.x;
1724 drawButtons(buttonGroup);
1725 drawLines();
1727 rb->lcd_update();
1729 prev_btn_row = btn_row;
1730 prev_btn_col = btn_col;
1731 if(btn & BUTTON_REL)
1733 btn = CALCULATOR_INPUT;
1734 switch(buttonGroup){
1735 case basicButtons:
1736 basicButtonsProcess();
1737 break;
1738 case sciButtons:
1739 sciButtonsProcess();
1740 break;
1742 btn = BUTTON_TOUCHSCREEN;
1746 #endif
1747 if (handleButton(btn) == -1)
1749 calStatus = cal_exit;
1750 printResult();
1752 else
1754 drawButtons(buttonGroup);
1755 drawLines();
1758 rb->lcd_update();
1760 if(rb->default_event_handler(btn) == SYS_USB_CONNECTED)
1761 return PLUGIN_USB_CONNECTED;
1763 if (btn != BUTTON_NONE)
1764 lastbtn = btn;
1765 } /* while (calStatus != cal_exit ) */
1767 rb->button_clear_queue();
1768 return PLUGIN_OK;
1771 #endif /* #ifdef HAVE_LCD_BITMAP */