* Fix plugin keymaps for VX777
[kugel-rb.git] / apps / plugins / calculator.c
blob74ee482b5dcdd787e6c7b49bc8e7795a9d9768b2
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)
223 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
224 #define CALCULATOR_LEFT BUTTON_LEFT
225 #define CALCULATOR_RIGHT BUTTON_RIGHT
226 #define CALCULATOR_UP BUTTON_UP
227 #define CALCULATOR_DOWN BUTTON_DOWN
228 #define CALCULATOR_QUIT BUTTON_POWER
229 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
230 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
231 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
232 #define CALCULATOR_CLEAR BUTTON_HOME
234 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
235 #define CALCULATOR_LEFT BUTTON_LEFT
236 #define CALCULATOR_RIGHT BUTTON_RIGHT
237 #define CALCULATOR_UP BUTTON_UP
238 #define CALCULATOR_DOWN BUTTON_DOWN
239 #define CALCULATOR_QUIT BUTTON_POWER
240 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
241 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
242 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
243 #define CALCULATOR_CLEAR (BUTTON_SELECT|BUTTON_UP)
245 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
247 #define CALCULATOR_LEFT BUTTON_LEFT
248 #define CALCULATOR_RIGHT BUTTON_RIGHT
249 #define CALCULATOR_UP BUTTON_SCROLL_UP
250 #define CALCULATOR_DOWN BUTTON_SCROLL_DOWN
251 #define CALCULATOR_QUIT BUTTON_POWER
252 #define CALCULATOR_INPUT_CALC_PRE BUTTON_PLAY
253 #define CALCULATOR_INPUT (BUTTON_PLAY | BUTTON_REL)
254 #define CALCULATOR_CALC (BUTTON_PLAY | BUTTON_REPEAT)
255 #define CALCULATOR_CLEAR BUTTON_REW
257 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
259 #define CALCULATOR_LEFT BUTTON_LEFT
260 #define CALCULATOR_RIGHT BUTTON_RIGHT
261 #define CALCULATOR_UP BUTTON_UP
262 #define CALCULATOR_DOWN BUTTON_DOWN
263 #define CALCULATOR_QUIT BUTTON_BACK
264 #define CALCULATOR_INPUT BUTTON_SELECT
265 #define CALCULATOR_CALC BUTTON_MENU
266 #define CALCULATOR_CLEAR BUTTON_PLAY
268 #elif (CONFIG_KEYPAD == MROBE100_PAD)
270 #define CALCULATOR_LEFT BUTTON_LEFT
271 #define CALCULATOR_RIGHT BUTTON_RIGHT
272 #define CALCULATOR_UP BUTTON_UP
273 #define CALCULATOR_DOWN BUTTON_DOWN
274 #define CALCULATOR_QUIT BUTTON_POWER
275 #define CALCULATOR_INPUT BUTTON_SELECT
276 #define CALCULATOR_CALC BUTTON_MENU
277 #define CALCULATOR_CLEAR BUTTON_DISPLAY
279 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
281 #define CALCULATOR_LEFT BUTTON_RC_REW
282 #define CALCULATOR_RIGHT BUTTON_RC_FF
283 #define CALCULATOR_UP BUTTON_RC_VOL_UP
284 #define CALCULATOR_DOWN BUTTON_RC_VOL_DOWN
285 #define CALCULATOR_QUIT BUTTON_RC_REC
286 #define CALCULATOR_INPUT BUTTON_RC_PLAY
287 #define CALCULATOR_CALC BUTTON_RC_MODE
288 #define CALCULATOR_CLEAR BUTTON_RC_MENU
290 #define CALCULATOR_RC_QUIT BUTTON_REC
292 #elif (CONFIG_KEYPAD == COWOND2_PAD)
294 #define CALCULATOR_QUIT BUTTON_POWER
295 #define CALCULATOR_CLEAR BUTTON_MENU
297 #elif CONFIG_KEYPAD == IAUDIO67_PAD
299 #define CALCULATOR_LEFT BUTTON_LEFT
300 #define CALCULATOR_RIGHT BUTTON_RIGHT
301 #define CALCULATOR_UP BUTTON_VOLUP
302 #define CALCULATOR_DOWN BUTTON_VOLDOWN
303 #define CALCULATOR_QUIT BUTTON_POWER
304 #define CALCULATOR_INPUT BUTTON_PLAY
305 #define CALCULATOR_CALC BUTTON_MENU
306 #define CALCULATOR_CLEAR BUTTON_STOP
308 #define CALCULATOR_RC_QUIT (BUTTON_MENU|BUTTON_PLAY)
310 #elif (CONFIG_KEYPAD == CREATIVEZVM_PAD)
312 #define CALCULATOR_LEFT BUTTON_LEFT
313 #define CALCULATOR_RIGHT BUTTON_RIGHT
314 #define CALCULATOR_UP BUTTON_UP
315 #define CALCULATOR_DOWN BUTTON_DOWN
316 #define CALCULATOR_QUIT BUTTON_BACK
317 #define CALCULATOR_INPUT BUTTON_SELECT
318 #define CALCULATOR_CALC BUTTON_MENU
319 #define CALCULATOR_CLEAR BUTTON_PLAY
321 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
323 #define CALCULATOR_LEFT BUTTON_LEFT
324 #define CALCULATOR_RIGHT BUTTON_RIGHT
325 #define CALCULATOR_UP BUTTON_UP
326 #define CALCULATOR_DOWN BUTTON_DOWN
327 #define CALCULATOR_QUIT BUTTON_POWER
328 #define CALCULATOR_INPUT BUTTON_SELECT
329 #define CALCULATOR_CALC BUTTON_MENU
330 #define CALCULATOR_CLEAR BUTTON_VIEW
332 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
334 #define CALCULATOR_QUIT BUTTON_POWER
335 #define CALCULATOR_CLEAR BUTTON_MENU
337 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
338 #define CALCULATOR_QUIT BUTTON_POWER
340 #elif CONFIG_KEYPAD == MROBE500_PAD
341 #define CALCULATOR_QUIT BUTTON_POWER
343 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
345 #define CALCULATOR_LEFT BUTTON_LEFT
346 #define CALCULATOR_RIGHT BUTTON_RIGHT
347 #define CALCULATOR_UP BUTTON_UP
348 #define CALCULATOR_DOWN BUTTON_DOWN
349 #define CALCULATOR_QUIT BUTTON_REC
350 #define CALCULATOR_INPUT BUTTON_PLAY
351 #define CALCULATOR_CALC BUTTON_FFWD
352 #define CALCULATOR_CLEAR BUTTON_REW
354 #else
355 #error No keymap defined!
356 #endif
358 #ifdef HAVE_TOUCHSCREEN
359 #ifndef CALCULATOR_LEFT
360 #define CALCULATOR_LEFT BUTTON_MIDLEFT
361 #endif
362 #ifndef CALCULATOR_RIGHT
363 #define CALCULATOR_RIGHT BUTTON_MIDRIGHT
364 #endif
365 #ifndef CALCULATOR_UP
366 #define CALCULATOR_UP BUTTON_TOPMIDDLE
367 #endif
368 #ifndef CALCULATOR_DOWN
369 #define CALCULATOR_DOWN BUTTON_BOTTOMMIDDLE
370 #endif
371 #ifndef CALCULATOR_CALC
372 #define CALCULATOR_CALC BUTTON_BOTTOMRIGHT
373 #endif
374 #ifndef CALCULATOR_INPUT
375 #define CALCULATOR_INPUT BUTTON_CENTER
376 #endif
377 #ifndef CALCULATOR_CLEAR
378 #define CALCULATOR_CLEAR BUTTON_TOPRIGHT
379 #endif
381 #include "lib/pluginlib_touchscreen.h"
382 static struct ts_raster calc_raster = { X_0_POS, Y_1_POS,
383 BUTTON_COLS*REC_WIDTH, BUTTON_ROWS*REC_HEIGHT, REC_WIDTH, REC_HEIGHT };
384 #endif
386 enum {
387 basicButtons,
388 sciButtons
389 } buttonGroup;
391 unsigned char* buttonChar[2][5][5] = {
392 { { "MR" , "M+" , "2nd" , "CE" , "C" },
393 { "7" , "8" , "9" , "/" , "sqr" },
394 { "4" , "5" , "6" , "*" , "x^2" },
395 { "1" , "2" , "3" , "-" , "1/x" },
396 { "0" , "+/-", "." , "+" , "=" } },
398 { { "n!" , "PI" , "1st" , "sin" , "asi" },
399 { "7" , "8" , "9" , "cos" , "aco" },
400 { "4" , "5" , "6" , "tan" , "ata" },
401 { "1" , "2" , "3" , "ln" , "e^x" },
402 { "0" , "+/-", "." , "log" , "x^y" } }
405 enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C ,
406 btn_7 , btn_8 , btn_9 , btn_div , btn_sqr ,
407 btn_4 , btn_5 , btn_6 , btn_time , btn_square ,
408 btn_1 , btn_2 , btn_3 , btn_minus , btn_rec ,
409 btn_0 , btn_sign , btn_dot , btn_add , btn_equal
412 enum { sci_fac, sci_pi , sci_sci , sci_sin , sci_asin ,
413 sci_7 , sci_8 , sci_9 , sci_cos , sci_acos ,
414 sci_4 , sci_5 , sci_6 , sci_tan , sci_atan ,
415 sci_1 , sci_2 , sci_3 , sci_ln , sci_exp ,
416 sci_0 , sci_sign , sci_dot , sci_log , sci_xy
419 #define MINIMUM 0.000000000001 /* e-12 */
420 /* ^ ^ ^ ^ */
421 /* 123456789abcdef */
423 #define DIGITLEN 10 /* must <= 10 */
424 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
425 /* 0.000 00000 0001 */
426 /* ^ ^ ^ ^ ^ ^ */
427 /* DIGITLEN 12345 6789a bcdef */
428 /* power 12 34567 89abc def */
429 /* 10^- 123 45678 9abcd ef */
431 unsigned char buf[19];/* 18 bytes of output line,
432 buf[0] is operator
433 buf[1] = 'M' if memTemp is not 0
434 buf[2] = ' '
436 if SCIENTIFIC_FORMAT
437 buf[2]-buf[12] or buf[3]-buf[13] = result;
438 format X.XXXXXXXX
439 buf[13] or buf[14] -buf[17] = power;
440 format eXXX or e-XXX
441 else
442 buf[3]-buf[6] = ' ';
443 buf[7]-buf[17] = result;
445 buf[18] = '\0' */
447 unsigned char typingbuf[DIGITLEN+2];/* byte 0 is sign or ' ',
448 byte 1~DIGITLEN are num and '.'
449 byte (DIGITLEN+1) is '\0' */
450 unsigned char* typingbufPointer = typingbuf;
452 double result = 0; /* main operand, format 0.xxxxx */
453 int power = 0; /* 10^power */
454 double modifier = 0.1; /* position of next input */
455 double operand = 0; /* second operand, format 0.xxxxx */
456 int operandPower = 0; /* 10^power of second operand */
457 char oper = ' '; /* operators: + - * / */
458 bool operInputted = false; /* false: do calculation first and
459 replace current oper
460 true: just replace current oper */
462 double memTemp = 0; /* temp memory */
463 int memTempPower = 0; /* 10^^power of memTemp */
465 int btn_row, btn_col; /* current position index for button */
466 int prev_btn_row, prev_btn_col; /* previous cursor position */
467 #define CAL_BUTTON (btn_row*5+btn_col)
469 int btn = BUTTON_NONE;
470 int lastbtn = BUTTON_NONE;
472 /* Status of calculator */
473 enum {cal_normal, /* 0, normal status, display result */
474 cal_typing, /* 1, currently typing, dot hasn't been typed */
475 cal_dotted, /* 2, currently typing, dot already has been typed. */
476 cal_error,
477 cal_exit,
478 cal_toDo
479 } calStatus;
481 /* constant table for CORDIC algorithm */
482 double cordicTable[51][2]= {
483 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
484 {1e+00, 7.853981633974483e-01},
485 {5e-01, 4.636476090008061e-01},
486 {2.5e-01, 2.449786631268641e-01},
487 {1.25e-01, 1.243549945467614e-01},
488 {6.25e-02, 6.241880999595735e-02},
489 {3.125e-02, 3.123983343026828e-02},
490 {1.5625e-02, 1.562372862047683e-02},
491 {7.8125e-03, 7.812341060101111e-03},
492 {3.90625e-03, 3.906230131966972e-03},
493 {1.953125e-03, 1.953122516478819e-03},
494 {9.765625e-04, 9.765621895593195e-04},
495 {4.8828125e-04, 4.882812111948983e-04},
496 {2.44140625e-04, 2.441406201493618e-04},
497 {1.220703125e-04, 1.220703118936702e-04},
498 {6.103515625e-05, 6.103515617420877e-05},
499 {3.0517578125e-05, 3.051757811552610e-05},
500 {1.52587890625e-05, 1.525878906131576e-05},
501 {7.62939453125e-06, 7.629394531101970e-06},
502 {3.814697265625e-06, 3.814697265606496e-06},
503 {1.9073486328125e-06, 1.907348632810187e-06},
504 {9.5367431640625e-07, 9.536743164059608e-07},
505 {4.76837158203125e-07, 4.768371582030888e-07},
506 {2.384185791015625e-07, 2.384185791015580e-07},
507 {1.1920928955078125e-07, 1.192092895507807e-07},
508 {5.9604644775390625e-08, 5.960464477539055e-08},
509 {2.98023223876953125e-08, 2.980232238769530e-08},
510 {1.490116119384765625e-08, 1.490116119384765e-08},
511 {7.450580596923828125e-09, 7.450580596923828e-09},
512 {3.7252902984619140625e-09, 3.725290298461914e-09},
513 {1.86264514923095703125e-09, 1.862645149230957e-09},
514 {9.31322574615478515625e-10, 9.313225746154785e-10},
515 {4.656612873077392578125e-10, 4.656612873077393e-10},
516 {2.3283064365386962890625e-10, 2.328306436538696e-10},
517 {1.16415321826934814453125e-10, 1.164153218269348e-10},
518 {5.82076609134674072265625e-11, 5.820766091346741e-11},
519 {2.910383045673370361328125e-11, 2.910383045673370e-11},
520 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
521 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
522 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
523 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
524 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
525 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
526 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
527 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
528 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
529 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
530 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
531 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
532 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
533 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
534 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
537 void doMultiple(double* operandOne, int* powerOne,
538 double operandTwo, int powerTwo);
539 void doAdd (double* operandOne, int* powerOne,
540 double operandTwo, int powerTwo);
541 void printResult(void);
542 void formatResult(void);
543 void oneOperand(void);
545 void drawLines(void);
546 void drawButtons(int group);
548 /* -----------------------------------------------------------------------
549 Handy funtions
550 ----------------------------------------------------------------------- */
551 void cleartypingbuf(void)
553 int k;
554 for( k=1; k<=(DIGITLEN+1); k++)
555 typingbuf[k] = 0;
556 typingbuf[0] = ' ';
557 typingbufPointer = typingbuf+1;
559 void clearbuf(void)
561 int k;
562 for(k=0;k<18;k++)
563 buf[k]=' ';
564 buf[18] = 0;
566 void clearResult(void)
568 result = 0;
569 power = 0;
570 modifier = 0.1;
573 void clearInput(void)
575 calStatus = cal_normal;
576 clearResult();
577 cleartypingbuf();
578 rb->lcd_clear_display();
579 drawButtons(buttonGroup);
580 drawLines();
583 void clearOperand(void)
585 operand = 0;
586 operandPower = 0;
589 void clearMemTemp(void)
591 memTemp = 0;
592 memTempPower = 0;
595 void clearOper(void)
597 oper = ' ';
598 operInputted = false;
601 void clearMem(void)
603 clearInput();
604 clearMemTemp();
605 clearOperand();
606 clearOper();
607 btn = BUTTON_NONE;
610 void switchOperands(void)
612 double tempr = operand;
613 int tempp = operandPower;
614 operand = result;
615 operandPower = power;
616 result = tempr;
617 power = tempp;
620 void drawLines(void)
622 int i;
623 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS-1);
624 for (i = 0; i < 5 ; i++)
625 rb->lcd_hline(0, LCD_WIDTH, Y_1_POS+i*REC_HEIGHT);
626 for (i = 0; i < 4 ; i++)
627 rb->lcd_vline(X_1_POS+i*REC_WIDTH, Y_1_POS, LCD_HEIGHT);
630 void drawButtons(int group)
632 int i, j, w, h;
633 for (i = 0; i <= 4; i++){
634 for (j = 0; j <= 4; j++){
635 rb->lcd_getstringsize( buttonChar[group][i][j],&w,&h);
636 if (i == btn_row && j == btn_col) /* selected item */
637 rb->lcd_set_drawmode(DRMODE_SOLID);
638 else
639 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
640 rb->lcd_fillrect( X_0_POS + j*REC_WIDTH,
641 Y_1_POS + i*REC_HEIGHT,
642 REC_WIDTH, REC_HEIGHT+1);
643 if (i == btn_row && j == btn_col) /* selected item */
644 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
645 else
646 rb->lcd_set_drawmode(DRMODE_SOLID);
647 rb->lcd_putsxy( X_0_POS + j*REC_WIDTH + (REC_WIDTH - w)/2,
648 Y_1_POS + i*REC_HEIGHT + (REC_HEIGHT - h)/2 + 1,
649 buttonChar[group][i][j] );
652 rb->lcd_set_drawmode(DRMODE_SOLID);
655 /* -----------------------------------------------------------------------
656 Initiate calculator
657 ----------------------------------------------------------------------- */
658 void cal_initial (void)
660 int w,h;
662 rb->lcd_getstringsize("2nd",&w,&h);
663 if (w > REC_WIDTH || h > REC_HEIGHT)
664 rb->lcd_setfont(FONT_SYSFIXED);
666 rb->lcd_clear_display();
668 #ifdef CALCULATOR_OPERATORS
669 /* basic operators are available through separate button */
670 buttonGroup = sciButtons;
671 #else
672 buttonGroup = basicButtons;
673 #endif
675 /* initially, invert button "5" */
676 btn_row = 2;
677 btn_col = 1;
678 prev_btn_row = btn_row;
679 prev_btn_col = btn_col;
680 drawButtons(buttonGroup);
681 drawLines();
682 rb->lcd_update();
684 /* initial mem and output display*/
685 clearMem();
686 printResult();
688 /* clear button queue */
689 rb->button_clear_queue();
692 /* -----------------------------------------------------------------------
693 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
694 in it's private case for sqrt.
695 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
696 ----------------------------------------------------------------------- */
697 double mySqrt(double square)
699 int k = 0;
700 double temp = 0;
701 double root= ABS(square+1)/2;
703 while( ABS(root - temp) > MINIMUM ){
704 temp = root;
705 root = (square/temp + temp)/2;
706 k++;
707 if (k>10000) return 0;
710 return root;
712 /* -----------------------------------------------------------------------
713 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
714 transcendFunc can do sin,cos,log,exp
715 input parameter is angle
716 ----------------------------------------------------------------------- */
717 void transcendFunc(char* func, double* tt, int* ttPower)
719 double t = (*tt)*M_PI/180; int tPower = *ttPower;
720 int sign = 1;
721 int n = 50; /* n <=50, tables are all <= 50 */
722 int j;
723 double x,y,z,xt,yt,zt;
725 if (tPower < -998) {
726 calStatus = cal_normal;
727 return;
729 if (tPower > 8) {
730 calStatus = cal_error;
731 return;
733 *ttPower = 0;
734 calStatus = cal_normal;
736 if( func[0] =='s' || func[0] =='S'|| func[0] =='t' || func[0] =='T')
737 sign = SIGN(t);
738 else {
739 /* if( func[0] =='c' || func[0] =='C') */
740 sign = 1;
742 t = ABS(t);
744 while (tPower > 0){
745 t *= 10;
746 tPower--;
748 while (tPower < 0) {
749 t /= 10;
750 tPower++;
752 j = 0;
753 while (t > j*M_TWOPI) {j++;}
754 t -= (j-1)*M_TWOPI;
755 if (M_PI_2 < t && t < 3*M_PI_2){
756 t = M_PI - t;
757 if (func[0] =='c' || func[0] =='C')
758 sign = -1;
759 else if (func[0] =='t' || func[0] =='T')
760 t*=-1;
762 else if ( 3*M_PI_2 <= t && t <= M_TWOPI)
763 t -= M_TWOPI;
765 x = 0.60725293500888; y = 0; z = t;
766 for (j=1;j<n+2;j++){
767 xt = x - SIGN(z) * y*cordicTable[j-1][0];
768 yt = y + SIGN(z) * x*cordicTable[j-1][0];
769 zt = z - SIGN(z) * cordicTable[j-1][1];
770 x = xt;
771 y=yt;
772 z=zt;
774 if( func[0] =='s' || func[0] =='S') {
775 *tt = sign*y;
776 return;
778 else if( func[0] =='c' || func[0] =='C') {
779 *tt = sign*x;
780 return;
782 else /*if( func[0] =='t' || func[0] =='T')*/ {
783 if(t==M_PI_2||t==-M_PI_2){
784 calStatus = cal_error;
785 return;
787 else{
788 *tt = sign*(y/x);
789 return;
794 /* -----------------------------------------------------------------------
795 add in scientific number format
796 ----------------------------------------------------------------------- */
797 void doAdd (double* operandOne, int* powerOne,
798 double operandTwo, int powerTwo)
800 if ( *powerOne >= powerTwo ){
801 if (*powerOne - powerTwo <= DIGITLEN+1){
802 while (powerTwo < *powerOne){
803 operandTwo /=10;
804 powerTwo++;
806 *operandOne += operandTwo;
808 /*do nothing if operandTwo is too small*/
810 else{
811 if (powerTwo - *powerOne <= DIGITLEN+1){
812 while(powerTwo > *powerOne){
813 *operandOne /=10;
814 (*powerOne)++;
816 (*operandOne) += operandTwo;
818 else{/* simply copy operandTwo if operandOne is too small */
819 *operandOne = operandTwo;
820 *powerOne = powerTwo;
824 /* -----------------------------------------------------------------------
825 multiple in scientific number format
826 ----------------------------------------------------------------------- */
827 void doMultiple(double* operandOne, int* powerOne,
828 double operandTwo, int powerTwo)
830 (*operandOne) *= operandTwo;
831 (*powerOne) += powerTwo;
834 /* -----------------------------------------------------------------------
835 Handles all one operand calculations
836 ----------------------------------------------------------------------- */
837 void oneOperand(void)
839 int k = 0;
840 if (buttonGroup == basicButtons){
841 switch(CAL_BUTTON){
842 case btn_sqr:
843 if (result<0)
844 calStatus = cal_error;
845 else{
846 if (power%2 == 1){
847 result = (mySqrt(result*10))/10;
848 power = (power+1) / 2;
850 else{
851 result = mySqrt(result);
852 power = power / 2;
854 calStatus = cal_normal;
856 break;
857 case btn_square:
858 power *= 2;
859 result *= result;
860 calStatus = cal_normal;
861 break;
863 case btn_rec:
864 if (result==0)
865 calStatus = cal_error;
866 else{
867 power = -power;
868 result = 1/result;
869 calStatus = cal_normal;
871 break;
872 default:
873 calStatus = cal_toDo;
874 break; /* just for the safety */
877 else{ /* sciButtons */
878 switch(CAL_BUTTON){
879 case sci_sin:
880 transcendFunc("sin", &result, &power);
881 break;
882 case sci_cos:
883 transcendFunc("cos", &result, &power);
884 break;
885 case sci_tan:
886 transcendFunc("tan", &result, &power);
887 break;
888 case sci_fac:
889 if (power<0 || power>8 || result<0 )
890 calStatus = cal_error;
891 else if(result == 0) {
892 result = 1;
893 power = 0;
895 else{
896 while(power > 0) {
897 result *= 10;
898 power--;
900 if ( ( result - (int)result) > MINIMUM )
901 calStatus = cal_error;
902 else {
903 k = result; result = 1;
904 while (k > 1){
905 doMultiple(&result, &power, k, 0);
906 formatResult();
907 k--;
909 calStatus = cal_normal;
912 break;
913 default:
914 calStatus = cal_toDo;
915 break; /* just for the safety */
921 /* -----------------------------------------------------------------------
922 Handles all two operands calculations
923 ----------------------------------------------------------------------- */
924 void twoOperands(void)
926 switch(oper){
927 case '-':
928 doAdd(&operand, &operandPower, -result, power);
929 break;
930 case '+':
931 doAdd(&operand, &operandPower, result, power);
932 break;
933 case '*':
934 doMultiple(&operand, &operandPower, result, power);
935 break;
936 case '/':
937 if ( ABS(result) > MINIMUM ){
938 doMultiple(&operand, &operandPower, 1/result, -power);
940 else
941 calStatus = cal_error;
942 break;
943 default: /* ' ' */
944 switchOperands(); /* counter switchOperands() below */
945 break;
946 } /* switch(oper) */
947 switchOperands();
948 clearOper();
951 /* First, increases *dimen1 by dimen1_delta modulo dimen1_modulo.
952 If dimen1 wraps, increases *dimen2 by dimen2_delta modulo dimen2_modulo.
954 static void move_with_wrap_and_shift(
955 int *dimen1, int dimen1_delta, int dimen1_modulo,
956 int *dimen2, int dimen2_delta, int dimen2_modulo)
958 bool wrapped = false;
960 *dimen1 += dimen1_delta;
961 if (*dimen1 < 0)
963 *dimen1 = dimen1_modulo - 1;
964 wrapped = true;
966 else if (*dimen1 >= dimen1_modulo)
968 *dimen1 = 0;
969 wrapped = true;
972 if (wrapped)
974 /* Make the dividend always positive to be sure about the result.
975 Adding dimen2_modulo does not change it since we do it modulo. */
976 *dimen2 = (*dimen2 + dimen2_modulo + dimen2_delta) % dimen2_modulo;
980 /* -----------------------------------------------------------------------
981 Print buttons when switching 1st and 2nd
982 int group = {basicButtons, sciButtons}
983 ----------------------------------------------------------------------- */
984 void printButtonGroups(int group)
986 drawButtons(group);
987 drawLines();
988 rb->lcd_update();
990 /* -----------------------------------------------------------------------
991 flash the currently marked button
992 ----------------------------------------------------------------------- */
993 void flashButton(void)
995 int k, w, h;
996 for (k=2;k>0;k--)
998 rb->lcd_getstringsize( buttonChar[buttonGroup][btn_row][btn_col],&w,&h);
999 rb->lcd_set_drawmode(DRMODE_SOLID|(k==1) ? 0 : DRMODE_INVERSEVID);
1000 rb->lcd_fillrect( X_0_POS + btn_col*REC_WIDTH + 1,
1001 Y_1_POS + btn_row*REC_HEIGHT + 1,
1002 REC_WIDTH - 1, REC_HEIGHT - 1);
1003 rb->lcd_putsxy( X_0_POS + btn_col*REC_WIDTH + (REC_WIDTH - w)/2,
1004 Y_1_POS + btn_row*REC_HEIGHT + (REC_HEIGHT - h)/2 +1,
1005 buttonChar[buttonGroup][btn_row][btn_col] );
1006 rb->lcd_update_rect( X_0_POS + btn_col*REC_WIDTH + 1,
1007 Y_1_POS + btn_row*REC_HEIGHT + 1,
1008 REC_WIDTH - 1, REC_HEIGHT - 1);
1010 if (k!= 1)
1011 rb->sleep(HZ/22);
1016 /* -----------------------------------------------------------------------
1017 pos is the position that needs animation. pos = [1~18]
1018 ----------------------------------------------------------------------- */
1019 void deleteAnimation(int pos)
1021 int k, w, h, x;
1022 if (pos<1 || pos >18)
1023 return;
1025 rb->lcd_getstringsize("0", &w, &h);
1026 x = (pos==1? 4: LCD_WIDTH - 4 - w);
1028 for (k=0;k<4;k++){
1029 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1030 rb->lcd_fillrect(x, Y_1_POS - h -1, w, h);
1031 rb->lcd_set_drawmode(DRMODE_SOLID);
1032 rb->lcd_fillrect(x + (w*k)/8, Y_1_POS - h -1 + (h*k)/8,
1033 (w*(4-k))/4, (h*(4-k))/4);
1034 rb->lcd_update_rect(x, Y_1_POS - h -1, w, h);
1035 rb->sleep(HZ/32);
1039 /* -----------------------------------------------------------------------
1040 result may be one of these formats:
1042 xxxx.xxxx
1043 0.xxxx
1044 0.0000xxxx
1046 formatResult() change result to standard format: 0.xxxx
1047 if result is close to 0, let it be 0;
1048 if result is close to 1, let it be 0.1 and power++;
1049 ----------------------------------------------------------------------- */
1050 void formatResult(void)
1052 int resultsign = SIGN(result);
1053 result = ABS(result);
1054 if(result > MINIMUM ){ /* doesn't check power, might have problem
1055 input wouldn't,
1056 + - * / of two formatted number wouldn't.
1057 only a calculation that makes a formatted
1058 number (0.xxxx) less than MINIMUM in only
1059 one operation */
1061 if (result<1){
1062 while( (int)(result*10) == 0 ){
1063 result *= 10;
1064 power--;
1065 modifier *= 10;
1068 else{ /* result >= 1 */
1069 while( (int)result != 0 ){
1070 result /= 10;
1071 power++;
1072 modifier /= 10;
1074 } /* if result<1 */
1076 if (result > (1-MINIMUM)){
1077 result = 0.1;
1078 power++;
1079 modifier /= 10;
1081 result *= resultsign;
1083 else {
1084 result = 0;
1085 power = 0;
1086 modifier = 0.1;
1090 /* -----------------------------------------------------------------------
1091 result2typingbuf() outputs standard format result to typingbuf.
1092 case SCIENTIFIC_FORMAT, let temppower = 1;
1093 case temppower > 0: print '.' in the middle
1094 case temppower <= 0: print '.' in the begining
1095 ----------------------------------------------------------------------- */
1096 void result2typingbuf(void)
1098 bool haveDot = false;
1099 char tempchar = 0;
1100 int k;
1101 double tempresult = ABS(result); /* positive num makes things simple */
1103 int temppower;
1104 double tempmodifier = 1;
1105 int count;
1107 if(SCIENTIFIC_FORMAT)
1108 temppower = 1; /* output x.xxxx format */
1109 else
1110 temppower = power;
1112 cleartypingbuf();
1114 if(tempresult < MINIMUM){ /* if 0,faster display and avoid complication*/
1115 typingbuf[0] = ' ';
1116 typingbuf[1] = '0';
1118 else{ /* tempresult > 0 */
1119 typingbuf[0] = (SIGN(result)<0)?'-':' ';
1121 typingbufPointer = typingbuf;
1122 if(temppower > 0){
1123 for (k = 0; k<DIGITLEN+1 ; k++){
1124 typingbufPointer++;
1125 if(temppower || *(typingbufPointer-1) == '.'){
1126 count = 0;
1127 tempmodifier = tempmodifier/10;
1128 while( (tempresult-tempmodifier*count) >
1129 (tempmodifier-MINIMUM)){
1130 count++;
1132 tempresult -= tempmodifier*count;
1133 tempresult = ABS(tempresult);
1134 temppower-- ;
1135 *typingbufPointer = count + '0';
1137 else{ /* temppower == 0 */
1138 *typingbufPointer = '.';
1139 haveDot = true;
1141 } /* for */
1143 else{
1144 haveDot = true;
1145 typingbufPointer++; *typingbufPointer = '0';
1146 typingbufPointer++; *typingbufPointer = '.';
1147 for (k = 2; k<DIGITLEN+1 ; k++){
1148 typingbufPointer++;
1149 count = 0;
1150 if ( (-temppower) < (k-1)){
1151 tempmodifier = tempmodifier/10;
1152 while((tempresult-tempmodifier*count)>(tempmodifier-MINIMUM)){
1153 count++;
1156 tempresult -= tempmodifier*count;
1157 tempresult = ABS(tempresult);
1158 temppower-- ;
1160 *typingbufPointer = count + '0';
1163 /* now, typingbufPointer = typingbuf + 16 */
1164 /* backward strip off 0 and '.' */
1165 if (haveDot){
1166 while( (*typingbufPointer == '0') || (*typingbufPointer == '.')){
1167 tempchar = *typingbufPointer;
1168 *typingbufPointer = 0;
1169 typingbufPointer--;
1170 if (tempchar == '.') break;
1173 typingbuf[DIGITLEN+1] = 0;
1174 } /* else tempresult > 0 */
1177 /* -----------------------------------------------------------------------
1178 printResult() generates LCD display.
1179 ----------------------------------------------------------------------- */
1180 void printResult(void)
1182 int k, w, h;
1184 char operbuf[3] = {0, 0, 0};
1186 switch_Status:
1187 switch(calStatus){
1188 case cal_exit:
1189 rb->lcd_clear_display();
1190 rb->splash(HZ/3, "Bye now!");
1191 break;
1192 case cal_error:
1193 clearbuf();
1194 rb->snprintf(buf, 19, "%18s","Error");
1195 break;
1196 case cal_toDo:
1197 clearbuf();
1198 rb->snprintf(buf, 19, "%18s","Coming soon ^_* ");
1199 break;
1201 case cal_normal:
1202 formatResult();
1204 if( power > 1000 ){ /* power -1 > 999 */
1205 calStatus = cal_error;
1206 goto switch_Status;
1208 if (power < -998 ) /* power -1 < -999 */
1209 clearResult(); /* too small, let it be 0 */
1211 result2typingbuf();
1212 clearbuf();
1214 operbuf[0] = oper;
1215 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1216 operbuf[2] = '\0';
1218 if(SCIENTIFIC_FORMAT){
1219 /* output format: X.XXXX eXXX */
1220 if(power > -98){ /* power-1 >= -99, eXXX or e-XX */
1221 rb->snprintf(buf, 12, "%11s",typingbuf);
1222 for(k=11;k<=14;k++) buf[k] = ' ';
1223 cleartypingbuf();
1224 rb->snprintf(typingbuf, 5, "e%d",power-1);
1225 rb->snprintf(buf+11, 5, "%4s",typingbuf);
1227 else{ /* power-1 <= -100, e-XXX */
1228 rb->snprintf(buf, 12, "%11s",typingbuf);
1229 rb->snprintf(buf+11, 6, "e%d",power-1);
1232 else{
1233 rb->snprintf(buf, 12, "%11s",typingbuf);
1234 } /* if SCIENTIFIC_FORMAT */
1235 break;
1236 case cal_typing:
1237 case cal_dotted:
1238 clearbuf();
1239 operbuf[0] = oper;
1240 operbuf[1] = ( ABS(memTemp) > MINIMUM )?'M':' ';
1241 rb->snprintf(buf, 12, "%11s",typingbuf);
1242 break;
1246 rb->lcd_getstringsize(buf, &w, &h);
1247 rb->screen_clear_area(rb->screens[0], 0, 0, LCD_WIDTH, Y_1_POS - 1);
1248 rb->lcd_putsxy(4, Y_1_POS - h -1, operbuf);
1249 rb->lcd_putsxy(LCD_WIDTH - w - 4, Y_1_POS - h -1, buf);
1250 rb->lcd_update_rect(0, 1, LCD_WIDTH, Y_1_POS);
1253 /* -----------------------------------------------------------------------
1254 Process typing buttons: 1-9, '.', sign
1255 main operand "result" and typingbuf are processed seperately here.
1256 ----------------------------------------------------------------------- */
1257 void typingProcess(void){
1258 switch( CAL_BUTTON ){
1259 case btn_sign:
1260 if (calStatus == cal_typing ||
1261 calStatus == cal_dotted)
1262 typingbuf[0] = (typingbuf[0]=='-')?' ':'-';
1263 result = -result;
1264 break;
1265 case btn_dot:
1266 operInputted = false;
1267 switch(calStatus){
1268 case cal_normal:
1269 clearInput();
1270 *typingbufPointer = '0';
1271 typingbufPointer++;
1272 case cal_typing:
1273 calStatus = cal_dotted;
1274 *typingbufPointer = '.';
1275 if (typingbufPointer != typingbuf+DIGITLEN+1)
1276 typingbufPointer++;
1277 break;
1278 default: /* cal_dotted */
1279 break;
1281 break;
1282 default: /* 0-9 */
1283 operInputted = false;
1284 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1285 switch(calStatus){
1286 case cal_normal:
1287 if(CAL_BUTTON == btn_0 )
1288 break; /* first input is 0, ignore */
1289 clearInput();
1290 /*no operator means start a new calculation*/
1291 if (oper ==' ')
1292 clearOperand();
1293 calStatus = cal_typing;
1294 /* go on typing, no break */
1295 case cal_typing:
1296 case cal_dotted:
1297 switch(CAL_BUTTON){
1298 case btn_0:
1299 *typingbufPointer = '0';
1300 break;
1301 default:
1302 *typingbufPointer=(7+btn_col-3*(btn_row-1))+ '0';
1303 break;
1305 if (typingbufPointer!=typingbuf+DIGITLEN+1){
1306 typingbufPointer++;
1308 {/* result processing */
1309 if (calStatus == cal_typing) power++;
1310 if (CAL_BUTTON != btn_0)
1311 result= result +
1312 SIGN(result)*
1313 (7+btn_col-3*(btn_row-1))*modifier;
1314 modifier /= 10;
1317 else /* last byte always '\0' */
1318 *typingbufPointer = 0;
1319 break;
1320 default: /* cal_error, cal_exit */
1321 break;
1323 break; /* default, 0-9 */
1324 } /* switch( CAL_BUTTON ) */
1327 /* -----------------------------------------------------------------------
1328 Handle delete operation
1329 main operand "result" and typingbuf are processed seperately here.
1330 ----------------------------------------------------------------------- */
1331 void doDelete(void){
1332 deleteAnimation(18);
1333 switch(calStatus){
1334 case cal_dotted:
1335 if (*(typingbufPointer-1) == '.'){
1336 /* if dotted and deleting '.',
1337 change status and delete '.' below */
1338 calStatus = cal_typing;
1340 else{ /* if dotted and not deleting '.',
1341 power stays */
1342 power++; /* counter "power--;" below */
1344 case cal_typing:
1345 typingbufPointer--;
1347 {/* result processing */ /* 0-9, '.' */
1348 /* if deleting '.', do nothing */
1349 if ( *typingbufPointer != '.'){
1350 power--;
1351 modifier *= 10;
1352 result = result - SIGN(result)*
1353 ((*typingbufPointer)- '0')*modifier;
1357 *typingbufPointer = 0;
1359 /* if (only one digit left and it's 0)
1360 or no digit left, change status*/
1361 if ( typingbufPointer == typingbuf+1 ||
1362 ( typingbufPointer == typingbuf+2 &&
1363 *(typingbufPointer-1) == '0' ))
1364 calStatus = cal_normal;
1365 break;
1366 default: /* normal, error, exit */
1367 break;
1370 /* -----------------------------------------------------------------------
1371 Handle buttons on basic screen
1372 ----------------------------------------------------------------------- */
1373 void basicButtonsProcess(void){
1374 switch (btn) {
1375 case CALCULATOR_INPUT:
1376 if (calStatus == cal_error && (CAL_BUTTON != btn_C) ) break;
1377 flashButton();
1378 switch( CAL_BUTTON ){
1379 case btn_MR:
1380 operInputted = false;
1381 result = memTemp; power = memTempPower;
1382 calStatus = cal_normal;
1383 break;
1384 case btn_M:
1385 formatResult();
1386 if (memTemp > MINIMUM)
1387 doAdd(&memTemp, &memTempPower, result, power);
1388 else {
1389 /* if result is too small and memTemp = 0,
1390 doAdd will not add */
1391 memTemp = result;
1392 memTempPower = power;
1394 calStatus = cal_normal;
1395 break;
1397 case btn_C: clearMem(); break;
1398 case btn_CE: clearInput(); break;
1400 case btn_bas:
1401 buttonGroup = sciButtons;
1402 printButtonGroups(buttonGroup);
1403 break;
1405 /* one operand calculation, may be changed to
1406 like sin, cos, log, etc */
1407 case btn_sqr:
1408 case btn_square:
1409 case btn_rec:
1410 formatResult(); /* not necessary, just for safty */
1411 oneOperand();
1412 break;
1414 case_btn_equal: /* F3 shortkey entrance */
1415 case btn_equal:
1416 formatResult();
1417 calStatus = cal_normal;
1418 operInputted = false;
1419 if (oper != ' ') twoOperands();
1420 break;
1422 case btn_div:
1423 case btn_time:
1424 case btn_minus:
1425 case btn_add:
1426 if(!operInputted) {twoOperands(); operInputted = true;}
1427 oper = buttonChar[basicButtons][btn_row][btn_col][0];
1428 #ifdef CALCULATOR_OPERATORS
1429 case_cycle_operators: /* F2 shortkey entrance */
1430 #endif
1431 calStatus = cal_normal;
1432 formatResult();
1433 operand = result;
1434 operandPower = power;
1436 break;
1438 case btn_sign:
1439 case btn_dot:
1440 default: /* 0-9 */
1441 typingProcess();
1442 break;
1443 } /* switch (CAL_BUTTON) */
1444 break;
1446 #ifdef CALCULATOR_OPERATORS
1447 case CALCULATOR_OPERATORS:
1448 if (calStatus == cal_error) break;
1449 if (!operInputted) {twoOperands(); operInputted = true;}
1450 switch (oper){
1451 case ' ':
1452 case '/': oper = '+'; flashButton(); break;
1453 case '+': oper = '-'; flashButton(); break;
1454 case '-': oper = '*'; flashButton(); break;
1455 case '*': oper = '/'; flashButton(); break;
1457 goto case_cycle_operators;
1458 break;
1459 #endif
1461 case CALCULATOR_CALC:
1462 if (calStatus == cal_error) break;
1463 flashButton();
1464 goto case_btn_equal;
1465 break;
1466 default: break;
1468 printResult();
1471 /* -----------------------------------------------------------------------
1472 Handle buttons on scientific screen
1473 ----------------------------------------------------------------------- */
1474 void sciButtonsProcess(void){
1475 switch (btn) {
1476 case CALCULATOR_INPUT:
1477 if (calStatus == cal_error && (CAL_BUTTON != sci_sci) ) break;
1478 flashButton();
1479 switch( CAL_BUTTON ){
1481 case sci_pi:
1482 result = M_PI; power = 0;
1483 calStatus = cal_normal;
1484 break;
1486 case sci_xy: break;
1488 case sci_sci:
1489 buttonGroup = basicButtons;
1490 printButtonGroups(basicButtons);
1491 break;
1493 case sci_fac:
1494 case sci_sin:
1495 case sci_asin:
1496 case sci_cos:
1497 case sci_acos:
1498 case sci_tan:
1499 case sci_atan:
1500 case sci_ln:
1501 case sci_exp:
1502 case sci_log:
1503 formatResult(); /* not necessary, just for safty */
1504 oneOperand();
1505 break;
1507 case btn_sign:
1508 case btn_dot:
1509 default: /* 0-9 */
1510 typingProcess();
1511 break;
1512 } /* switch (CAL_BUTTON) */
1513 break;
1515 #ifdef CALCULATOR_OPERATORS
1516 case CALCULATOR_OPERATORS:
1517 if (calStatus == cal_error) break;
1518 if (!operInputted) {twoOperands(); operInputted = true;}
1519 switch (oper){
1520 case ' ': oper = '+'; break;
1521 case '/': oper = '+'; deleteAnimation(1); break;
1522 case '+': oper = '-'; deleteAnimation(1); break;
1523 case '-': oper = '*'; deleteAnimation(1); break;
1524 case '*': oper = '/'; deleteAnimation(1); break;
1526 calStatus = cal_normal;
1527 formatResult();
1528 operand = result;
1529 operandPower = power;
1530 break;
1531 #endif
1533 case CALCULATOR_CALC:
1534 if (calStatus == cal_error) break;
1535 formatResult();
1536 calStatus = cal_normal;
1537 operInputted = false;
1538 if (oper != ' ') twoOperands();
1539 break;
1540 default: break;
1542 printResult();
1545 /* -----------------------------------------------------------------------
1546 move button index
1547 Invert display new button, invert back previous button
1548 ----------------------------------------------------------------------- */
1549 int handleButton(int button){
1550 switch(button)
1552 case CALCULATOR_INPUT:
1553 case CALCULATOR_CALC:
1554 #ifdef CALCULATOR_INPUT_CALC_PRE
1555 if (lastbtn != CALCULATOR_INPUT_CALC_PRE)
1556 break;
1557 /* no unconditional break; here! */
1558 #endif
1559 #ifdef CALCULATOR_OPERATORS
1560 case CALCULATOR_OPERATORS:
1561 #endif
1562 switch(buttonGroup){
1563 case basicButtons:
1564 basicButtonsProcess();
1565 break;
1566 case sciButtons:
1567 sciButtonsProcess();
1568 break;
1570 break;
1572 #ifdef CALCULATOR_CLEAR
1573 case CALCULATOR_CLEAR:
1574 switch(calStatus){
1575 case cal_typing:
1576 case cal_dotted:
1577 doDelete();
1578 break;
1579 default: /* cal_normal, cal_error, cal_exit */
1580 clearMem();
1581 break;
1583 printResult();
1584 break;
1585 #endif
1586 case CALCULATOR_LEFT:
1587 case CALCULATOR_LEFT | BUTTON_REPEAT:
1588 move_with_wrap_and_shift(
1589 &btn_col, -1, BUTTON_COLS,
1590 &btn_row, 0, BUTTON_ROWS);
1591 break;
1593 case CALCULATOR_RIGHT:
1594 case CALCULATOR_RIGHT | BUTTON_REPEAT:
1595 move_with_wrap_and_shift(
1596 &btn_col, 1, BUTTON_COLS,
1597 &btn_row, 0, BUTTON_ROWS);
1598 break;
1600 #ifdef CALCULATOR_UP
1601 case CALCULATOR_UP:
1602 case CALCULATOR_UP | BUTTON_REPEAT:
1603 move_with_wrap_and_shift(
1604 &btn_row, -1, BUTTON_ROWS,
1605 &btn_col, 0, BUTTON_COLS);
1606 break;
1607 #endif
1608 #ifdef CALCULATOR_DOWN
1609 case CALCULATOR_DOWN:
1610 case CALCULATOR_DOWN | BUTTON_REPEAT:
1611 move_with_wrap_and_shift(
1612 &btn_row, 1, BUTTON_ROWS,
1613 &btn_col, 0, BUTTON_COLS);
1614 break;
1615 #endif
1617 #ifdef CALCULATOR_UP_W_SHIFT
1618 case CALCULATOR_UP_W_SHIFT:
1619 case CALCULATOR_UP_W_SHIFT | BUTTON_REPEAT:
1620 move_with_wrap_and_shift(
1621 &btn_row, -1, BUTTON_ROWS,
1622 &btn_col, -1, BUTTON_COLS);
1623 break;
1624 #endif
1625 #ifdef CALCULATOR_DOWN_W_SHIFT
1626 case CALCULATOR_DOWN_W_SHIFT:
1627 case CALCULATOR_DOWN_W_SHIFT | BUTTON_REPEAT:
1628 move_with_wrap_and_shift(
1629 &btn_row, 1, BUTTON_ROWS,
1630 &btn_col, 1, BUTTON_COLS);
1631 break;
1632 #endif
1633 #ifdef CALCULATOR_LEFT_W_SHIFT
1634 case CALCULATOR_LEFT_W_SHIFT:
1635 case CALCULATOR_LEFT_W_SHIFT | BUTTON_REPEAT:
1636 move_with_wrap_and_shift(
1637 &btn_col, -1, BUTTON_COLS,
1638 &btn_row, -1, BUTTON_ROWS);
1639 break;
1640 #endif
1641 #ifdef CALCULATOR_RIGHT_W_SHIFT
1642 case CALCULATOR_RIGHT_W_SHIFT:
1643 case CALCULATOR_RIGHT_W_SHIFT | BUTTON_REPEAT:
1644 move_with_wrap_and_shift(
1645 &btn_col, 1, BUTTON_COLS,
1646 &btn_row, 1, BUTTON_ROWS);
1647 break;
1648 #endif
1649 #ifdef CALCULATOR_RC_QUIT
1650 case CALCULATOR_RC_QUIT:
1651 #endif
1652 case CALCULATOR_QUIT:
1653 return -1;
1656 return 0;
1658 prev_btn_row = btn_row;
1659 prev_btn_col = btn_col;
1662 /* -----------------------------------------------------------------------
1663 Main();
1664 ----------------------------------------------------------------------- */
1665 enum plugin_status plugin_start(const void* parameter)
1667 (void)parameter;
1669 /* now go ahead and have fun! */
1671 #ifdef HAVE_TOUCHSCREEN
1672 rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
1673 #endif
1675 cal_initial();
1677 while (calStatus != cal_exit ) {
1678 btn = rb->button_get_w_tmo(HZ/2);
1679 #ifdef HAVE_TOUCHSCREEN
1680 if(btn & BUTTON_TOUCHSCREEN)
1682 struct ts_raster_result res;
1683 if(touchscreen_map_raster(&calc_raster, rb->button_get_data() >> 16,
1684 rb->button_get_data() & 0xffff, &res) == 1)
1686 btn_row = res.y;
1687 btn_col = res.x;
1688 drawButtons(buttonGroup);
1689 drawLines();
1691 rb->lcd_update();
1693 prev_btn_row = btn_row;
1694 prev_btn_col = btn_col;
1695 if(btn & BUTTON_REL)
1697 btn = CALCULATOR_INPUT;
1698 switch(buttonGroup){
1699 case basicButtons:
1700 basicButtonsProcess();
1701 break;
1702 case sciButtons:
1703 sciButtonsProcess();
1704 break;
1706 btn = BUTTON_TOUCHSCREEN;
1710 #endif
1711 if (handleButton(btn) == -1)
1713 calStatus = cal_exit;
1714 printResult();
1716 else
1718 drawButtons(buttonGroup);
1719 drawLines();
1722 rb->lcd_update();
1724 if(rb->default_event_handler(btn) == SYS_USB_CONNECTED)
1725 return PLUGIN_USB_CONNECTED;
1727 if (btn != BUTTON_NONE)
1728 lastbtn = btn;
1729 } /* while (calStatus != cal_exit ) */
1731 rb->button_clear_queue();
1732 return PLUGIN_OK;
1735 #endif /* #ifdef HAVE_LCD_BITMAP */