MPIO HD200: rename button defines to adhere how they are labeled on the device.
[maemo-rb.git] / apps / plugins / calculator.c
blob0507ce81d64d906f4db7bc7d1ba7b45c84246733
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"
79 #include "math.h"
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_HDD6330_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 == PHILIPS_SA9200_PAD
352 #define CALCULATOR_LEFT BUTTON_PREV
353 #define CALCULATOR_RIGHT BUTTON_NEXT
354 #define CALCULATOR_UP BUTTON_UP
355 #define CALCULATOR_DOWN BUTTON_DOWN
356 #define CALCULATOR_QUIT BUTTON_POWER
357 #define CALCULATOR_INPUT BUTTON_PLAY
358 #define CALCULATOR_CALC BUTTON_MENU
359 #define CALCULATOR_CLEAR BUTTON_RIGHT
361 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
363 #define CALCULATOR_QUIT BUTTON_POWER
364 #define CALCULATOR_CLEAR BUTTON_MENU
366 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
367 #define CALCULATOR_QUIT BUTTON_POWER
369 #elif CONFIG_KEYPAD == MROBE500_PAD
370 #define CALCULATOR_QUIT BUTTON_POWER
372 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
374 #define CALCULATOR_LEFT BUTTON_LEFT
375 #define CALCULATOR_RIGHT BUTTON_RIGHT
376 #define CALCULATOR_UP BUTTON_UP
377 #define CALCULATOR_DOWN BUTTON_DOWN
378 #define CALCULATOR_QUIT BUTTON_REC
379 #define CALCULATOR_INPUT BUTTON_PLAY
380 #define CALCULATOR_CALC BUTTON_FFWD
381 #define CALCULATOR_CLEAR BUTTON_REW
383 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
385 #define CALCULATOR_LEFT BUTTON_PREV
386 #define CALCULATOR_RIGHT BUTTON_NEXT
387 #define CALCULATOR_UP BUTTON_UP
388 #define CALCULATOR_DOWN BUTTON_DOWN
389 #define CALCULATOR_QUIT BUTTON_REC
390 #define CALCULATOR_INPUT BUTTON_OK
391 #define CALCULATOR_CALC BUTTON_PLAY
392 #define CALCULATOR_CLEAR BUTTON_CANCEL
394 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
395 #define CALCULATOR_LEFT BUTTON_REW
396 #define CALCULATOR_RIGHT BUTTON_FF
397 #define CALCULATOR_QUIT (BUTTON_REC|BUTTON_PLAY)
398 #define CALCULATOR_INPUT BUTTON_FUNC
399 #define CALCULATOR_CALC BUTTON_PLAY
401 #else
402 #error No keymap defined!
403 #endif
405 #ifdef HAVE_TOUCHSCREEN
406 #ifndef CALCULATOR_LEFT
407 #define CALCULATOR_LEFT BUTTON_MIDLEFT
408 #endif
409 #ifndef CALCULATOR_RIGHT
410 #define CALCULATOR_RIGHT BUTTON_MIDRIGHT
411 #endif
412 #ifndef CALCULATOR_UP
413 #define CALCULATOR_UP BUTTON_TOPMIDDLE
414 #endif
415 #ifndef CALCULATOR_DOWN
416 #define CALCULATOR_DOWN BUTTON_BOTTOMMIDDLE
417 #endif
418 #ifndef CALCULATOR_CALC
419 #define CALCULATOR_CALC BUTTON_BOTTOMRIGHT
420 #endif
421 #ifndef CALCULATOR_INPUT
422 #define CALCULATOR_INPUT BUTTON_CENTER
423 #endif
424 #ifndef CALCULATOR_CLEAR
425 #define CALCULATOR_CLEAR BUTTON_TOPRIGHT
426 #endif
428 #include "lib/pluginlib_touchscreen.h"
429 static struct ts_raster calc_raster = { X_0_POS, Y_1_POS,
430 BUTTON_COLS*REC_WIDTH, BUTTON_ROWS*REC_HEIGHT, REC_WIDTH, REC_HEIGHT };
431 #endif
433 enum {
434 basicButtons,
435 sciButtons
436 } buttonGroup;
438 unsigned char* buttonChar[2][5][5] = {
439 { { "MR" , "M+" , "2nd" , "CE" , "C" },
440 { "7" , "8" , "9" , "/" , "sqr" },
441 { "4" , "5" , "6" , "*" , "x^2" },
442 { "1" , "2" , "3" , "-" , "1/x" },
443 { "0" , "+/-", "." , "+" , "=" } },
445 { { "n!" , "PI" , "1st" , "sin" , "asi" },
446 { "7" , "8" , "9" , "cos" , "aco" },
447 { "4" , "5" , "6" , "tan" , "ata" },
448 { "1" , "2" , "3" , "ln" , "e^x" },
449 { "0" , "+/-", "." , "log" , "x^y" } }
452 enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C ,
453 btn_7 , btn_8 , btn_9 , btn_div , btn_sqr ,
454 btn_4 , btn_5 , btn_6 , btn_time , btn_square ,
455 btn_1 , btn_2 , btn_3 , btn_minus , btn_rec ,
456 btn_0 , btn_sign , btn_dot , btn_add , btn_equal
459 enum { sci_fac, sci_pi , sci_sci , sci_sin , sci_asin ,
460 sci_7 , sci_8 , sci_9 , sci_cos , sci_acos ,
461 sci_4 , sci_5 , sci_6 , sci_tan , sci_atan ,
462 sci_1 , sci_2 , sci_3 , sci_ln , sci_exp ,
463 sci_0 , sci_sign , sci_dot , sci_log , sci_xy
466 #define MINIMUM 0.000000000001 /* e-12 */
467 /* ^ ^ ^ ^ */
468 /* 123456789abcdef */
470 #define DIGITLEN 10 /* must <= 10 */
471 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
472 /* 0.000 00000 0001 */
473 /* ^ ^ ^ ^ ^ ^ */
474 /* DIGITLEN 12345 6789a bcdef */
475 /* power 12 34567 89abc def */
476 /* 10^- 123 45678 9abcd ef */
478 unsigned char buf[19];/* 18 bytes of output line,
479 buf[0] is operator
480 buf[1] = 'M' if memTemp is not 0
481 buf[2] = ' '
483 if SCIENTIFIC_FORMAT
484 buf[2]-buf[12] or buf[3]-buf[13] = result;
485 format X.XXXXXXXX
486 buf[13] or buf[14] -buf[17] = power;
487 format eXXX or e-XXX
488 else
489 buf[3]-buf[6] = ' ';
490 buf[7]-buf[17] = result;
492 buf[18] = '\0' */
494 unsigned char typingbuf[DIGITLEN+2];/* byte 0 is sign or ' ',
495 byte 1~DIGITLEN are num and '.'
496 byte (DIGITLEN+1) is '\0' */
497 unsigned char* typingbufPointer = typingbuf;
499 double result = 0; /* main operand, format 0.xxxxx */
500 int power = 0; /* 10^power */
501 double modifier = 0.1; /* position of next input */
502 double operand = 0; /* second operand, format 0.xxxxx */
503 int operandPower = 0; /* 10^power of second operand */
504 char oper = ' '; /* operators: + - * / */
505 bool operInputted = false; /* false: do calculation first and
506 replace current oper
507 true: just replace current oper */
509 double memTemp = 0; /* temp memory */
510 int memTempPower = 0; /* 10^^power of memTemp */
512 int btn_row, btn_col; /* current position index for button */
513 int prev_btn_row, prev_btn_col; /* previous cursor position */
514 #define CAL_BUTTON (btn_row*5+btn_col)
516 int btn = BUTTON_NONE;
517 int lastbtn = BUTTON_NONE;
519 /* Status of calculator */
520 enum {cal_normal, /* 0, normal status, display result */
521 cal_typing, /* 1, currently typing, dot hasn't been typed */
522 cal_dotted, /* 2, currently typing, dot already has been typed. */
523 cal_error,
524 cal_exit,
525 cal_toDo
526 } calStatus;
528 /* constant table for CORDIC algorithm */
529 double cordicTable[51][2]= {
530 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
531 {1e+00, 7.853981633974483e-01},
532 {5e-01, 4.636476090008061e-01},
533 {2.5e-01, 2.449786631268641e-01},
534 {1.25e-01, 1.243549945467614e-01},
535 {6.25e-02, 6.241880999595735e-02},
536 {3.125e-02, 3.123983343026828e-02},
537 {1.5625e-02, 1.562372862047683e-02},
538 {7.8125e-03, 7.812341060101111e-03},
539 {3.90625e-03, 3.906230131966972e-03},
540 {1.953125e-03, 1.953122516478819e-03},
541 {9.765625e-04, 9.765621895593195e-04},
542 {4.8828125e-04, 4.882812111948983e-04},
543 {2.44140625e-04, 2.441406201493618e-04},
544 {1.220703125e-04, 1.220703118936702e-04},
545 {6.103515625e-05, 6.103515617420877e-05},
546 {3.0517578125e-05, 3.051757811552610e-05},
547 {1.52587890625e-05, 1.525878906131576e-05},
548 {7.62939453125e-06, 7.629394531101970e-06},
549 {3.814697265625e-06, 3.814697265606496e-06},
550 {1.9073486328125e-06, 1.907348632810187e-06},
551 {9.5367431640625e-07, 9.536743164059608e-07},
552 {4.76837158203125e-07, 4.768371582030888e-07},
553 {2.384185791015625e-07, 2.384185791015580e-07},
554 {1.1920928955078125e-07, 1.192092895507807e-07},
555 {5.9604644775390625e-08, 5.960464477539055e-08},
556 {2.98023223876953125e-08, 2.980232238769530e-08},
557 {1.490116119384765625e-08, 1.490116119384765e-08},
558 {7.450580596923828125e-09, 7.450580596923828e-09},
559 {3.7252902984619140625e-09, 3.725290298461914e-09},
560 {1.86264514923095703125e-09, 1.862645149230957e-09},
561 {9.31322574615478515625e-10, 9.313225746154785e-10},
562 {4.656612873077392578125e-10, 4.656612873077393e-10},
563 {2.3283064365386962890625e-10, 2.328306436538696e-10},
564 {1.16415321826934814453125e-10, 1.164153218269348e-10},
565 {5.82076609134674072265625e-11, 5.820766091346741e-11},
566 {2.910383045673370361328125e-11, 2.910383045673370e-11},
567 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
568 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
569 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
570 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
571 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
572 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
573 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
574 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
575 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
576 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
577 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
578 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
579 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
580 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
581 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
584 void doMultiple(double* operandOne, int* powerOne,
585 double operandTwo, int powerTwo);
586 void doAdd (double* operandOne, int* powerOne,
587 double operandTwo, int powerTwo);
588 void printResult(void);
589 void formatResult(void);
590 void oneOperand(void);
592 void drawLines(void);
593 void drawButtons(int group);
595 /* -----------------------------------------------------------------------
596 Handy funtions
597 ----------------------------------------------------------------------- */
598 void cleartypingbuf(void)
600 int k;
601 for( k=1; k<=(DIGITLEN+1); k++)
602 typingbuf[k] = 0;
603 typingbuf[0] = ' ';
604 typingbufPointer = typingbuf+1;
606 void clearbuf(void)
608 int k;
609 for(k=0;k<18;k++)
610 buf[k]=' ';
611 buf[18] = 0;
613 void clearResult(void)
615 result = 0;
616 power = 0;
617 modifier = 0.1;
620 void clearInput(void)
622 calStatus = cal_normal;
623 clearResult();
624 cleartypingbuf();
625 rb->lcd_clear_display();
626 drawButtons(buttonGroup);
627 drawLines();
630 void clearOperand(void)
632 operand = 0;
633 operandPower = 0;
636 void clearMemTemp(void)
638 memTemp = 0;
639 memTempPower = 0;
642 void clearOper(void)
644 oper = ' ';
645 operInputted = false;
648 void clearMem(void)
650 clearInput();
651 clearMemTemp();
652 clearOperand();
653 clearOper();
654 btn = BUTTON_NONE;
657 void switchOperands(void)
659 double tempr = operand;
660 int tempp = operandPower;
661 operand = result;
662 operandPower = power;
663 result = tempr;
664 power = tempp;
667 void drawLines(void)
669 int i;
670 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS-1);
671 for (i = 0; i < 5 ; i++)
672 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS+i*REC_HEIGHT);
673 for (i = 0; i < 4 ; i++)
674 rb->lcd_vline(X_1_POS+i*REC_WIDTH, Y_1_POS, LCD_HEIGHT);
677 void drawButtons(int group)
679 int i, j, w, h;
680 for (i = 0; i <= 4; i++){
681 for (j = 0; j <= 4; j++){
682 rb->lcd_getstringsize( buttonChar[group][i][j],&w,&h);
683 if (i == btn_row && j == btn_col) /* selected item */
684 rb->lcd_set_drawmode(DRMODE_SOLID);
685 else
686 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
687 rb->lcd_fillrect( X_0_POS + j*REC_WIDTH,
688 Y_1_POS + i*REC_HEIGHT,
689 REC_WIDTH, REC_HEIGHT+1);
690 if (i == btn_row && j == btn_col) /* selected item */
691 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
692 else
693 rb->lcd_set_drawmode(DRMODE_SOLID);
694 rb->lcd_putsxy( X_0_POS + j*REC_WIDTH + (REC_WIDTH - w)/2,
695 Y_1_POS + i*REC_HEIGHT + (REC_HEIGHT - h)/2 + 1,
696 buttonChar[group][i][j] );
699 rb->lcd_set_drawmode(DRMODE_SOLID);
702 /* -----------------------------------------------------------------------
703 Initiate calculator
704 ----------------------------------------------------------------------- */
705 void cal_initial (void)
707 int w,h;
709 rb->lcd_getstringsize("2nd",&w,&h);
710 if (w > REC_WIDTH || h > REC_HEIGHT)
711 rb->lcd_setfont(FONT_SYSFIXED);
713 rb->lcd_clear_display();
715 #ifdef CALCULATOR_OPERATORS
716 /* basic operators are available through separate button */
717 buttonGroup = sciButtons;
718 #else
719 buttonGroup = basicButtons;
720 #endif
722 /* initially, invert button "5" */
723 btn_row = 2;
724 btn_col = 1;
725 prev_btn_row = btn_row;
726 prev_btn_col = btn_col;
727 drawButtons(buttonGroup);
728 drawLines();
729 rb->lcd_update();
731 /* initial mem and output display*/
732 clearMem();
733 printResult();
735 /* clear button queue */
736 rb->button_clear_queue();
739 /* -----------------------------------------------------------------------
740 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
741 in it's private case for sqrt.
742 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
743 ----------------------------------------------------------------------- */
744 double mySqrt(double square)
746 int k = 0;
747 double temp = 0;
748 double root= ABS(square+1)/2;
750 while( ABS(root - temp) > MINIMUM ){
751 temp = root;
752 root = (square/temp + temp)/2;
753 k++;
754 if (k>10000) return 0;
757 return root;
759 /* -----------------------------------------------------------------------
760 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
761 transcendFunc can do sin,cos,log,exp
762 input parameter is angle
763 ----------------------------------------------------------------------- */
764 void transcendFunc(char* func, double* tt, int* ttPower)
766 double t = (*tt)*M_PI/180; int tPower = *ttPower;
767 int sign = 1;
768 int n = 50; /* n <=50, tables are all <= 50 */
769 int j;
770 double x,y,z,xt,yt,zt;
772 if (tPower < -998) {
773 calStatus = cal_normal;
774 return;
776 if (tPower > 8) {
777 calStatus = cal_error;
778 return;
780 *ttPower = 0;
781 calStatus = cal_normal;
783 if( func[0] =='s' || func[0] =='S'|| func[0] =='t' || func[0] =='T')
784 sign = SIGN(t);
785 else {
786 /* if( func[0] =='c' || func[0] =='C') */
787 sign = 1;
789 t = ABS(t);
791 while (tPower > 0){
792 t *= 10;
793 tPower--;
795 while (tPower < 0) {
796 t /= 10;
797 tPower++;
799 j = 0;
800 while (t > j*M_TWOPI) {j++;}
801 t -= (j-1)*M_TWOPI;
802 if (M_PI_2 < t && t < 3*M_PI_2){
803 t = M_PI - t;
804 if (func[0] =='c' || func[0] =='C')
805 sign = -1;
806 else if (func[0] =='t' || func[0] =='T')
807 t*=-1;
809 else if ( 3*M_PI_2 <= t && t <= M_TWOPI)
810 t -= M_TWOPI;
812 x = 0.60725293500888; y = 0; z = t;
813 for (j=1;j<n+2;j++){
814 xt = x - SIGN(z) * y*cordicTable[j-1][0];
815 yt = y + SIGN(z) * x*cordicTable[j-1][0];
816 zt = z - SIGN(z) * cordicTable[j-1][1];
817 x = xt;
818 y=yt;
819 z=zt;
821 if( func[0] =='s' || func[0] =='S') {
822 *tt = sign*y;
823 return;
825 else if( func[0] =='c' || func[0] =='C') {
826 *tt = sign*x;
827 return;
829 else /*if( func[0] =='t' || func[0] =='T')*/ {
830 if(t==M_PI_2||t==-M_PI_2){
831 calStatus = cal_error;
832 return;
834 else{
835 *tt = sign*(y/x);
836 return;
841 /* -----------------------------------------------------------------------
842 add in scientific number format
843 ----------------------------------------------------------------------- */
844 void doAdd (double* operandOne, int* powerOne,
845 double operandTwo, int powerTwo)
847 if ( *powerOne >= powerTwo ){
848 if (*powerOne - powerTwo <= DIGITLEN+1){
849 while (powerTwo < *powerOne){
850 operandTwo /=10;
851 powerTwo++;
853 *operandOne += operandTwo;
855 /*do nothing if operandTwo is too small*/
857 else{
858 if (powerTwo - *powerOne <= DIGITLEN+1){
859 while(powerTwo > *powerOne){
860 *operandOne /=10;
861 (*powerOne)++;
863 (*operandOne) += operandTwo;
865 else{/* simply copy operandTwo if operandOne is too small */
866 *operandOne = operandTwo;
867 *powerOne = powerTwo;
871 /* -----------------------------------------------------------------------
872 multiple in scientific number format
873 ----------------------------------------------------------------------- */
874 void doMultiple(double* operandOne, int* powerOne,
875 double operandTwo, int powerTwo)
877 (*operandOne) *= operandTwo;
878 (*powerOne) += powerTwo;
881 /* -----------------------------------------------------------------------
882 Handles all one operand calculations
883 ----------------------------------------------------------------------- */
884 void oneOperand(void)
886 int k = 0;
887 if (buttonGroup == basicButtons){
888 switch(CAL_BUTTON){
889 case btn_sqr:
890 if (result<0)
891 calStatus = cal_error;
892 else{
893 if (power%2 == 1){
894 result = (mySqrt(result*10))/10;
895 power = (power+1) / 2;
897 else{
898 result = mySqrt(result);
899 power = power / 2;
901 calStatus = cal_normal;
903 break;
904 case btn_square:
905 power *= 2;
906 result *= result;
907 calStatus = cal_normal;
908 break;
910 case btn_rec:
911 if (result==0)
912 calStatus = cal_error;
913 else{
914 power = -power;
915 result = 1/result;
916 calStatus = cal_normal;
918 break;
919 default:
920 calStatus = cal_toDo;
921 break; /* just for the safety */
924 else{ /* sciButtons */
925 switch(CAL_BUTTON){
926 case sci_sin:
927 transcendFunc("sin", &result, &power);
928 break;
929 case sci_cos:
930 transcendFunc("cos", &result, &power);
931 break;
932 case sci_tan:
933 transcendFunc("tan", &result, &power);
934 break;
935 case sci_fac:
936 if (power<0 || power>8 || result<0 )
937 calStatus = cal_error;
938 else if(result == 0) {
939 result = 1;
940 power = 0;
942 else{
943 while(power > 0) {
944 result *= 10;
945 power--;
947 if ( ( result - (int)result) > MINIMUM )
948 calStatus = cal_error;
949 else {
950 k = result; result = 1;
951 while (k > 1){
952 doMultiple(&result, &power, k, 0);
953 formatResult();
954 k--;
956 calStatus = cal_normal;
959 break;
960 default:
961 calStatus = cal_toDo;
962 break; /* just for the safety */
968 /* -----------------------------------------------------------------------
969 Handles all two operands calculations
970 ----------------------------------------------------------------------- */
971 void twoOperands(void)
973 switch(oper){
974 case '-':
975 doAdd(&operand, &operandPower, -result, power);
976 break;
977 case '+':
978 doAdd(&operand, &operandPower, result, power);
979 break;
980 case '*':
981 doMultiple(&operand, &operandPower, result, power);
982 break;
983 case '/':
984 if ( ABS(result) > MINIMUM ){
985 doMultiple(&operand, &operandPower, 1/result, -power);
987 else
988 calStatus = cal_error;
989 break;
990 default: /* ' ' */
991 switchOperands(); /* counter switchOperands() below */
992 break;
993 } /* switch(oper) */
994 switchOperands();
995 clearOper();
998 /* First, increases *dimen1 by dimen1_delta modulo dimen1_modulo.
999 If dimen1 wraps, increases *dimen2 by dimen2_delta modulo dimen2_modulo.
1001 static void move_with_wrap_and_shift(
1002 int *dimen1, int dimen1_delta, int dimen1_modulo,
1003 int *dimen2, int dimen2_delta, int dimen2_modulo)
1005 bool wrapped = false;
1007 *dimen1 += dimen1_delta;
1008 if (*dimen1 < 0)
1010 *dimen1 = dimen1_modulo - 1;
1011 wrapped = true;
1013 else if (*dimen1 >= dimen1_modulo)
1015 *dimen1 = 0;
1016 wrapped = true;
1019 if (wrapped)
1021 /* Make the dividend always positive to be sure about the result.
1022 Adding dimen2_modulo does not change it since we do it modulo. */
1023 *dimen2 = (*dimen2 + dimen2_modulo + dimen2_delta) % dimen2_modulo;
1027 /* -----------------------------------------------------------------------
1028 Print buttons when switching 1st and 2nd
1029 int group = {basicButtons, sciButtons}
1030 ----------------------------------------------------------------------- */
1031 void printButtonGroups(int group)
1033 drawButtons(group);
1034 drawLines();
1035 rb->lcd_update();
1037 /* -----------------------------------------------------------------------
1038 flash the currently marked button
1039 ----------------------------------------------------------------------- */
1040 void flashButton(void)
1042 int k, w, h;
1043 for (k=2;k>0;k--)
1045 rb->lcd_getstringsize( buttonChar[buttonGroup][btn_row][btn_col],&w,&h);
1046 rb->lcd_set_drawmode(DRMODE_SOLID|(k==1) ? 0 : DRMODE_INVERSEVID);
1047 rb->lcd_fillrect( X_0_POS + btn_col*REC_WIDTH + 1,
1048 Y_1_POS + btn_row*REC_HEIGHT + 1,
1049 REC_WIDTH - 1, REC_HEIGHT - 1);
1050 rb->lcd_putsxy( X_0_POS + btn_col*REC_WIDTH + (REC_WIDTH - w)/2,
1051 Y_1_POS + btn_row*REC_HEIGHT + (REC_HEIGHT - h)/2 +1,
1052 buttonChar[buttonGroup][btn_row][btn_col] );
1053 rb->lcd_update_rect( X_0_POS + btn_col*REC_WIDTH + 1,
1054 Y_1_POS + btn_row*REC_HEIGHT + 1,
1055 REC_WIDTH - 1, REC_HEIGHT - 1);
1057 if (k!= 1)
1058 rb->sleep(HZ/22);
1063 /* -----------------------------------------------------------------------
1064 pos is the position that needs animation. pos = [1~18]
1065 ----------------------------------------------------------------------- */
1066 void deleteAnimation(int pos)
1068 int k, w, h, x;
1069 if (pos<1 || pos >18)
1070 return;
1072 rb->lcd_getstringsize("0", &w, &h);
1073 x = (pos==1? 4: LCD_WIDTH - 4 - w);
1075 for (k=0;k<4;k++){
1076 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1077 rb->lcd_fillrect(x, Y_1_POS - h -1, w, h);
1078 rb->lcd_set_drawmode(DRMODE_SOLID);
1079 rb->lcd_fillrect(x + (w*k)/8, Y_1_POS - h -1 + (h*k)/8,
1080 (w*(4-k))/4, (h*(4-k))/4);
1081 rb->lcd_update_rect(x, Y_1_POS - h -1, w, h);
1082 rb->sleep(HZ/32);
1086 /* -----------------------------------------------------------------------
1087 result may be one of these formats:
1089 xxxx.xxxx
1090 0.xxxx
1091 0.0000xxxx
1093 formatResult() change result to standard format: 0.xxxx
1094 if result is close to 0, let it be 0;
1095 if result is close to 1, let it be 0.1 and power++;
1096 ----------------------------------------------------------------------- */
1097 void formatResult(void)
1099 int resultsign = SIGN(result);
1100 result = ABS(result);
1101 if(result > MINIMUM ){ /* doesn't check power, might have problem
1102 input wouldn't,
1103 + - * / of two formatted number wouldn't.
1104 only a calculation that makes a formatted
1105 number (0.xxxx) less than MINIMUM in only
1106 one operation */
1108 if (result<1){
1109 while( (int)(result*10) == 0 ){
1110 result *= 10;
1111 power--;
1112 modifier *= 10;
1115 else{ /* result >= 1 */
1116 while( (int)result != 0 ){
1117 result /= 10;
1118 power++;
1119 modifier /= 10;
1121 } /* if result<1 */
1123 if (result > (1-MINIMUM)){
1124 result = 0.1;
1125 power++;
1126 modifier /= 10;
1128 result *= resultsign;
1130 else {
1131 result = 0;
1132 power = 0;
1133 modifier = 0.1;
1137 /* -----------------------------------------------------------------------
1138 result2typingbuf() outputs standard format result to typingbuf.
1139 case SCIENTIFIC_FORMAT, let temppower = 1;
1140 case temppower > 0: print '.' in the middle
1141 case temppower <= 0: print '.' in the begining
1142 ----------------------------------------------------------------------- */
1143 void result2typingbuf(void)
1145 bool haveDot = false;
1146 char tempchar = 0;
1147 int k;
1148 double tempresult = ABS(result); /* positive num makes things simple */
1150 int temppower;
1151 double tempmodifier = 1;
1152 int count;
1154 if(SCIENTIFIC_FORMAT)
1155 temppower = 1; /* output x.xxxx format */
1156 else
1157 temppower = power;
1159 cleartypingbuf();
1161 if(tempresult < MINIMUM){ /* if 0,faster display and avoid complication*/
1162 typingbuf[0] = ' ';
1163 typingbuf[1] = '0';
1165 else{ /* tempresult > 0 */
1166 typingbuf[0] = (SIGN(result)<0)?'-':' ';
1168 typingbufPointer = typingbuf;
1169 if(temppower > 0){
1170 for (k = 0; k<DIGITLEN+1 ; k++){
1171 typingbufPointer++;
1172 if(temppower || *(typingbufPointer-1) == '.'){
1173 count = 0;
1174 tempmodifier = tempmodifier/10;
1175 while( (tempresult-tempmodifier*count) >
1176 (tempmodifier-MINIMUM)){
1177 count++;
1179 tempresult -= tempmodifier*count;
1180 tempresult = ABS(tempresult);
1181 temppower-- ;
1182 *typingbufPointer = count + '0';
1184 else{ /* temppower == 0 */
1185 *typingbufPointer = '.';
1186 haveDot = true;
1188 } /* for */
1190 else{
1191 haveDot = true;
1192 typingbufPointer++; *typingbufPointer = '0';
1193 typingbufPointer++; *typingbufPointer = '.';
1194 for (k = 2; k<DIGITLEN+1 ; k++){
1195 typingbufPointer++;
1196 count = 0;
1197 if ( (-temppower) < (k-1)){
1198 tempmodifier = tempmodifier/10;
1199 while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){
1200 count++;
1203 tempresult -= tempmodifier*count;
1204 tempresult = ABS(tempresult);
1205 temppower-- ;
1207 *typingbufPointer = count + '0';
1210 /* now, typingbufPointer = typingbuf + 16 */
1211 /* backward strip off 0 and '.' */
1212 if (haveDot){
1213 while( (*typingbufPointer == '0') || (*typingbufPointer == '.')){
1214 tempchar = *typingbufPointer;
1215 *typingbufPointer = 0;
1216 typingbufPointer--;
1217 if (tempchar == '.') break;
1220 typingbuf[DIGITLEN+1] = 0;
1221 } /* else tempresult > 0 */
1224 /* -----------------------------------------------------------------------
1225 printResult() generates LCD display.
1226 ----------------------------------------------------------------------- */
1227 void printResult(void)
1229 int k, w, h;
1231 char operbuf[3] = {0, 0, 0};
1233 switch_Status:
1234 switch(calStatus){
1235 case cal_exit:
1236 rb->lcd_clear_display();
1237 rb->splash(HZ/3, "Bye now!");
1238 break;
1239 case cal_error:
1240 clearbuf();
1241 rb->snprintf(buf, 19, "%18s","Error");
1242 break;
1243 case cal_toDo:
1244 clearbuf();
1245 rb->snprintf(buf, 19, "%18s","Coming soon ^_* ");
1246 break;
1248 case cal_normal:
1249 formatResult();
1251 if( power > 1000 ){ /* power -1 > 999 */
1252 calStatus = cal_error;
1253 goto switch_Status;
1255 if (power < -998 ) /* power -1 < -999 */
1256 clearResult(); /* too small, let it be 0 */
1258 result2typingbuf();
1259 clearbuf();
1261 operbuf[0] = oper;
1262 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1263 operbuf[2] = '\0';
1265 if(SCIENTIFIC_FORMAT){
1266 /* output format: X.XXXX eXXX */
1267 if(power > -98){ /* power-1 >= -99, eXXX or e-XX */
1268 rb->snprintf(buf, 12, "%11s",typingbuf);
1269 for(k=11;k<=14;k++) buf[k] = ' ';
1270 cleartypingbuf();
1271 rb->snprintf(typingbuf, 5, "e%d",power-1);
1272 rb->snprintf(buf+11, 5, "%4s",typingbuf);
1274 else{ /* power-1 <= -100, e-XXX */
1275 rb->snprintf(buf, 12, "%11s",typingbuf);
1276 rb->snprintf(buf+11, 6, "e%d",power-1);
1279 else{
1280 rb->snprintf(buf, 12, "%11s",typingbuf);
1281 } /* if SCIENTIFIC_FORMAT */
1282 break;
1283 case cal_typing:
1284 case cal_dotted:
1285 clearbuf();
1286 operbuf[0] = oper;
1287 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1288 rb->snprintf(buf, 12, "%11s",typingbuf);
1289 break;
1293 rb->lcd_getstringsize(buf, &w, &h);
1294 rb->screen_clear_area(rb->screens[0], 0, 0, LCD_WIDTH, Y_1_POS - 1);
1295 rb->lcd_putsxy(4, Y_1_POS - h -1, operbuf);
1296 rb->lcd_putsxy(LCD_WIDTH - w - 4, Y_1_POS - h -1, buf);
1297 rb->lcd_update_rect(0, 1, LCD_WIDTH, Y_1_POS);
1300 /* -----------------------------------------------------------------------
1301 Process typing buttons: 1-9, '.', sign
1302 main operand "result" and typingbuf are processed seperately here.
1303 ----------------------------------------------------------------------- */
1304 void typingProcess(void){
1305 switch( CAL_BUTTON ){
1306 case btn_sign:
1307 if (calStatus == cal_typing ||
1308 calStatus == cal_dotted)
1309 typingbuf[0] = (typingbuf[0]=='-')?' ':'-';
1310 result = -result;
1311 break;
1312 case btn_dot:
1313 operInputted = false;
1314 switch(calStatus){
1315 case cal_normal:
1316 clearInput();
1317 *typingbufPointer = '0';
1318 typingbufPointer++;
1319 case cal_typing:
1320 calStatus = cal_dotted;
1321 *typingbufPointer = '.';
1322 if (typingbufPointer != typingbuf+DIGITLEN+1)
1323 typingbufPointer++;
1324 break;
1325 default: /* cal_dotted */
1326 break;
1328 break;
1329 default: /* 0-9 */
1330 operInputted = false;
1331 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1332 switch(calStatus){
1333 case cal_normal:
1334 if(CAL_BUTTON == btn_0 )
1335 break; /* first input is 0, ignore */
1336 clearInput();
1337 /*no operator means start a new calculation*/
1338 if (oper ==' ')
1339 clearOperand();
1340 calStatus = cal_typing;
1341 /* go on typing, no break */
1342 case cal_typing:
1343 case cal_dotted:
1344 switch(CAL_BUTTON){
1345 case btn_0:
1346 *typingbufPointer = '0';
1347 break;
1348 default:
1349 *typingbufPointer=(7+btn_col-3*(btn_row-1))+ '0';
1350 break;
1352 if (typingbufPointer!=typingbuf+DIGITLEN+1){
1353 typingbufPointer++;
1355 {/* result processing */
1356 if (calStatus == cal_typing) power++;
1357 if (CAL_BUTTON != btn_0)
1358 result= result +
1359 SIGN(result)*
1360 (7+btn_col-3*(btn_row-1))*modifier;
1361 modifier /= 10;
1364 else /* last byte always '\0' */
1365 *typingbufPointer = 0;
1366 break;
1367 default: /* cal_error, cal_exit */
1368 break;
1370 break; /* default, 0-9 */
1371 } /* switch( CAL_BUTTON ) */
1374 /* -----------------------------------------------------------------------
1375 Handle delete operation
1376 main operand "result" and typingbuf are processed seperately here.
1377 ----------------------------------------------------------------------- */
1378 void doDelete(void){
1379 deleteAnimation(18);
1380 switch(calStatus){
1381 case cal_dotted:
1382 if (*(typingbufPointer-1) == '.'){
1383 /* if dotted and deleting '.',
1384 change status and delete '.' below */
1385 calStatus = cal_typing;
1387 else{ /* if dotted and not deleting '.',
1388 power stays */
1389 power++; /* counter "power--;" below */
1391 case cal_typing:
1392 typingbufPointer--;
1394 {/* result processing */ /* 0-9, '.' */
1395 /* if deleting '.', do nothing */
1396 if ( *typingbufPointer != '.'){
1397 power--;
1398 modifier *= 10;
1399 result = result - SIGN(result)*
1400 ((*typingbufPointer)- '0')*modifier;
1404 *typingbufPointer = 0;
1406 /* if (only one digit left and it's 0)
1407 or no digit left, change status*/
1408 if ( typingbufPointer == typingbuf+1 ||
1409 ( typingbufPointer == typingbuf+2 &&
1410 *(typingbufPointer-1) == '0' ))
1411 calStatus = cal_normal;
1412 break;
1413 default: /* normal, error, exit */
1414 break;
1417 /* -----------------------------------------------------------------------
1418 Handle buttons on basic screen
1419 ----------------------------------------------------------------------- */
1420 void basicButtonsProcess(void){
1421 switch (btn) {
1422 case CALCULATOR_INPUT:
1423 if (calStatus == cal_error && (CAL_BUTTON != btn_C) ) break;
1424 flashButton();
1425 switch( CAL_BUTTON ){
1426 case btn_MR:
1427 operInputted = false;
1428 result = memTemp; power = memTempPower;
1429 calStatus = cal_normal;
1430 break;
1431 case btn_M:
1432 formatResult();
1433 if (memTemp > MINIMUM)
1434 doAdd(&memTemp, &memTempPower, result, power);
1435 else {
1436 /* if result is too small and memTemp = 0,
1437 doAdd will not add */
1438 memTemp = result;
1439 memTempPower = power;
1441 calStatus = cal_normal;
1442 break;
1444 case btn_C: clearMem(); break;
1445 case btn_CE: clearInput(); break;
1447 case btn_bas:
1448 buttonGroup = sciButtons;
1449 printButtonGroups(buttonGroup);
1450 break;
1452 /* one operand calculation, may be changed to
1453 like sin, cos, log, etc */
1454 case btn_sqr:
1455 case btn_square:
1456 case btn_rec:
1457 formatResult(); /* not necessary, just for safty */
1458 oneOperand();
1459 break;
1461 case_btn_equal: /* F3 shortkey entrance */
1462 case btn_equal:
1463 formatResult();
1464 calStatus = cal_normal;
1465 operInputted = false;
1466 if (oper != ' ') twoOperands();
1467 break;
1469 case btn_div:
1470 case btn_time:
1471 case btn_minus:
1472 case btn_add:
1473 if(!operInputted) {twoOperands(); operInputted = true;}
1474 oper = buttonChar[basicButtons][btn_row][btn_col][0];
1475 #ifdef CALCULATOR_OPERATORS
1476 case_cycle_operators: /* F2 shortkey entrance */
1477 #endif
1478 calStatus = cal_normal;
1479 formatResult();
1480 operand = result;
1481 operandPower = power;
1483 break;
1485 case btn_sign:
1486 case btn_dot:
1487 default: /* 0-9 */
1488 typingProcess();
1489 break;
1490 } /* switch (CAL_BUTTON) */
1491 break;
1493 #ifdef CALCULATOR_OPERATORS
1494 case CALCULATOR_OPERATORS:
1495 if (calStatus == cal_error) break;
1496 if (!operInputted) {twoOperands(); operInputted = true;}
1497 switch (oper){
1498 case ' ':
1499 case '/': oper = '+'; flashButton(); break;
1500 case '+': oper = '-'; flashButton(); break;
1501 case '-': oper = '*'; flashButton(); break;
1502 case '*': oper = '/'; flashButton(); break;
1504 goto case_cycle_operators;
1505 break;
1506 #endif
1508 case CALCULATOR_CALC:
1509 if (calStatus == cal_error) break;
1510 flashButton();
1511 goto case_btn_equal;
1512 break;
1513 default: break;
1515 printResult();
1518 /* -----------------------------------------------------------------------
1519 Handle buttons on scientific screen
1520 ----------------------------------------------------------------------- */
1521 void sciButtonsProcess(void){
1522 switch (btn) {
1523 case CALCULATOR_INPUT:
1524 if (calStatus == cal_error && (CAL_BUTTON != sci_sci) ) break;
1525 flashButton();
1526 switch( CAL_BUTTON ){
1528 case sci_pi:
1529 result = M_PI; power = 0;
1530 calStatus = cal_normal;
1531 break;
1533 case sci_xy: break;
1535 case sci_sci:
1536 buttonGroup = basicButtons;
1537 printButtonGroups(basicButtons);
1538 break;
1540 case sci_fac:
1541 case sci_sin:
1542 case sci_asin:
1543 case sci_cos:
1544 case sci_acos:
1545 case sci_tan:
1546 case sci_atan:
1547 case sci_ln:
1548 case sci_exp:
1549 case sci_log:
1550 formatResult(); /* not necessary, just for safty */
1551 oneOperand();
1552 break;
1554 case btn_sign:
1555 case btn_dot:
1556 default: /* 0-9 */
1557 typingProcess();
1558 break;
1559 } /* switch (CAL_BUTTON) */
1560 break;
1562 #ifdef CALCULATOR_OPERATORS
1563 case CALCULATOR_OPERATORS:
1564 if (calStatus == cal_error) break;
1565 if (!operInputted) {twoOperands(); operInputted = true;}
1566 switch (oper){
1567 case ' ': oper = '+'; break;
1568 case '/': oper = '+'; deleteAnimation(1); break;
1569 case '+': oper = '-'; deleteAnimation(1); break;
1570 case '-': oper = '*'; deleteAnimation(1); break;
1571 case '*': oper = '/'; deleteAnimation(1); break;
1573 calStatus = cal_normal;
1574 formatResult();
1575 operand = result;
1576 operandPower = power;
1577 break;
1578 #endif
1580 case CALCULATOR_CALC:
1581 if (calStatus == cal_error) break;
1582 formatResult();
1583 calStatus = cal_normal;
1584 operInputted = false;
1585 if (oper != ' ') twoOperands();
1586 break;
1587 default: break;
1589 printResult();
1592 /* -----------------------------------------------------------------------
1593 move button index
1594 Invert display new button, invert back previous button
1595 ----------------------------------------------------------------------- */
1596 int handleButton(int button){
1597 switch(button)
1599 case CALCULATOR_INPUT:
1600 case CALCULATOR_CALC:
1601 #ifdef CALCULATOR_INPUT_CALC_PRE
1602 if (lastbtn != CALCULATOR_INPUT_CALC_PRE)
1603 break;
1604 /* no unconditional break; here! */
1605 #endif
1606 #ifdef CALCULATOR_OPERATORS
1607 case CALCULATOR_OPERATORS:
1608 #endif
1609 switch(buttonGroup){
1610 case basicButtons:
1611 basicButtonsProcess();
1612 break;
1613 case sciButtons:
1614 sciButtonsProcess();
1615 break;
1617 break;
1619 #ifdef CALCULATOR_CLEAR
1620 case CALCULATOR_CLEAR:
1621 switch(calStatus){
1622 case cal_typing:
1623 case cal_dotted:
1624 doDelete();
1625 break;
1626 default: /* cal_normal, cal_error, cal_exit */
1627 clearMem();
1628 break;
1630 printResult();
1631 break;
1632 #endif
1633 case CALCULATOR_LEFT:
1634 case CALCULATOR_LEFT | BUTTON_REPEAT:
1635 move_with_wrap_and_shift(
1636 &btn_col, -1, BUTTON_COLS,
1637 &btn_row, 0, BUTTON_ROWS);
1638 break;
1640 case CALCULATOR_RIGHT:
1641 case CALCULATOR_RIGHT | BUTTON_REPEAT:
1642 move_with_wrap_and_shift(
1643 &btn_col, 1, BUTTON_COLS,
1644 &btn_row, 0, BUTTON_ROWS);
1645 break;
1647 #ifdef CALCULATOR_UP
1648 case CALCULATOR_UP:
1649 case CALCULATOR_UP | BUTTON_REPEAT:
1650 move_with_wrap_and_shift(
1651 &btn_row, -1, BUTTON_ROWS,
1652 &btn_col, 0, BUTTON_COLS);
1653 break;
1654 #endif
1655 #ifdef CALCULATOR_DOWN
1656 case CALCULATOR_DOWN:
1657 case CALCULATOR_DOWN | BUTTON_REPEAT:
1658 move_with_wrap_and_shift(
1659 &btn_row, 1, BUTTON_ROWS,
1660 &btn_col, 0, BUTTON_COLS);
1661 break;
1662 #endif
1664 #ifdef CALCULATOR_UP_W_SHIFT
1665 case CALCULATOR_UP_W_SHIFT:
1666 case CALCULATOR_UP_W_SHIFT | BUTTON_REPEAT:
1667 move_with_wrap_and_shift(
1668 &btn_row, -1, BUTTON_ROWS,
1669 &btn_col, -1, BUTTON_COLS);
1670 break;
1671 #endif
1672 #ifdef CALCULATOR_DOWN_W_SHIFT
1673 case CALCULATOR_DOWN_W_SHIFT:
1674 case CALCULATOR_DOWN_W_SHIFT | BUTTON_REPEAT:
1675 move_with_wrap_and_shift(
1676 &btn_row, 1, BUTTON_ROWS,
1677 &btn_col, 1, BUTTON_COLS);
1678 break;
1679 #endif
1680 #ifdef CALCULATOR_LEFT_W_SHIFT
1681 case CALCULATOR_LEFT_W_SHIFT:
1682 case CALCULATOR_LEFT_W_SHIFT | BUTTON_REPEAT:
1683 move_with_wrap_and_shift(
1684 &btn_col, -1, BUTTON_COLS,
1685 &btn_row, -1, BUTTON_ROWS);
1686 break;
1687 #endif
1688 #ifdef CALCULATOR_RIGHT_W_SHIFT
1689 case CALCULATOR_RIGHT_W_SHIFT:
1690 case CALCULATOR_RIGHT_W_SHIFT | BUTTON_REPEAT:
1691 move_with_wrap_and_shift(
1692 &btn_col, 1, BUTTON_COLS,
1693 &btn_row, 1, BUTTON_ROWS);
1694 break;
1695 #endif
1696 #ifdef CALCULATOR_RC_QUIT
1697 case CALCULATOR_RC_QUIT:
1698 #endif
1699 case CALCULATOR_QUIT:
1700 return -1;
1703 return 0;
1705 prev_btn_row = btn_row;
1706 prev_btn_col = btn_col;
1709 /* -----------------------------------------------------------------------
1710 Main();
1711 ----------------------------------------------------------------------- */
1712 enum plugin_status plugin_start(const void* parameter)
1714 (void)parameter;
1716 /* now go ahead and have fun! */
1718 #ifdef HAVE_TOUCHSCREEN
1719 rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
1720 #endif
1722 cal_initial();
1724 while (calStatus != cal_exit ) {
1725 btn = rb->button_get_w_tmo(HZ/2);
1726 #ifdef HAVE_TOUCHSCREEN
1727 if(btn & BUTTON_TOUCHSCREEN)
1729 struct ts_raster_result res;
1730 if(touchscreen_map_raster(&calc_raster, rb->button_get_data() >> 16,
1731 rb->button_get_data() & 0xffff, &res) == 1)
1733 btn_row = res.y;
1734 btn_col = res.x;
1735 drawButtons(buttonGroup);
1736 drawLines();
1738 rb->lcd_update();
1740 prev_btn_row = btn_row;
1741 prev_btn_col = btn_col;
1742 if(btn & BUTTON_REL)
1744 btn = CALCULATOR_INPUT;
1745 switch(buttonGroup){
1746 case basicButtons:
1747 basicButtonsProcess();
1748 break;
1749 case sciButtons:
1750 sciButtonsProcess();
1751 break;
1753 btn = BUTTON_TOUCHSCREEN;
1757 #endif
1758 if (handleButton(btn) == -1)
1760 calStatus = cal_exit;
1761 printResult();
1763 else
1765 drawButtons(buttonGroup);
1766 drawLines();
1769 rb->lcd_update();
1771 if(rb->default_event_handler(btn) == SYS_USB_CONNECTED)
1772 return PLUGIN_USB_CONNECTED;
1774 if (btn != BUTTON_NONE)
1775 lastbtn = btn;
1776 } /* while (calStatus != cal_exit ) */
1778 rb->button_clear_queue();
1779 return PLUGIN_OK;