1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 |-----------|-----------|-----------|-----------|-----------|
26 |***********|***********|***********|***********|***********|
27 |***********|***********|***********|***********|***********|
29 12 |-----------|-----------|-----------|-----------|-----------|
30 13 |-----------|-----------|-----------|-----------|-----------| y1
34 23 |-----------|-----------|-----------|-----------|-----------| y2
38 33 |-----------|-----------|-----------|-----------|-----------| y3
42 43 |-----------|-----------|-----------|-----------|-----------| y4
46 53 |-----------|-----------|-----------|-----------|-----------| y5
50 63 |-----------|-----------|-----------|-----------|-----------| y6
54 /*---------------------------------------------------------------------------
56 - Scientific number format core code. Support range 10^-999 ~ 10^999
57 - Number of significant figures up to 10
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.
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 "+, -, *, /"
72 "MR" : load temp memory
73 "M+" : add currently display to temp memory
74 "C" : reset calculator
75 ---------------------------------------------------------------------------*/
84 #define M_TWOPI (M_PI * 2.0)
89 #define REC_HEIGHT (int)(LCD_HEIGHT / (BUTTON_ROWS + 1))
90 #define REC_WIDTH (int)(LCD_WIDTH / BUTTON_COLS)
92 #define Y_6_POS (LCD_HEIGHT) /* Leave room for the border */
93 #define Y_5_POS (Y_6_POS - REC_HEIGHT) /* y5 = 53 */
94 #define Y_4_POS (Y_5_POS - REC_HEIGHT) /* y4 = 43 */
95 #define Y_3_POS (Y_4_POS - REC_HEIGHT) /* y3 = 33 */
96 #define Y_2_POS (Y_3_POS - REC_HEIGHT) /* y2 = 23 */
97 #define Y_1_POS (Y_2_POS - REC_HEIGHT) /* y1 = 13 */
98 #define Y_0_POS 0 /* y0 = 0 */
100 #define X_0_POS 0 /* x0 = 0 */
101 #define X_1_POS (X_0_POS + REC_WIDTH) /* x1 = 22 */
102 #define X_2_POS (X_1_POS + REC_WIDTH) /* x2 = 44 */
103 #define X_3_POS (X_2_POS + REC_WIDTH) /* x3 = 66 */
104 #define X_4_POS (X_3_POS + REC_WIDTH) /* x4 = 88 */
105 #define X_5_POS (X_4_POS + REC_WIDTH) /* x5 = 110, column 111 left blank */
107 #define SIGN(x) ((x)<0?-1:1)
109 #define ABS(a) (((a) < 0) ? -(a) : (a))
112 /* variable button definitions */
113 #if CONFIG_KEYPAD == RECORDER_PAD
114 #define CALCULATOR_LEFT BUTTON_LEFT
115 #define CALCULATOR_RIGHT BUTTON_RIGHT
116 #define CALCULATOR_UP BUTTON_UP
117 #define CALCULATOR_DOWN BUTTON_DOWN
118 #define CALCULATOR_QUIT BUTTON_OFF
119 #define CALCULATOR_INPUT BUTTON_PLAY
120 #define CALCULATOR_CALC BUTTON_F3
121 #define CALCULATOR_OPERATORS BUTTON_F2
122 #define CALCULATOR_CLEAR BUTTON_F1
124 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
125 #define CALCULATOR_LEFT BUTTON_LEFT
126 #define CALCULATOR_RIGHT BUTTON_RIGHT
127 #define CALCULATOR_UP BUTTON_UP
128 #define CALCULATOR_DOWN BUTTON_DOWN
129 #define CALCULATOR_QUIT BUTTON_OFF
130 #define CALCULATOR_INPUT BUTTON_SELECT
131 #define CALCULATOR_CALC BUTTON_F3
132 #define CALCULATOR_OPERATORS BUTTON_F2
133 #define CALCULATOR_CLEAR BUTTON_F1
135 #elif CONFIG_KEYPAD == ONDIO_PAD
136 #define CALCULATOR_LEFT BUTTON_LEFT
137 #define CALCULATOR_RIGHT BUTTON_RIGHT
138 #define CALCULATOR_UP BUTTON_UP
139 #define CALCULATOR_DOWN BUTTON_DOWN
140 #define CALCULATOR_QUIT BUTTON_OFF
141 #define CALCULATOR_INPUT_CALC_PRE BUTTON_MENU
142 #define CALCULATOR_INPUT (BUTTON_MENU | BUTTON_REL)
143 #define CALCULATOR_CALC (BUTTON_MENU | BUTTON_REPEAT)
145 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
146 (CONFIG_KEYPAD == IRIVER_H300_PAD)
147 #define CALCULATOR_LEFT BUTTON_LEFT
148 #define CALCULATOR_RIGHT BUTTON_RIGHT
149 #define CALCULATOR_UP BUTTON_UP
150 #define CALCULATOR_DOWN BUTTON_DOWN
151 #define CALCULATOR_QUIT BUTTON_OFF
152 #define CALCULATOR_INPUT BUTTON_SELECT
153 #define CALCULATOR_CALC BUTTON_ON
154 #define CALCULATOR_OPERATORS BUTTON_MODE
155 #define CALCULATOR_CLEAR BUTTON_REC
157 #define CALCULATOR_RC_QUIT BUTTON_RC_STOP
159 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
160 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
161 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
163 #define CALCULATOR_LEFT BUTTON_LEFT
164 #define CALCULATOR_RIGHT BUTTON_RIGHT
165 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
166 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
167 #define CALCULATOR_QUIT BUTTON_MENU
168 #define CALCULATOR_INPUT BUTTON_SELECT
169 #define CALCULATOR_CALC BUTTON_PLAY
171 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
173 #define CALCULATOR_LEFT BUTTON_LEFT
174 #define CALCULATOR_RIGHT BUTTON_RIGHT
175 #define CALCULATOR_UP BUTTON_UP
176 #define CALCULATOR_DOWN BUTTON_DOWN
177 #define CALCULATOR_QUIT BUTTON_POWER
178 #define CALCULATOR_INPUT BUTTON_SELECT
179 #define CALCULATOR_CALC BUTTON_PLAY
180 #define CALCULATOR_CLEAR BUTTON_REC
182 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
184 #define CALCULATOR_LEFT BUTTON_LEFT
185 #define CALCULATOR_RIGHT BUTTON_RIGHT
186 #define CALCULATOR_UP BUTTON_UP
187 #define CALCULATOR_DOWN BUTTON_DOWN
188 #define CALCULATOR_QUIT BUTTON_POWER
189 #define CALCULATOR_INPUT BUTTON_SELECT
190 #define CALCULATOR_CALC BUTTON_MENU
191 #define CALCULATOR_CLEAR BUTTON_A
193 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
194 #define CALCULATOR_LEFT BUTTON_LEFT
195 #define CALCULATOR_RIGHT BUTTON_RIGHT
196 #define CALCULATOR_UP BUTTON_UP
197 #define CALCULATOR_DOWN BUTTON_DOWN
198 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
199 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
200 #define CALCULATOR_QUIT BUTTON_POWER
201 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
202 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
203 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
204 #define CALCULATOR_CLEAR BUTTON_REC
206 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
207 #define CALCULATOR_LEFT BUTTON_LEFT
208 #define CALCULATOR_RIGHT BUTTON_RIGHT
209 #define CALCULATOR_UP BUTTON_UP
210 #define CALCULATOR_DOWN BUTTON_DOWN
211 #define CALCULATOR_QUIT BUTTON_POWER
212 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
213 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
214 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
216 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
217 #define CALCULATOR_LEFT BUTTON_LEFT
218 #define CALCULATOR_RIGHT BUTTON_RIGHT
219 #define CALCULATOR_UP BUTTON_UP
220 #define CALCULATOR_DOWN BUTTON_DOWN
221 #define CALCULATOR_UP_W_SHIFT BUTTON_SCROLL_BACK
222 #define CALCULATOR_DOWN_W_SHIFT BUTTON_SCROLL_FWD
223 #define CALCULATOR_QUIT (BUTTON_HOME|BUTTON_REPEAT)
224 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
225 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
226 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
227 #define CALCULATOR_CLEAR BUTTON_HOME
230 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
231 #define CALCULATOR_LEFT BUTTON_LEFT
232 #define CALCULATOR_RIGHT BUTTON_RIGHT
233 #define CALCULATOR_UP BUTTON_UP
234 #define CALCULATOR_DOWN BUTTON_DOWN
235 #define CALCULATOR_QUIT BUTTON_POWER
236 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
237 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
238 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
239 #define CALCULATOR_CLEAR BUTTON_HOME
241 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
242 #define CALCULATOR_LEFT BUTTON_LEFT
243 #define CALCULATOR_RIGHT BUTTON_RIGHT
244 #define CALCULATOR_UP BUTTON_UP
245 #define CALCULATOR_DOWN BUTTON_DOWN
246 #define CALCULATOR_QUIT BUTTON_POWER
247 #define CALCULATOR_INPUT_CALC_PRE BUTTON_SELECT
248 #define CALCULATOR_INPUT (BUTTON_SELECT|BUTTON_REL)
249 #define CALCULATOR_CALC (BUTTON_SELECT|BUTTON_REPEAT)
250 #define CALCULATOR_CLEAR (BUTTON_SELECT|BUTTON_UP)
252 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
254 #define CALCULATOR_LEFT BUTTON_LEFT
255 #define CALCULATOR_RIGHT BUTTON_RIGHT
256 #define CALCULATOR_UP BUTTON_SCROLL_UP
257 #define CALCULATOR_DOWN BUTTON_SCROLL_DOWN
258 #define CALCULATOR_QUIT BUTTON_POWER
259 #define CALCULATOR_INPUT_CALC_PRE BUTTON_PLAY
260 #define CALCULATOR_INPUT (BUTTON_PLAY | BUTTON_REL)
261 #define CALCULATOR_CALC (BUTTON_PLAY | BUTTON_REPEAT)
262 #define CALCULATOR_CLEAR BUTTON_REW
264 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
266 #define CALCULATOR_LEFT BUTTON_LEFT
267 #define CALCULATOR_RIGHT BUTTON_RIGHT
268 #define CALCULATOR_UP BUTTON_UP
269 #define CALCULATOR_DOWN BUTTON_DOWN
270 #define CALCULATOR_QUIT BUTTON_BACK
271 #define CALCULATOR_INPUT BUTTON_SELECT
272 #define CALCULATOR_CALC BUTTON_MENU
273 #define CALCULATOR_CLEAR BUTTON_PLAY
275 #elif (CONFIG_KEYPAD == MROBE100_PAD)
277 #define CALCULATOR_LEFT BUTTON_LEFT
278 #define CALCULATOR_RIGHT BUTTON_RIGHT
279 #define CALCULATOR_UP BUTTON_UP
280 #define CALCULATOR_DOWN BUTTON_DOWN
281 #define CALCULATOR_QUIT BUTTON_POWER
282 #define CALCULATOR_INPUT BUTTON_SELECT
283 #define CALCULATOR_CALC BUTTON_MENU
284 #define CALCULATOR_CLEAR BUTTON_DISPLAY
286 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
288 #define CALCULATOR_LEFT BUTTON_RC_REW
289 #define CALCULATOR_RIGHT BUTTON_RC_FF
290 #define CALCULATOR_UP BUTTON_RC_VOL_UP
291 #define CALCULATOR_DOWN BUTTON_RC_VOL_DOWN
292 #define CALCULATOR_QUIT BUTTON_RC_REC
293 #define CALCULATOR_INPUT BUTTON_RC_PLAY
294 #define CALCULATOR_CALC BUTTON_RC_MODE
295 #define CALCULATOR_CLEAR BUTTON_RC_MENU
297 #define CALCULATOR_RC_QUIT BUTTON_REC
299 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
301 #define CALCULATOR_QUIT BUTTON_POWER
302 #define CALCULATOR_CLEAR BUTTON_MENU
304 #elif CONFIG_KEYPAD == IAUDIO67_PAD
306 #define CALCULATOR_LEFT BUTTON_LEFT
307 #define CALCULATOR_RIGHT BUTTON_RIGHT
308 #define CALCULATOR_UP BUTTON_VOLUP
309 #define CALCULATOR_DOWN BUTTON_VOLDOWN
310 #define CALCULATOR_QUIT BUTTON_POWER
311 #define CALCULATOR_INPUT BUTTON_PLAY
312 #define CALCULATOR_CALC BUTTON_MENU
313 #define CALCULATOR_CLEAR BUTTON_STOP
315 #define CALCULATOR_RC_QUIT (BUTTON_MENU|BUTTON_PLAY)
317 #elif (CONFIG_KEYPAD == CREATIVEZVM_PAD)
319 #define CALCULATOR_LEFT BUTTON_LEFT
320 #define CALCULATOR_RIGHT BUTTON_RIGHT
321 #define CALCULATOR_UP BUTTON_UP
322 #define CALCULATOR_DOWN BUTTON_DOWN
323 #define CALCULATOR_QUIT BUTTON_BACK
324 #define CALCULATOR_INPUT BUTTON_SELECT
325 #define CALCULATOR_CALC BUTTON_MENU
326 #define CALCULATOR_CLEAR BUTTON_PLAY
328 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
330 #define CALCULATOR_LEFT BUTTON_LEFT
331 #define CALCULATOR_RIGHT BUTTON_RIGHT
332 #define CALCULATOR_UP BUTTON_UP
333 #define CALCULATOR_DOWN BUTTON_DOWN
334 #define CALCULATOR_QUIT BUTTON_POWER
335 #define CALCULATOR_INPUT BUTTON_SELECT
336 #define CALCULATOR_CALC BUTTON_MENU
337 #define CALCULATOR_CLEAR BUTTON_VIEW
339 #elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
341 #define CALCULATOR_LEFT BUTTON_PREV
342 #define CALCULATOR_RIGHT BUTTON_NEXT
343 #define CALCULATOR_UP BUTTON_UP
344 #define CALCULATOR_DOWN BUTTON_DOWN
345 #define CALCULATOR_QUIT BUTTON_POWER
346 #define CALCULATOR_INPUT BUTTON_PLAY
347 #define CALCULATOR_CALC BUTTON_MENU
348 #define CALCULATOR_CLEAR BUTTON_RIGHT
350 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
352 #define CALCULATOR_LEFT BUTTON_PREV
353 #define CALCULATOR_RIGHT BUTTON_NEXT
354 #define CALCULATOR_UP BUTTON_UP
355 #define CALCULATOR_DOWN BUTTON_DOWN
356 #define CALCULATOR_QUIT BUTTON_POWER
357 #define CALCULATOR_INPUT BUTTON_PLAY
358 #define CALCULATOR_CALC BUTTON_MENU
359 #define CALCULATOR_CLEAR BUTTON_RIGHT
361 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
363 #define CALCULATOR_QUIT BUTTON_POWER
364 #define CALCULATOR_CLEAR BUTTON_MENU
366 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
367 #define CALCULATOR_QUIT BUTTON_POWER
369 #elif CONFIG_KEYPAD == MROBE500_PAD
370 #define CALCULATOR_QUIT BUTTON_POWER
372 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
374 #define CALCULATOR_LEFT BUTTON_LEFT
375 #define CALCULATOR_RIGHT BUTTON_RIGHT
376 #define CALCULATOR_UP BUTTON_UP
377 #define CALCULATOR_DOWN BUTTON_DOWN
378 #define CALCULATOR_QUIT BUTTON_REC
379 #define CALCULATOR_INPUT BUTTON_PLAY
380 #define CALCULATOR_CALC BUTTON_FFWD
381 #define CALCULATOR_CLEAR BUTTON_REW
383 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
385 #define CALCULATOR_LEFT BUTTON_PREV
386 #define CALCULATOR_RIGHT BUTTON_NEXT
387 #define CALCULATOR_UP BUTTON_UP
388 #define CALCULATOR_DOWN BUTTON_DOWN
389 #define CALCULATOR_QUIT BUTTON_REC
390 #define CALCULATOR_INPUT BUTTON_OK
391 #define CALCULATOR_CALC BUTTON_PLAY
392 #define CALCULATOR_CLEAR BUTTON_CANCEL
394 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
395 #define CALCULATOR_LEFT BUTTON_REW
396 #define CALCULATOR_RIGHT BUTTON_FF
397 #define CALCULATOR_QUIT (BUTTON_REC|BUTTON_PLAY)
398 #define CALCULATOR_INPUT BUTTON_FUNC
399 #define CALCULATOR_CALC BUTTON_PLAY
401 #elif CONFIG_KEYPAD == MPIO_HD300_PAD
402 #define CALCULATOR_LEFT BUTTON_REW
403 #define CALCULATOR_RIGHT BUTTON_FF
404 #define CALCULATOR_UP BUTTON_UP
405 #define CALCULATOR_DOWN BUTTON_DOWN
406 #define CALCULATOR_QUIT (BUTTON_REC|BUTTON_REPEAT)
407 #define CALCULATOR_INPUT BUTTON_ENTER
408 #define CALCULATOR_CALC BUTTON_PLAY
409 #define CALCULATOR_CLEAR BUTTON_MENU
412 #error No keymap defined!
415 #ifdef HAVE_TOUCHSCREEN
416 #ifndef CALCULATOR_LEFT
417 #define CALCULATOR_LEFT BUTTON_MIDLEFT
419 #ifndef CALCULATOR_RIGHT
420 #define CALCULATOR_RIGHT BUTTON_MIDRIGHT
422 #ifndef CALCULATOR_UP
423 #define CALCULATOR_UP BUTTON_TOPMIDDLE
425 #ifndef CALCULATOR_DOWN
426 #define CALCULATOR_DOWN BUTTON_BOTTOMMIDDLE
428 #ifndef CALCULATOR_CALC
429 #define CALCULATOR_CALC BUTTON_BOTTOMRIGHT
431 #ifndef CALCULATOR_INPUT
432 #define CALCULATOR_INPUT BUTTON_CENTER
434 #ifndef CALCULATOR_CLEAR
435 #define CALCULATOR_CLEAR BUTTON_TOPRIGHT
438 #include "lib/pluginlib_touchscreen.h"
439 static struct ts_raster calc_raster
= { X_0_POS
, Y_1_POS
,
440 BUTTON_COLS
*REC_WIDTH
, BUTTON_ROWS
*REC_HEIGHT
, REC_WIDTH
, REC_HEIGHT
};
448 unsigned char* buttonChar
[2][5][5] = {
449 { { "MR" , "M+" , "2nd" , "CE" , "C" },
450 { "7" , "8" , "9" , "/" , "sqr" },
451 { "4" , "5" , "6" , "*" , "x^2" },
452 { "1" , "2" , "3" , "-" , "1/x" },
453 { "0" , "+/-", "." , "+" , "=" } },
455 { { "n!" , "PI" , "1st" , "sin" , "asi" },
456 { "7" , "8" , "9" , "cos" , "aco" },
457 { "4" , "5" , "6" , "tan" , "ata" },
458 { "1" , "2" , "3" , "ln" , "e^x" },
459 { "0" , "+/-", "." , "log" , "x^y" } }
462 enum { btn_MR
, btn_M
, btn_bas
, btn_CE
, btn_C
,
463 btn_7
, btn_8
, btn_9
, btn_div
, btn_sqr
,
464 btn_4
, btn_5
, btn_6
, btn_time
, btn_square
,
465 btn_1
, btn_2
, btn_3
, btn_minus
, btn_rec
,
466 btn_0
, btn_sign
, btn_dot
, btn_add
, btn_equal
469 enum { sci_fac
, sci_pi
, sci_sci
, sci_sin
, sci_asin
,
470 sci_7
, sci_8
, sci_9
, sci_cos
, sci_acos
,
471 sci_4
, sci_5
, sci_6
, sci_tan
, sci_atan
,
472 sci_1
, sci_2
, sci_3
, sci_ln
, sci_exp
,
473 sci_0
, sci_sign
, sci_dot
, sci_log
, sci_xy
476 #define MINIMUM 0.000000000001 /* e-12 */
478 /* 123456789abcdef */
480 #define DIGITLEN 10 /* must <= 10 */
481 #define SCIENTIFIC_FORMAT ( power < -(DIGITLEN-3) || power > (DIGITLEN))
482 /* 0.000 00000 0001 */
484 /* DIGITLEN 12345 6789a bcdef */
485 /* power 12 34567 89abc def */
486 /* 10^- 123 45678 9abcd ef */
488 unsigned char buf
[19];/* 18 bytes of output line,
490 buf[1] = 'M' if memTemp is not 0
494 buf[2]-buf[12] or buf[3]-buf[13] = result;
496 buf[13] or buf[14] -buf[17] = power;
500 buf[7]-buf[17] = result;
504 unsigned char typingbuf
[DIGITLEN
+2];/* byte 0 is sign or ' ',
505 byte 1~DIGITLEN are num and '.'
506 byte (DIGITLEN+1) is '\0' */
507 unsigned char* typingbufPointer
= typingbuf
;
509 double result
= 0; /* main operand, format 0.xxxxx */
510 int power
= 0; /* 10^power */
511 double modifier
= 0.1; /* position of next input */
512 double operand
= 0; /* second operand, format 0.xxxxx */
513 int operandPower
= 0; /* 10^power of second operand */
514 char oper
= ' '; /* operators: + - * / */
515 bool operInputted
= false; /* false: do calculation first and
517 true: just replace current oper */
519 double memTemp
= 0; /* temp memory */
520 int memTempPower
= 0; /* 10^^power of memTemp */
522 int btn_row
, btn_col
; /* current position index for button */
523 int prev_btn_row
, prev_btn_col
; /* previous cursor position */
524 #define CAL_BUTTON (btn_row*5+btn_col)
526 int btn
= BUTTON_NONE
;
527 int lastbtn
= BUTTON_NONE
;
529 /* Status of calculator */
530 enum {cal_normal
, /* 0, normal status, display result */
531 cal_typing
, /* 1, currently typing, dot hasn't been typed */
532 cal_dotted
, /* 2, currently typing, dot already has been typed. */
538 /* constant table for CORDIC algorithm */
539 double cordicTable
[51][2]= {
540 /* pow(2,0) - pow(2,-50) atan(pow(2,0) - atan(pow(2,-50) */
541 {1e+00, 7.853981633974483e-01},
542 {5e-01, 4.636476090008061e-01},
543 {2.5e-01, 2.449786631268641e-01},
544 {1.25e-01, 1.243549945467614e-01},
545 {6.25e-02, 6.241880999595735e-02},
546 {3.125e-02, 3.123983343026828e-02},
547 {1.5625e-02, 1.562372862047683e-02},
548 {7.8125e-03, 7.812341060101111e-03},
549 {3.90625e-03, 3.906230131966972e-03},
550 {1.953125e-03, 1.953122516478819e-03},
551 {9.765625e-04, 9.765621895593195e-04},
552 {4.8828125e-04, 4.882812111948983e-04},
553 {2.44140625e-04, 2.441406201493618e-04},
554 {1.220703125e-04, 1.220703118936702e-04},
555 {6.103515625e-05, 6.103515617420877e-05},
556 {3.0517578125e-05, 3.051757811552610e-05},
557 {1.52587890625e-05, 1.525878906131576e-05},
558 {7.62939453125e-06, 7.629394531101970e-06},
559 {3.814697265625e-06, 3.814697265606496e-06},
560 {1.9073486328125e-06, 1.907348632810187e-06},
561 {9.5367431640625e-07, 9.536743164059608e-07},
562 {4.76837158203125e-07, 4.768371582030888e-07},
563 {2.384185791015625e-07, 2.384185791015580e-07},
564 {1.1920928955078125e-07, 1.192092895507807e-07},
565 {5.9604644775390625e-08, 5.960464477539055e-08},
566 {2.98023223876953125e-08, 2.980232238769530e-08},
567 {1.490116119384765625e-08, 1.490116119384765e-08},
568 {7.450580596923828125e-09, 7.450580596923828e-09},
569 {3.7252902984619140625e-09, 3.725290298461914e-09},
570 {1.86264514923095703125e-09, 1.862645149230957e-09},
571 {9.31322574615478515625e-10, 9.313225746154785e-10},
572 {4.656612873077392578125e-10, 4.656612873077393e-10},
573 {2.3283064365386962890625e-10, 2.328306436538696e-10},
574 {1.16415321826934814453125e-10, 1.164153218269348e-10},
575 {5.82076609134674072265625e-11, 5.820766091346741e-11},
576 {2.910383045673370361328125e-11, 2.910383045673370e-11},
577 {1.4551915228366851806640625e-11, 1.455191522836685e-11},
578 {7.2759576141834259033203125e-12, 7.275957614183426e-12},
579 {3.63797880709171295166015625e-12, 3.637978807091713e-12},
580 {1.818989403545856475830078125e-12, 1.818989403545856e-12},
581 {9.094947017729282379150390625e-13, 9.094947017729282e-13},
582 {4.5474735088646411895751953125e-13, 4.547473508864641e-13},
583 {2.27373675443232059478759765625e-13, 2.273736754432321e-13},
584 {1.136868377216160297393798828125e-13, 1.136868377216160e-13},
585 {5.684341886080801486968994140625e-14, 5.684341886080801e-14},
586 {2.8421709430404007434844970703125e-14, 2.842170943040401e-14},
587 {1.42108547152020037174224853515625e-14, 1.421085471520200e-14},
588 {7.10542735760100185871124267578125e-15, 7.105427357601002e-15},
589 {3.552713678800500929355621337890625e-15, 3.552713678800501e-15},
590 {1.7763568394002504646778106689453125e-15, 1.776356839400250e-15},
591 {8.8817841970012523233890533447265625e-16, 8.881784197001252e-16}
594 void doMultiple(double* operandOne
, int* powerOne
,
595 double operandTwo
, int powerTwo
);
596 void doAdd (double* operandOne
, int* powerOne
,
597 double operandTwo
, int powerTwo
);
598 void printResult(void);
599 void formatResult(void);
600 void oneOperand(void);
602 void drawLines(void);
603 void drawButtons(int group
);
605 /* -----------------------------------------------------------------------
607 ----------------------------------------------------------------------- */
608 void cleartypingbuf(void)
611 for( k
=1; k
<=(DIGITLEN
+1); k
++)
614 typingbufPointer
= typingbuf
+1;
623 void clearResult(void)
630 void clearInput(void)
632 calStatus
= cal_normal
;
635 rb
->lcd_clear_display();
636 drawButtons(buttonGroup
);
640 void clearOperand(void)
646 void clearMemTemp(void)
655 operInputted
= false;
667 void switchOperands(void)
669 double tempr
= operand
;
670 int tempp
= operandPower
;
672 operandPower
= power
;
680 rb
->lcd_hline(0, LCD_WIDTH
, Y_1_POS
-1);
681 for (i
= 0; i
< 5 ; i
++)
682 rb
->lcd_hline(0, LCD_WIDTH
, Y_1_POS
+i
*REC_HEIGHT
);
683 for (i
= 0; i
< 4 ; i
++)
684 rb
->lcd_vline(X_1_POS
+i
*REC_WIDTH
, Y_1_POS
, LCD_HEIGHT
);
687 void drawButtons(int group
)
690 for (i
= 0; i
<= 4; i
++){
691 for (j
= 0; j
<= 4; j
++){
692 rb
->lcd_getstringsize( buttonChar
[group
][i
][j
],&w
,&h
);
693 if (i
== btn_row
&& j
== btn_col
) /* selected item */
694 rb
->lcd_set_drawmode(DRMODE_SOLID
);
696 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
697 rb
->lcd_fillrect( X_0_POS
+ j
*REC_WIDTH
,
698 Y_1_POS
+ i
*REC_HEIGHT
,
699 REC_WIDTH
, REC_HEIGHT
+1);
700 if (i
== btn_row
&& j
== btn_col
) /* selected item */
701 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
703 rb
->lcd_set_drawmode(DRMODE_SOLID
);
704 rb
->lcd_putsxy( X_0_POS
+ j
*REC_WIDTH
+ (REC_WIDTH
- w
)/2,
705 Y_1_POS
+ i
*REC_HEIGHT
+ (REC_HEIGHT
- h
)/2 + 1,
706 buttonChar
[group
][i
][j
] );
709 rb
->lcd_set_drawmode(DRMODE_SOLID
);
712 /* -----------------------------------------------------------------------
714 ----------------------------------------------------------------------- */
715 void cal_initial (void)
719 rb
->lcd_getstringsize("2nd",&w
,&h
);
720 if (w
> REC_WIDTH
|| h
> REC_HEIGHT
)
721 rb
->lcd_setfont(FONT_SYSFIXED
);
723 rb
->lcd_clear_display();
725 #ifdef CALCULATOR_OPERATORS
726 /* basic operators are available through separate button */
727 buttonGroup
= sciButtons
;
729 buttonGroup
= basicButtons
;
732 /* initially, invert button "5" */
735 prev_btn_row
= btn_row
;
736 prev_btn_col
= btn_col
;
737 drawButtons(buttonGroup
);
741 /* initial mem and output display*/
745 /* clear button queue */
746 rb
->button_clear_queue();
749 /* -----------------------------------------------------------------------
750 mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
751 in it's private case for sqrt.
752 Thanks BlueChip for his intro text and Dave Straayer for the actual name.
753 ----------------------------------------------------------------------- */
754 double mySqrt(double square
)
758 double root
= ABS(square
+1)/2;
760 while( ABS(root
- temp
) > MINIMUM
){
762 root
= (square
/temp
+ temp
)/2;
764 if (k
>10000) return 0;
769 /* -----------------------------------------------------------------------
770 transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
771 transcendFunc can do sin,cos,log,exp
772 input parameter is angle
773 ----------------------------------------------------------------------- */
774 void transcendFunc(char* func
, double* tt
, int* ttPower
)
776 double t
= (*tt
)*M_PI
/180; int tPower
= *ttPower
;
778 int n
= 50; /* n <=50, tables are all <= 50 */
780 double x
,y
,z
,xt
,yt
,zt
;
783 calStatus
= cal_normal
;
787 calStatus
= cal_error
;
791 calStatus
= cal_normal
;
793 if( func
[0] =='s' || func
[0] =='S'|| func
[0] =='t' || func
[0] =='T')
796 /* if( func[0] =='c' || func[0] =='C') */
810 while (t
> j
*M_TWOPI
) {j
++;}
812 if (M_PI_2
< t
&& t
< 3*M_PI_2
){
814 if (func
[0] =='c' || func
[0] =='C')
816 else if (func
[0] =='t' || func
[0] =='T')
819 else if ( 3*M_PI_2
<= t
&& t
<= M_TWOPI
)
822 x
= 0.60725293500888; y
= 0; z
= t
;
824 xt
= x
- SIGN(z
) * y
*cordicTable
[j
-1][0];
825 yt
= y
+ SIGN(z
) * x
*cordicTable
[j
-1][0];
826 zt
= z
- SIGN(z
) * cordicTable
[j
-1][1];
831 if( func
[0] =='s' || func
[0] =='S') {
835 else if( func
[0] =='c' || func
[0] =='C') {
839 else /*if( func[0] =='t' || func[0] =='T')*/ {
840 if(t
==M_PI_2
||t
==-M_PI_2
){
841 calStatus
= cal_error
;
851 /* -----------------------------------------------------------------------
852 add in scientific number format
853 ----------------------------------------------------------------------- */
854 void doAdd (double* operandOne
, int* powerOne
,
855 double operandTwo
, int powerTwo
)
857 if ( *powerOne
>= powerTwo
){
858 if (*powerOne
- powerTwo
<= DIGITLEN
+1){
859 while (powerTwo
< *powerOne
){
863 *operandOne
+= operandTwo
;
865 /*do nothing if operandTwo is too small*/
868 if (powerTwo
- *powerOne
<= DIGITLEN
+1){
869 while(powerTwo
> *powerOne
){
873 (*operandOne
) += operandTwo
;
875 else{/* simply copy operandTwo if operandOne is too small */
876 *operandOne
= operandTwo
;
877 *powerOne
= powerTwo
;
881 /* -----------------------------------------------------------------------
882 multiple in scientific number format
883 ----------------------------------------------------------------------- */
884 void doMultiple(double* operandOne
, int* powerOne
,
885 double operandTwo
, int powerTwo
)
887 (*operandOne
) *= operandTwo
;
888 (*powerOne
) += powerTwo
;
891 /* -----------------------------------------------------------------------
892 Handles all one operand calculations
893 ----------------------------------------------------------------------- */
894 void oneOperand(void)
897 if (buttonGroup
== basicButtons
){
901 calStatus
= cal_error
;
904 result
= (mySqrt(result
*10))/10;
905 power
= (power
+1) / 2;
908 result
= mySqrt(result
);
911 calStatus
= cal_normal
;
917 calStatus
= cal_normal
;
922 calStatus
= cal_error
;
926 calStatus
= cal_normal
;
930 calStatus
= cal_toDo
;
931 break; /* just for the safety */
934 else{ /* sciButtons */
937 transcendFunc("sin", &result
, &power
);
940 transcendFunc("cos", &result
, &power
);
943 transcendFunc("tan", &result
, &power
);
946 if (power
<0 || power
>8 || result
<0 )
947 calStatus
= cal_error
;
948 else if(result
== 0) {
957 if ( ( result
- (int)result
) > MINIMUM
)
958 calStatus
= cal_error
;
960 k
= result
; result
= 1;
962 doMultiple(&result
, &power
, k
, 0);
966 calStatus
= cal_normal
;
971 calStatus
= cal_toDo
;
972 break; /* just for the safety */
978 /* -----------------------------------------------------------------------
979 Handles all two operands calculations
980 ----------------------------------------------------------------------- */
981 void twoOperands(void)
985 doAdd(&operand
, &operandPower
, -result
, power
);
988 doAdd(&operand
, &operandPower
, result
, power
);
991 doMultiple(&operand
, &operandPower
, result
, power
);
994 if ( ABS(result
) > MINIMUM
){
995 doMultiple(&operand
, &operandPower
, 1/result
, -power
);
998 calStatus
= cal_error
;
1001 switchOperands(); /* counter switchOperands() below */
1003 } /* switch(oper) */
1008 /* First, increases *dimen1 by dimen1_delta modulo dimen1_modulo.
1009 If dimen1 wraps, increases *dimen2 by dimen2_delta modulo dimen2_modulo.
1011 static void move_with_wrap_and_shift(
1012 int *dimen1
, int dimen1_delta
, int dimen1_modulo
,
1013 int *dimen2
, int dimen2_delta
, int dimen2_modulo
)
1015 bool wrapped
= false;
1017 *dimen1
+= dimen1_delta
;
1020 *dimen1
= dimen1_modulo
- 1;
1023 else if (*dimen1
>= dimen1_modulo
)
1031 /* Make the dividend always positive to be sure about the result.
1032 Adding dimen2_modulo does not change it since we do it modulo. */
1033 *dimen2
= (*dimen2
+ dimen2_modulo
+ dimen2_delta
) % dimen2_modulo
;
1037 /* -----------------------------------------------------------------------
1038 Print buttons when switching 1st and 2nd
1039 int group = {basicButtons, sciButtons}
1040 ----------------------------------------------------------------------- */
1041 void printButtonGroups(int group
)
1047 /* -----------------------------------------------------------------------
1048 flash the currently marked button
1049 ----------------------------------------------------------------------- */
1050 void flashButton(void)
1055 rb
->lcd_getstringsize( buttonChar
[buttonGroup
][btn_row
][btn_col
],&w
,&h
);
1056 rb
->lcd_set_drawmode(DRMODE_SOLID
|(k
==1) ? 0 : DRMODE_INVERSEVID
);
1057 rb
->lcd_fillrect( X_0_POS
+ btn_col
*REC_WIDTH
+ 1,
1058 Y_1_POS
+ btn_row
*REC_HEIGHT
+ 1,
1059 REC_WIDTH
- 1, REC_HEIGHT
- 1);
1060 rb
->lcd_putsxy( X_0_POS
+ btn_col
*REC_WIDTH
+ (REC_WIDTH
- w
)/2,
1061 Y_1_POS
+ btn_row
*REC_HEIGHT
+ (REC_HEIGHT
- h
)/2 +1,
1062 buttonChar
[buttonGroup
][btn_row
][btn_col
] );
1063 rb
->lcd_update_rect( X_0_POS
+ btn_col
*REC_WIDTH
+ 1,
1064 Y_1_POS
+ btn_row
*REC_HEIGHT
+ 1,
1065 REC_WIDTH
- 1, REC_HEIGHT
- 1);
1073 /* -----------------------------------------------------------------------
1074 pos is the position that needs animation. pos = [1~18]
1075 ----------------------------------------------------------------------- */
1076 void deleteAnimation(int pos
)
1079 if (pos
<1 || pos
>18)
1082 rb
->lcd_getstringsize("0", &w
, &h
);
1083 x
= (pos
==1? 4: LCD_WIDTH
- 4 - w
);
1086 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1087 rb
->lcd_fillrect(x
, Y_1_POS
- h
-1, w
, h
);
1088 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1089 rb
->lcd_fillrect(x
+ (w
*k
)/8, Y_1_POS
- h
-1 + (h
*k
)/8,
1090 (w
*(4-k
))/4, (h
*(4-k
))/4);
1091 rb
->lcd_update_rect(x
, Y_1_POS
- h
-1, w
, h
);
1096 /* -----------------------------------------------------------------------
1097 result may be one of these formats:
1103 formatResult() change result to standard format: 0.xxxx
1104 if result is close to 0, let it be 0;
1105 if result is close to 1, let it be 0.1 and power++;
1106 ----------------------------------------------------------------------- */
1107 void formatResult(void)
1109 int resultsign
= SIGN(result
);
1110 result
= ABS(result
);
1111 if(result
> MINIMUM
){ /* doesn't check power, might have problem
1113 + - * / of two formatted number wouldn't.
1114 only a calculation that makes a formatted
1115 number (0.xxxx) less than MINIMUM in only
1119 while( (int)(result
*10) == 0 ){
1125 else{ /* result >= 1 */
1126 while( (int)result
!= 0 ){
1133 if (result
> (1-MINIMUM
)){
1138 result
*= resultsign
;
1147 /* -----------------------------------------------------------------------
1148 result2typingbuf() outputs standard format result to typingbuf.
1149 case SCIENTIFIC_FORMAT, let temppower = 1;
1150 case temppower > 0: print '.' in the middle
1151 case temppower <= 0: print '.' in the begining
1152 ----------------------------------------------------------------------- */
1153 void result2typingbuf(void)
1155 bool haveDot
= false;
1158 double tempresult
= ABS(result
); /* positive num makes things simple */
1161 double tempmodifier
= 1;
1164 if(SCIENTIFIC_FORMAT
)
1165 temppower
= 1; /* output x.xxxx format */
1171 if(tempresult
< MINIMUM
){ /* if 0,faster display and avoid complication*/
1175 else{ /* tempresult > 0 */
1176 typingbuf
[0] = (SIGN(result
)<0)?'-':' ';
1178 typingbufPointer
= typingbuf
;
1180 for (k
= 0; k
<DIGITLEN
+1 ; k
++){
1182 if(temppower
|| *(typingbufPointer
-1) == '.'){
1184 tempmodifier
= tempmodifier
/10;
1185 while( (tempresult
-tempmodifier
*count
) >
1186 (tempmodifier
-MINIMUM
)){
1189 tempresult
-= tempmodifier
*count
;
1190 tempresult
= ABS(tempresult
);
1192 *typingbufPointer
= count
+ '0';
1194 else{ /* temppower == 0 */
1195 *typingbufPointer
= '.';
1202 typingbufPointer
++; *typingbufPointer
= '0';
1203 typingbufPointer
++; *typingbufPointer
= '.';
1204 for (k
= 2; k
<DIGITLEN
+1 ; k
++){
1207 if ( (-temppower
) < (k
-1)){
1208 tempmodifier
= tempmodifier
/10;
1209 while((tempresult
-tempmodifier
*count
)>(tempmodifier
-MINIMUM
)){
1213 tempresult
-= tempmodifier
*count
;
1214 tempresult
= ABS(tempresult
);
1217 *typingbufPointer
= count
+ '0';
1220 /* now, typingbufPointer = typingbuf + 16 */
1221 /* backward strip off 0 and '.' */
1223 while( (*typingbufPointer
== '0') || (*typingbufPointer
== '.')){
1224 tempchar
= *typingbufPointer
;
1225 *typingbufPointer
= 0;
1227 if (tempchar
== '.') break;
1230 typingbuf
[DIGITLEN
+1] = 0;
1231 } /* else tempresult > 0 */
1234 /* -----------------------------------------------------------------------
1235 printResult() generates LCD display.
1236 ----------------------------------------------------------------------- */
1237 void printResult(void)
1241 char operbuf
[3] = {0, 0, 0};
1246 rb
->lcd_clear_display();
1247 rb
->splash(HZ
/3, "Bye now!");
1251 rb
->snprintf(buf
, 19, "%18s","Error");
1255 rb
->snprintf(buf
, 19, "%18s","Coming soon ^_* ");
1261 if( power
> 1000 ){ /* power -1 > 999 */
1262 calStatus
= cal_error
;
1265 if (power
< -998 ) /* power -1 < -999 */
1266 clearResult(); /* too small, let it be 0 */
1272 operbuf
[1] = ( ABS(memTemp
) > MINIMUM
)?'M':' ';
1275 if(SCIENTIFIC_FORMAT
){
1276 /* output format: X.XXXX eXXX */
1277 if(power
> -98){ /* power-1 >= -99, eXXX or e-XX */
1278 rb
->snprintf(buf
, 12, "%11s",typingbuf
);
1279 for(k
=11;k
<=14;k
++) buf
[k
] = ' ';
1281 rb
->snprintf(typingbuf
, 5, "e%d",power
-1);
1282 rb
->snprintf(buf
+11, 5, "%4s",typingbuf
);
1284 else{ /* power-1 <= -100, e-XXX */
1285 rb
->snprintf(buf
, 12, "%11s",typingbuf
);
1286 rb
->snprintf(buf
+11, 6, "e%d",power
-1);
1290 rb
->snprintf(buf
, 12, "%11s",typingbuf
);
1291 } /* if SCIENTIFIC_FORMAT */
1297 operbuf
[1] = ( ABS(memTemp
) > MINIMUM
)?'M':' ';
1298 rb
->snprintf(buf
, 12, "%11s",typingbuf
);
1303 rb
->lcd_getstringsize(buf
, &w
, &h
);
1304 rb
->screen_clear_area(rb
->screens
[0], 0, 0, LCD_WIDTH
, Y_1_POS
- 1);
1305 rb
->lcd_putsxy(4, Y_1_POS
- h
-1, operbuf
);
1306 rb
->lcd_putsxy(LCD_WIDTH
- w
- 4, Y_1_POS
- h
-1, buf
);
1307 rb
->lcd_update_rect(0, 1, LCD_WIDTH
, Y_1_POS
);
1310 /* -----------------------------------------------------------------------
1311 Process typing buttons: 1-9, '.', sign
1312 main operand "result" and typingbuf are processed seperately here.
1313 ----------------------------------------------------------------------- */
1314 void typingProcess(void){
1315 switch( CAL_BUTTON
){
1317 if (calStatus
== cal_typing
||
1318 calStatus
== cal_dotted
)
1319 typingbuf
[0] = (typingbuf
[0]=='-')?' ':'-';
1323 operInputted
= false;
1327 *typingbufPointer
= '0';
1330 calStatus
= cal_dotted
;
1331 *typingbufPointer
= '.';
1332 if (typingbufPointer
!= typingbuf
+DIGITLEN
+1)
1335 default: /* cal_dotted */
1340 operInputted
= false;
1341 /* normal,0; normal,1-9; typing,0; typing,1-9 */
1344 if(CAL_BUTTON
== btn_0
)
1345 break; /* first input is 0, ignore */
1347 /*no operator means start a new calculation*/
1350 calStatus
= cal_typing
;
1351 /* go on typing, no break */
1356 *typingbufPointer
= '0';
1359 *typingbufPointer
=(7+btn_col
-3*(btn_row
-1))+ '0';
1362 if (typingbufPointer
!=typingbuf
+DIGITLEN
+1){
1365 {/* result processing */
1366 if (calStatus
== cal_typing
) power
++;
1367 if (CAL_BUTTON
!= btn_0
)
1370 (7+btn_col
-3*(btn_row
-1))*modifier
;
1374 else /* last byte always '\0' */
1375 *typingbufPointer
= 0;
1377 default: /* cal_error, cal_exit */
1380 break; /* default, 0-9 */
1381 } /* switch( CAL_BUTTON ) */
1384 /* -----------------------------------------------------------------------
1385 Handle delete operation
1386 main operand "result" and typingbuf are processed seperately here.
1387 ----------------------------------------------------------------------- */
1388 void doDelete(void){
1389 deleteAnimation(18);
1392 if (*(typingbufPointer
-1) == '.'){
1393 /* if dotted and deleting '.',
1394 change status and delete '.' below */
1395 calStatus
= cal_typing
;
1397 else{ /* if dotted and not deleting '.',
1399 power
++; /* counter "power--;" below */
1404 {/* result processing */ /* 0-9, '.' */
1405 /* if deleting '.', do nothing */
1406 if ( *typingbufPointer
!= '.'){
1409 result
= result
- SIGN(result
)*
1410 ((*typingbufPointer
)- '0')*modifier
;
1414 *typingbufPointer
= 0;
1416 /* if (only one digit left and it's 0)
1417 or no digit left, change status*/
1418 if ( typingbufPointer
== typingbuf
+1 ||
1419 ( typingbufPointer
== typingbuf
+2 &&
1420 *(typingbufPointer
-1) == '0' ))
1421 calStatus
= cal_normal
;
1423 default: /* normal, error, exit */
1427 /* -----------------------------------------------------------------------
1428 Handle buttons on basic screen
1429 ----------------------------------------------------------------------- */
1430 void basicButtonsProcess(void){
1432 case CALCULATOR_INPUT
:
1433 if (calStatus
== cal_error
&& (CAL_BUTTON
!= btn_C
) ) break;
1435 switch( CAL_BUTTON
){
1437 operInputted
= false;
1438 result
= memTemp
; power
= memTempPower
;
1439 calStatus
= cal_normal
;
1443 if (memTemp
> MINIMUM
)
1444 doAdd(&memTemp
, &memTempPower
, result
, power
);
1446 /* if result is too small and memTemp = 0,
1447 doAdd will not add */
1449 memTempPower
= power
;
1451 calStatus
= cal_normal
;
1454 case btn_C
: clearMem(); break;
1455 case btn_CE
: clearInput(); break;
1458 buttonGroup
= sciButtons
;
1459 printButtonGroups(buttonGroup
);
1462 /* one operand calculation, may be changed to
1463 like sin, cos, log, etc */
1467 formatResult(); /* not necessary, just for safty */
1471 case_btn_equal
: /* F3 shortkey entrance */
1474 calStatus
= cal_normal
;
1475 operInputted
= false;
1476 if (oper
!= ' ') twoOperands();
1483 if(!operInputted
) {twoOperands(); operInputted
= true;}
1484 oper
= buttonChar
[basicButtons
][btn_row
][btn_col
][0];
1485 #ifdef CALCULATOR_OPERATORS
1486 case_cycle_operators
: /* F2 shortkey entrance */
1488 calStatus
= cal_normal
;
1491 operandPower
= power
;
1500 } /* switch (CAL_BUTTON) */
1503 #ifdef CALCULATOR_OPERATORS
1504 case CALCULATOR_OPERATORS
:
1505 if (calStatus
== cal_error
) break;
1506 if (!operInputted
) {twoOperands(); operInputted
= true;}
1509 case '/': oper
= '+'; flashButton(); break;
1510 case '+': oper
= '-'; flashButton(); break;
1511 case '-': oper
= '*'; flashButton(); break;
1512 case '*': oper
= '/'; flashButton(); break;
1514 goto case_cycle_operators
;
1518 case CALCULATOR_CALC
:
1519 if (calStatus
== cal_error
) break;
1521 goto case_btn_equal
;
1528 /* -----------------------------------------------------------------------
1529 Handle buttons on scientific screen
1530 ----------------------------------------------------------------------- */
1531 void sciButtonsProcess(void){
1533 case CALCULATOR_INPUT
:
1534 if (calStatus
== cal_error
&& (CAL_BUTTON
!= sci_sci
) ) break;
1536 switch( CAL_BUTTON
){
1539 result
= M_PI
; power
= 0;
1540 calStatus
= cal_normal
;
1546 buttonGroup
= basicButtons
;
1547 printButtonGroups(basicButtons
);
1560 formatResult(); /* not necessary, just for safty */
1569 } /* switch (CAL_BUTTON) */
1572 #ifdef CALCULATOR_OPERATORS
1573 case CALCULATOR_OPERATORS
:
1574 if (calStatus
== cal_error
) break;
1575 if (!operInputted
) {twoOperands(); operInputted
= true;}
1577 case ' ': oper
= '+'; break;
1578 case '/': oper
= '+'; deleteAnimation(1); break;
1579 case '+': oper
= '-'; deleteAnimation(1); break;
1580 case '-': oper
= '*'; deleteAnimation(1); break;
1581 case '*': oper
= '/'; deleteAnimation(1); break;
1583 calStatus
= cal_normal
;
1586 operandPower
= power
;
1590 case CALCULATOR_CALC
:
1591 if (calStatus
== cal_error
) break;
1593 calStatus
= cal_normal
;
1594 operInputted
= false;
1595 if (oper
!= ' ') twoOperands();
1602 /* -----------------------------------------------------------------------
1604 Invert display new button, invert back previous button
1605 ----------------------------------------------------------------------- */
1606 int handleButton(int button
){
1609 case CALCULATOR_INPUT
:
1610 case CALCULATOR_CALC
:
1611 #ifdef CALCULATOR_INPUT_CALC_PRE
1612 if (lastbtn
!= CALCULATOR_INPUT_CALC_PRE
)
1614 /* no unconditional break; here! */
1616 #ifdef CALCULATOR_OPERATORS
1617 case CALCULATOR_OPERATORS
:
1619 switch(buttonGroup
){
1621 basicButtonsProcess();
1624 sciButtonsProcess();
1629 #ifdef CALCULATOR_CLEAR
1630 case CALCULATOR_CLEAR
:
1636 default: /* cal_normal, cal_error, cal_exit */
1643 case CALCULATOR_LEFT
:
1644 case CALCULATOR_LEFT
| BUTTON_REPEAT
:
1645 move_with_wrap_and_shift(
1646 &btn_col
, -1, BUTTON_COLS
,
1647 &btn_row
, 0, BUTTON_ROWS
);
1650 case CALCULATOR_RIGHT
:
1651 case CALCULATOR_RIGHT
| BUTTON_REPEAT
:
1652 move_with_wrap_and_shift(
1653 &btn_col
, 1, BUTTON_COLS
,
1654 &btn_row
, 0, BUTTON_ROWS
);
1657 #ifdef CALCULATOR_UP
1659 case CALCULATOR_UP
| BUTTON_REPEAT
:
1660 move_with_wrap_and_shift(
1661 &btn_row
, -1, BUTTON_ROWS
,
1662 &btn_col
, 0, BUTTON_COLS
);
1665 #ifdef CALCULATOR_DOWN
1666 case CALCULATOR_DOWN
:
1667 case CALCULATOR_DOWN
| BUTTON_REPEAT
:
1668 move_with_wrap_and_shift(
1669 &btn_row
, 1, BUTTON_ROWS
,
1670 &btn_col
, 0, BUTTON_COLS
);
1674 #ifdef CALCULATOR_UP_W_SHIFT
1675 case CALCULATOR_UP_W_SHIFT
:
1676 case CALCULATOR_UP_W_SHIFT
| BUTTON_REPEAT
:
1677 move_with_wrap_and_shift(
1678 &btn_row
, -1, BUTTON_ROWS
,
1679 &btn_col
, -1, BUTTON_COLS
);
1682 #ifdef CALCULATOR_DOWN_W_SHIFT
1683 case CALCULATOR_DOWN_W_SHIFT
:
1684 case CALCULATOR_DOWN_W_SHIFT
| BUTTON_REPEAT
:
1685 move_with_wrap_and_shift(
1686 &btn_row
, 1, BUTTON_ROWS
,
1687 &btn_col
, 1, BUTTON_COLS
);
1690 #ifdef CALCULATOR_LEFT_W_SHIFT
1691 case CALCULATOR_LEFT_W_SHIFT
:
1692 case CALCULATOR_LEFT_W_SHIFT
| BUTTON_REPEAT
:
1693 move_with_wrap_and_shift(
1694 &btn_col
, -1, BUTTON_COLS
,
1695 &btn_row
, -1, BUTTON_ROWS
);
1698 #ifdef CALCULATOR_RIGHT_W_SHIFT
1699 case CALCULATOR_RIGHT_W_SHIFT
:
1700 case CALCULATOR_RIGHT_W_SHIFT
| BUTTON_REPEAT
:
1701 move_with_wrap_and_shift(
1702 &btn_col
, 1, BUTTON_COLS
,
1703 &btn_row
, 1, BUTTON_ROWS
);
1706 #ifdef CALCULATOR_RC_QUIT
1707 case CALCULATOR_RC_QUIT
:
1709 case CALCULATOR_QUIT
:
1715 prev_btn_row
= btn_row
;
1716 prev_btn_col
= btn_col
;
1719 /* -----------------------------------------------------------------------
1721 ----------------------------------------------------------------------- */
1722 enum plugin_status
plugin_start(const void* parameter
)
1726 /* now go ahead and have fun! */
1728 #ifdef HAVE_TOUCHSCREEN
1729 rb
->touchscreen_set_mode(TOUCHSCREEN_POINT
);
1734 while (calStatus
!= cal_exit
) {
1735 btn
= rb
->button_get_w_tmo(HZ
/2);
1736 #ifdef HAVE_TOUCHSCREEN
1737 if(btn
& BUTTON_TOUCHSCREEN
)
1739 struct ts_raster_result res
;
1740 if(touchscreen_map_raster(&calc_raster
, rb
->button_get_data() >> 16,
1741 rb
->button_get_data() & 0xffff, &res
) == 1)
1745 drawButtons(buttonGroup
);
1750 prev_btn_row
= btn_row
;
1751 prev_btn_col
= btn_col
;
1752 if(btn
& BUTTON_REL
)
1754 btn
= CALCULATOR_INPUT
;
1755 switch(buttonGroup
){
1757 basicButtonsProcess();
1760 sciButtonsProcess();
1763 btn
= BUTTON_TOUCHSCREEN
;
1768 if (handleButton(btn
) == -1)
1770 calStatus
= cal_exit
;
1775 drawButtons(buttonGroup
);
1781 if(rb
->default_event_handler(btn
) == SYS_USB_CONNECTED
)
1782 return PLUGIN_USB_CONNECTED
;
1784 if (btn
!= BUTTON_NONE
)
1786 } /* while (calStatus != cal_exit ) */
1788 rb
->button_clear_queue();