Bump plugin API version. This should have been done in r24587. Also, because the...
[kugel-rb.git] / apps / plugins / calculator.c
blobf7cafc8f24b36c6c163f84a0ff27ddee3266d093
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 Pengxuan Liu (Isaac)
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 00 01 21 22 23 43 44 45 65 66 67 87 88 89 109110111
24 00 |-----------|-----------|-----------|-----------|-----------|
25 01 | | | | | |
26 |***********|***********|***********|***********|***********|
27 |***********|***********|***********|***********|***********|
28 11 | | | | | |
29 12 |-----------|-----------|-----------|-----------|-----------|
30 13 |-----------|-----------|-----------|-----------|-----------| y1
31 14 | | | | | |
32 | | | | | |
33 22 | | | | | |
34 23 |-----------|-----------|-----------|-----------|-----------| y2
35 24 | | | | | |
36 | | | | | |
37 32 | | | | | |
38 33 |-----------|-----------|-----------|-----------|-----------| y3
39 34 | | | | | |
40 | | | | | |
41 42 | | | | | |
42 43 |-----------|-----------|-----------|-----------|-----------| y4
43 44 | | | | | |
44 | | | | | |
45 52 | | | | | |
46 53 |-----------|-----------|-----------|-----------|-----------| y5
47 54 | | | | | |
48 | | | | | |
49 62 | | | | | |
50 63 |-----------|-----------|-----------|-----------|-----------| y6
51 x0 x1 x2 x3 x4 x5
54 /*---------------------------------------------------------------------------
55 Features:
56 - Scientific number format core code. Support range 10^-999 ~ 10^999
57 - Number of significant figures up to 10
59 Limitations:
60 - Right now, only accept "num, operator (+,-,*,/), num, =" input sequence.
61 Input "3, +, 5, -, 2, =", the calculator will only do 5-2 and result = 3
62 You have to input "3, +, 5, =, -, 2, =" to get 3+5-2 = 6
64 - "*,/" have no priority. Actually you can't input 3+5*2 yet.
66 User Instructions:
67 use arrow button to move cursor, "play" button to select, "off" button to exit
68 F1: if typing numbers, it's equal to "Del"; otherwise, equal to "C"
69 F2: circle input "+, -, *, /"
70 F3: equal to "="
72 "MR" : load temp memory
73 "M+" : add currently display to temp memory
74 "C" : reset calculator
75 ---------------------------------------------------------------------------*/
77 #include "plugin.h"
78 #ifdef HAVE_LCD_BITMAP
79 #include "math.h"
81 PLUGIN_HEADER
83 #define BUTTON_ROWS 5
84 #define BUTTON_COLS 5
86 #define REC_HEIGHT (int)(LCD_HEIGHT / (BUTTON_ROWS + 1))
87 #define REC_WIDTH (int)(LCD_WIDTH / BUTTON_COLS)
89 #define Y_6_POS (LCD_HEIGHT) /* Leave room for the border */
90 #define Y_5_POS (Y_6_POS - REC_HEIGHT) /* y5 = 53 */
91 #define Y_4_POS (Y_5_POS - REC_HEIGHT) /* y4 = 43 */
92 #define Y_3_POS (Y_4_POS - REC_HEIGHT) /* y3 = 33 */
93 #define Y_2_POS (Y_3_POS - REC_HEIGHT) /* y2 = 23 */
94 #define Y_1_POS (Y_2_POS - REC_HEIGHT) /* y1 = 13 */
95 #define Y_0_POS 0 /* y0 = 0 */
97 #define X_0_POS 0 /* x0 = 0 */
98 #define X_1_POS (X_0_POS + REC_WIDTH) /* x1 = 22 */
99 #define X_2_POS (X_1_POS + REC_WIDTH) /* x2 = 44 */
100 #define X_3_POS (X_2_POS + REC_WIDTH) /* x3 = 66 */
101 #define X_4_POS (X_3_POS + REC_WIDTH) /* x4 = 88 */
102 #define X_5_POS (X_4_POS + REC_WIDTH) /* x5 = 110, column 111 left blank */
104 #define SIGN(x) ((x)<0?-1:1)
106 /* variable button definitions */
107 #if CONFIG_KEYPAD == RECORDER_PAD
108 #define CALCULATOR_LEFT BUTTON_LEFT
109 #define CALCULATOR_RIGHT BUTTON_RIGHT
110 #define CALCULATOR_UP BUTTON_UP
111 #define CALCULATOR_DOWN BUTTON_DOWN
112 #define CALCULATOR_QUIT BUTTON_OFF
113 #define CALCULATOR_INPUT BUTTON_PLAY
114 #define CALCULATOR_CALC BUTTON_F3
115 #define CALCULATOR_OPERATORS BUTTON_F2
116 #define CALCULATOR_CLEAR BUTTON_F1
118 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
119 #define CALCULATOR_LEFT BUTTON_LEFT
120 #define CALCULATOR_RIGHT BUTTON_RIGHT
121 #define CALCULATOR_UP BUTTON_UP
122 #define CALCULATOR_DOWN BUTTON_DOWN
123 #define CALCULATOR_QUIT BUTTON_OFF
124 #define CALCULATOR_INPUT BUTTON_SELECT
125 #define CALCULATOR_CALC BUTTON_F3
126 #define CALCULATOR_OPERATORS BUTTON_F2
127 #define CALCULATOR_CLEAR BUTTON_F1
129 #elif CONFIG_KEYPAD == ONDIO_PAD
130 #define CALCULATOR_LEFT BUTTON_LEFT
131 #define CALCULATOR_RIGHT BUTTON_RIGHT
132 #define CALCULATOR_UP BUTTON_UP
133 #define CALCULATOR_DOWN BUTTON_DOWN
134 #define CALCULATOR_QUIT BUTTON_OFF
135 #define CALCULATOR_INPUT_CALC_PRE BUTTON_MENU
136 #define CALCULATOR_INPUT (BUTTON_MENU | BUTTON_REL)
137 #define CALCULATOR_CALC (BUTTON_MENU | BUTTON_REPEAT)
139 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
140 (CONFIG_KEYPAD == IRIVER_H300_PAD)
141 #define CALCULATOR_LEFT BUTTON_LEFT
142 #define CALCULATOR_RIGHT BUTTON_RIGHT
143 #define CALCULATOR_UP BUTTON_UP
144 #define CALCULATOR_DOWN BUTTON_DOWN
145 #define CALCULATOR_QUIT BUTTON_OFF
146 #define CALCULATOR_INPUT BUTTON_SELECT
147 #define CALCULATOR_CALC BUTTON_ON
148 #define CALCULATOR_OPERATORS BUTTON_MODE
149 #define CALCULATOR_CLEAR BUTTON_REC
151 #define CALCULATOR_RC_QUIT BUTTON_RC_STOP
153 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
154 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
155 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
157 #define CALCULATOR_LEFT BUTTON_LEFT
158 #define CALCULATOR_RIGHT BUTTON_RIGHT
159 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
160 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
161 #define CALCULATOR_QUIT BUTTON_MENU
162 #define CALCULATOR_INPUT BUTTON_SELECT
163 #define CALCULATOR_CALC BUTTON_PLAY
165 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
167 #define CALCULATOR_LEFT BUTTON_LEFT
168 #define CALCULATOR_RIGHT BUTTON_RIGHT
169 #define CALCULATOR_UP BUTTON_UP
170 #define CALCULATOR_DOWN BUTTON_DOWN
171 #define CALCULATOR_QUIT BUTTON_POWER
172 #define CALCULATOR_INPUT BUTTON_SELECT
173 #define CALCULATOR_CALC BUTTON_PLAY
174 #define CALCULATOR_CLEAR BUTTON_REC
176 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
178 #define CALCULATOR_LEFT BUTTON_LEFT
179 #define CALCULATOR_RIGHT BUTTON_RIGHT
180 #define CALCULATOR_UP BUTTON_UP
181 #define CALCULATOR_DOWN BUTTON_DOWN
182 #define CALCULATOR_QUIT BUTTON_POWER
183 #define CALCULATOR_INPUT BUTTON_SELECT
184 #define CALCULATOR_CALC BUTTON_MENU
185 #define CALCULATOR_CLEAR BUTTON_A
187 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
188 #define CALCULATOR_LEFT BUTTON_LEFT
189 #define CALCULATOR_RIGHT BUTTON_RIGHT
190 #define CALCULATOR_UP BUTTON_UP
191 #define CALCULATOR_DOWN BUTTON_DOWN
192 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
193 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
194 #define CALCULATOR_QUIT BUTTON_POWER
195 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
196 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
197 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
198 #define CALCULATOR_CLEAR BUTTON_REC
200 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
201 #define CALCULATOR_LEFT BUTTON_LEFT
202 #define CALCULATOR_RIGHT BUTTON_RIGHT
203 #define CALCULATOR_UP BUTTON_UP
204 #define CALCULATOR_DOWN BUTTON_DOWN
205 #define CALCULATOR_QUIT BUTTON_POWER
206 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
207 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
208 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
210 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
211 #define CALCULATOR_LEFT BUTTON_LEFT
212 #define CALCULATOR_RIGHT BUTTON_RIGHT
213 #define CALCULATOR_UP BUTTON_UP
214 #define CALCULATOR_DOWN BUTTON_DOWN
215 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
216 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
217 #define CALCULATOR_QUIT (BUTTON_HOME|BUTTON_REPEAT)
218 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
219 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
220 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
221 #define CALCULATOR_CLEAR BUTTON_HOME
224 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
225 #define CALCULATOR_LEFT BUTTON_LEFT
226 #define CALCULATOR_RIGHT BUTTON_RIGHT
227 #define CALCULATOR_UP BUTTON_UP
228 #define CALCULATOR_DOWN BUTTON_DOWN
229 #define CALCULATOR_QUIT BUTTON_POWER
230 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
231 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
232 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
233 #define CALCULATOR_CLEAR BUTTON_HOME
235 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
236 #define CALCULATOR_LEFT BUTTON_LEFT
237 #define CALCULATOR_RIGHT BUTTON_RIGHT
238 #define CALCULATOR_UP BUTTON_UP
239 #define CALCULATOR_DOWN BUTTON_DOWN
240 #define CALCULATOR_QUIT BUTTON_POWER
241 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
242 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
243 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
244 #define CALCULATOR_CLEAR (BUTTON_SELECT|BUTTON_UP)
246 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
248 #define CALCULATOR_LEFT BUTTON_LEFT
249 #define CALCULATOR_RIGHT BUTTON_RIGHT
250 #define CALCULATOR_UP BUTTON_SCROLL_UP
251 #define CALCULATOR_DOWN BUTTON_SCROLL_DOWN
252 #define CALCULATOR_QUIT BUTTON_POWER
253 #define CALCULATOR_INPUT_CALC_PRE BUTTON_PLAY
254 #define CALCULATOR_INPUT (BUTTON_PLAY | BUTTON_REL)
255 #define CALCULATOR_CALC (BUTTON_PLAY | BUTTON_REPEAT)
256 #define CALCULATOR_CLEAR BUTTON_REW
258 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
260 #define CALCULATOR_LEFT BUTTON_LEFT
261 #define CALCULATOR_RIGHT BUTTON_RIGHT
262 #define CALCULATOR_UP BUTTON_UP
263 #define CALCULATOR_DOWN BUTTON_DOWN
264 #define CALCULATOR_QUIT BUTTON_BACK
265 #define CALCULATOR_INPUT BUTTON_SELECT
266 #define CALCULATOR_CALC BUTTON_MENU
267 #define CALCULATOR_CLEAR BUTTON_PLAY
269 #elif (CONFIG_KEYPAD == MROBE100_PAD)
271 #define CALCULATOR_LEFT BUTTON_LEFT
272 #define CALCULATOR_RIGHT BUTTON_RIGHT
273 #define CALCULATOR_UP BUTTON_UP
274 #define CALCULATOR_DOWN BUTTON_DOWN
275 #define CALCULATOR_QUIT BUTTON_POWER
276 #define CALCULATOR_INPUT BUTTON_SELECT
277 #define CALCULATOR_CALC BUTTON_MENU
278 #define CALCULATOR_CLEAR BUTTON_DISPLAY
280 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
282 #define CALCULATOR_LEFT BUTTON_RC_REW
283 #define CALCULATOR_RIGHT BUTTON_RC_FF
284 #define CALCULATOR_UP BUTTON_RC_VOL_UP
285 #define CALCULATOR_DOWN BUTTON_RC_VOL_DOWN
286 #define CALCULATOR_QUIT BUTTON_RC_REC
287 #define CALCULATOR_INPUT BUTTON_RC_PLAY
288 #define CALCULATOR_CALC BUTTON_RC_MODE
289 #define CALCULATOR_CLEAR BUTTON_RC_MENU
291 #define CALCULATOR_RC_QUIT BUTTON_REC
293 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
295 #define CALCULATOR_QUIT BUTTON_POWER
296 #define CALCULATOR_CLEAR BUTTON_MENU
298 #elif CONFIG_KEYPAD == IAUDIO67_PAD
300 #define CALCULATOR_LEFT BUTTON_LEFT
301 #define CALCULATOR_RIGHT BUTTON_RIGHT
302 #define CALCULATOR_UP BUTTON_VOLUP
303 #define CALCULATOR_DOWN BUTTON_VOLDOWN
304 #define CALCULATOR_QUIT BUTTON_POWER
305 #define CALCULATOR_INPUT BUTTON_PLAY
306 #define CALCULATOR_CALC BUTTON_MENU
307 #define CALCULATOR_CLEAR BUTTON_STOP
309 #define CALCULATOR_RC_QUIT (BUTTON_MENU|BUTTON_PLAY)
311 #elif (CONFIG_KEYPAD == CREATIVEZVM_PAD)
313 #define CALCULATOR_LEFT BUTTON_LEFT
314 #define CALCULATOR_RIGHT BUTTON_RIGHT
315 #define CALCULATOR_UP BUTTON_UP
316 #define CALCULATOR_DOWN BUTTON_DOWN
317 #define CALCULATOR_QUIT BUTTON_BACK
318 #define CALCULATOR_INPUT BUTTON_SELECT
319 #define CALCULATOR_CALC BUTTON_MENU
320 #define CALCULATOR_CLEAR BUTTON_PLAY
322 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
324 #define CALCULATOR_LEFT BUTTON_LEFT
325 #define CALCULATOR_RIGHT BUTTON_RIGHT
326 #define CALCULATOR_UP BUTTON_UP
327 #define CALCULATOR_DOWN BUTTON_DOWN
328 #define CALCULATOR_QUIT BUTTON_POWER
329 #define CALCULATOR_INPUT BUTTON_SELECT
330 #define CALCULATOR_CALC BUTTON_MENU
331 #define CALCULATOR_CLEAR BUTTON_VIEW
333 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
335 #define CALCULATOR_LEFT BUTTON_PREV
336 #define CALCULATOR_RIGHT BUTTON_NEXT
337 #define CALCULATOR_UP BUTTON_UP
338 #define CALCULATOR_DOWN BUTTON_DOWN
339 #define CALCULATOR_QUIT BUTTON_POWER
340 #define CALCULATOR_INPUT BUTTON_PLAY
341 #define CALCULATOR_CALC BUTTON_MENU
342 #define CALCULATOR_CLEAR BUTTON_RIGHT
344 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
346 #define CALCULATOR_QUIT BUTTON_POWER
347 #define CALCULATOR_CLEAR BUTTON_MENU
349 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
350 #define CALCULATOR_QUIT BUTTON_POWER
352 #elif CONFIG_KEYPAD == MROBE500_PAD
353 #define CALCULATOR_QUIT BUTTON_POWER
355 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
357 #define CALCULATOR_LEFT BUTTON_LEFT
358 #define CALCULATOR_RIGHT BUTTON_RIGHT
359 #define CALCULATOR_UP BUTTON_UP
360 #define CALCULATOR_DOWN BUTTON_DOWN
361 #define CALCULATOR_QUIT BUTTON_REC
362 #define CALCULATOR_INPUT BUTTON_PLAY
363 #define CALCULATOR_CALC BUTTON_FFWD
364 #define CALCULATOR_CLEAR BUTTON_REW
366 #else
367 #error No keymap defined!
368 #endif
370 #ifdef HAVE_TOUCHSCREEN
371 #ifndef CALCULATOR_LEFT
372 #define CALCULATOR_LEFT BUTTON_MIDLEFT
373 #endif
374 #ifndef CALCULATOR_RIGHT
375 #define CALCULATOR_RIGHT BUTTON_MIDRIGHT
376 #endif
377 #ifndef CALCULATOR_UP
378 #define CALCULATOR_UP BUTTON_TOPMIDDLE
379 #endif
380 #ifndef CALCULATOR_DOWN
381 #define CALCULATOR_DOWN BUTTON_BOTTOMMIDDLE
382 #endif
383 #ifndef CALCULATOR_CALC
384 #define CALCULATOR_CALC BUTTON_BOTTOMRIGHT
385 #endif
386 #ifndef CALCULATOR_INPUT
387 #define CALCULATOR_INPUT BUTTON_CENTER
388 #endif
389 #ifndef CALCULATOR_CLEAR
390 #define CALCULATOR_CLEAR BUTTON_TOPRIGHT
391 #endif
393 #include "lib/pluginlib_touchscreen.h"
394 static struct ts_raster calc_raster = { X_0_POS, Y_1_POS,
395 BUTTON_COLS*REC_WIDTH, BUTTON_ROWS*REC_HEIGHT, REC_WIDTH, REC_HEIGHT };
396 #endif
398 enum {
399 basicButtons,
400 sciButtons
401 } buttonGroup;
403 unsigned char* buttonChar[2][5][5] = {
404 { { "MR" , "M+" , "2nd" , "CE" , "C" },
405 { "7" , "8" , "9" , "/" , "sqr" },
406 { "4" , "5" , "6" , "*" , "x^2" },
407 { "1" , "2" , "3" , "-" , "1/x" },
408 { "0" , "+/-", "." , "+" , "=" } },
410 { { "n!" , "PI" , "1st" , "sin" , "asi" },
411 { "7" , "8" , "9" , "cos" , "aco" },
412 { "4" , "5" , "6" , "tan" , "ata" },
413 { "1" , "2" , "3" , "ln" , "e^x" },
414 { "0" , "+/-", "." , "log" , "x^y" } }
417 enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C ,
418 btn_7 , btn_8 , btn_9 , btn_div , btn_sqr ,
419 btn_4 , btn_5 , btn_6 , btn_time , btn_square ,
420 btn_1 , btn_2 , btn_3 , btn_minus , btn_rec ,
421 btn_0 , btn_sign , btn_dot , btn_add , btn_equal
424 enum { sci_fac, sci_pi , sci_sci , sci_sin , sci_asin ,
425 sci_7 , sci_8 , sci_9 , sci_cos , sci_acos ,
426 sci_4 , sci_5 , sci_6 , sci_tan , sci_atan ,
427 sci_1 , sci_2 , sci_3 , sci_ln , sci_exp ,
428 sci_0 , sci_sign , sci_dot , sci_log , sci_xy
431 #define MINIMUM 0.000000000001 /* e-12 */
432 /* ^ ^ ^ ^ */
433 /* 123456789abcdef */
435 #define DIGITLEN 10 /* must <= 10 */
436 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
437 /* 0.000 00000 0001 */
438 /* ^ ^ ^ ^ ^ ^ */
439 /* DIGITLEN 12345 6789a bcdef */
440 /* power 12 34567 89abc def */
441 /* 10^- 123 45678 9abcd ef */
443 unsigned char buf[19];/* 18 bytes of output line,
444 buf[0] is operator
445 buf[1] = 'M' if memTemp is not 0
446 buf[2] = ' '
448 if SCIENTIFIC_FORMAT
449 buf[2]-buf[12] or buf[3]-buf[13] = result;
450 format X.XXXXXXXX
451 buf[13] or buf[14] -buf[17] = power;
452 format eXXX or e-XXX
453 else
454 buf[3]-buf[6] = ' ';
455 buf[7]-buf[17] = result;
457 buf[18] = '\0' */
459 unsigned char typingbuf[DIGITLEN+2];/* byte 0 is sign or ' ',
460 byte 1~DIGITLEN are num and '.'
461 byte (DIGITLEN+1) is '\0' */
462 unsigned char* typingbufPointer = typingbuf;
464 double result = 0; /* main operand, format 0.xxxxx */
465 int power = 0; /* 10^power */
466 double modifier = 0.1; /* position of next input */
467 double operand = 0; /* second operand, format 0.xxxxx */
468 int operandPower = 0; /* 10^power of second operand */
469 char oper = ' '; /* operators: + - * / */
470 bool operInputted = false; /* false: do calculation first and
471 replace current oper
472 true: just replace current oper */
474 double memTemp = 0; /* temp memory */
475 int memTempPower = 0; /* 10^^power of memTemp */
477 int btn_row, btn_col; /* current position index for button */
478 int prev_btn_row, prev_btn_col; /* previous cursor position */
479 #define CAL_BUTTON (btn_row*5+btn_col)
481 int btn = BUTTON_NONE;
482 int lastbtn = BUTTON_NONE;
484 /* Status of calculator */
485 enum {cal_normal, /* 0, normal status, display result */
486 cal_typing, /* 1, currently typing, dot hasn't been typed */
487 cal_dotted, /* 2, currently typing, dot already has been typed. */
488 cal_error,
489 cal_exit,
490 cal_toDo
491 } calStatus;
493 /* constant table for CORDIC algorithm */
494 double cordicTable[51][2]= {
495 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
496 {1e+00, 7.853981633974483e-01},
497 {5e-01, 4.636476090008061e-01},
498 {2.5e-01, 2.449786631268641e-01},
499 {1.25e-01, 1.243549945467614e-01},
500 {6.25e-02, 6.241880999595735e-02},
501 {3.125e-02, 3.123983343026828e-02},
502 {1.5625e-02, 1.562372862047683e-02},
503 {7.8125e-03, 7.812341060101111e-03},
504 {3.90625e-03, 3.906230131966972e-03},
505 {1.953125e-03, 1.953122516478819e-03},
506 {9.765625e-04, 9.765621895593195e-04},
507 {4.8828125e-04, 4.882812111948983e-04},
508 {2.44140625e-04, 2.441406201493618e-04},
509 {1.220703125e-04, 1.220703118936702e-04},
510 {6.103515625e-05, 6.103515617420877e-05},
511 {3.0517578125e-05, 3.051757811552610e-05},
512 {1.52587890625e-05, 1.525878906131576e-05},
513 {7.62939453125e-06, 7.629394531101970e-06},
514 {3.814697265625e-06, 3.814697265606496e-06},
515 {1.9073486328125e-06, 1.907348632810187e-06},
516 {9.5367431640625e-07, 9.536743164059608e-07},
517 {4.76837158203125e-07, 4.768371582030888e-07},
518 {2.384185791015625e-07, 2.384185791015580e-07},
519 {1.1920928955078125e-07, 1.192092895507807e-07},
520 {5.9604644775390625e-08, 5.960464477539055e-08},
521 {2.98023223876953125e-08, 2.980232238769530e-08},
522 {1.490116119384765625e-08, 1.490116119384765e-08},
523 {7.450580596923828125e-09, 7.450580596923828e-09},
524 {3.7252902984619140625e-09, 3.725290298461914e-09},
525 {1.86264514923095703125e-09, 1.862645149230957e-09},
526 {9.31322574615478515625e-10, 9.313225746154785e-10},
527 {4.656612873077392578125e-10, 4.656612873077393e-10},
528 {2.3283064365386962890625e-10, 2.328306436538696e-10},
529 {1.16415321826934814453125e-10, 1.164153218269348e-10},
530 {5.82076609134674072265625e-11, 5.820766091346741e-11},
531 {2.910383045673370361328125e-11, 2.910383045673370e-11},
532 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
533 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
534 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
535 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
536 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
537 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
538 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
539 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
540 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
541 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
542 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
543 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
544 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
545 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
546 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
549 void doMultiple(double* operandOne, int* powerOne,
550 double operandTwo, int powerTwo);
551 void doAdd (double* operandOne, int* powerOne,
552 double operandTwo, int powerTwo);
553 void printResult(void);
554 void formatResult(void);
555 void oneOperand(void);
557 void drawLines(void);
558 void drawButtons(int group);
560 /* -----------------------------------------------------------------------
561 Handy funtions
562 ----------------------------------------------------------------------- */
563 void cleartypingbuf(void)
565 int k;
566 for( k=1; k<=(DIGITLEN+1); k++)
567 typingbuf[k] = 0;
568 typingbuf[0] = ' ';
569 typingbufPointer = typingbuf+1;
571 void clearbuf(void)
573 int k;
574 for(k=0;k<18;k++)
575 buf[k]=' ';
576 buf[18] = 0;
578 void clearResult(void)
580 result = 0;
581 power = 0;
582 modifier = 0.1;
585 void clearInput(void)
587 calStatus = cal_normal;
588 clearResult();
589 cleartypingbuf();
590 rb->lcd_clear_display();
591 drawButtons(buttonGroup);
592 drawLines();
595 void clearOperand(void)
597 operand = 0;
598 operandPower = 0;
601 void clearMemTemp(void)
603 memTemp = 0;
604 memTempPower = 0;
607 void clearOper(void)
609 oper = ' ';
610 operInputted = false;
613 void clearMem(void)
615 clearInput();
616 clearMemTemp();
617 clearOperand();
618 clearOper();
619 btn = BUTTON_NONE;
622 void switchOperands(void)
624 double tempr = operand;
625 int tempp = operandPower;
626 operand = result;
627 operandPower = power;
628 result = tempr;
629 power = tempp;
632 void drawLines(void)
634 int i;
635 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS-1);
636 for (i = 0; i < 5 ; i++)
637 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS+i*REC_HEIGHT);
638 for (i = 0; i < 4 ; i++)
639 rb->lcd_vline(X_1_POS+i*REC_WIDTH, Y_1_POS, LCD_HEIGHT);
642 void drawButtons(int group)
644 int i, j, w, h;
645 for (i = 0; i <= 4; i++){
646 for (j = 0; j <= 4; j++){
647 rb->lcd_getstringsize( buttonChar[group][i][j],&w,&h);
648 if (i == btn_row && j == btn_col) /* selected item */
649 rb->lcd_set_drawmode(DRMODE_SOLID);
650 else
651 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
652 rb->lcd_fillrect( X_0_POS + j*REC_WIDTH,
653 Y_1_POS + i*REC_HEIGHT,
654 REC_WIDTH, REC_HEIGHT+1);
655 if (i == btn_row && j == btn_col) /* selected item */
656 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
657 else
658 rb->lcd_set_drawmode(DRMODE_SOLID);
659 rb->lcd_putsxy( X_0_POS + j*REC_WIDTH + (REC_WIDTH - w)/2,
660 Y_1_POS + i*REC_HEIGHT + (REC_HEIGHT - h)/2 + 1,
661 buttonChar[group][i][j] );
664 rb->lcd_set_drawmode(DRMODE_SOLID);
667 /* -----------------------------------------------------------------------
668 Initiate calculator
669 ----------------------------------------------------------------------- */
670 void cal_initial (void)
672 int w,h;
674 rb->lcd_getstringsize("2nd",&w,&h);
675 if (w > REC_WIDTH || h > REC_HEIGHT)
676 rb->lcd_setfont(FONT_SYSFIXED);
678 rb->lcd_clear_display();
680 #ifdef CALCULATOR_OPERATORS
681 /* basic operators are available through separate button */
682 buttonGroup = sciButtons;
683 #else
684 buttonGroup = basicButtons;
685 #endif
687 /* initially, invert button "5" */
688 btn_row = 2;
689 btn_col = 1;
690 prev_btn_row = btn_row;
691 prev_btn_col = btn_col;
692 drawButtons(buttonGroup);
693 drawLines();
694 rb->lcd_update();
696 /* initial mem and output display*/
697 clearMem();
698 printResult();
700 /* clear button queue */
701 rb->button_clear_queue();
704 /* -----------------------------------------------------------------------
705 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
706 in it's private case for sqrt.
707 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
708 ----------------------------------------------------------------------- */
709 double mySqrt(double square)
711 int k = 0;
712 double temp = 0;
713 double root= ABS(square+1)/2;
715 while( ABS(root - temp) > MINIMUM ){
716 temp = root;
717 root = (square/temp + temp)/2;
718 k++;
719 if (k>10000) return 0;
722 return root;
724 /* -----------------------------------------------------------------------
725 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
726 transcendFunc can do sin,cos,log,exp
727 input parameter is angle
728 ----------------------------------------------------------------------- */
729 void transcendFunc(char* func, double* tt, int* ttPower)
731 double t = (*tt)*M_PI/180; int tPower = *ttPower;
732 int sign = 1;
733 int n = 50; /* n <=50, tables are all <= 50 */
734 int j;
735 double x,y,z,xt,yt,zt;
737 if (tPower < -998) {
738 calStatus = cal_normal;
739 return;
741 if (tPower > 8) {
742 calStatus = cal_error;
743 return;
745 *ttPower = 0;
746 calStatus = cal_normal;
748 if( func[0] =='s' || func[0] =='S'|| func[0] =='t' || func[0] =='T')
749 sign = SIGN(t);
750 else {
751 /* if( func[0] =='c' || func[0] =='C') */
752 sign = 1;
754 t = ABS(t);
756 while (tPower > 0){
757 t *= 10;
758 tPower--;
760 while (tPower < 0) {
761 t /= 10;
762 tPower++;
764 j = 0;
765 while (t > j*M_TWOPI) {j++;}
766 t -= (j-1)*M_TWOPI;
767 if (M_PI_2 < t && t < 3*M_PI_2){
768 t = M_PI - t;
769 if (func[0] =='c' || func[0] =='C')
770 sign = -1;
771 else if (func[0] =='t' || func[0] =='T')
772 t*=-1;
774 else if ( 3*M_PI_2 <= t && t <= M_TWOPI)
775 t -= M_TWOPI;
777 x = 0.60725293500888; y = 0; z = t;
778 for (j=1;j<n+2;j++){
779 xt = x - SIGN(z) * y*cordicTable[j-1][0];
780 yt = y + SIGN(z) * x*cordicTable[j-1][0];
781 zt = z - SIGN(z) * cordicTable[j-1][1];
782 x = xt;
783 y=yt;
784 z=zt;
786 if( func[0] =='s' || func[0] =='S') {
787 *tt = sign*y;
788 return;
790 else if( func[0] =='c' || func[0] =='C') {
791 *tt = sign*x;
792 return;
794 else /*if( func[0] =='t' || func[0] =='T')*/ {
795 if(t==M_PI_2||t==-M_PI_2){
796 calStatus = cal_error;
797 return;
799 else{
800 *tt = sign*(y/x);
801 return;
806 /* -----------------------------------------------------------------------
807 add in scientific number format
808 ----------------------------------------------------------------------- */
809 void doAdd (double* operandOne, int* powerOne,
810 double operandTwo, int powerTwo)
812 if ( *powerOne >= powerTwo ){
813 if (*powerOne - powerTwo <= DIGITLEN+1){
814 while (powerTwo < *powerOne){
815 operandTwo /=10;
816 powerTwo++;
818 *operandOne += operandTwo;
820 /*do nothing if operandTwo is too small*/
822 else{
823 if (powerTwo - *powerOne <= DIGITLEN+1){
824 while(powerTwo > *powerOne){
825 *operandOne /=10;
826 (*powerOne)++;
828 (*operandOne) += operandTwo;
830 else{/* simply copy operandTwo if operandOne is too small */
831 *operandOne = operandTwo;
832 *powerOne = powerTwo;
836 /* -----------------------------------------------------------------------
837 multiple in scientific number format
838 ----------------------------------------------------------------------- */
839 void doMultiple(double* operandOne, int* powerOne,
840 double operandTwo, int powerTwo)
842 (*operandOne) *= operandTwo;
843 (*powerOne) += powerTwo;
846 /* -----------------------------------------------------------------------
847 Handles all one operand calculations
848 ----------------------------------------------------------------------- */
849 void oneOperand(void)
851 int k = 0;
852 if (buttonGroup == basicButtons){
853 switch(CAL_BUTTON){
854 case btn_sqr:
855 if (result<0)
856 calStatus = cal_error;
857 else{
858 if (power%2 == 1){
859 result = (mySqrt(result*10))/10;
860 power = (power+1) / 2;
862 else{
863 result = mySqrt(result);
864 power = power / 2;
866 calStatus = cal_normal;
868 break;
869 case btn_square:
870 power *= 2;
871 result *= result;
872 calStatus = cal_normal;
873 break;
875 case btn_rec:
876 if (result==0)
877 calStatus = cal_error;
878 else{
879 power = -power;
880 result = 1/result;
881 calStatus = cal_normal;
883 break;
884 default:
885 calStatus = cal_toDo;
886 break; /* just for the safety */
889 else{ /* sciButtons */
890 switch(CAL_BUTTON){
891 case sci_sin:
892 transcendFunc("sin", &result, &power);
893 break;
894 case sci_cos:
895 transcendFunc("cos", &result, &power);
896 break;
897 case sci_tan:
898 transcendFunc("tan", &result, &power);
899 break;
900 case sci_fac:
901 if (power<0 || power>8 || result<0 )
902 calStatus = cal_error;
903 else if(result == 0) {
904 result = 1;
905 power = 0;
907 else{
908 while(power > 0) {
909 result *= 10;
910 power--;
912 if ( ( result - (int)result) > MINIMUM )
913 calStatus = cal_error;
914 else {
915 k = result; result = 1;
916 while (k > 1){
917 doMultiple(&result, &power, k, 0);
918 formatResult();
919 k--;
921 calStatus = cal_normal;
924 break;
925 default:
926 calStatus = cal_toDo;
927 break; /* just for the safety */
933 /* -----------------------------------------------------------------------
934 Handles all two operands calculations
935 ----------------------------------------------------------------------- */
936 void twoOperands(void)
938 switch(oper){
939 case '-':
940 doAdd(&operand, &operandPower, -result, power);
941 break;
942 case '+':
943 doAdd(&operand, &operandPower, result, power);
944 break;
945 case '*':
946 doMultiple(&operand, &operandPower, result, power);
947 break;
948 case '/':
949 if ( ABS(result) > MINIMUM ){
950 doMultiple(&operand, &operandPower, 1/result, -power);
952 else
953 calStatus = cal_error;
954 break;
955 default: /* ' ' */
956 switchOperands(); /* counter switchOperands() below */
957 break;
958 } /* switch(oper) */
959 switchOperands();
960 clearOper();
963 /* First, increases *dimen1 by dimen1_delta modulo dimen1_modulo.
964 If dimen1 wraps, increases *dimen2 by dimen2_delta modulo dimen2_modulo.
966 static void move_with_wrap_and_shift(
967 int *dimen1, int dimen1_delta, int dimen1_modulo,
968 int *dimen2, int dimen2_delta, int dimen2_modulo)
970 bool wrapped = false;
972 *dimen1 += dimen1_delta;
973 if (*dimen1 < 0)
975 *dimen1 = dimen1_modulo - 1;
976 wrapped = true;
978 else if (*dimen1 >= dimen1_modulo)
980 *dimen1 = 0;
981 wrapped = true;
984 if (wrapped)
986 /* Make the dividend always positive to be sure about the result.
987 Adding dimen2_modulo does not change it since we do it modulo. */
988 *dimen2 = (*dimen2 + dimen2_modulo + dimen2_delta) % dimen2_modulo;
992 /* -----------------------------------------------------------------------
993 Print buttons when switching 1st and 2nd
994 int group = {basicButtons, sciButtons}
995 ----------------------------------------------------------------------- */
996 void printButtonGroups(int group)
998 drawButtons(group);
999 drawLines();
1000 rb->lcd_update();
1002 /* -----------------------------------------------------------------------
1003 flash the currently marked button
1004 ----------------------------------------------------------------------- */
1005 void flashButton(void)
1007 int k, w, h;
1008 for (k=2;k>0;k--)
1010 rb->lcd_getstringsize( buttonChar[buttonGroup][btn_row][btn_col],&w,&h);
1011 rb->lcd_set_drawmode(DRMODE_SOLID|(k==1) ? 0 : DRMODE_INVERSEVID);
1012 rb->lcd_fillrect( X_0_POS + btn_col*REC_WIDTH + 1,
1013 Y_1_POS + btn_row*REC_HEIGHT + 1,
1014 REC_WIDTH - 1, REC_HEIGHT - 1);
1015 rb->lcd_putsxy( X_0_POS + btn_col*REC_WIDTH + (REC_WIDTH - w)/2,
1016 Y_1_POS + btn_row*REC_HEIGHT + (REC_HEIGHT - h)/2 +1,
1017 buttonChar[buttonGroup][btn_row][btn_col] );
1018 rb->lcd_update_rect( X_0_POS + btn_col*REC_WIDTH + 1,
1019 Y_1_POS + btn_row*REC_HEIGHT + 1,
1020 REC_WIDTH - 1, REC_HEIGHT - 1);
1022 if (k!= 1)
1023 rb->sleep(HZ/22);
1028 /* -----------------------------------------------------------------------
1029 pos is the position that needs animation. pos = [1~18]
1030 ----------------------------------------------------------------------- */
1031 void deleteAnimation(int pos)
1033 int k, w, h, x;
1034 if (pos<1 || pos >18)
1035 return;
1037 rb->lcd_getstringsize("0", &w, &h);
1038 x = (pos==1? 4: LCD_WIDTH - 4 - w);
1040 for (k=0;k<4;k++){
1041 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1042 rb->lcd_fillrect(x, Y_1_POS - h -1, w, h);
1043 rb->lcd_set_drawmode(DRMODE_SOLID);
1044 rb->lcd_fillrect(x + (w*k)/8, Y_1_POS - h -1 + (h*k)/8,
1045 (w*(4-k))/4, (h*(4-k))/4);
1046 rb->lcd_update_rect(x, Y_1_POS - h -1, w, h);
1047 rb->sleep(HZ/32);
1051 /* -----------------------------------------------------------------------
1052 result may be one of these formats:
1054 xxxx.xxxx
1055 0.xxxx
1056 0.0000xxxx
1058 formatResult() change result to standard format: 0.xxxx
1059 if result is close to 0, let it be 0;
1060 if result is close to 1, let it be 0.1 and power++;
1061 ----------------------------------------------------------------------- */
1062 void formatResult(void)
1064 int resultsign = SIGN(result);
1065 result = ABS(result);
1066 if(result > MINIMUM ){ /* doesn't check power, might have problem
1067 input wouldn't,
1068 + - * / of two formatted number wouldn't.
1069 only a calculation that makes a formatted
1070 number (0.xxxx) less than MINIMUM in only
1071 one operation */
1073 if (result<1){
1074 while( (int)(result*10) == 0 ){
1075 result *= 10;
1076 power--;
1077 modifier *= 10;
1080 else{ /* result >= 1 */
1081 while( (int)result != 0 ){
1082 result /= 10;
1083 power++;
1084 modifier /= 10;
1086 } /* if result<1 */
1088 if (result > (1-MINIMUM)){
1089 result = 0.1;
1090 power++;
1091 modifier /= 10;
1093 result *= resultsign;
1095 else {
1096 result = 0;
1097 power = 0;
1098 modifier = 0.1;
1102 /* -----------------------------------------------------------------------
1103 result2typingbuf() outputs standard format result to typingbuf.
1104 case SCIENTIFIC_FORMAT, let temppower = 1;
1105 case temppower > 0: print '.' in the middle
1106 case temppower <= 0: print '.' in the begining
1107 ----------------------------------------------------------------------- */
1108 void result2typingbuf(void)
1110 bool haveDot = false;
1111 char tempchar = 0;
1112 int k;
1113 double tempresult = ABS(result); /* positive num makes things simple */
1115 int temppower;
1116 double tempmodifier = 1;
1117 int count;
1119 if(SCIENTIFIC_FORMAT)
1120 temppower = 1; /* output x.xxxx format */
1121 else
1122 temppower = power;
1124 cleartypingbuf();
1126 if(tempresult < MINIMUM){ /* if 0,faster display and avoid complication*/
1127 typingbuf[0] = ' ';
1128 typingbuf[1] = '0';
1130 else{ /* tempresult > 0 */
1131 typingbuf[0] = (SIGN(result)<0)?'-':' ';
1133 typingbufPointer = typingbuf;
1134 if(temppower > 0){
1135 for (k = 0; k<DIGITLEN+1 ; k++){
1136 typingbufPointer++;
1137 if(temppower || *(typingbufPointer-1) == '.'){
1138 count = 0;
1139 tempmodifier = tempmodifier/10;
1140 while( (tempresult-tempmodifier*count) >
1141 (tempmodifier-MINIMUM)){
1142 count++;
1144 tempresult -= tempmodifier*count;
1145 tempresult = ABS(tempresult);
1146 temppower-- ;
1147 *typingbufPointer = count + '0';
1149 else{ /* temppower == 0 */
1150 *typingbufPointer = '.';
1151 haveDot = true;
1153 } /* for */
1155 else{
1156 haveDot = true;
1157 typingbufPointer++; *typingbufPointer = '0';
1158 typingbufPointer++; *typingbufPointer = '.';
1159 for (k = 2; k<DIGITLEN+1 ; k++){
1160 typingbufPointer++;
1161 count = 0;
1162 if ( (-temppower) < (k-1)){
1163 tempmodifier = tempmodifier/10;
1164 while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){
1165 count++;
1168 tempresult -= tempmodifier*count;
1169 tempresult = ABS(tempresult);
1170 temppower-- ;
1172 *typingbufPointer = count + '0';
1175 /* now, typingbufPointer = typingbuf + 16 */
1176 /* backward strip off 0 and '.' */
1177 if (haveDot){
1178 while( (*typingbufPointer == '0') || (*typingbufPointer == '.')){
1179 tempchar = *typingbufPointer;
1180 *typingbufPointer = 0;
1181 typingbufPointer--;
1182 if (tempchar == '.') break;
1185 typingbuf[DIGITLEN+1] = 0;
1186 } /* else tempresult > 0 */
1189 /* -----------------------------------------------------------------------
1190 printResult() generates LCD display.
1191 ----------------------------------------------------------------------- */
1192 void printResult(void)
1194 int k, w, h;
1196 char operbuf[3] = {0, 0, 0};
1198 switch_Status:
1199 switch(calStatus){
1200 case cal_exit:
1201 rb->lcd_clear_display();
1202 rb->splash(HZ/3, "Bye now!");
1203 break;
1204 case cal_error:
1205 clearbuf();
1206 rb->snprintf(buf, 19, "%18s","Error");
1207 break;
1208 case cal_toDo:
1209 clearbuf();
1210 rb->snprintf(buf, 19, "%18s","Coming soon ^_* ");
1211 break;
1213 case cal_normal:
1214 formatResult();
1216 if( power > 1000 ){ /* power -1 > 999 */
1217 calStatus = cal_error;
1218 goto switch_Status;
1220 if (power < -998 ) /* power -1 < -999 */
1221 clearResult(); /* too small, let it be 0 */
1223 result2typingbuf();
1224 clearbuf();
1226 operbuf[0] = oper;
1227 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1228 operbuf[2] = '\0';
1230 if(SCIENTIFIC_FORMAT){
1231 /* output format: X.XXXX eXXX */
1232 if(power > -98){ /* power-1 >= -99, eXXX or e-XX */
1233 rb->snprintf(buf, 12, "%11s",typingbuf);
1234 for(k=11;k<=14;k++) buf[k] = ' ';
1235 cleartypingbuf();
1236 rb->snprintf(typingbuf, 5, "e%d",power-1);
1237 rb->snprintf(buf+11, 5, "%4s",typingbuf);
1239 else{ /* power-1 <= -100, e-XXX */
1240 rb->snprintf(buf, 12, "%11s",typingbuf);
1241 rb->snprintf(buf+11, 6, "e%d",power-1);
1244 else{
1245 rb->snprintf(buf, 12, "%11s",typingbuf);
1246 } /* if SCIENTIFIC_FORMAT */
1247 break;
1248 case cal_typing:
1249 case cal_dotted:
1250 clearbuf();
1251 operbuf[0] = oper;
1252 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1253 rb->snprintf(buf, 12, "%11s",typingbuf);
1254 break;
1258 rb->lcd_getstringsize(buf, &w, &h);
1259 rb->screen_clear_area(rb->screens[0], 0, 0, LCD_WIDTH, Y_1_POS - 1);
1260 rb->lcd_putsxy(4, Y_1_POS - h -1, operbuf);
1261 rb->lcd_putsxy(LCD_WIDTH - w - 4, Y_1_POS - h -1, buf);
1262 rb->lcd_update_rect(0, 1, LCD_WIDTH, Y_1_POS);
1265 /* -----------------------------------------------------------------------
1266 Process typing buttons: 1-9, '.', sign
1267 main operand "result" and typingbuf are processed seperately here.
1268 ----------------------------------------------------------------------- */
1269 void typingProcess(void){
1270 switch( CAL_BUTTON ){
1271 case btn_sign:
1272 if (calStatus == cal_typing ||
1273 calStatus == cal_dotted)
1274 typingbuf[0] = (typingbuf[0]=='-')?' ':'-';
1275 result = -result;
1276 break;
1277 case btn_dot:
1278 operInputted = false;
1279 switch(calStatus){
1280 case cal_normal:
1281 clearInput();
1282 *typingbufPointer = '0';
1283 typingbufPointer++;
1284 case cal_typing:
1285 calStatus = cal_dotted;
1286 *typingbufPointer = '.';
1287 if (typingbufPointer != typingbuf+DIGITLEN+1)
1288 typingbufPointer++;
1289 break;
1290 default: /* cal_dotted */
1291 break;
1293 break;
1294 default: /* 0-9 */
1295 operInputted = false;
1296 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1297 switch(calStatus){
1298 case cal_normal:
1299 if(CAL_BUTTON == btn_0 )
1300 break; /* first input is 0, ignore */
1301 clearInput();
1302 /*no operator means start a new calculation*/
1303 if (oper ==' ')
1304 clearOperand();
1305 calStatus = cal_typing;
1306 /* go on typing, no break */
1307 case cal_typing:
1308 case cal_dotted:
1309 switch(CAL_BUTTON){
1310 case btn_0:
1311 *typingbufPointer = '0';
1312 break;
1313 default:
1314 *typingbufPointer=(7+btn_col-3*(btn_row-1))+ '0';
1315 break;
1317 if (typingbufPointer!=typingbuf+DIGITLEN+1){
1318 typingbufPointer++;
1320 {/* result processing */
1321 if (calStatus == cal_typing) power++;
1322 if (CAL_BUTTON != btn_0)
1323 result= result +
1324 SIGN(result)*
1325 (7+btn_col-3*(btn_row-1))*modifier;
1326 modifier /= 10;
1329 else /* last byte always '\0' */
1330 *typingbufPointer = 0;
1331 break;
1332 default: /* cal_error, cal_exit */
1333 break;
1335 break; /* default, 0-9 */
1336 } /* switch( CAL_BUTTON ) */
1339 /* -----------------------------------------------------------------------
1340 Handle delete operation
1341 main operand "result" and typingbuf are processed seperately here.
1342 ----------------------------------------------------------------------- */
1343 void doDelete(void){
1344 deleteAnimation(18);
1345 switch(calStatus){
1346 case cal_dotted:
1347 if (*(typingbufPointer-1) == '.'){
1348 /* if dotted and deleting '.',
1349 change status and delete '.' below */
1350 calStatus = cal_typing;
1352 else{ /* if dotted and not deleting '.',
1353 power stays */
1354 power++; /* counter "power--;" below */
1356 case cal_typing:
1357 typingbufPointer--;
1359 {/* result processing */ /* 0-9, '.' */
1360 /* if deleting '.', do nothing */
1361 if ( *typingbufPointer != '.'){
1362 power--;
1363 modifier *= 10;
1364 result = result - SIGN(result)*
1365 ((*typingbufPointer)- '0')*modifier;
1369 *typingbufPointer = 0;
1371 /* if (only one digit left and it's 0)
1372 or no digit left, change status*/
1373 if ( typingbufPointer == typingbuf+1 ||
1374 ( typingbufPointer == typingbuf+2 &&
1375 *(typingbufPointer-1) == '0' ))
1376 calStatus = cal_normal;
1377 break;
1378 default: /* normal, error, exit */
1379 break;
1382 /* -----------------------------------------------------------------------
1383 Handle buttons on basic screen
1384 ----------------------------------------------------------------------- */
1385 void basicButtonsProcess(void){
1386 switch (btn) {
1387 case CALCULATOR_INPUT:
1388 if (calStatus == cal_error && (CAL_BUTTON != btn_C) ) break;
1389 flashButton();
1390 switch( CAL_BUTTON ){
1391 case btn_MR:
1392 operInputted = false;
1393 result = memTemp; power = memTempPower;
1394 calStatus = cal_normal;
1395 break;
1396 case btn_M:
1397 formatResult();
1398 if (memTemp > MINIMUM)
1399 doAdd(&memTemp, &memTempPower, result, power);
1400 else {
1401 /* if result is too small and memTemp = 0,
1402 doAdd will not add */
1403 memTemp = result;
1404 memTempPower = power;
1406 calStatus = cal_normal;
1407 break;
1409 case btn_C: clearMem(); break;
1410 case btn_CE: clearInput(); break;
1412 case btn_bas:
1413 buttonGroup = sciButtons;
1414 printButtonGroups(buttonGroup);
1415 break;
1417 /* one operand calculation, may be changed to
1418 like sin, cos, log, etc */
1419 case btn_sqr:
1420 case btn_square:
1421 case btn_rec:
1422 formatResult(); /* not necessary, just for safty */
1423 oneOperand();
1424 break;
1426 case_btn_equal: /* F3 shortkey entrance */
1427 case btn_equal:
1428 formatResult();
1429 calStatus = cal_normal;
1430 operInputted = false;
1431 if (oper != ' ') twoOperands();
1432 break;
1434 case btn_div:
1435 case btn_time:
1436 case btn_minus:
1437 case btn_add:
1438 if(!operInputted) {twoOperands(); operInputted = true;}
1439 oper = buttonChar[basicButtons][btn_row][btn_col][0];
1440 #ifdef CALCULATOR_OPERATORS
1441 case_cycle_operators: /* F2 shortkey entrance */
1442 #endif
1443 calStatus = cal_normal;
1444 formatResult();
1445 operand = result;
1446 operandPower = power;
1448 break;
1450 case btn_sign:
1451 case btn_dot:
1452 default: /* 0-9 */
1453 typingProcess();
1454 break;
1455 } /* switch (CAL_BUTTON) */
1456 break;
1458 #ifdef CALCULATOR_OPERATORS
1459 case CALCULATOR_OPERATORS:
1460 if (calStatus == cal_error) break;
1461 if (!operInputted) {twoOperands(); operInputted = true;}
1462 switch (oper){
1463 case ' ':
1464 case '/': oper = '+'; flashButton(); break;
1465 case '+': oper = '-'; flashButton(); break;
1466 case '-': oper = '*'; flashButton(); break;
1467 case '*': oper = '/'; flashButton(); break;
1469 goto case_cycle_operators;
1470 break;
1471 #endif
1473 case CALCULATOR_CALC:
1474 if (calStatus == cal_error) break;
1475 flashButton();
1476 goto case_btn_equal;
1477 break;
1478 default: break;
1480 printResult();
1483 /* -----------------------------------------------------------------------
1484 Handle buttons on scientific screen
1485 ----------------------------------------------------------------------- */
1486 void sciButtonsProcess(void){
1487 switch (btn) {
1488 case CALCULATOR_INPUT:
1489 if (calStatus == cal_error && (CAL_BUTTON != sci_sci) ) break;
1490 flashButton();
1491 switch( CAL_BUTTON ){
1493 case sci_pi:
1494 result = M_PI; power = 0;
1495 calStatus = cal_normal;
1496 break;
1498 case sci_xy: break;
1500 case sci_sci:
1501 buttonGroup = basicButtons;
1502 printButtonGroups(basicButtons);
1503 break;
1505 case sci_fac:
1506 case sci_sin:
1507 case sci_asin:
1508 case sci_cos:
1509 case sci_acos:
1510 case sci_tan:
1511 case sci_atan:
1512 case sci_ln:
1513 case sci_exp:
1514 case sci_log:
1515 formatResult(); /* not necessary, just for safty */
1516 oneOperand();
1517 break;
1519 case btn_sign:
1520 case btn_dot:
1521 default: /* 0-9 */
1522 typingProcess();
1523 break;
1524 } /* switch (CAL_BUTTON) */
1525 break;
1527 #ifdef CALCULATOR_OPERATORS
1528 case CALCULATOR_OPERATORS:
1529 if (calStatus == cal_error) break;
1530 if (!operInputted) {twoOperands(); operInputted = true;}
1531 switch (oper){
1532 case ' ': oper = '+'; break;
1533 case '/': oper = '+'; deleteAnimation(1); break;
1534 case '+': oper = '-'; deleteAnimation(1); break;
1535 case '-': oper = '*'; deleteAnimation(1); break;
1536 case '*': oper = '/'; deleteAnimation(1); break;
1538 calStatus = cal_normal;
1539 formatResult();
1540 operand = result;
1541 operandPower = power;
1542 break;
1543 #endif
1545 case CALCULATOR_CALC:
1546 if (calStatus == cal_error) break;
1547 formatResult();
1548 calStatus = cal_normal;
1549 operInputted = false;
1550 if (oper != ' ') twoOperands();
1551 break;
1552 default: break;
1554 printResult();
1557 /* -----------------------------------------------------------------------
1558 move button index
1559 Invert display new button, invert back previous button
1560 ----------------------------------------------------------------------- */
1561 int handleButton(int button){
1562 switch(button)
1564 case CALCULATOR_INPUT:
1565 case CALCULATOR_CALC:
1566 #ifdef CALCULATOR_INPUT_CALC_PRE
1567 if (lastbtn != CALCULATOR_INPUT_CALC_PRE)
1568 break;
1569 /* no unconditional break; here! */
1570 #endif
1571 #ifdef CALCULATOR_OPERATORS
1572 case CALCULATOR_OPERATORS:
1573 #endif
1574 switch(buttonGroup){
1575 case basicButtons:
1576 basicButtonsProcess();
1577 break;
1578 case sciButtons:
1579 sciButtonsProcess();
1580 break;
1582 break;
1584 #ifdef CALCULATOR_CLEAR
1585 case CALCULATOR_CLEAR:
1586 switch(calStatus){
1587 case cal_typing:
1588 case cal_dotted:
1589 doDelete();
1590 break;
1591 default: /* cal_normal, cal_error, cal_exit */
1592 clearMem();
1593 break;
1595 printResult();
1596 break;
1597 #endif
1598 case CALCULATOR_LEFT:
1599 case CALCULATOR_LEFT | BUTTON_REPEAT:
1600 move_with_wrap_and_shift(
1601 &btn_col, -1, BUTTON_COLS,
1602 &btn_row, 0, BUTTON_ROWS);
1603 break;
1605 case CALCULATOR_RIGHT:
1606 case CALCULATOR_RIGHT | BUTTON_REPEAT:
1607 move_with_wrap_and_shift(
1608 &btn_col, 1, BUTTON_COLS,
1609 &btn_row, 0, BUTTON_ROWS);
1610 break;
1612 #ifdef CALCULATOR_UP
1613 case CALCULATOR_UP:
1614 case CALCULATOR_UP | BUTTON_REPEAT:
1615 move_with_wrap_and_shift(
1616 &btn_row, -1, BUTTON_ROWS,
1617 &btn_col, 0, BUTTON_COLS);
1618 break;
1619 #endif
1620 #ifdef CALCULATOR_DOWN
1621 case CALCULATOR_DOWN:
1622 case CALCULATOR_DOWN | BUTTON_REPEAT:
1623 move_with_wrap_and_shift(
1624 &btn_row, 1, BUTTON_ROWS,
1625 &btn_col, 0, BUTTON_COLS);
1626 break;
1627 #endif
1629 #ifdef CALCULATOR_UP_W_SHIFT
1630 case CALCULATOR_UP_W_SHIFT:
1631 case CALCULATOR_UP_W_SHIFT | BUTTON_REPEAT:
1632 move_with_wrap_and_shift(
1633 &btn_row, -1, BUTTON_ROWS,
1634 &btn_col, -1, BUTTON_COLS);
1635 break;
1636 #endif
1637 #ifdef CALCULATOR_DOWN_W_SHIFT
1638 case CALCULATOR_DOWN_W_SHIFT:
1639 case CALCULATOR_DOWN_W_SHIFT | BUTTON_REPEAT:
1640 move_with_wrap_and_shift(
1641 &btn_row, 1, BUTTON_ROWS,
1642 &btn_col, 1, BUTTON_COLS);
1643 break;
1644 #endif
1645 #ifdef CALCULATOR_LEFT_W_SHIFT
1646 case CALCULATOR_LEFT_W_SHIFT:
1647 case CALCULATOR_LEFT_W_SHIFT | BUTTON_REPEAT:
1648 move_with_wrap_and_shift(
1649 &btn_col, -1, BUTTON_COLS,
1650 &btn_row, -1, BUTTON_ROWS);
1651 break;
1652 #endif
1653 #ifdef CALCULATOR_RIGHT_W_SHIFT
1654 case CALCULATOR_RIGHT_W_SHIFT:
1655 case CALCULATOR_RIGHT_W_SHIFT | BUTTON_REPEAT:
1656 move_with_wrap_and_shift(
1657 &btn_col, 1, BUTTON_COLS,
1658 &btn_row, 1, BUTTON_ROWS);
1659 break;
1660 #endif
1661 #ifdef CALCULATOR_RC_QUIT
1662 case CALCULATOR_RC_QUIT:
1663 #endif
1664 case CALCULATOR_QUIT:
1665 return -1;
1668 return 0;
1670 prev_btn_row = btn_row;
1671 prev_btn_col = btn_col;
1674 /* -----------------------------------------------------------------------
1675 Main();
1676 ----------------------------------------------------------------------- */
1677 enum plugin_status plugin_start(const void* parameter)
1679 (void)parameter;
1681 /* now go ahead and have fun! */
1683 #ifdef HAVE_TOUCHSCREEN
1684 rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
1685 #endif
1687 cal_initial();
1689 while (calStatus != cal_exit ) {
1690 btn = rb->button_get_w_tmo(HZ/2);
1691 #ifdef HAVE_TOUCHSCREEN
1692 if(btn & BUTTON_TOUCHSCREEN)
1694 struct ts_raster_result res;
1695 if(touchscreen_map_raster(&calc_raster, rb->button_get_data() >> 16,
1696 rb->button_get_data() & 0xffff, &res) == 1)
1698 btn_row = res.y;
1699 btn_col = res.x;
1700 drawButtons(buttonGroup);
1701 drawLines();
1703 rb->lcd_update();
1705 prev_btn_row = btn_row;
1706 prev_btn_col = btn_col;
1707 if(btn & BUTTON_REL)
1709 btn = CALCULATOR_INPUT;
1710 switch(buttonGroup){
1711 case basicButtons:
1712 basicButtonsProcess();
1713 break;
1714 case sciButtons:
1715 sciButtonsProcess();
1716 break;
1718 btn = BUTTON_TOUCHSCREEN;
1722 #endif
1723 if (handleButton(btn) == -1)
1725 calStatus = cal_exit;
1726 printResult();
1728 else
1730 drawButtons(buttonGroup);
1731 drawLines();
1734 rb->lcd_update();
1736 if(rb->default_event_handler(btn) == SYS_USB_CONNECTED)
1737 return PLUGIN_USB_CONNECTED;
1739 if (btn != BUTTON_NONE)
1740 lastbtn = btn;
1741 } /* while (calStatus != cal_exit ) */
1743 rb->button_clear_queue();
1744 return PLUGIN_OK;
1747 #endif /* #ifdef HAVE_LCD_BITMAP */