/* 07/11/2001: initial version 0.1 */ /* 15/11/2001: bug reported by "jpa" corrected: display buffer was too short */ /********** INCLUDES ***********/ #include #include #include #include #include // # include /****** BEGINNING of manually included ******/ /********** DISPLAY POSITIONS & SIZES***********/ #define DISP_X 25 #define DISP1_Y 8 #define DISP2_Y 28 #define DISP_W 130 #define DISP_H 20 #define DISPRES_X DISP_X+5 #define DISPRES_Y1 DISP1_Y+5 #define DISPRES_Y2 DISP2_Y+5 #define DISPRES_W DISP_W-6 #define DISPRES_H DISP_H-6 #define DISPLAY_MAXLEN 7 /********** KEYPAD POSITIONS & SIZES ***********/ #define KEYX0 30 /* pos of left edge of keypad */ #define KEYY0 52 /* pos of upper edge of keypad */ #define KEYXW 24 /* width of each key */ #define KEYYW 17 /* height of each key */ /********** KEYPAD DEF. ***********/ #define KEYID_0 0x50 // 0 #define KEYID_1 0x51 // 1 #define KEYID_2 0x52 // 2 #define KEYID_3 0x53 // 3 #define KEYID_4 0x54 // 4 #define KEYID_5 0x55 // 5 #define KEYID_6 0x56 // 6 #define KEYID_7 0x57 // 7 #define KEYID_8 0x58 // 8 #define KEYID_9 0x59 // 9 #define KEYID_DOT 0x5A // . #define KEYID_OPP 0x5B // +/- #define KEYID_EQ 0x5C // = #define KEYID_ADD 0x5D // + #define KEYID_SUB 0x5E // - #define KEYID_MUL 0x5F // x #define KEYID_DIV 0x60 // / #define KEYID_CLR 0x61 // C; Clear #define KEYID_AC 0x62 // AC; All Clear #define KEYID_MOD 0x63 // MODE; change mode #define KEYID_CUR 0x64 // Currency; change currency #define KEYID_OFF 0x65 // OFF /********** CURRENCIES MODES ***********/ #define MODE_EUR -1 //special euro mode #define MODE_BEF 0 #define MODE_DEM 1 #define MODE_ESP 2 #define MODE_FRF 3 #define MODE_IEP 4 #define MODE_ITL 5 #define MODE_LUF 6 #define MODE_NLG 7 #define MODE_ATS 8 #define MODE_PTE 9 #define MODE_FIM 10 #define MODE_GRD 11 /********** CURRENCIES LABELS ***********/ const char curr_names [] = { 'B','E','F', //BEF 'D','E','M', //DEM 'E','S','P', //ESP 'F','R','F', //FRF 'I','E','P', //IEP 'I','T','L', //ITL 'L','U','F', //LUF 'N','L','G', //NLG 'A','T','S', //ATS 'P','T','E', //PTE 'F','I','M', //FIM 'G','R','D'}; //GRD /********** EURO sign BITMAP ***********/ const char i_euro[] = {6, 9, 0x3c, 0x44, 0x80, 0xf8, 0x80, 0xf0, 0x80, 0x84, 0x78 }; /********** GLOBAL VARS ***********/ /* REM: don't initialize here */ float res1, res2, res_tmp; unsigned char mMode, mModeLoc; unsigned char dispreset, dotted; char operation; char cdisplay[DISPLAY_MAXLEN+1]; unsigned char pos; unsigned char i; //"for loops" generic integer char cur[4]; //string for currencies label float rates[12]; //array of european fixed change rates /****** END of manually included ******/ ////////////////////////// C PROG Starts here////////////////////////// /* COMPILED with REXDX */ /* with -lm & -doublestr options */ /** [zcc +rex -lm -doublestr -create-app -oeurocalc.bin eurocalc.c] **/ /*********************************************************************/ ///// set Display to 0 DisplayRaz() { pos = 0; cdisplay[0] = '0'; cdisplay[1] = '\0'; } ///// set All to 0 Raz() { DisplayRaz(); res2 = 0; res_tmp = 0; dotted = 0; dispreset = 1; operation = ' '; } ///// change local currency string // ATTN: cur[4] must be set to 0 (cf. initialize); change_cur(MLoc) unsigned char MLoc; { for (i=0; i<3; i++) cur[i] = curr_names[3*MLoc+i]; } ClearScreen() { DsDisplayBlockClear(0, 0, 240, 120); } /*** Display Results ***/ DisplayRes() { char cRes[15]; unsigned char precision; res2 = atof(cdisplay); /******* conversion *******/ if (mMode == MODE_EUR) res1 = res2 / rates[mModeLoc]; else res1 = res2 * rates[mModeLoc]; /***** Display numbers ****/ precision = 2; if (!dotted) precision = 0; DsDisplayBlockClear(DISPRES_X,DISPRES_Y1,DISPRES_W,DISPRES_H); DsDisplayBlockClear(DISPRES_X,DISPRES_Y2,DISPRES_W,DISPRES_H); ftoa(res1,2,cRes); DsPrintf(DISPRES_X,DISPRES_Y1,16,cRes); DsPrintf(DISPRES_X,DISPRES_Y2,16,cdisplay); /***** Display currencies ****/ if (mMode==MODE_EUR) { DsDisplayBitmapDraw(125,DISPRES_Y1, i_euro , 0); DsPrintf(120,DISPRES_Y2,16,cur); } else { DsDisplayBitmapDraw(125,DISPRES_Y2, i_euro , 0); DsPrintf(120,DISPRES_Y1,16,cur); } } ///// initialize all (screen, button events, rates, default values...) ///// and draw screen initialize() { DsEventClear(); ClearScreen(); /* Draw Display 1 */ DsDisplayBlock(DISP_X, DISP1_Y, DISP_W, DISP_H, 0); /* Draw Display 2 */ DsDisplayBlock(DISP_X, DISP2_Y, DISP_W, DISP_H, 0); /* deco */ for (i=DISP1_Y; i new number switch (operation) { //previous op. case ' ': res_tmp = res2; break; case '+': res_tmp = res_tmp + res2; break; case '-': res_tmp = res_tmp - res2; break; case 'x': res_tmp = res_tmp * res2; break; case '/': res_tmp = res_tmp / res2; break; } ftoa(res_tmp,2,cdisplay); DisplayRes(); } // display new operation "oper" // if (oper=='=') oper = ' '; operation = oper; cOp[0] = operation; cOp[1] = '\0'; DsDisplayBlockClear(145, DISPRES_Y2, 8, 11); DsPrintf(145,DISPRES_Y2,16,cOp); } ///// called when digit pressed PressNum(bCode) unsigned char bCode; { unsigned char num_in; // maxlen: can not add digit if (pos==DISPLAY_MAXLEN) return; // if display reset must be done if (dispreset) { // reset display dispreset = 0; res2 = 0; dotted = 0; DisplayRaz(); } if (bCode==KEYID_DOT) { if (!dotted) { dotted = 1; if (cdisplay[0]=='0') pos++; cdisplay[pos] = '.'; pos++; cdisplay[pos] = '\0'; } } else { // add digit to display cdisplay[pos] = '0' + bCode - KEYID_0; pos++; cdisplay[pos] = '\0'; } //display DisplayRes(); } /**********************************************/ /**********************************************/ main() { MSG msg; char flag; initialize(); flag = 1; while(flag) { DsEventMessageGet(&msg); switch (msg.message) { case MSG_DS_CLOSE: flag = 0; break; case MSG_DS_KEY_DOWN: if (msg.sCode==KEY_TOP_C) flag = 0; break; case MSG_DS_PAINT: break; case MSG_DS_COMMAND: if (KEYID_0<=msg.bCode && msg.bCode0; i--) cdisplay[i] = cdisplay[i-1]; cdisplay[0] = '-'; pos++; } DisplayRes(); break; case KEYID_ADD: // [+] PressOperat('+'); break; case KEYID_SUB: // [-] PressOperat('-'); break; case KEYID_MUL: // [x] PressOperat('x'); break; case KEYID_DIV: // [/] PressOperat('/'); break; case KEYID_EQ: // [=] PressOperat(' '); break; case KEYID_CUR: // [cur] mModeLoc ++; if (mModeLoc > MODE_GRD) mModeLoc = MODE_BEF; if (mMode > MODE_EUR) mMode = mModeLoc; change_cur(mModeLoc); DisplayRes(); break; case KEYID_AC: // [AC] Raz(); DisplayRes(); break; case KEYID_MOD: // [MODE] // change mode: Local currency <=> EURO if (mMode==MODE_EUR) mMode = mModeLoc; //change to local mode else mMode = MODE_EUR; //change to euro mode DisplayRes(); break; case KEYID_CLR: // [C] if (pos > 1) { pos--; cdisplay[pos] = '\0'; } else DisplayRaz(); DisplayRes(); break; case KEYID_OFF: // [OFF] flag = 0; break; } break; } } ClearScreen(); DsAddinTerminate(); }