1
0
Fork 0
forked from len0rd/rockbox

I am a wannabe code-police

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@4646 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Daniel Stenberg 2004-05-19 10:21:42 +00:00
parent 367aac9519
commit e4e3337671

View file

@ -133,7 +133,10 @@ F3: equal to "="
static struct plugin_api* rb; static struct plugin_api* rb;
enum {basicButtons, sciButtons} buttonGroup; enum {
basicButtons,
sciButtons
} buttonGroup;
unsigned char* buttonChar[2][5][5] = { unsigned char* buttonChar[2][5][5] = {
{ { "MR" , "M+" , "2nd" , "CE" , "C" }, { { "MR" , "M+" , "2nd" , "CE" , "C" },
{ "7" , "8" , "9" , "/" , "sqr" }, { "7" , "8" , "9" , "/" , "sqr" },
@ -146,7 +149,7 @@ unsigned char* buttonChar[2][5][5] = {
{ "4" , "5" , "6" , "tan" , "ata" }, { "4" , "5" , "6" , "tan" , "ata" },
{ "1" , "2" , "3" , "ln" , "e^x" }, { "1" , "2" , "3" , "ln" , "e^x" },
{ "0" , "+/-", "." , "log" , "x^y" } } { "0" , "+/-", "." , "log" , "x^y" } }
}; };
enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C , enum { btn_MR , btn_M , btn_bas , btn_CE , btn_C ,
btn_7 , btn_8 , btn_9 , btn_div , btn_sqr , btn_7 , btn_8 , btn_9 , btn_div , btn_sqr ,
btn_4 , btn_5 , btn_6 , btn_time , btn_square , btn_4 , btn_5 , btn_6 , btn_time , btn_square ,
@ -338,41 +341,55 @@ void oneOperand(void);
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
Handy funtions Handy funtions
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
void cleartypingbuf(void){ void cleartypingbuf(void)
{
int k; int k;
for( k=1; k<=(DIGITLEN+1); k++) for( k=1; k<=(DIGITLEN+1); k++)
typingbuf[k] = 0; typingbuf[k] = 0;
typingbuf[0] = ' '; typingbuf[0] = ' ';
typingbufPointer = typingbuf+1; typingbufPointer = typingbuf+1;
} }
void clearbuf(void){ void clearbuf(void)
{
int k; int k;
for(k=0;k<18;k++) buf[k]=' '; for(k=0;k<18;k++)
buf[k]=' ';
buf[18] = 0; buf[18] = 0;
} }
void clearResult(void){ void clearResult(void)
{
result = 0; result = 0;
power = 0; power = 0;
modifier = 0.1; modifier = 0.1;
} }
void clearInput(void){
void clearInput(void)
{
calStatus = cal_normal; calStatus = cal_normal;
clearResult(); clearResult();
cleartypingbuf(); cleartypingbuf();
} }
void clearOperand(void){
void clearOperand(void)
{
operand = 0; operand = 0;
operandPower = 0; operandPower = 0;
} }
void clearMemTemp(void){
void clearMemTemp(void)
{
memTemp = 0; memTemp = 0;
memTempPower = 0; memTempPower = 0;
} }
void clearOper(void){
void clearOper(void)
{
oper = ' '; oper = ' ';
operInputted = false; operInputted = false;
} }
void clearMem(void){
void clearMem(void)
{
clearInput(); clearInput();
clearMemTemp(); clearMemTemp();
clearOperand(); clearOperand();
@ -380,7 +397,8 @@ void clearMem(void){
btn = BUTTON_NONE; btn = BUTTON_NONE;
} }
void switchOperands(void){ void switchOperands(void)
{
double tempr = operand; double tempr = operand;
int tempp = operandPower; int tempp = operandPower;
operand = result; operand = result;
@ -392,7 +410,8 @@ void switchOperands(void){
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
Initiate calculator Initiate calculator
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
void cal_initial (void){ void cal_initial (void)
{
int i,j,w,h; int i,j,w,h;
rb->lcd_setfont(FONT_SYSFIXED); rb->lcd_setfont(FONT_SYSFIXED);
rb->lcd_clear_display(); rb->lcd_clear_display();
@ -419,8 +438,10 @@ void cal_initial (void){
} }
/* initially, invert button "5" */ /* initially, invert button "5" */
m = 2; n = 1; m = 2;
prev_m = m; prev_n = n; n = 1;
prev_m = m;
prev_n = n;
rb->lcd_invertrect( X_0_POS + n*REC_WIDTH + 1, rb->lcd_invertrect( X_0_POS + n*REC_WIDTH + 1,
Y_1_POS + m*REC_HEIGHT + 1, Y_1_POS + m*REC_HEIGHT + 1,
REC_WIDTH - 1, REC_HEIGHT - 1); REC_WIDTH - 1, REC_HEIGHT - 1);
@ -435,11 +456,12 @@ void cal_initial (void){
} }
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
in it's private case for sqrt. in it's private case for sqrt.
Thanks BlueChip for his intro text and Dave Straayer for the actual name. Thanks BlueChip for his intro text and Dave Straayer for the actual name.
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
double mySqrt(double square){ double mySqrt(double square)
{
int k = 0; int k = 0;
double temp = 0; double temp = 0;
double root= ABS(square+1)/2; double root= ABS(square+1)/2;
@ -454,56 +476,81 @@ double mySqrt(double square){
return root; return root;
} }
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
transcendFunc can do sin,cos,log,exp transcendFunc can do sin,cos,log,exp
input parameter is angle input parameter is angle
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
void transcendFunc(char* func, double* tt, int* ttPower){ void transcendFunc(char* func, double* tt, int* ttPower)
{
double t = (*tt)*PI/180; int tPower = *ttPower; double t = (*tt)*PI/180; int tPower = *ttPower;
if (tPower < -998) {calStatus = cal_normal; return;}
if (tPower > 8) {calStatus = cal_error; return;}
*ttPower = 0;
calStatus = cal_normal;
int sign = 1; int sign = 1;
int n = 50; /* n <=50, tables are all <= 50 */ int n = 50; /* n <=50, tables are all <= 50 */
int j; int j;
double x,y,z,xt,yt,zt; double x,y,z,xt,yt,zt;
if( func[0] =='s' || func[0] =='S') sign = SIGN(t); if (tPower < -998) {
else { /* if( func[0] =='c' || func[0] =='C') */ sign = 1; } calStatus = cal_normal;
return;
}
if (tPower > 8) {
calStatus = cal_error;
return;
}
*ttPower = 0;
calStatus = cal_normal;
if( func[0] =='s' || func[0] =='S')
sign = SIGN(t);
else {
/* if( func[0] =='c' || func[0] =='C') */
sign = 1;
}
t = ABS(t); t = ABS(t);
while (tPower > 0){ t *= 10; tPower--; } while (tPower > 0){
while (tPower < 0){ t /= 10; tPower++; } t *= 10;
tPower--;
}
while (tPower < 0) {
t /= 10;
tPower++;
}
j = 0; j = 0;
while (t > j*2*PI) {j++;} while (t > j*2*PI) {j++;}
t -= (j-1)*2*PI; t -= (j-1)*2*PI;
if (PI/2 < t && t < 3*PI/2){ if (PI/2 < t && t < 3*PI/2){
t = PI - t; t = PI - t;
if (func[0] =='c' || func[0] =='C') sign = -1; if (func[0] =='c' || func[0] =='C')
sign = -1;
} }
else if ( 3*PI/2 <= t && t <= 2*PI) t -= 2*PI; else if ( 3*PI/2 <= t && t <= 2*PI)
t -= 2*PI;
x = 0.60725293500888; y = 0; z = t; x = 0.60725293500888; y = 0; z = t;
for (j=1;j<n+2;j++){ for (j=1;j<n+2;j++){
xt = x - SIGN(z) * y*cordicTable[j-1][0]; xt = x - SIGN(z) * y*cordicTable[j-1][0];
yt = y + SIGN(z) * x*cordicTable[j-1][0]; yt = y + SIGN(z) * x*cordicTable[j-1][0];
zt = z - SIGN(z) * cordicTable[j-1][1]; zt = z - SIGN(z) * cordicTable[j-1][1];
x = xt;y=yt;z=zt; x = xt;
y=yt;
z=zt;
}
if( func[0] =='s' || func[0] =='S') {
*tt = sign*y;
return;
}
else /* if( func[0] =='c' || func[0] =='C')*/ {
*tt = sign*x;
return;
} }
if( func[0] =='s' || func[0] =='S') {*tt = sign*y; return;}
else /* if( func[0] =='c' || func[0] =='C')*/ {*tt = sign*x; return;}
} }
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
add in scientific number format add in scientific number format
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
void doAdd (double* operandOne, int* powerOne, void doAdd (double* operandOne, int* powerOne,
double operandTwo, int powerTwo){ double operandTwo, int powerTwo)
{
if ( *powerOne >= powerTwo ){ if ( *powerOne >= powerTwo ){
if (*powerOne - powerTwo <= DIGITLEN+1){ if (*powerOne - powerTwo <= DIGITLEN+1){
while (powerTwo < *powerOne){ while (powerTwo < *powerOne){
@ -532,20 +579,23 @@ void doAdd (double* operandOne, int* powerOne,
multiple in scientific number format multiple in scientific number format
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
void doMultiple(double* operandOne, int* powerOne, void doMultiple(double* operandOne, int* powerOne,
double operandTwo, int powerTwo){ double operandTwo, int powerTwo)
(*operandOne) *= operandTwo; {
(*powerOne) += powerTwo; (*operandOne) *= operandTwo;
(*powerOne) += powerTwo;
} }
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
Handles all one operand calculations Handles all one operand calculations
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
void oneOperand(void){ void oneOperand(void)
{
int k = 0; int k = 0;
if (buttonGroup == basicButtons){ if (buttonGroup == basicButtons){
switch(CAL_BUTTON){ switch(CAL_BUTTON){
case btn_sqr: case btn_sqr:
if (result<0) calStatus = cal_error; if (result<0)
calStatus = cal_error;
else{ else{
if (power%2 == 1){ if (power%2 == 1){
result = (mySqrt(result*10))/10; result = (mySqrt(result*10))/10;
@ -565,7 +615,8 @@ void oneOperand(void){
break; break;
case btn_rec: case btn_rec:
if (result==0) calStatus = cal_error; if (result==0)
calStatus = cal_error;
else{ else{
power = -power; power = -power;
result = 1/result; result = 1/result;
@ -586,10 +637,17 @@ void oneOperand(void){
transcendFunc("cos", &result, &power); transcendFunc("cos", &result, &power);
break; break;
case sci_fac: case sci_fac:
if (power<0 || power>8 || result<0 ) calStatus = cal_error; if (power<0 || power>8 || result<0 )
else if(result == 0) {result = 1; power = 0; } calStatus = cal_error;
else if(result == 0) {
result = 1;
power = 0;
}
else{ else{
while(power > 0) {result *= 10; power--;} while(power > 0) {
result *= 10;
power--;
}
if ( ( result - (int)result) > MINIMUM ) if ( ( result - (int)result) > MINIMUM )
calStatus = cal_error; calStatus = cal_error;
else { else {
@ -614,7 +672,8 @@ void oneOperand(void){
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
Handles all two operands calculations Handles all two operands calculations
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
void twoOperands(void){ void twoOperands(void)
{
switch(oper){ switch(oper){
case '-': case '-':
doAdd(&operand, &operandPower, -result, power); doAdd(&operand, &operandPower, -result, power);
@ -629,7 +688,8 @@ void twoOperands(void){
if ( ABS(result) > MINIMUM ){ if ( ABS(result) > MINIMUM ){
doMultiple(&operand, &operandPower, 1/result, -power); doMultiple(&operand, &operandPower, 1/result, -power);
} }
else calStatus = cal_error; else
calStatus = cal_error;
break; break;
default: /* ' ' */ default: /* ' ' */
switchOperands(); /* counter switchOperands() below */ switchOperands(); /* counter switchOperands() below */
@ -646,26 +706,34 @@ void moveButton(void){
switch(btn){ switch(btn){
case BUTTON_LEFT: case BUTTON_LEFT:
case BUTTON_LEFT | BUTTON_REPEAT: case BUTTON_LEFT | BUTTON_REPEAT:
if (n == 0) n = 4; if (n == 0)
else n--; n = 4;
else
n--;
break; break;
case BUTTON_RIGHT: case BUTTON_RIGHT:
case BUTTON_RIGHT | BUTTON_REPEAT: case BUTTON_RIGHT | BUTTON_REPEAT:
if (n == 4) n = 0; if (n == 4)
else n++; n = 0;
else
n++;
break; break;
case BUTTON_UP: case BUTTON_UP:
case BUTTON_UP | BUTTON_REPEAT: case BUTTON_UP | BUTTON_REPEAT:
if (m == 0) m = 4; if (m == 0)
else m--; m = 4;
else
m--;
break; break;
case BUTTON_DOWN: case BUTTON_DOWN:
case BUTTON_DOWN | BUTTON_REPEAT: case BUTTON_DOWN | BUTTON_REPEAT:
if (m == 4) m = 0; if (m == 4)
else m++; m = 0;
else
m++;
break; break;
} }
@ -685,14 +753,15 @@ void moveButton(void){
Y_1_POS + m*REC_HEIGHT + 1, Y_1_POS + m*REC_HEIGHT + 1,
REC_WIDTH - 1, REC_HEIGHT - 1); REC_WIDTH - 1, REC_HEIGHT - 1);
prev_m = m; prev_n = n; prev_m = m;
prev_n = n;
} }
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
Print buttons when switching 1st and 2nd Print buttons when switching 1st and 2nd
int group = {basicButtons, sciButtons} int group = {basicButtons, sciButtons}
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
void printButtonGroups(int group){ void printButtonGroups(int group)
{
int i,j,w,h; int i,j,w,h;
for (i = 0; i < 5; i++){ for (i = 0; i < 5; i++){
for (j = 3; j <= 4; j++){ for (j = 3; j <= 4; j++){
@ -725,7 +794,8 @@ void printButtonGroups(int group){
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
flash the button pressed flash the button pressed
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
void flashButton(int b){ void flashButton(int b)
{
int i = b/5; int j = b - i*5; int i = b/5; int j = b - i*5;
int k; int k;
for (k=1*2;k>0;k--){ for (k=1*2;k>0;k--){
@ -745,9 +815,11 @@ void flashButton(int b){
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
pos is the position that needs animation. pos = [1~18] pos is the position that needs animation. pos = [1~18]
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
void deleteAnimation(int pos){ void deleteAnimation(int pos)
{
int k; int k;
if (pos<1 || pos >18) return; if (pos<1 || pos >18)
return;
pos--; pos--;
rb->lcd_fillrect(1+pos*6, TEXT_1_POS, 6, 8); rb->lcd_fillrect(1+pos*6, TEXT_1_POS, 6, 8);
rb->lcd_update_rect(1+pos*6, TEXT_1_POS, 6, 8); rb->lcd_update_rect(1+pos*6, TEXT_1_POS, 6, 8);
@ -773,7 +845,8 @@ formatResult() change result to standard format: 0.xxxx
if result is close to 0, let it be 0; if result is close to 0, let it be 0;
if result is close to 1, let it be 0.1 and power++; if result is close to 1, let it be 0.1 and power++;
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
void formatResult(void){ void formatResult(void)
{
int resultsign = SIGN(result); int resultsign = SIGN(result);
result = ABS(result); result = ABS(result);
if(result > MINIMUM ){ /* doesn't check power, might have problem if(result > MINIMUM ){ /* doesn't check power, might have problem
@ -785,21 +858,31 @@ void formatResult(void){
if (result<1){ if (result<1){
while( (int)(result*10) == 0 ){ while( (int)(result*10) == 0 ){
result *= 10; power--; modifier *= 10; result *= 10;
power--;
modifier *= 10;
} }
} }
else{ /* result >= 1 */ else{ /* result >= 1 */
while( (int)result != 0 ){ while( (int)result != 0 ){
result /= 10; power++; modifier /= 10; result /= 10;
power++;
modifier /= 10;
} }
} /* if result<1 */ } /* if result<1 */
if (result > (1-MINIMUM)){ if (result > (1-MINIMUM)){
result = 0.1; power++; modifier /= 10; result = 0.1;
power++;
modifier /= 10;
} }
result *= resultsign; result *= resultsign;
} }
else{ result = 0; power = 0; modifier = 0.1; } else {
result = 0;
power = 0;
modifier = 0.1;
}
} }
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
@ -808,15 +891,18 @@ case SCIENTIFIC_FORMAT, let temppower = 1;
case temppower > 0: print '.' in the middle case temppower > 0: print '.' in the middle
case temppower <= 0: print '.' in the begining case temppower <= 0: print '.' in the begining
----------------------------------------------------------------------- */ ----------------------------------------------------------------------- */
void result2typingbuf(void){ void result2typingbuf(void)
{
bool haveDot = false; bool haveDot = false;
char tempchar = 0; char tempchar = 0;
int k; int k;
double tempresult = ABS(result); /* positive num makes things simple */ double tempresult = ABS(result); /* positive num makes things simple */
int temppower; int temppower;
if(SCIENTIFIC_FORMAT) temppower = 1; /* output x.xxxx format */ if(SCIENTIFIC_FORMAT)
else temppower = power; temppower = 1; /* output x.xxxx format */
else
temppower = power;
double tempmodifier = 1; double tempmodifier = 1;
int count; int count;