new skin token: %cx - 24 hour time format enabled in the setting.. e.g %?cx<24 hour...
[kugel-rb.git] / apps / plugins / calculator.c
bloba14748f40919cd24fa76db7216efeeaddae78106
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 == COWOND2_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 == ONDAVX747_PAD)
335 #define CALCULATOR_QUIT BUTTON_POWER
336 #define CALCULATOR_CLEAR BUTTON_MENU
338 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
339 #define CALCULATOR_QUIT BUTTON_POWER
341 #elif CONFIG_KEYPAD == MROBE500_PAD
342 #define CALCULATOR_QUIT BUTTON_POWER
344 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
346 #define CALCULATOR_LEFT BUTTON_LEFT
347 #define CALCULATOR_RIGHT BUTTON_RIGHT
348 #define CALCULATOR_UP BUTTON_UP
349 #define CALCULATOR_DOWN BUTTON_DOWN
350 #define CALCULATOR_QUIT BUTTON_REC
351 #define CALCULATOR_INPUT BUTTON_PLAY
352 #define CALCULATOR_CALC BUTTON_FFWD
353 #define CALCULATOR_CLEAR BUTTON_REW
355 #else
356 #error No keymap defined!
357 #endif
359 #ifdef HAVE_TOUCHSCREEN
360 #ifndef CALCULATOR_LEFT
361 #define CALCULATOR_LEFT BUTTON_MIDLEFT
362 #endif
363 #ifndef CALCULATOR_RIGHT
364 #define CALCULATOR_RIGHT BUTTON_MIDRIGHT
365 #endif
366 #ifndef CALCULATOR_UP
367 #define CALCULATOR_UP BUTTON_TOPMIDDLE
368 #endif
369 #ifndef CALCULATOR_DOWN
370 #define CALCULATOR_DOWN BUTTON_BOTTOMMIDDLE
371 #endif
372 #ifndef CALCULATOR_CALC
373 #define CALCULATOR_CALC BUTTON_BOTTOMRIGHT
374 #endif
375 #ifndef CALCULATOR_INPUT
376 #define CALCULATOR_INPUT BUTTON_CENTER
377 #endif
378 #ifndef CALCULATOR_CLEAR
379 #define CALCULATOR_CLEAR BUTTON_TOPRIGHT
380 #endif
382 #include "lib/pluginlib_touchscreen.h"
383 static struct ts_raster calc_raster = { X_0_POS, Y_1_POS,
384 BUTTON_COLS*REC_WIDTH, BUTTON_ROWS*REC_HEIGHT, REC_WIDTH, REC_HEIGHT };
385 #endif
387 enum {
388 basicButtons,
389 sciButtons
390 } buttonGroup;
392 unsigned char* buttonChar[2][5][5] = {
393 { { "MR" , "M+" , "2nd" , "CE" , "C" },
394 { "7" , "8" , "9" , "/" , "sqr" },
395 { "4" , "5" , "6" , "*" , "x^2" },
396 { "1" , "2" , "3" , "-" , "1/x" },
397 { "0" , "+/-", "." , "+" , "=" } },
399 { { "n!" , "PI" , "1st" , "sin" , "asi" },
400 { "7" , "8" , "9" , "cos" , "aco" },
401 { "4" , "5" , "6" , "tan" , "ata" },
402 { "1" , "2" , "3" , "ln" , "e^x" },
403 { "0" , "+/-", "." , "log" , "x^y" } }
406 enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C ,
407 btn_7 , btn_8 , btn_9 , btn_div , btn_sqr ,
408 btn_4 , btn_5 , btn_6 , btn_time , btn_square ,
409 btn_1 , btn_2 , btn_3 , btn_minus , btn_rec ,
410 btn_0 , btn_sign , btn_dot , btn_add , btn_equal
413 enum { sci_fac, sci_pi , sci_sci , sci_sin , sci_asin ,
414 sci_7 , sci_8 , sci_9 , sci_cos , sci_acos ,
415 sci_4 , sci_5 , sci_6 , sci_tan , sci_atan ,
416 sci_1 , sci_2 , sci_3 , sci_ln , sci_exp ,
417 sci_0 , sci_sign , sci_dot , sci_log , sci_xy
420 #define MINIMUM 0.000000000001 /* e-12 */
421 /* ^ ^ ^ ^ */
422 /* 123456789abcdef */
424 #define DIGITLEN 10 /* must <= 10 */
425 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
426 /* 0.000 00000 0001 */
427 /* ^ ^ ^ ^ ^ ^ */
428 /* DIGITLEN 12345 6789a bcdef */
429 /* power 12 34567 89abc def */
430 /* 10^- 123 45678 9abcd ef */
432 unsigned char buf[19];/* 18 bytes of output line,
433 buf[0] is operator
434 buf[1] = 'M' if memTemp is not 0
435 buf[2] = ' '
437 if SCIENTIFIC_FORMAT
438 buf[2]-buf[12] or buf[3]-buf[13] = result;
439 format X.XXXXXXXX
440 buf[13] or buf[14] -buf[17] = power;
441 format eXXX or e-XXX
442 else
443 buf[3]-buf[6] = ' ';
444 buf[7]-buf[17] = result;
446 buf[18] = '\0' */
448 unsigned char typingbuf[DIGITLEN+2];/* byte 0 is sign or ' ',
449 byte 1~DIGITLEN are num and '.'
450 byte (DIGITLEN+1) is '\0' */
451 unsigned char* typingbufPointer = typingbuf;
453 double result = 0; /* main operand, format 0.xxxxx */
454 int power = 0; /* 10^power */
455 double modifier = 0.1; /* position of next input */
456 double operand = 0; /* second operand, format 0.xxxxx */
457 int operandPower = 0; /* 10^power of second operand */
458 char oper = ' '; /* operators: + - * / */
459 bool operInputted = false; /* false: do calculation first and
460 replace current oper
461 true: just replace current oper */
463 double memTemp = 0; /* temp memory */
464 int memTempPower = 0; /* 10^^power of memTemp */
466 int btn_row, btn_col; /* current position index for button */
467 int prev_btn_row, prev_btn_col; /* previous cursor position */
468 #define CAL_BUTTON (btn_row*5+btn_col)
470 int btn = BUTTON_NONE;
471 int lastbtn = BUTTON_NONE;
473 /* Status of calculator */
474 enum {cal_normal, /* 0, normal status, display result */
475 cal_typing, /* 1, currently typing, dot hasn't been typed */
476 cal_dotted, /* 2, currently typing, dot already has been typed. */
477 cal_error,
478 cal_exit,
479 cal_toDo
480 } calStatus;
482 /* constant table for CORDIC algorithm */
483 double cordicTable[51][2]= {
484 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
485 {1e+00, 7.853981633974483e-01},
486 {5e-01, 4.636476090008061e-01},
487 {2.5e-01, 2.449786631268641e-01},
488 {1.25e-01, 1.243549945467614e-01},
489 {6.25e-02, 6.241880999595735e-02},
490 {3.125e-02, 3.123983343026828e-02},
491 {1.5625e-02, 1.562372862047683e-02},
492 {7.8125e-03, 7.812341060101111e-03},
493 {3.90625e-03, 3.906230131966972e-03},
494 {1.953125e-03, 1.953122516478819e-03},
495 {9.765625e-04, 9.765621895593195e-04},
496 {4.8828125e-04, 4.882812111948983e-04},
497 {2.44140625e-04, 2.441406201493618e-04},
498 {1.220703125e-04, 1.220703118936702e-04},
499 {6.103515625e-05, 6.103515617420877e-05},
500 {3.0517578125e-05, 3.051757811552610e-05},
501 {1.52587890625e-05, 1.525878906131576e-05},
502 {7.62939453125e-06, 7.629394531101970e-06},
503 {3.814697265625e-06, 3.814697265606496e-06},
504 {1.9073486328125e-06, 1.907348632810187e-06},
505 {9.5367431640625e-07, 9.536743164059608e-07},
506 {4.76837158203125e-07, 4.768371582030888e-07},
507 {2.384185791015625e-07, 2.384185791015580e-07},
508 {1.1920928955078125e-07, 1.192092895507807e-07},
509 {5.9604644775390625e-08, 5.960464477539055e-08},
510 {2.98023223876953125e-08, 2.980232238769530e-08},
511 {1.490116119384765625e-08, 1.490116119384765e-08},
512 {7.450580596923828125e-09, 7.450580596923828e-09},
513 {3.7252902984619140625e-09, 3.725290298461914e-09},
514 {1.86264514923095703125e-09, 1.862645149230957e-09},
515 {9.31322574615478515625e-10, 9.313225746154785e-10},
516 {4.656612873077392578125e-10, 4.656612873077393e-10},
517 {2.3283064365386962890625e-10, 2.328306436538696e-10},
518 {1.16415321826934814453125e-10, 1.164153218269348e-10},
519 {5.82076609134674072265625e-11, 5.820766091346741e-11},
520 {2.910383045673370361328125e-11, 2.910383045673370e-11},
521 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
522 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
523 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
524 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
525 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
526 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
527 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
528 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
529 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
530 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
531 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
532 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
533 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
534 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
535 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
538 void doMultiple(double* operandOne, int* powerOne,
539 double operandTwo, int powerTwo);
540 void doAdd (double* operandOne, int* powerOne,
541 double operandTwo, int powerTwo);
542 void printResult(void);
543 void formatResult(void);
544 void oneOperand(void);
546 void drawLines(void);
547 void drawButtons(int group);
549 /* -----------------------------------------------------------------------
550 Handy funtions
551 ----------------------------------------------------------------------- */
552 void cleartypingbuf(void)
554 int k;
555 for( k=1; k<=(DIGITLEN+1); k++)
556 typingbuf[k] = 0;
557 typingbuf[0] = ' ';
558 typingbufPointer = typingbuf+1;
560 void clearbuf(void)
562 int k;
563 for(k=0;k<18;k++)
564 buf[k]=' ';
565 buf[18] = 0;
567 void clearResult(void)
569 result = 0;
570 power = 0;
571 modifier = 0.1;
574 void clearInput(void)
576 calStatus = cal_normal;
577 clearResult();
578 cleartypingbuf();
579 rb->lcd_clear_display();
580 drawButtons(buttonGroup);
581 drawLines();
584 void clearOperand(void)
586 operand = 0;
587 operandPower = 0;
590 void clearMemTemp(void)
592 memTemp = 0;
593 memTempPower = 0;
596 void clearOper(void)
598 oper = ' ';
599 operInputted = false;
602 void clearMem(void)
604 clearInput();
605 clearMemTemp();
606 clearOperand();
607 clearOper();
608 btn = BUTTON_NONE;
611 void switchOperands(void)
613 double tempr = operand;
614 int tempp = operandPower;
615 operand = result;
616 operandPower = power;
617 result = tempr;
618 power = tempp;
621 void drawLines(void)
623 int i;
624 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS-1);
625 for (i = 0; i < 5 ; i++)
626 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS+i*REC_HEIGHT);
627 for (i = 0; i < 4 ; i++)
628 rb->lcd_vline(X_1_POS+i*REC_WIDTH, Y_1_POS, LCD_HEIGHT);
631 void drawButtons(int group)
633 int i, j, w, h;
634 for (i = 0; i <= 4; i++){
635 for (j = 0; j <= 4; j++){
636 rb->lcd_getstringsize( buttonChar[group][i][j],&w,&h);
637 if (i == btn_row && j == btn_col) /* selected item */
638 rb->lcd_set_drawmode(DRMODE_SOLID);
639 else
640 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
641 rb->lcd_fillrect( X_0_POS + j*REC_WIDTH,
642 Y_1_POS + i*REC_HEIGHT,
643 REC_WIDTH, REC_HEIGHT+1);
644 if (i == btn_row && j == btn_col) /* selected item */
645 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
646 else
647 rb->lcd_set_drawmode(DRMODE_SOLID);
648 rb->lcd_putsxy( X_0_POS + j*REC_WIDTH + (REC_WIDTH - w)/2,
649 Y_1_POS + i*REC_HEIGHT + (REC_HEIGHT - h)/2 + 1,
650 buttonChar[group][i][j] );
653 rb->lcd_set_drawmode(DRMODE_SOLID);
656 /* -----------------------------------------------------------------------
657 Initiate calculator
658 ----------------------------------------------------------------------- */
659 void cal_initial (void)
661 int w,h;
663 rb->lcd_getstringsize("2nd",&w,&h);
664 if (w > REC_WIDTH || h > REC_HEIGHT)
665 rb->lcd_setfont(FONT_SYSFIXED);
667 rb->lcd_clear_display();
669 #ifdef CALCULATOR_OPERATORS
670 /* basic operators are available through separate button */
671 buttonGroup = sciButtons;
672 #else
673 buttonGroup = basicButtons;
674 #endif
676 /* initially, invert button "5" */
677 btn_row = 2;
678 btn_col = 1;
679 prev_btn_row = btn_row;
680 prev_btn_col = btn_col;
681 drawButtons(buttonGroup);
682 drawLines();
683 rb->lcd_update();
685 /* initial mem and output display*/
686 clearMem();
687 printResult();
689 /* clear button queue */
690 rb->button_clear_queue();
693 /* -----------------------------------------------------------------------
694 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
695 in it's private case for sqrt.
696 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
697 ----------------------------------------------------------------------- */
698 double mySqrt(double square)
700 int k = 0;
701 double temp = 0;
702 double root= ABS(square+1)/2;
704 while( ABS(root - temp) > MINIMUM ){
705 temp = root;
706 root = (square/temp + temp)/2;
707 k++;
708 if (k>10000) return 0;
711 return root;
713 /* -----------------------------------------------------------------------
714 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
715 transcendFunc can do sin,cos,log,exp
716 input parameter is angle
717 ----------------------------------------------------------------------- */
718 void transcendFunc(char* func, double* tt, int* ttPower)
720 double t = (*tt)*M_PI/180; int tPower = *ttPower;
721 int sign = 1;
722 int n = 50; /* n <=50, tables are all <= 50 */
723 int j;
724 double x,y,z,xt,yt,zt;
726 if (tPower < -998) {
727 calStatus = cal_normal;
728 return;
730 if (tPower > 8) {
731 calStatus = cal_error;
732 return;
734 *ttPower = 0;
735 calStatus = cal_normal;
737 if( func[0] =='s' || func[0] =='S'|| func[0] =='t' || func[0] =='T')
738 sign = SIGN(t);
739 else {
740 /* if( func[0] =='c' || func[0] =='C') */
741 sign = 1;
743 t = ABS(t);
745 while (tPower > 0){
746 t *= 10;
747 tPower--;
749 while (tPower < 0) {
750 t /= 10;
751 tPower++;
753 j = 0;
754 while (t > j*M_TWOPI) {j++;}
755 t -= (j-1)*M_TWOPI;
756 if (M_PI_2 < t && t < 3*M_PI_2){
757 t = M_PI - t;
758 if (func[0] =='c' || func[0] =='C')
759 sign = -1;
760 else if (func[0] =='t' || func[0] =='T')
761 t*=-1;
763 else if ( 3*M_PI_2 <= t && t <= M_TWOPI)
764 t -= M_TWOPI;
766 x = 0.60725293500888; y = 0; z = t;
767 for (j=1;j<n+2;j++){
768 xt = x - SIGN(z) * y*cordicTable[j-1][0];
769 yt = y + SIGN(z) * x*cordicTable[j-1][0];
770 zt = z - SIGN(z) * cordicTable[j-1][1];
771 x = xt;
772 y=yt;
773 z=zt;
775 if( func[0] =='s' || func[0] =='S') {
776 *tt = sign*y;
777 return;
779 else if( func[0] =='c' || func[0] =='C') {
780 *tt = sign*x;
781 return;
783 else /*if( func[0] =='t' || func[0] =='T')*/ {
784 if(t==M_PI_2||t==-M_PI_2){
785 calStatus = cal_error;
786 return;
788 else{
789 *tt = sign*(y/x);
790 return;
795 /* -----------------------------------------------------------------------
796 add in scientific number format
797 ----------------------------------------------------------------------- */
798 void doAdd (double* operandOne, int* powerOne,
799 double operandTwo, int powerTwo)
801 if ( *powerOne >= powerTwo ){
802 if (*powerOne - powerTwo <= DIGITLEN+1){
803 while (powerTwo < *powerOne){
804 operandTwo /=10;
805 powerTwo++;
807 *operandOne += operandTwo;
809 /*do nothing if operandTwo is too small*/
811 else{
812 if (powerTwo - *powerOne <= DIGITLEN+1){
813 while(powerTwo > *powerOne){
814 *operandOne /=10;
815 (*powerOne)++;
817 (*operandOne) += operandTwo;
819 else{/* simply copy operandTwo if operandOne is too small */
820 *operandOne = operandTwo;
821 *powerOne = powerTwo;
825 /* -----------------------------------------------------------------------
826 multiple in scientific number format
827 ----------------------------------------------------------------------- */
828 void doMultiple(double* operandOne, int* powerOne,
829 double operandTwo, int powerTwo)
831 (*operandOne) *= operandTwo;
832 (*powerOne) += powerTwo;
835 /* -----------------------------------------------------------------------
836 Handles all one operand calculations
837 ----------------------------------------------------------------------- */
838 void oneOperand(void)
840 int k = 0;
841 if (buttonGroup == basicButtons){
842 switch(CAL_BUTTON){
843 case btn_sqr:
844 if (result<0)
845 calStatus = cal_error;
846 else{
847 if (power%2 == 1){
848 result = (mySqrt(result*10))/10;
849 power = (power+1) / 2;
851 else{
852 result = mySqrt(result);
853 power = power / 2;
855 calStatus = cal_normal;
857 break;
858 case btn_square:
859 power *= 2;
860 result *= result;
861 calStatus = cal_normal;
862 break;
864 case btn_rec:
865 if (result==0)
866 calStatus = cal_error;
867 else{
868 power = -power;
869 result = 1/result;
870 calStatus = cal_normal;
872 break;
873 default:
874 calStatus = cal_toDo;
875 break; /* just for the safety */
878 else{ /* sciButtons */
879 switch(CAL_BUTTON){
880 case sci_sin:
881 transcendFunc("sin", &result, &power);
882 break;
883 case sci_cos:
884 transcendFunc("cos", &result, &power);
885 break;
886 case sci_tan:
887 transcendFunc("tan", &result, &power);
888 break;
889 case sci_fac:
890 if (power<0 || power>8 || result<0 )
891 calStatus = cal_error;
892 else if(result == 0) {
893 result = 1;
894 power = 0;
896 else{
897 while(power > 0) {
898 result *= 10;
899 power--;
901 if ( ( result - (int)result) > MINIMUM )
902 calStatus = cal_error;
903 else {
904 k = result; result = 1;
905 while (k > 1){
906 doMultiple(&result, &power, k, 0);
907 formatResult();
908 k--;
910 calStatus = cal_normal;
913 break;
914 default:
915 calStatus = cal_toDo;
916 break; /* just for the safety */
922 /* -----------------------------------------------------------------------
923 Handles all two operands calculations
924 ----------------------------------------------------------------------- */
925 void twoOperands(void)
927 switch(oper){
928 case '-':
929 doAdd(&operand, &operandPower, -result, power);
930 break;
931 case '+':
932 doAdd(&operand, &operandPower, result, power);
933 break;
934 case '*':
935 doMultiple(&operand, &operandPower, result, power);
936 break;
937 case '/':
938 if ( ABS(result) > MINIMUM ){
939 doMultiple(&operand, &operandPower, 1/result, -power);
941 else
942 calStatus = cal_error;
943 break;
944 default: /* ' ' */
945 switchOperands(); /* counter switchOperands() below */
946 break;
947 } /* switch(oper) */
948 switchOperands();
949 clearOper();
952 /* First, increases *dimen1 by dimen1_delta modulo dimen1_modulo.
953 If dimen1 wraps, increases *dimen2 by dimen2_delta modulo dimen2_modulo.
955 static void move_with_wrap_and_shift(
956 int *dimen1, int dimen1_delta, int dimen1_modulo,
957 int *dimen2, int dimen2_delta, int dimen2_modulo)
959 bool wrapped = false;
961 *dimen1 += dimen1_delta;
962 if (*dimen1 < 0)
964 *dimen1 = dimen1_modulo - 1;
965 wrapped = true;
967 else if (*dimen1 >= dimen1_modulo)
969 *dimen1 = 0;
970 wrapped = true;
973 if (wrapped)
975 /* Make the dividend always positive to be sure about the result.
976 Adding dimen2_modulo does not change it since we do it modulo. */
977 *dimen2 = (*dimen2 + dimen2_modulo + dimen2_delta) % dimen2_modulo;
981 /* -----------------------------------------------------------------------
982 Print buttons when switching 1st and 2nd
983 int group = {basicButtons, sciButtons}
984 ----------------------------------------------------------------------- */
985 void printButtonGroups(int group)
987 drawButtons(group);
988 drawLines();
989 rb->lcd_update();
991 /* -----------------------------------------------------------------------
992 flash the currently marked button
993 ----------------------------------------------------------------------- */
994 void flashButton(void)
996 int k, w, h;
997 for (k=2;k>0;k--)
999 rb->lcd_getstringsize( buttonChar[buttonGroup][btn_row][btn_col],&w,&h);
1000 rb->lcd_set_drawmode(DRMODE_SOLID|(k==1) ? 0 : DRMODE_INVERSEVID);
1001 rb->lcd_fillrect( X_0_POS + btn_col*REC_WIDTH + 1,
1002 Y_1_POS + btn_row*REC_HEIGHT + 1,
1003 REC_WIDTH - 1, REC_HEIGHT - 1);
1004 rb->lcd_putsxy( X_0_POS + btn_col*REC_WIDTH + (REC_WIDTH - w)/2,
1005 Y_1_POS + btn_row*REC_HEIGHT + (REC_HEIGHT - h)/2 +1,
1006 buttonChar[buttonGroup][btn_row][btn_col] );
1007 rb->lcd_update_rect( X_0_POS + btn_col*REC_WIDTH + 1,
1008 Y_1_POS + btn_row*REC_HEIGHT + 1,
1009 REC_WIDTH - 1, REC_HEIGHT - 1);
1011 if (k!= 1)
1012 rb->sleep(HZ/22);
1017 /* -----------------------------------------------------------------------
1018 pos is the position that needs animation. pos = [1~18]
1019 ----------------------------------------------------------------------- */
1020 void deleteAnimation(int pos)
1022 int k, w, h, x;
1023 if (pos<1 || pos >18)
1024 return;
1026 rb->lcd_getstringsize("0", &w, &h);
1027 x = (pos==1? 4: LCD_WIDTH - 4 - w);
1029 for (k=0;k<4;k++){
1030 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1031 rb->lcd_fillrect(x, Y_1_POS - h -1, w, h);
1032 rb->lcd_set_drawmode(DRMODE_SOLID);
1033 rb->lcd_fillrect(x + (w*k)/8, Y_1_POS - h -1 + (h*k)/8,
1034 (w*(4-k))/4, (h*(4-k))/4);
1035 rb->lcd_update_rect(x, Y_1_POS - h -1, w, h);
1036 rb->sleep(HZ/32);
1040 /* -----------------------------------------------------------------------
1041 result may be one of these formats:
1043 xxxx.xxxx
1044 0.xxxx
1045 0.0000xxxx
1047 formatResult() change result to standard format: 0.xxxx
1048 if result is close to 0, let it be 0;
1049 if result is close to 1, let it be 0.1 and power++;
1050 ----------------------------------------------------------------------- */
1051 void formatResult(void)
1053 int resultsign = SIGN(result);
1054 result = ABS(result);
1055 if(result > MINIMUM ){ /* doesn't check power, might have problem
1056 input wouldn't,
1057 + - * / of two formatted number wouldn't.
1058 only a calculation that makes a formatted
1059 number (0.xxxx) less than MINIMUM in only
1060 one operation */
1062 if (result<1){
1063 while( (int)(result*10) == 0 ){
1064 result *= 10;
1065 power--;
1066 modifier *= 10;
1069 else{ /* result >= 1 */
1070 while( (int)result != 0 ){
1071 result /= 10;
1072 power++;
1073 modifier /= 10;
1075 } /* if result<1 */
1077 if (result > (1-MINIMUM)){
1078 result = 0.1;
1079 power++;
1080 modifier /= 10;
1082 result *= resultsign;
1084 else {
1085 result = 0;
1086 power = 0;
1087 modifier = 0.1;
1091 /* -----------------------------------------------------------------------
1092 result2typingbuf() outputs standard format result to typingbuf.
1093 case SCIENTIFIC_FORMAT, let temppower = 1;
1094 case temppower > 0: print '.' in the middle
1095 case temppower <= 0: print '.' in the begining
1096 ----------------------------------------------------------------------- */
1097 void result2typingbuf(void)
1099 bool haveDot = false;
1100 char tempchar = 0;
1101 int k;
1102 double tempresult = ABS(result); /* positive num makes things simple */
1104 int temppower;
1105 double tempmodifier = 1;
1106 int count;
1108 if(SCIENTIFIC_FORMAT)
1109 temppower = 1; /* output x.xxxx format */
1110 else
1111 temppower = power;
1113 cleartypingbuf();
1115 if(tempresult < MINIMUM){ /* if 0,faster display and avoid complication*/
1116 typingbuf[0] = ' ';
1117 typingbuf[1] = '0';
1119 else{ /* tempresult > 0 */
1120 typingbuf[0] = (SIGN(result)<0)?'-':' ';
1122 typingbufPointer = typingbuf;
1123 if(temppower > 0){
1124 for (k = 0; k<DIGITLEN+1 ; k++){
1125 typingbufPointer++;
1126 if(temppower || *(typingbufPointer-1) == '.'){
1127 count = 0;
1128 tempmodifier = tempmodifier/10;
1129 while( (tempresult-tempmodifier*count) >
1130 (tempmodifier-MINIMUM)){
1131 count++;
1133 tempresult -= tempmodifier*count;
1134 tempresult = ABS(tempresult);
1135 temppower-- ;
1136 *typingbufPointer = count + '0';
1138 else{ /* temppower == 0 */
1139 *typingbufPointer = '.';
1140 haveDot = true;
1142 } /* for */
1144 else{
1145 haveDot = true;
1146 typingbufPointer++; *typingbufPointer = '0';
1147 typingbufPointer++; *typingbufPointer = '.';
1148 for (k = 2; k<DIGITLEN+1 ; k++){
1149 typingbufPointer++;
1150 count = 0;
1151 if ( (-temppower) < (k-1)){
1152 tempmodifier = tempmodifier/10;
1153 while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){
1154 count++;
1157 tempresult -= tempmodifier*count;
1158 tempresult = ABS(tempresult);
1159 temppower-- ;
1161 *typingbufPointer = count + '0';
1164 /* now, typingbufPointer = typingbuf + 16 */
1165 /* backward strip off 0 and '.' */
1166 if (haveDot){
1167 while( (*typingbufPointer == '0') || (*typingbufPointer == '.')){
1168 tempchar = *typingbufPointer;
1169 *typingbufPointer = 0;
1170 typingbufPointer--;
1171 if (tempchar == '.') break;
1174 typingbuf[DIGITLEN+1] = 0;
1175 } /* else tempresult > 0 */
1178 /* -----------------------------------------------------------------------
1179 printResult() generates LCD display.
1180 ----------------------------------------------------------------------- */
1181 void printResult(void)
1183 int k, w, h;
1185 char operbuf[3] = {0, 0, 0};
1187 switch_Status:
1188 switch(calStatus){
1189 case cal_exit:
1190 rb->lcd_clear_display();
1191 rb->splash(HZ/3, "Bye now!");
1192 break;
1193 case cal_error:
1194 clearbuf();
1195 rb->snprintf(buf, 19, "%18s","Error");
1196 break;
1197 case cal_toDo:
1198 clearbuf();
1199 rb->snprintf(buf, 19, "%18s","Coming soon ^_* ");
1200 break;
1202 case cal_normal:
1203 formatResult();
1205 if( power > 1000 ){ /* power -1 > 999 */
1206 calStatus = cal_error;
1207 goto switch_Status;
1209 if (power < -998 ) /* power -1 < -999 */
1210 clearResult(); /* too small, let it be 0 */
1212 result2typingbuf();
1213 clearbuf();
1215 operbuf[0] = oper;
1216 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1217 operbuf[2] = '\0';
1219 if(SCIENTIFIC_FORMAT){
1220 /* output format: X.XXXX eXXX */
1221 if(power > -98){ /* power-1 >= -99, eXXX or e-XX */
1222 rb->snprintf(buf, 12, "%11s",typingbuf);
1223 for(k=11;k<=14;k++) buf[k] = ' ';
1224 cleartypingbuf();
1225 rb->snprintf(typingbuf, 5, "e%d",power-1);
1226 rb->snprintf(buf+11, 5, "%4s",typingbuf);
1228 else{ /* power-1 <= -100, e-XXX */
1229 rb->snprintf(buf, 12, "%11s",typingbuf);
1230 rb->snprintf(buf+11, 6, "e%d",power-1);
1233 else{
1234 rb->snprintf(buf, 12, "%11s",typingbuf);
1235 } /* if SCIENTIFIC_FORMAT */
1236 break;
1237 case cal_typing:
1238 case cal_dotted:
1239 clearbuf();
1240 operbuf[0] = oper;
1241 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1242 rb->snprintf(buf, 12, "%11s",typingbuf);
1243 break;
1247 rb->lcd_getstringsize(buf, &w, &h);
1248 rb->screen_clear_area(rb->screens[0], 0, 0, LCD_WIDTH, Y_1_POS - 1);
1249 rb->lcd_putsxy(4, Y_1_POS - h -1, operbuf);
1250 rb->lcd_putsxy(LCD_WIDTH - w - 4, Y_1_POS - h -1, buf);
1251 rb->lcd_update_rect(0, 1, LCD_WIDTH, Y_1_POS);
1254 /* -----------------------------------------------------------------------
1255 Process typing buttons: 1-9, '.', sign
1256 main operand "result" and typingbuf are processed seperately here.
1257 ----------------------------------------------------------------------- */
1258 void typingProcess(void){
1259 switch( CAL_BUTTON ){
1260 case btn_sign:
1261 if (calStatus == cal_typing ||
1262 calStatus == cal_dotted)
1263 typingbuf[0] = (typingbuf[0]=='-')?' ':'-';
1264 result = -result;
1265 break;
1266 case btn_dot:
1267 operInputted = false;
1268 switch(calStatus){
1269 case cal_normal:
1270 clearInput();
1271 *typingbufPointer = '0';
1272 typingbufPointer++;
1273 case cal_typing:
1274 calStatus = cal_dotted;
1275 *typingbufPointer = '.';
1276 if (typingbufPointer != typingbuf+DIGITLEN+1)
1277 typingbufPointer++;
1278 break;
1279 default: /* cal_dotted */
1280 break;
1282 break;
1283 default: /* 0-9 */
1284 operInputted = false;
1285 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1286 switch(calStatus){
1287 case cal_normal:
1288 if(CAL_BUTTON == btn_0 )
1289 break; /* first input is 0, ignore */
1290 clearInput();
1291 /*no operator means start a new calculation*/
1292 if (oper ==' ')
1293 clearOperand();
1294 calStatus = cal_typing;
1295 /* go on typing, no break */
1296 case cal_typing:
1297 case cal_dotted:
1298 switch(CAL_BUTTON){
1299 case btn_0:
1300 *typingbufPointer = '0';
1301 break;
1302 default:
1303 *typingbufPointer=(7+btn_col-3*(btn_row-1))+ '0';
1304 break;
1306 if (typingbufPointer!=typingbuf+DIGITLEN+1){
1307 typingbufPointer++;
1309 {/* result processing */
1310 if (calStatus == cal_typing) power++;
1311 if (CAL_BUTTON != btn_0)
1312 result= result +
1313 SIGN(result)*
1314 (7+btn_col-3*(btn_row-1))*modifier;
1315 modifier /= 10;
1318 else /* last byte always '\0' */
1319 *typingbufPointer = 0;
1320 break;
1321 default: /* cal_error, cal_exit */
1322 break;
1324 break; /* default, 0-9 */
1325 } /* switch( CAL_BUTTON ) */
1328 /* -----------------------------------------------------------------------
1329 Handle delete operation
1330 main operand "result" and typingbuf are processed seperately here.
1331 ----------------------------------------------------------------------- */
1332 void doDelete(void){
1333 deleteAnimation(18);
1334 switch(calStatus){
1335 case cal_dotted:
1336 if (*(typingbufPointer-1) == '.'){
1337 /* if dotted and deleting '.',
1338 change status and delete '.' below */
1339 calStatus = cal_typing;
1341 else{ /* if dotted and not deleting '.',
1342 power stays */
1343 power++; /* counter "power--;" below */
1345 case cal_typing:
1346 typingbufPointer--;
1348 {/* result processing */ /* 0-9, '.' */
1349 /* if deleting '.', do nothing */
1350 if ( *typingbufPointer != '.'){
1351 power--;
1352 modifier *= 10;
1353 result = result - SIGN(result)*
1354 ((*typingbufPointer)- '0')*modifier;
1358 *typingbufPointer = 0;
1360 /* if (only one digit left and it's 0)
1361 or no digit left, change status*/
1362 if ( typingbufPointer == typingbuf+1 ||
1363 ( typingbufPointer == typingbuf+2 &&
1364 *(typingbufPointer-1) == '0' ))
1365 calStatus = cal_normal;
1366 break;
1367 default: /* normal, error, exit */
1368 break;
1371 /* -----------------------------------------------------------------------
1372 Handle buttons on basic screen
1373 ----------------------------------------------------------------------- */
1374 void basicButtonsProcess(void){
1375 switch (btn) {
1376 case CALCULATOR_INPUT:
1377 if (calStatus == cal_error && (CAL_BUTTON != btn_C) ) break;
1378 flashButton();
1379 switch( CAL_BUTTON ){
1380 case btn_MR:
1381 operInputted = false;
1382 result = memTemp; power = memTempPower;
1383 calStatus = cal_normal;
1384 break;
1385 case btn_M:
1386 formatResult();
1387 if (memTemp > MINIMUM)
1388 doAdd(&memTemp, &memTempPower, result, power);
1389 else {
1390 /* if result is too small and memTemp = 0,
1391 doAdd will not add */
1392 memTemp = result;
1393 memTempPower = power;
1395 calStatus = cal_normal;
1396 break;
1398 case btn_C: clearMem(); break;
1399 case btn_CE: clearInput(); break;
1401 case btn_bas:
1402 buttonGroup = sciButtons;
1403 printButtonGroups(buttonGroup);
1404 break;
1406 /* one operand calculation, may be changed to
1407 like sin, cos, log, etc */
1408 case btn_sqr:
1409 case btn_square:
1410 case btn_rec:
1411 formatResult(); /* not necessary, just for safty */
1412 oneOperand();
1413 break;
1415 case_btn_equal: /* F3 shortkey entrance */
1416 case btn_equal:
1417 formatResult();
1418 calStatus = cal_normal;
1419 operInputted = false;
1420 if (oper != ' ') twoOperands();
1421 break;
1423 case btn_div:
1424 case btn_time:
1425 case btn_minus:
1426 case btn_add:
1427 if(!operInputted) {twoOperands(); operInputted = true;}
1428 oper = buttonChar[basicButtons][btn_row][btn_col][0];
1429 #ifdef CALCULATOR_OPERATORS
1430 case_cycle_operators: /* F2 shortkey entrance */
1431 #endif
1432 calStatus = cal_normal;
1433 formatResult();
1434 operand = result;
1435 operandPower = power;
1437 break;
1439 case btn_sign:
1440 case btn_dot:
1441 default: /* 0-9 */
1442 typingProcess();
1443 break;
1444 } /* switch (CAL_BUTTON) */
1445 break;
1447 #ifdef CALCULATOR_OPERATORS
1448 case CALCULATOR_OPERATORS:
1449 if (calStatus == cal_error) break;
1450 if (!operInputted) {twoOperands(); operInputted = true;}
1451 switch (oper){
1452 case ' ':
1453 case '/': oper = '+'; flashButton(); break;
1454 case '+': oper = '-'; flashButton(); break;
1455 case '-': oper = '*'; flashButton(); break;
1456 case '*': oper = '/'; flashButton(); break;
1458 goto case_cycle_operators;
1459 break;
1460 #endif
1462 case CALCULATOR_CALC:
1463 if (calStatus == cal_error) break;
1464 flashButton();
1465 goto case_btn_equal;
1466 break;
1467 default: break;
1469 printResult();
1472 /* -----------------------------------------------------------------------
1473 Handle buttons on scientific screen
1474 ----------------------------------------------------------------------- */
1475 void sciButtonsProcess(void){
1476 switch (btn) {
1477 case CALCULATOR_INPUT:
1478 if (calStatus == cal_error && (CAL_BUTTON != sci_sci) ) break;
1479 flashButton();
1480 switch( CAL_BUTTON ){
1482 case sci_pi:
1483 result = M_PI; power = 0;
1484 calStatus = cal_normal;
1485 break;
1487 case sci_xy: break;
1489 case sci_sci:
1490 buttonGroup = basicButtons;
1491 printButtonGroups(basicButtons);
1492 break;
1494 case sci_fac:
1495 case sci_sin:
1496 case sci_asin:
1497 case sci_cos:
1498 case sci_acos:
1499 case sci_tan:
1500 case sci_atan:
1501 case sci_ln:
1502 case sci_exp:
1503 case sci_log:
1504 formatResult(); /* not necessary, just for safty */
1505 oneOperand();
1506 break;
1508 case btn_sign:
1509 case btn_dot:
1510 default: /* 0-9 */
1511 typingProcess();
1512 break;
1513 } /* switch (CAL_BUTTON) */
1514 break;
1516 #ifdef CALCULATOR_OPERATORS
1517 case CALCULATOR_OPERATORS:
1518 if (calStatus == cal_error) break;
1519 if (!operInputted) {twoOperands(); operInputted = true;}
1520 switch (oper){
1521 case ' ': oper = '+'; break;
1522 case '/': oper = '+'; deleteAnimation(1); break;
1523 case '+': oper = '-'; deleteAnimation(1); break;
1524 case '-': oper = '*'; deleteAnimation(1); break;
1525 case '*': oper = '/'; deleteAnimation(1); break;
1527 calStatus = cal_normal;
1528 formatResult();
1529 operand = result;
1530 operandPower = power;
1531 break;
1532 #endif
1534 case CALCULATOR_CALC:
1535 if (calStatus == cal_error) break;
1536 formatResult();
1537 calStatus = cal_normal;
1538 operInputted = false;
1539 if (oper != ' ') twoOperands();
1540 break;
1541 default: break;
1543 printResult();
1546 /* -----------------------------------------------------------------------
1547 move button index
1548 Invert display new button, invert back previous button
1549 ----------------------------------------------------------------------- */
1550 int handleButton(int button){
1551 switch(button)
1553 case CALCULATOR_INPUT:
1554 case CALCULATOR_CALC:
1555 #ifdef CALCULATOR_INPUT_CALC_PRE
1556 if (lastbtn != CALCULATOR_INPUT_CALC_PRE)
1557 break;
1558 /* no unconditional break; here! */
1559 #endif
1560 #ifdef CALCULATOR_OPERATORS
1561 case CALCULATOR_OPERATORS:
1562 #endif
1563 switch(buttonGroup){
1564 case basicButtons:
1565 basicButtonsProcess();
1566 break;
1567 case sciButtons:
1568 sciButtonsProcess();
1569 break;
1571 break;
1573 #ifdef CALCULATOR_CLEAR
1574 case CALCULATOR_CLEAR:
1575 switch(calStatus){
1576 case cal_typing:
1577 case cal_dotted:
1578 doDelete();
1579 break;
1580 default: /* cal_normal, cal_error, cal_exit */
1581 clearMem();
1582 break;
1584 printResult();
1585 break;
1586 #endif
1587 case CALCULATOR_LEFT:
1588 case CALCULATOR_LEFT | BUTTON_REPEAT:
1589 move_with_wrap_and_shift(
1590 &btn_col, -1, BUTTON_COLS,
1591 &btn_row, 0, BUTTON_ROWS);
1592 break;
1594 case CALCULATOR_RIGHT:
1595 case CALCULATOR_RIGHT | BUTTON_REPEAT:
1596 move_with_wrap_and_shift(
1597 &btn_col, 1, BUTTON_COLS,
1598 &btn_row, 0, BUTTON_ROWS);
1599 break;
1601 #ifdef CALCULATOR_UP
1602 case CALCULATOR_UP:
1603 case CALCULATOR_UP | BUTTON_REPEAT:
1604 move_with_wrap_and_shift(
1605 &btn_row, -1, BUTTON_ROWS,
1606 &btn_col, 0, BUTTON_COLS);
1607 break;
1608 #endif
1609 #ifdef CALCULATOR_DOWN
1610 case CALCULATOR_DOWN:
1611 case CALCULATOR_DOWN | BUTTON_REPEAT:
1612 move_with_wrap_and_shift(
1613 &btn_row, 1, BUTTON_ROWS,
1614 &btn_col, 0, BUTTON_COLS);
1615 break;
1616 #endif
1618 #ifdef CALCULATOR_UP_W_SHIFT
1619 case CALCULATOR_UP_W_SHIFT:
1620 case CALCULATOR_UP_W_SHIFT | BUTTON_REPEAT:
1621 move_with_wrap_and_shift(
1622 &btn_row, -1, BUTTON_ROWS,
1623 &btn_col, -1, BUTTON_COLS);
1624 break;
1625 #endif
1626 #ifdef CALCULATOR_DOWN_W_SHIFT
1627 case CALCULATOR_DOWN_W_SHIFT:
1628 case CALCULATOR_DOWN_W_SHIFT | BUTTON_REPEAT:
1629 move_with_wrap_and_shift(
1630 &btn_row, 1, BUTTON_ROWS,
1631 &btn_col, 1, BUTTON_COLS);
1632 break;
1633 #endif
1634 #ifdef CALCULATOR_LEFT_W_SHIFT
1635 case CALCULATOR_LEFT_W_SHIFT:
1636 case CALCULATOR_LEFT_W_SHIFT | BUTTON_REPEAT:
1637 move_with_wrap_and_shift(
1638 &btn_col, -1, BUTTON_COLS,
1639 &btn_row, -1, BUTTON_ROWS);
1640 break;
1641 #endif
1642 #ifdef CALCULATOR_RIGHT_W_SHIFT
1643 case CALCULATOR_RIGHT_W_SHIFT:
1644 case CALCULATOR_RIGHT_W_SHIFT | BUTTON_REPEAT:
1645 move_with_wrap_and_shift(
1646 &btn_col, 1, BUTTON_COLS,
1647 &btn_row, 1, BUTTON_ROWS);
1648 break;
1649 #endif
1650 #ifdef CALCULATOR_RC_QUIT
1651 case CALCULATOR_RC_QUIT:
1652 #endif
1653 case CALCULATOR_QUIT:
1654 return -1;
1657 return 0;
1659 prev_btn_row = btn_row;
1660 prev_btn_col = btn_col;
1663 /* -----------------------------------------------------------------------
1664 Main();
1665 ----------------------------------------------------------------------- */
1666 enum plugin_status plugin_start(const void* parameter)
1668 (void)parameter;
1670 /* now go ahead and have fun! */
1672 #ifdef HAVE_TOUCHSCREEN
1673 rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
1674 #endif
1676 cal_initial();
1678 while (calStatus != cal_exit ) {
1679 btn = rb->button_get_w_tmo(HZ/2);
1680 #ifdef HAVE_TOUCHSCREEN
1681 if(btn & BUTTON_TOUCHSCREEN)
1683 struct ts_raster_result res;
1684 if(touchscreen_map_raster(&calc_raster, rb->button_get_data() >> 16,
1685 rb->button_get_data() & 0xffff, &res) == 1)
1687 btn_row = res.y;
1688 btn_col = res.x;
1689 drawButtons(buttonGroup);
1690 drawLines();
1692 rb->lcd_update();
1694 prev_btn_row = btn_row;
1695 prev_btn_col = btn_col;
1696 if(btn & BUTTON_REL)
1698 btn = CALCULATOR_INPUT;
1699 switch(buttonGroup){
1700 case basicButtons:
1701 basicButtonsProcess();
1702 break;
1703 case sciButtons:
1704 sciButtonsProcess();
1705 break;
1707 btn = BUTTON_TOUCHSCREEN;
1711 #endif
1712 if (handleButton(btn) == -1)
1714 calStatus = cal_exit;
1715 printResult();
1717 else
1719 drawButtons(buttonGroup);
1720 drawLines();
1723 rb->lcd_update();
1725 if(rb->default_event_handler(btn) == SYS_USB_CONNECTED)
1726 return PLUGIN_USB_CONNECTED;
1728 if (btn != BUTTON_NONE)
1729 lastbtn = btn;
1730 } /* while (calStatus != cal_exit ) */
1732 rb->button_clear_queue();
1733 return PLUGIN_OK;
1736 #endif /* #ifdef HAVE_LCD_BITMAP */