Fuzev2: revert r25741 and r25746
[kugel-rb.git] / apps / plugins / calculator.c
blob942f031890a3bdc2daf933ea7d851f971b06e1ca
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)
221 #define CALCULATOR_CLEAR BUTTON_HOME
224 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
225 #define CALCULATOR_LEFT BUTTON_LEFT
226 #define CALCULATOR_RIGHT BUTTON_RIGHT
227 #define CALCULATOR_UP BUTTON_UP
228 #define CALCULATOR_DOWN BUTTON_DOWN
229 #define CALCULATOR_QUIT BUTTON_POWER
230 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
231 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
232 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
233 #define CALCULATOR_CLEAR BUTTON_HOME
235 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
236 #define CALCULATOR_LEFT BUTTON_LEFT
237 #define CALCULATOR_RIGHT BUTTON_RIGHT
238 #define CALCULATOR_UP BUTTON_UP
239 #define CALCULATOR_DOWN BUTTON_DOWN
240 #define CALCULATOR_QUIT BUTTON_POWER
241 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
242 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
243 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
244 #define CALCULATOR_CLEAR (BUTTON_SELECT|BUTTON_UP)
246 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
248 #define CALCULATOR_LEFT BUTTON_LEFT
249 #define CALCULATOR_RIGHT BUTTON_RIGHT
250 #define CALCULATOR_UP BUTTON_SCROLL_UP
251 #define CALCULATOR_DOWN BUTTON_SCROLL_DOWN
252 #define CALCULATOR_QUIT BUTTON_POWER
253 #define CALCULATOR_INPUT_CALC_PRE BUTTON_PLAY
254 #define CALCULATOR_INPUT (BUTTON_PLAY | BUTTON_REL)
255 #define CALCULATOR_CALC (BUTTON_PLAY | BUTTON_REPEAT)
256 #define CALCULATOR_CLEAR BUTTON_REW
258 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
260 #define CALCULATOR_LEFT BUTTON_LEFT
261 #define CALCULATOR_RIGHT BUTTON_RIGHT
262 #define CALCULATOR_UP BUTTON_UP
263 #define CALCULATOR_DOWN BUTTON_DOWN
264 #define CALCULATOR_QUIT BUTTON_BACK
265 #define CALCULATOR_INPUT BUTTON_SELECT
266 #define CALCULATOR_CALC BUTTON_MENU
267 #define CALCULATOR_CLEAR BUTTON_PLAY
269 #elif (CONFIG_KEYPAD == MROBE100_PAD)
271 #define CALCULATOR_LEFT BUTTON_LEFT
272 #define CALCULATOR_RIGHT BUTTON_RIGHT
273 #define CALCULATOR_UP BUTTON_UP
274 #define CALCULATOR_DOWN BUTTON_DOWN
275 #define CALCULATOR_QUIT BUTTON_POWER
276 #define CALCULATOR_INPUT BUTTON_SELECT
277 #define CALCULATOR_CALC BUTTON_MENU
278 #define CALCULATOR_CLEAR BUTTON_DISPLAY
280 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
282 #define CALCULATOR_LEFT BUTTON_RC_REW
283 #define CALCULATOR_RIGHT BUTTON_RC_FF
284 #define CALCULATOR_UP BUTTON_RC_VOL_UP
285 #define CALCULATOR_DOWN BUTTON_RC_VOL_DOWN
286 #define CALCULATOR_QUIT BUTTON_RC_REC
287 #define CALCULATOR_INPUT BUTTON_RC_PLAY
288 #define CALCULATOR_CALC BUTTON_RC_MODE
289 #define CALCULATOR_CLEAR BUTTON_RC_MENU
291 #define CALCULATOR_RC_QUIT BUTTON_REC
293 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
295 #define CALCULATOR_QUIT BUTTON_POWER
296 #define CALCULATOR_CLEAR BUTTON_MENU
298 #elif CONFIG_KEYPAD == IAUDIO67_PAD
300 #define CALCULATOR_LEFT BUTTON_LEFT
301 #define CALCULATOR_RIGHT BUTTON_RIGHT
302 #define CALCULATOR_UP BUTTON_VOLUP
303 #define CALCULATOR_DOWN BUTTON_VOLDOWN
304 #define CALCULATOR_QUIT BUTTON_POWER
305 #define CALCULATOR_INPUT BUTTON_PLAY
306 #define CALCULATOR_CALC BUTTON_MENU
307 #define CALCULATOR_CLEAR BUTTON_STOP
309 #define CALCULATOR_RC_QUIT (BUTTON_MENU|BUTTON_PLAY)
311 #elif (CONFIG_KEYPAD == CREATIVEZVM_PAD)
313 #define CALCULATOR_LEFT BUTTON_LEFT
314 #define CALCULATOR_RIGHT BUTTON_RIGHT
315 #define CALCULATOR_UP BUTTON_UP
316 #define CALCULATOR_DOWN BUTTON_DOWN
317 #define CALCULATOR_QUIT BUTTON_BACK
318 #define CALCULATOR_INPUT BUTTON_SELECT
319 #define CALCULATOR_CALC BUTTON_MENU
320 #define CALCULATOR_CLEAR BUTTON_PLAY
322 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
324 #define CALCULATOR_LEFT BUTTON_LEFT
325 #define CALCULATOR_RIGHT BUTTON_RIGHT
326 #define CALCULATOR_UP BUTTON_UP
327 #define CALCULATOR_DOWN BUTTON_DOWN
328 #define CALCULATOR_QUIT BUTTON_POWER
329 #define CALCULATOR_INPUT BUTTON_SELECT
330 #define CALCULATOR_CALC BUTTON_MENU
331 #define CALCULATOR_CLEAR BUTTON_VIEW
333 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
335 #define CALCULATOR_LEFT BUTTON_PREV
336 #define CALCULATOR_RIGHT BUTTON_NEXT
337 #define CALCULATOR_UP BUTTON_UP
338 #define CALCULATOR_DOWN BUTTON_DOWN
339 #define CALCULATOR_QUIT BUTTON_POWER
340 #define CALCULATOR_INPUT BUTTON_PLAY
341 #define CALCULATOR_CALC BUTTON_MENU
342 #define CALCULATOR_CLEAR BUTTON_RIGHT
344 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
346 #define CALCULATOR_QUIT BUTTON_POWER
347 #define CALCULATOR_CLEAR BUTTON_MENU
349 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
350 #define CALCULATOR_QUIT BUTTON_POWER
352 #elif CONFIG_KEYPAD == MROBE500_PAD
353 #define CALCULATOR_QUIT BUTTON_POWER
355 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
357 #define CALCULATOR_LEFT BUTTON_LEFT
358 #define CALCULATOR_RIGHT BUTTON_RIGHT
359 #define CALCULATOR_UP BUTTON_UP
360 #define CALCULATOR_DOWN BUTTON_DOWN
361 #define CALCULATOR_QUIT BUTTON_REC
362 #define CALCULATOR_INPUT BUTTON_PLAY
363 #define CALCULATOR_CALC BUTTON_FFWD
364 #define CALCULATOR_CLEAR BUTTON_REW
366 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
368 #define CALCULATOR_LEFT BUTTON_PREV
369 #define CALCULATOR_RIGHT BUTTON_NEXT
370 #define CALCULATOR_UP BUTTON_UP
371 #define CALCULATOR_DOWN BUTTON_DOWN
372 #define CALCULATOR_QUIT BUTTON_REC
373 #define CALCULATOR_INPUT BUTTON_OK
374 #define CALCULATOR_CALC BUTTON_PLAY
375 #define CALCULATOR_CLEAR BUTTON_CANCEL
377 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
378 #define CALCULATOR_LEFT BUTTON_PREV
379 #define CALCULATOR_RIGHT BUTTON_NEXT
380 #define CALCULATOR_QUIT (BUTTON_REC|BUTTON_PLAY)
381 #define CALCULATOR_INPUT BUTTON_SELECT
382 #define CALCULATOR_CALC BUTTON_PLAY
384 #else
385 #error No keymap defined!
386 #endif
388 #ifdef HAVE_TOUCHSCREEN
389 #ifndef CALCULATOR_LEFT
390 #define CALCULATOR_LEFT BUTTON_MIDLEFT
391 #endif
392 #ifndef CALCULATOR_RIGHT
393 #define CALCULATOR_RIGHT BUTTON_MIDRIGHT
394 #endif
395 #ifndef CALCULATOR_UP
396 #define CALCULATOR_UP BUTTON_TOPMIDDLE
397 #endif
398 #ifndef CALCULATOR_DOWN
399 #define CALCULATOR_DOWN BUTTON_BOTTOMMIDDLE
400 #endif
401 #ifndef CALCULATOR_CALC
402 #define CALCULATOR_CALC BUTTON_BOTTOMRIGHT
403 #endif
404 #ifndef CALCULATOR_INPUT
405 #define CALCULATOR_INPUT BUTTON_CENTER
406 #endif
407 #ifndef CALCULATOR_CLEAR
408 #define CALCULATOR_CLEAR BUTTON_TOPRIGHT
409 #endif
411 #include "lib/pluginlib_touchscreen.h"
412 static struct ts_raster calc_raster = { X_0_POS, Y_1_POS,
413 BUTTON_COLS*REC_WIDTH, BUTTON_ROWS*REC_HEIGHT, REC_WIDTH, REC_HEIGHT };
414 #endif
416 enum {
417 basicButtons,
418 sciButtons
419 } buttonGroup;
421 unsigned char* buttonChar[2][5][5] = {
422 { { "MR" , "M+" , "2nd" , "CE" , "C" },
423 { "7" , "8" , "9" , "/" , "sqr" },
424 { "4" , "5" , "6" , "*" , "x^2" },
425 { "1" , "2" , "3" , "-" , "1/x" },
426 { "0" , "+/-", "." , "+" , "=" } },
428 { { "n!" , "PI" , "1st" , "sin" , "asi" },
429 { "7" , "8" , "9" , "cos" , "aco" },
430 { "4" , "5" , "6" , "tan" , "ata" },
431 { "1" , "2" , "3" , "ln" , "e^x" },
432 { "0" , "+/-", "." , "log" , "x^y" } }
435 enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C ,
436 btn_7 , btn_8 , btn_9 , btn_div , btn_sqr ,
437 btn_4 , btn_5 , btn_6 , btn_time , btn_square ,
438 btn_1 , btn_2 , btn_3 , btn_minus , btn_rec ,
439 btn_0 , btn_sign , btn_dot , btn_add , btn_equal
442 enum { sci_fac, sci_pi , sci_sci , sci_sin , sci_asin ,
443 sci_7 , sci_8 , sci_9 , sci_cos , sci_acos ,
444 sci_4 , sci_5 , sci_6 , sci_tan , sci_atan ,
445 sci_1 , sci_2 , sci_3 , sci_ln , sci_exp ,
446 sci_0 , sci_sign , sci_dot , sci_log , sci_xy
449 #define MINIMUM 0.000000000001 /* e-12 */
450 /* ^ ^ ^ ^ */
451 /* 123456789abcdef */
453 #define DIGITLEN 10 /* must <= 10 */
454 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
455 /* 0.000 00000 0001 */
456 /* ^ ^ ^ ^ ^ ^ */
457 /* DIGITLEN 12345 6789a bcdef */
458 /* power 12 34567 89abc def */
459 /* 10^- 123 45678 9abcd ef */
461 unsigned char buf[19];/* 18 bytes of output line,
462 buf[0] is operator
463 buf[1] = 'M' if memTemp is not 0
464 buf[2] = ' '
466 if SCIENTIFIC_FORMAT
467 buf[2]-buf[12] or buf[3]-buf[13] = result;
468 format X.XXXXXXXX
469 buf[13] or buf[14] -buf[17] = power;
470 format eXXX or e-XXX
471 else
472 buf[3]-buf[6] = ' ';
473 buf[7]-buf[17] = result;
475 buf[18] = '\0' */
477 unsigned char typingbuf[DIGITLEN+2];/* byte 0 is sign or ' ',
478 byte 1~DIGITLEN are num and '.'
479 byte (DIGITLEN+1) is '\0' */
480 unsigned char* typingbufPointer = typingbuf;
482 double result = 0; /* main operand, format 0.xxxxx */
483 int power = 0; /* 10^power */
484 double modifier = 0.1; /* position of next input */
485 double operand = 0; /* second operand, format 0.xxxxx */
486 int operandPower = 0; /* 10^power of second operand */
487 char oper = ' '; /* operators: + - * / */
488 bool operInputted = false; /* false: do calculation first and
489 replace current oper
490 true: just replace current oper */
492 double memTemp = 0; /* temp memory */
493 int memTempPower = 0; /* 10^^power of memTemp */
495 int btn_row, btn_col; /* current position index for button */
496 int prev_btn_row, prev_btn_col; /* previous cursor position */
497 #define CAL_BUTTON (btn_row*5+btn_col)
499 int btn = BUTTON_NONE;
500 int lastbtn = BUTTON_NONE;
502 /* Status of calculator */
503 enum {cal_normal, /* 0, normal status, display result */
504 cal_typing, /* 1, currently typing, dot hasn't been typed */
505 cal_dotted, /* 2, currently typing, dot already has been typed. */
506 cal_error,
507 cal_exit,
508 cal_toDo
509 } calStatus;
511 /* constant table for CORDIC algorithm */
512 double cordicTable[51][2]= {
513 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
514 {1e+00, 7.853981633974483e-01},
515 {5e-01, 4.636476090008061e-01},
516 {2.5e-01, 2.449786631268641e-01},
517 {1.25e-01, 1.243549945467614e-01},
518 {6.25e-02, 6.241880999595735e-02},
519 {3.125e-02, 3.123983343026828e-02},
520 {1.5625e-02, 1.562372862047683e-02},
521 {7.8125e-03, 7.812341060101111e-03},
522 {3.90625e-03, 3.906230131966972e-03},
523 {1.953125e-03, 1.953122516478819e-03},
524 {9.765625e-04, 9.765621895593195e-04},
525 {4.8828125e-04, 4.882812111948983e-04},
526 {2.44140625e-04, 2.441406201493618e-04},
527 {1.220703125e-04, 1.220703118936702e-04},
528 {6.103515625e-05, 6.103515617420877e-05},
529 {3.0517578125e-05, 3.051757811552610e-05},
530 {1.52587890625e-05, 1.525878906131576e-05},
531 {7.62939453125e-06, 7.629394531101970e-06},
532 {3.814697265625e-06, 3.814697265606496e-06},
533 {1.9073486328125e-06, 1.907348632810187e-06},
534 {9.5367431640625e-07, 9.536743164059608e-07},
535 {4.76837158203125e-07, 4.768371582030888e-07},
536 {2.384185791015625e-07, 2.384185791015580e-07},
537 {1.1920928955078125e-07, 1.192092895507807e-07},
538 {5.9604644775390625e-08, 5.960464477539055e-08},
539 {2.98023223876953125e-08, 2.980232238769530e-08},
540 {1.490116119384765625e-08, 1.490116119384765e-08},
541 {7.450580596923828125e-09, 7.450580596923828e-09},
542 {3.7252902984619140625e-09, 3.725290298461914e-09},
543 {1.86264514923095703125e-09, 1.862645149230957e-09},
544 {9.31322574615478515625e-10, 9.313225746154785e-10},
545 {4.656612873077392578125e-10, 4.656612873077393e-10},
546 {2.3283064365386962890625e-10, 2.328306436538696e-10},
547 {1.16415321826934814453125e-10, 1.164153218269348e-10},
548 {5.82076609134674072265625e-11, 5.820766091346741e-11},
549 {2.910383045673370361328125e-11, 2.910383045673370e-11},
550 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
551 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
552 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
553 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
554 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
555 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
556 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
557 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
558 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
559 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
560 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
561 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
562 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
563 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
564 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
567 void doMultiple(double* operandOne, int* powerOne,
568 double operandTwo, int powerTwo);
569 void doAdd (double* operandOne, int* powerOne,
570 double operandTwo, int powerTwo);
571 void printResult(void);
572 void formatResult(void);
573 void oneOperand(void);
575 void drawLines(void);
576 void drawButtons(int group);
578 /* -----------------------------------------------------------------------
579 Handy funtions
580 ----------------------------------------------------------------------- */
581 void cleartypingbuf(void)
583 int k;
584 for( k=1; k<=(DIGITLEN+1); k++)
585 typingbuf[k] = 0;
586 typingbuf[0] = ' ';
587 typingbufPointer = typingbuf+1;
589 void clearbuf(void)
591 int k;
592 for(k=0;k<18;k++)
593 buf[k]=' ';
594 buf[18] = 0;
596 void clearResult(void)
598 result = 0;
599 power = 0;
600 modifier = 0.1;
603 void clearInput(void)
605 calStatus = cal_normal;
606 clearResult();
607 cleartypingbuf();
608 rb->lcd_clear_display();
609 drawButtons(buttonGroup);
610 drawLines();
613 void clearOperand(void)
615 operand = 0;
616 operandPower = 0;
619 void clearMemTemp(void)
621 memTemp = 0;
622 memTempPower = 0;
625 void clearOper(void)
627 oper = ' ';
628 operInputted = false;
631 void clearMem(void)
633 clearInput();
634 clearMemTemp();
635 clearOperand();
636 clearOper();
637 btn = BUTTON_NONE;
640 void switchOperands(void)
642 double tempr = operand;
643 int tempp = operandPower;
644 operand = result;
645 operandPower = power;
646 result = tempr;
647 power = tempp;
650 void drawLines(void)
652 int i;
653 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS-1);
654 for (i = 0; i < 5 ; i++)
655 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS+i*REC_HEIGHT);
656 for (i = 0; i < 4 ; i++)
657 rb->lcd_vline(X_1_POS+i*REC_WIDTH, Y_1_POS, LCD_HEIGHT);
660 void drawButtons(int group)
662 int i, j, w, h;
663 for (i = 0; i <= 4; i++){
664 for (j = 0; j <= 4; j++){
665 rb->lcd_getstringsize( buttonChar[group][i][j],&w,&h);
666 if (i == btn_row && j == btn_col) /* selected item */
667 rb->lcd_set_drawmode(DRMODE_SOLID);
668 else
669 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
670 rb->lcd_fillrect( X_0_POS + j*REC_WIDTH,
671 Y_1_POS + i*REC_HEIGHT,
672 REC_WIDTH, REC_HEIGHT+1);
673 if (i == btn_row && j == btn_col) /* selected item */
674 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
675 else
676 rb->lcd_set_drawmode(DRMODE_SOLID);
677 rb->lcd_putsxy( X_0_POS + j*REC_WIDTH + (REC_WIDTH - w)/2,
678 Y_1_POS + i*REC_HEIGHT + (REC_HEIGHT - h)/2 + 1,
679 buttonChar[group][i][j] );
682 rb->lcd_set_drawmode(DRMODE_SOLID);
685 /* -----------------------------------------------------------------------
686 Initiate calculator
687 ----------------------------------------------------------------------- */
688 void cal_initial (void)
690 int w,h;
692 rb->lcd_getstringsize("2nd",&w,&h);
693 if (w > REC_WIDTH || h > REC_HEIGHT)
694 rb->lcd_setfont(FONT_SYSFIXED);
696 rb->lcd_clear_display();
698 #ifdef CALCULATOR_OPERATORS
699 /* basic operators are available through separate button */
700 buttonGroup = sciButtons;
701 #else
702 buttonGroup = basicButtons;
703 #endif
705 /* initially, invert button "5" */
706 btn_row = 2;
707 btn_col = 1;
708 prev_btn_row = btn_row;
709 prev_btn_col = btn_col;
710 drawButtons(buttonGroup);
711 drawLines();
712 rb->lcd_update();
714 /* initial mem and output display*/
715 clearMem();
716 printResult();
718 /* clear button queue */
719 rb->button_clear_queue();
722 /* -----------------------------------------------------------------------
723 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
724 in it's private case for sqrt.
725 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
726 ----------------------------------------------------------------------- */
727 double mySqrt(double square)
729 int k = 0;
730 double temp = 0;
731 double root= ABS(square+1)/2;
733 while( ABS(root - temp) > MINIMUM ){
734 temp = root;
735 root = (square/temp + temp)/2;
736 k++;
737 if (k>10000) return 0;
740 return root;
742 /* -----------------------------------------------------------------------
743 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
744 transcendFunc can do sin,cos,log,exp
745 input parameter is angle
746 ----------------------------------------------------------------------- */
747 void transcendFunc(char* func, double* tt, int* ttPower)
749 double t = (*tt)*M_PI/180; int tPower = *ttPower;
750 int sign = 1;
751 int n = 50; /* n <=50, tables are all <= 50 */
752 int j;
753 double x,y,z,xt,yt,zt;
755 if (tPower < -998) {
756 calStatus = cal_normal;
757 return;
759 if (tPower > 8) {
760 calStatus = cal_error;
761 return;
763 *ttPower = 0;
764 calStatus = cal_normal;
766 if( func[0] =='s' || func[0] =='S'|| func[0] =='t' || func[0] =='T')
767 sign = SIGN(t);
768 else {
769 /* if( func[0] =='c' || func[0] =='C') */
770 sign = 1;
772 t = ABS(t);
774 while (tPower > 0){
775 t *= 10;
776 tPower--;
778 while (tPower < 0) {
779 t /= 10;
780 tPower++;
782 j = 0;
783 while (t > j*M_TWOPI) {j++;}
784 t -= (j-1)*M_TWOPI;
785 if (M_PI_2 < t && t < 3*M_PI_2){
786 t = M_PI - t;
787 if (func[0] =='c' || func[0] =='C')
788 sign = -1;
789 else if (func[0] =='t' || func[0] =='T')
790 t*=-1;
792 else if ( 3*M_PI_2 <= t && t <= M_TWOPI)
793 t -= M_TWOPI;
795 x = 0.60725293500888; y = 0; z = t;
796 for (j=1;j<n+2;j++){
797 xt = x - SIGN(z) * y*cordicTable[j-1][0];
798 yt = y + SIGN(z) * x*cordicTable[j-1][0];
799 zt = z - SIGN(z) * cordicTable[j-1][1];
800 x = xt;
801 y=yt;
802 z=zt;
804 if( func[0] =='s' || func[0] =='S') {
805 *tt = sign*y;
806 return;
808 else if( func[0] =='c' || func[0] =='C') {
809 *tt = sign*x;
810 return;
812 else /*if( func[0] =='t' || func[0] =='T')*/ {
813 if(t==M_PI_2||t==-M_PI_2){
814 calStatus = cal_error;
815 return;
817 else{
818 *tt = sign*(y/x);
819 return;
824 /* -----------------------------------------------------------------------
825 add in scientific number format
826 ----------------------------------------------------------------------- */
827 void doAdd (double* operandOne, int* powerOne,
828 double operandTwo, int powerTwo)
830 if ( *powerOne >= powerTwo ){
831 if (*powerOne - powerTwo <= DIGITLEN+1){
832 while (powerTwo < *powerOne){
833 operandTwo /=10;
834 powerTwo++;
836 *operandOne += operandTwo;
838 /*do nothing if operandTwo is too small*/
840 else{
841 if (powerTwo - *powerOne <= DIGITLEN+1){
842 while(powerTwo > *powerOne){
843 *operandOne /=10;
844 (*powerOne)++;
846 (*operandOne) += operandTwo;
848 else{/* simply copy operandTwo if operandOne is too small */
849 *operandOne = operandTwo;
850 *powerOne = powerTwo;
854 /* -----------------------------------------------------------------------
855 multiple in scientific number format
856 ----------------------------------------------------------------------- */
857 void doMultiple(double* operandOne, int* powerOne,
858 double operandTwo, int powerTwo)
860 (*operandOne) *= operandTwo;
861 (*powerOne) += powerTwo;
864 /* -----------------------------------------------------------------------
865 Handles all one operand calculations
866 ----------------------------------------------------------------------- */
867 void oneOperand(void)
869 int k = 0;
870 if (buttonGroup == basicButtons){
871 switch(CAL_BUTTON){
872 case btn_sqr:
873 if (result<0)
874 calStatus = cal_error;
875 else{
876 if (power%2 == 1){
877 result = (mySqrt(result*10))/10;
878 power = (power+1) / 2;
880 else{
881 result = mySqrt(result);
882 power = power / 2;
884 calStatus = cal_normal;
886 break;
887 case btn_square:
888 power *= 2;
889 result *= result;
890 calStatus = cal_normal;
891 break;
893 case btn_rec:
894 if (result==0)
895 calStatus = cal_error;
896 else{
897 power = -power;
898 result = 1/result;
899 calStatus = cal_normal;
901 break;
902 default:
903 calStatus = cal_toDo;
904 break; /* just for the safety */
907 else{ /* sciButtons */
908 switch(CAL_BUTTON){
909 case sci_sin:
910 transcendFunc("sin", &result, &power);
911 break;
912 case sci_cos:
913 transcendFunc("cos", &result, &power);
914 break;
915 case sci_tan:
916 transcendFunc("tan", &result, &power);
917 break;
918 case sci_fac:
919 if (power<0 || power>8 || result<0 )
920 calStatus = cal_error;
921 else if(result == 0) {
922 result = 1;
923 power = 0;
925 else{
926 while(power > 0) {
927 result *= 10;
928 power--;
930 if ( ( result - (int)result) > MINIMUM )
931 calStatus = cal_error;
932 else {
933 k = result; result = 1;
934 while (k > 1){
935 doMultiple(&result, &power, k, 0);
936 formatResult();
937 k--;
939 calStatus = cal_normal;
942 break;
943 default:
944 calStatus = cal_toDo;
945 break; /* just for the safety */
951 /* -----------------------------------------------------------------------
952 Handles all two operands calculations
953 ----------------------------------------------------------------------- */
954 void twoOperands(void)
956 switch(oper){
957 case '-':
958 doAdd(&operand, &operandPower, -result, power);
959 break;
960 case '+':
961 doAdd(&operand, &operandPower, result, power);
962 break;
963 case '*':
964 doMultiple(&operand, &operandPower, result, power);
965 break;
966 case '/':
967 if ( ABS(result) > MINIMUM ){
968 doMultiple(&operand, &operandPower, 1/result, -power);
970 else
971 calStatus = cal_error;
972 break;
973 default: /* ' ' */
974 switchOperands(); /* counter switchOperands() below */
975 break;
976 } /* switch(oper) */
977 switchOperands();
978 clearOper();
981 /* First, increases *dimen1 by dimen1_delta modulo dimen1_modulo.
982 If dimen1 wraps, increases *dimen2 by dimen2_delta modulo dimen2_modulo.
984 static void move_with_wrap_and_shift(
985 int *dimen1, int dimen1_delta, int dimen1_modulo,
986 int *dimen2, int dimen2_delta, int dimen2_modulo)
988 bool wrapped = false;
990 *dimen1 += dimen1_delta;
991 if (*dimen1 < 0)
993 *dimen1 = dimen1_modulo - 1;
994 wrapped = true;
996 else if (*dimen1 >= dimen1_modulo)
998 *dimen1 = 0;
999 wrapped = true;
1002 if (wrapped)
1004 /* Make the dividend always positive to be sure about the result.
1005 Adding dimen2_modulo does not change it since we do it modulo. */
1006 *dimen2 = (*dimen2 + dimen2_modulo + dimen2_delta) % dimen2_modulo;
1010 /* -----------------------------------------------------------------------
1011 Print buttons when switching 1st and 2nd
1012 int group = {basicButtons, sciButtons}
1013 ----------------------------------------------------------------------- */
1014 void printButtonGroups(int group)
1016 drawButtons(group);
1017 drawLines();
1018 rb->lcd_update();
1020 /* -----------------------------------------------------------------------
1021 flash the currently marked button
1022 ----------------------------------------------------------------------- */
1023 void flashButton(void)
1025 int k, w, h;
1026 for (k=2;k>0;k--)
1028 rb->lcd_getstringsize( buttonChar[buttonGroup][btn_row][btn_col],&w,&h);
1029 rb->lcd_set_drawmode(DRMODE_SOLID|(k==1) ? 0 : DRMODE_INVERSEVID);
1030 rb->lcd_fillrect( X_0_POS + btn_col*REC_WIDTH + 1,
1031 Y_1_POS + btn_row*REC_HEIGHT + 1,
1032 REC_WIDTH - 1, REC_HEIGHT - 1);
1033 rb->lcd_putsxy( X_0_POS + btn_col*REC_WIDTH + (REC_WIDTH - w)/2,
1034 Y_1_POS + btn_row*REC_HEIGHT + (REC_HEIGHT - h)/2 +1,
1035 buttonChar[buttonGroup][btn_row][btn_col] );
1036 rb->lcd_update_rect( X_0_POS + btn_col*REC_WIDTH + 1,
1037 Y_1_POS + btn_row*REC_HEIGHT + 1,
1038 REC_WIDTH - 1, REC_HEIGHT - 1);
1040 if (k!= 1)
1041 rb->sleep(HZ/22);
1046 /* -----------------------------------------------------------------------
1047 pos is the position that needs animation. pos = [1~18]
1048 ----------------------------------------------------------------------- */
1049 void deleteAnimation(int pos)
1051 int k, w, h, x;
1052 if (pos<1 || pos >18)
1053 return;
1055 rb->lcd_getstringsize("0", &w, &h);
1056 x = (pos==1? 4: LCD_WIDTH - 4 - w);
1058 for (k=0;k<4;k++){
1059 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1060 rb->lcd_fillrect(x, Y_1_POS - h -1, w, h);
1061 rb->lcd_set_drawmode(DRMODE_SOLID);
1062 rb->lcd_fillrect(x + (w*k)/8, Y_1_POS - h -1 + (h*k)/8,
1063 (w*(4-k))/4, (h*(4-k))/4);
1064 rb->lcd_update_rect(x, Y_1_POS - h -1, w, h);
1065 rb->sleep(HZ/32);
1069 /* -----------------------------------------------------------------------
1070 result may be one of these formats:
1072 xxxx.xxxx
1073 0.xxxx
1074 0.0000xxxx
1076 formatResult() change result to standard format: 0.xxxx
1077 if result is close to 0, let it be 0;
1078 if result is close to 1, let it be 0.1 and power++;
1079 ----------------------------------------------------------------------- */
1080 void formatResult(void)
1082 int resultsign = SIGN(result);
1083 result = ABS(result);
1084 if(result > MINIMUM ){ /* doesn't check power, might have problem
1085 input wouldn't,
1086 + - * / of two formatted number wouldn't.
1087 only a calculation that makes a formatted
1088 number (0.xxxx) less than MINIMUM in only
1089 one operation */
1091 if (result<1){
1092 while( (int)(result*10) == 0 ){
1093 result *= 10;
1094 power--;
1095 modifier *= 10;
1098 else{ /* result >= 1 */
1099 while( (int)result != 0 ){
1100 result /= 10;
1101 power++;
1102 modifier /= 10;
1104 } /* if result<1 */
1106 if (result > (1-MINIMUM)){
1107 result = 0.1;
1108 power++;
1109 modifier /= 10;
1111 result *= resultsign;
1113 else {
1114 result = 0;
1115 power = 0;
1116 modifier = 0.1;
1120 /* -----------------------------------------------------------------------
1121 result2typingbuf() outputs standard format result to typingbuf.
1122 case SCIENTIFIC_FORMAT, let temppower = 1;
1123 case temppower > 0: print '.' in the middle
1124 case temppower <= 0: print '.' in the begining
1125 ----------------------------------------------------------------------- */
1126 void result2typingbuf(void)
1128 bool haveDot = false;
1129 char tempchar = 0;
1130 int k;
1131 double tempresult = ABS(result); /* positive num makes things simple */
1133 int temppower;
1134 double tempmodifier = 1;
1135 int count;
1137 if(SCIENTIFIC_FORMAT)
1138 temppower = 1; /* output x.xxxx format */
1139 else
1140 temppower = power;
1142 cleartypingbuf();
1144 if(tempresult < MINIMUM){ /* if 0,faster display and avoid complication*/
1145 typingbuf[0] = ' ';
1146 typingbuf[1] = '0';
1148 else{ /* tempresult > 0 */
1149 typingbuf[0] = (SIGN(result)<0)?'-':' ';
1151 typingbufPointer = typingbuf;
1152 if(temppower > 0){
1153 for (k = 0; k<DIGITLEN+1 ; k++){
1154 typingbufPointer++;
1155 if(temppower || *(typingbufPointer-1) == '.'){
1156 count = 0;
1157 tempmodifier = tempmodifier/10;
1158 while( (tempresult-tempmodifier*count) >
1159 (tempmodifier-MINIMUM)){
1160 count++;
1162 tempresult -= tempmodifier*count;
1163 tempresult = ABS(tempresult);
1164 temppower-- ;
1165 *typingbufPointer = count + '0';
1167 else{ /* temppower == 0 */
1168 *typingbufPointer = '.';
1169 haveDot = true;
1171 } /* for */
1173 else{
1174 haveDot = true;
1175 typingbufPointer++; *typingbufPointer = '0';
1176 typingbufPointer++; *typingbufPointer = '.';
1177 for (k = 2; k<DIGITLEN+1 ; k++){
1178 typingbufPointer++;
1179 count = 0;
1180 if ( (-temppower) < (k-1)){
1181 tempmodifier = tempmodifier/10;
1182 while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){
1183 count++;
1186 tempresult -= tempmodifier*count;
1187 tempresult = ABS(tempresult);
1188 temppower-- ;
1190 *typingbufPointer = count + '0';
1193 /* now, typingbufPointer = typingbuf + 16 */
1194 /* backward strip off 0 and '.' */
1195 if (haveDot){
1196 while( (*typingbufPointer == '0') || (*typingbufPointer == '.')){
1197 tempchar = *typingbufPointer;
1198 *typingbufPointer = 0;
1199 typingbufPointer--;
1200 if (tempchar == '.') break;
1203 typingbuf[DIGITLEN+1] = 0;
1204 } /* else tempresult > 0 */
1207 /* -----------------------------------------------------------------------
1208 printResult() generates LCD display.
1209 ----------------------------------------------------------------------- */
1210 void printResult(void)
1212 int k, w, h;
1214 char operbuf[3] = {0, 0, 0};
1216 switch_Status:
1217 switch(calStatus){
1218 case cal_exit:
1219 rb->lcd_clear_display();
1220 rb->splash(HZ/3, "Bye now!");
1221 break;
1222 case cal_error:
1223 clearbuf();
1224 rb->snprintf(buf, 19, "%18s","Error");
1225 break;
1226 case cal_toDo:
1227 clearbuf();
1228 rb->snprintf(buf, 19, "%18s","Coming soon ^_* ");
1229 break;
1231 case cal_normal:
1232 formatResult();
1234 if( power > 1000 ){ /* power -1 > 999 */
1235 calStatus = cal_error;
1236 goto switch_Status;
1238 if (power < -998 ) /* power -1 < -999 */
1239 clearResult(); /* too small, let it be 0 */
1241 result2typingbuf();
1242 clearbuf();
1244 operbuf[0] = oper;
1245 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1246 operbuf[2] = '\0';
1248 if(SCIENTIFIC_FORMAT){
1249 /* output format: X.XXXX eXXX */
1250 if(power > -98){ /* power-1 >= -99, eXXX or e-XX */
1251 rb->snprintf(buf, 12, "%11s",typingbuf);
1252 for(k=11;k<=14;k++) buf[k] = ' ';
1253 cleartypingbuf();
1254 rb->snprintf(typingbuf, 5, "e%d",power-1);
1255 rb->snprintf(buf+11, 5, "%4s",typingbuf);
1257 else{ /* power-1 <= -100, e-XXX */
1258 rb->snprintf(buf, 12, "%11s",typingbuf);
1259 rb->snprintf(buf+11, 6, "e%d",power-1);
1262 else{
1263 rb->snprintf(buf, 12, "%11s",typingbuf);
1264 } /* if SCIENTIFIC_FORMAT */
1265 break;
1266 case cal_typing:
1267 case cal_dotted:
1268 clearbuf();
1269 operbuf[0] = oper;
1270 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1271 rb->snprintf(buf, 12, "%11s",typingbuf);
1272 break;
1276 rb->lcd_getstringsize(buf, &w, &h);
1277 rb->screen_clear_area(rb->screens[0], 0, 0, LCD_WIDTH, Y_1_POS - 1);
1278 rb->lcd_putsxy(4, Y_1_POS - h -1, operbuf);
1279 rb->lcd_putsxy(LCD_WIDTH - w - 4, Y_1_POS - h -1, buf);
1280 rb->lcd_update_rect(0, 1, LCD_WIDTH, Y_1_POS);
1283 /* -----------------------------------------------------------------------
1284 Process typing buttons: 1-9, '.', sign
1285 main operand "result" and typingbuf are processed seperately here.
1286 ----------------------------------------------------------------------- */
1287 void typingProcess(void){
1288 switch( CAL_BUTTON ){
1289 case btn_sign:
1290 if (calStatus == cal_typing ||
1291 calStatus == cal_dotted)
1292 typingbuf[0] = (typingbuf[0]=='-')?' ':'-';
1293 result = -result;
1294 break;
1295 case btn_dot:
1296 operInputted = false;
1297 switch(calStatus){
1298 case cal_normal:
1299 clearInput();
1300 *typingbufPointer = '0';
1301 typingbufPointer++;
1302 case cal_typing:
1303 calStatus = cal_dotted;
1304 *typingbufPointer = '.';
1305 if (typingbufPointer != typingbuf+DIGITLEN+1)
1306 typingbufPointer++;
1307 break;
1308 default: /* cal_dotted */
1309 break;
1311 break;
1312 default: /* 0-9 */
1313 operInputted = false;
1314 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1315 switch(calStatus){
1316 case cal_normal:
1317 if(CAL_BUTTON == btn_0 )
1318 break; /* first input is 0, ignore */
1319 clearInput();
1320 /*no operator means start a new calculation*/
1321 if (oper ==' ')
1322 clearOperand();
1323 calStatus = cal_typing;
1324 /* go on typing, no break */
1325 case cal_typing:
1326 case cal_dotted:
1327 switch(CAL_BUTTON){
1328 case btn_0:
1329 *typingbufPointer = '0';
1330 break;
1331 default:
1332 *typingbufPointer=(7+btn_col-3*(btn_row-1))+ '0';
1333 break;
1335 if (typingbufPointer!=typingbuf+DIGITLEN+1){
1336 typingbufPointer++;
1338 {/* result processing */
1339 if (calStatus == cal_typing) power++;
1340 if (CAL_BUTTON != btn_0)
1341 result= result +
1342 SIGN(result)*
1343 (7+btn_col-3*(btn_row-1))*modifier;
1344 modifier /= 10;
1347 else /* last byte always '\0' */
1348 *typingbufPointer = 0;
1349 break;
1350 default: /* cal_error, cal_exit */
1351 break;
1353 break; /* default, 0-9 */
1354 } /* switch( CAL_BUTTON ) */
1357 /* -----------------------------------------------------------------------
1358 Handle delete operation
1359 main operand "result" and typingbuf are processed seperately here.
1360 ----------------------------------------------------------------------- */
1361 void doDelete(void){
1362 deleteAnimation(18);
1363 switch(calStatus){
1364 case cal_dotted:
1365 if (*(typingbufPointer-1) == '.'){
1366 /* if dotted and deleting '.',
1367 change status and delete '.' below */
1368 calStatus = cal_typing;
1370 else{ /* if dotted and not deleting '.',
1371 power stays */
1372 power++; /* counter "power--;" below */
1374 case cal_typing:
1375 typingbufPointer--;
1377 {/* result processing */ /* 0-9, '.' */
1378 /* if deleting '.', do nothing */
1379 if ( *typingbufPointer != '.'){
1380 power--;
1381 modifier *= 10;
1382 result = result - SIGN(result)*
1383 ((*typingbufPointer)- '0')*modifier;
1387 *typingbufPointer = 0;
1389 /* if (only one digit left and it's 0)
1390 or no digit left, change status*/
1391 if ( typingbufPointer == typingbuf+1 ||
1392 ( typingbufPointer == typingbuf+2 &&
1393 *(typingbufPointer-1) == '0' ))
1394 calStatus = cal_normal;
1395 break;
1396 default: /* normal, error, exit */
1397 break;
1400 /* -----------------------------------------------------------------------
1401 Handle buttons on basic screen
1402 ----------------------------------------------------------------------- */
1403 void basicButtonsProcess(void){
1404 switch (btn) {
1405 case CALCULATOR_INPUT:
1406 if (calStatus == cal_error && (CAL_BUTTON != btn_C) ) break;
1407 flashButton();
1408 switch( CAL_BUTTON ){
1409 case btn_MR:
1410 operInputted = false;
1411 result = memTemp; power = memTempPower;
1412 calStatus = cal_normal;
1413 break;
1414 case btn_M:
1415 formatResult();
1416 if (memTemp > MINIMUM)
1417 doAdd(&memTemp, &memTempPower, result, power);
1418 else {
1419 /* if result is too small and memTemp = 0,
1420 doAdd will not add */
1421 memTemp = result;
1422 memTempPower = power;
1424 calStatus = cal_normal;
1425 break;
1427 case btn_C: clearMem(); break;
1428 case btn_CE: clearInput(); break;
1430 case btn_bas:
1431 buttonGroup = sciButtons;
1432 printButtonGroups(buttonGroup);
1433 break;
1435 /* one operand calculation, may be changed to
1436 like sin, cos, log, etc */
1437 case btn_sqr:
1438 case btn_square:
1439 case btn_rec:
1440 formatResult(); /* not necessary, just for safty */
1441 oneOperand();
1442 break;
1444 case_btn_equal: /* F3 shortkey entrance */
1445 case btn_equal:
1446 formatResult();
1447 calStatus = cal_normal;
1448 operInputted = false;
1449 if (oper != ' ') twoOperands();
1450 break;
1452 case btn_div:
1453 case btn_time:
1454 case btn_minus:
1455 case btn_add:
1456 if(!operInputted) {twoOperands(); operInputted = true;}
1457 oper = buttonChar[basicButtons][btn_row][btn_col][0];
1458 #ifdef CALCULATOR_OPERATORS
1459 case_cycle_operators: /* F2 shortkey entrance */
1460 #endif
1461 calStatus = cal_normal;
1462 formatResult();
1463 operand = result;
1464 operandPower = power;
1466 break;
1468 case btn_sign:
1469 case btn_dot:
1470 default: /* 0-9 */
1471 typingProcess();
1472 break;
1473 } /* switch (CAL_BUTTON) */
1474 break;
1476 #ifdef CALCULATOR_OPERATORS
1477 case CALCULATOR_OPERATORS:
1478 if (calStatus == cal_error) break;
1479 if (!operInputted) {twoOperands(); operInputted = true;}
1480 switch (oper){
1481 case ' ':
1482 case '/': oper = '+'; flashButton(); break;
1483 case '+': oper = '-'; flashButton(); break;
1484 case '-': oper = '*'; flashButton(); break;
1485 case '*': oper = '/'; flashButton(); break;
1487 goto case_cycle_operators;
1488 break;
1489 #endif
1491 case CALCULATOR_CALC:
1492 if (calStatus == cal_error) break;
1493 flashButton();
1494 goto case_btn_equal;
1495 break;
1496 default: break;
1498 printResult();
1501 /* -----------------------------------------------------------------------
1502 Handle buttons on scientific screen
1503 ----------------------------------------------------------------------- */
1504 void sciButtonsProcess(void){
1505 switch (btn) {
1506 case CALCULATOR_INPUT:
1507 if (calStatus == cal_error && (CAL_BUTTON != sci_sci) ) break;
1508 flashButton();
1509 switch( CAL_BUTTON ){
1511 case sci_pi:
1512 result = M_PI; power = 0;
1513 calStatus = cal_normal;
1514 break;
1516 case sci_xy: break;
1518 case sci_sci:
1519 buttonGroup = basicButtons;
1520 printButtonGroups(basicButtons);
1521 break;
1523 case sci_fac:
1524 case sci_sin:
1525 case sci_asin:
1526 case sci_cos:
1527 case sci_acos:
1528 case sci_tan:
1529 case sci_atan:
1530 case sci_ln:
1531 case sci_exp:
1532 case sci_log:
1533 formatResult(); /* not necessary, just for safty */
1534 oneOperand();
1535 break;
1537 case btn_sign:
1538 case btn_dot:
1539 default: /* 0-9 */
1540 typingProcess();
1541 break;
1542 } /* switch (CAL_BUTTON) */
1543 break;
1545 #ifdef CALCULATOR_OPERATORS
1546 case CALCULATOR_OPERATORS:
1547 if (calStatus == cal_error) break;
1548 if (!operInputted) {twoOperands(); operInputted = true;}
1549 switch (oper){
1550 case ' ': oper = '+'; break;
1551 case '/': oper = '+'; deleteAnimation(1); break;
1552 case '+': oper = '-'; deleteAnimation(1); break;
1553 case '-': oper = '*'; deleteAnimation(1); break;
1554 case '*': oper = '/'; deleteAnimation(1); break;
1556 calStatus = cal_normal;
1557 formatResult();
1558 operand = result;
1559 operandPower = power;
1560 break;
1561 #endif
1563 case CALCULATOR_CALC:
1564 if (calStatus == cal_error) break;
1565 formatResult();
1566 calStatus = cal_normal;
1567 operInputted = false;
1568 if (oper != ' ') twoOperands();
1569 break;
1570 default: break;
1572 printResult();
1575 /* -----------------------------------------------------------------------
1576 move button index
1577 Invert display new button, invert back previous button
1578 ----------------------------------------------------------------------- */
1579 int handleButton(int button){
1580 switch(button)
1582 case CALCULATOR_INPUT:
1583 case CALCULATOR_CALC:
1584 #ifdef CALCULATOR_INPUT_CALC_PRE
1585 if (lastbtn != CALCULATOR_INPUT_CALC_PRE)
1586 break;
1587 /* no unconditional break; here! */
1588 #endif
1589 #ifdef CALCULATOR_OPERATORS
1590 case CALCULATOR_OPERATORS:
1591 #endif
1592 switch(buttonGroup){
1593 case basicButtons:
1594 basicButtonsProcess();
1595 break;
1596 case sciButtons:
1597 sciButtonsProcess();
1598 break;
1600 break;
1602 #ifdef CALCULATOR_CLEAR
1603 case CALCULATOR_CLEAR:
1604 switch(calStatus){
1605 case cal_typing:
1606 case cal_dotted:
1607 doDelete();
1608 break;
1609 default: /* cal_normal, cal_error, cal_exit */
1610 clearMem();
1611 break;
1613 printResult();
1614 break;
1615 #endif
1616 case CALCULATOR_LEFT:
1617 case CALCULATOR_LEFT | BUTTON_REPEAT:
1618 move_with_wrap_and_shift(
1619 &btn_col, -1, BUTTON_COLS,
1620 &btn_row, 0, BUTTON_ROWS);
1621 break;
1623 case CALCULATOR_RIGHT:
1624 case CALCULATOR_RIGHT | BUTTON_REPEAT:
1625 move_with_wrap_and_shift(
1626 &btn_col, 1, BUTTON_COLS,
1627 &btn_row, 0, BUTTON_ROWS);
1628 break;
1630 #ifdef CALCULATOR_UP
1631 case CALCULATOR_UP:
1632 case CALCULATOR_UP | BUTTON_REPEAT:
1633 move_with_wrap_and_shift(
1634 &btn_row, -1, BUTTON_ROWS,
1635 &btn_col, 0, BUTTON_COLS);
1636 break;
1637 #endif
1638 #ifdef CALCULATOR_DOWN
1639 case CALCULATOR_DOWN:
1640 case CALCULATOR_DOWN | BUTTON_REPEAT:
1641 move_with_wrap_and_shift(
1642 &btn_row, 1, BUTTON_ROWS,
1643 &btn_col, 0, BUTTON_COLS);
1644 break;
1645 #endif
1647 #ifdef CALCULATOR_UP_W_SHIFT
1648 case CALCULATOR_UP_W_SHIFT:
1649 case CALCULATOR_UP_W_SHIFT | BUTTON_REPEAT:
1650 move_with_wrap_and_shift(
1651 &btn_row, -1, BUTTON_ROWS,
1652 &btn_col, -1, BUTTON_COLS);
1653 break;
1654 #endif
1655 #ifdef CALCULATOR_DOWN_W_SHIFT
1656 case CALCULATOR_DOWN_W_SHIFT:
1657 case CALCULATOR_DOWN_W_SHIFT | BUTTON_REPEAT:
1658 move_with_wrap_and_shift(
1659 &btn_row, 1, BUTTON_ROWS,
1660 &btn_col, 1, BUTTON_COLS);
1661 break;
1662 #endif
1663 #ifdef CALCULATOR_LEFT_W_SHIFT
1664 case CALCULATOR_LEFT_W_SHIFT:
1665 case CALCULATOR_LEFT_W_SHIFT | BUTTON_REPEAT:
1666 move_with_wrap_and_shift(
1667 &btn_col, -1, BUTTON_COLS,
1668 &btn_row, -1, BUTTON_ROWS);
1669 break;
1670 #endif
1671 #ifdef CALCULATOR_RIGHT_W_SHIFT
1672 case CALCULATOR_RIGHT_W_SHIFT:
1673 case CALCULATOR_RIGHT_W_SHIFT | BUTTON_REPEAT:
1674 move_with_wrap_and_shift(
1675 &btn_col, 1, BUTTON_COLS,
1676 &btn_row, 1, BUTTON_ROWS);
1677 break;
1678 #endif
1679 #ifdef CALCULATOR_RC_QUIT
1680 case CALCULATOR_RC_QUIT:
1681 #endif
1682 case CALCULATOR_QUIT:
1683 return -1;
1686 return 0;
1688 prev_btn_row = btn_row;
1689 prev_btn_col = btn_col;
1692 /* -----------------------------------------------------------------------
1693 Main();
1694 ----------------------------------------------------------------------- */
1695 enum plugin_status plugin_start(const void* parameter)
1697 (void)parameter;
1699 /* now go ahead and have fun! */
1701 #ifdef HAVE_TOUCHSCREEN
1702 rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
1703 #endif
1705 cal_initial();
1707 while (calStatus != cal_exit ) {
1708 btn = rb->button_get_w_tmo(HZ/2);
1709 #ifdef HAVE_TOUCHSCREEN
1710 if(btn & BUTTON_TOUCHSCREEN)
1712 struct ts_raster_result res;
1713 if(touchscreen_map_raster(&calc_raster, rb->button_get_data() >> 16,
1714 rb->button_get_data() & 0xffff, &res) == 1)
1716 btn_row = res.y;
1717 btn_col = res.x;
1718 drawButtons(buttonGroup);
1719 drawLines();
1721 rb->lcd_update();
1723 prev_btn_row = btn_row;
1724 prev_btn_col = btn_col;
1725 if(btn & BUTTON_REL)
1727 btn = CALCULATOR_INPUT;
1728 switch(buttonGroup){
1729 case basicButtons:
1730 basicButtonsProcess();
1731 break;
1732 case sciButtons:
1733 sciButtonsProcess();
1734 break;
1736 btn = BUTTON_TOUCHSCREEN;
1740 #endif
1741 if (handleButton(btn) == -1)
1743 calStatus = cal_exit;
1744 printResult();
1746 else
1748 drawButtons(buttonGroup);
1749 drawLines();
1752 rb->lcd_update();
1754 if(rb->default_event_handler(btn) == SYS_USB_CONNECTED)
1755 return PLUGIN_USB_CONNECTED;
1757 if (btn != BUTTON_NONE)
1758 lastbtn = btn;
1759 } /* while (calStatus != cal_exit ) */
1761 rb->button_clear_queue();
1762 return PLUGIN_OK;
1765 #endif /* #ifdef HAVE_LCD_BITMAP */