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;
enum {basicButtons, sciButtons} buttonGroup;
enum {
basicButtons,
sciButtons
} buttonGroup;
unsigned char* buttonChar[2][5][5] = {
{ { "MR" , "M+" , "2nd" , "CE" , "C" },
{ "7" , "8" , "9" , "/" , "sqr" },
@ -338,41 +341,55 @@ void oneOperand(void);
/* -----------------------------------------------------------------------
Handy funtions
----------------------------------------------------------------------- */
void cleartypingbuf(void){
void cleartypingbuf(void)
{
int k;
for( k=1; k<=(DIGITLEN+1); k++)
typingbuf[k] = 0;
typingbuf[0] = ' ';
typingbufPointer = typingbuf+1;
}
void clearbuf(void){
void clearbuf(void)
{
int k;
for(k=0;k<18;k++) buf[k]=' ';
for(k=0;k<18;k++)
buf[k]=' ';
buf[18] = 0;
}
void clearResult(void){
void clearResult(void)
{
result = 0;
power = 0;
modifier = 0.1;
}
void clearInput(void){
void clearInput(void)
{
calStatus = cal_normal;
clearResult();
cleartypingbuf();
}
void clearOperand(void){
void clearOperand(void)
{
operand = 0;
operandPower = 0;
}
void clearMemTemp(void){
void clearMemTemp(void)
{
memTemp = 0;
memTempPower = 0;
}
void clearOper(void){
void clearOper(void)
{
oper = ' ';
operInputted = false;
}
void clearMem(void){
void clearMem(void)
{
clearInput();
clearMemTemp();
clearOperand();
@ -380,7 +397,8 @@ void clearMem(void){
btn = BUTTON_NONE;
}
void switchOperands(void){
void switchOperands(void)
{
double tempr = operand;
int tempp = operandPower;
operand = result;
@ -392,7 +410,8 @@ void switchOperands(void){
/* -----------------------------------------------------------------------
Initiate calculator
----------------------------------------------------------------------- */
void cal_initial (void){
void cal_initial (void)
{
int i,j,w,h;
rb->lcd_setfont(FONT_SYSFIXED);
rb->lcd_clear_display();
@ -419,8 +438,10 @@ void cal_initial (void){
}
/* initially, invert button "5" */
m = 2; n = 1;
prev_m = m; prev_n = n;
m = 2;
n = 1;
prev_m = m;
prev_n = n;
rb->lcd_invertrect( X_0_POS + n*REC_WIDTH + 1,
Y_1_POS + m*REC_HEIGHT + 1,
REC_WIDTH - 1, REC_HEIGHT - 1);
@ -439,7 +460,8 @@ mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
in it's private case for sqrt.
Thanks BlueChip for his intro text and Dave Straayer for the actual name.
----------------------------------------------------------------------- */
double mySqrt(double square){
double mySqrt(double square)
{
int k = 0;
double temp = 0;
double root= ABS(square+1)/2;
@ -458,52 +480,77 @@ transcendFunc uses CORDIC (COordinate Rotation DIgital Computer) method
transcendFunc can do sin,cos,log,exp
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;
if (tPower < -998) {calStatus = cal_normal; return;}
if (tPower > 8) {calStatus = cal_error; return;}
*ttPower = 0;
calStatus = cal_normal;
int sign = 1;
int n = 50; /* n <=50, tables are all <= 50 */
int j;
double x,y,z,xt,yt,zt;
if( func[0] =='s' || func[0] =='S') sign = SIGN(t);
else { /* if( func[0] =='c' || func[0] =='C') */ sign = 1; }
if (tPower < -998) {
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);
while (tPower > 0){ t *= 10; tPower--; }
while (tPower < 0){ t /= 10; tPower++; }
while (tPower > 0){
t *= 10;
tPower--;
}
while (tPower < 0) {
t /= 10;
tPower++;
}
j = 0;
while (t > j*2*PI) {j++;}
t -= (j-1)*2*PI;
if (PI/2 < t && t < 3*PI/2){
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;
for (j=1;j<n+2;j++){
xt = x - SIGN(z) * y*cordicTable[j-1][0];
yt = y + SIGN(z) * x*cordicTable[j-1][0];
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
----------------------------------------------------------------------- */
void doAdd (double* operandOne, int* powerOne,
double operandTwo, int powerTwo){
double operandTwo, int powerTwo)
{
if ( *powerOne >= powerTwo ){
if (*powerOne - powerTwo <= DIGITLEN+1){
while (powerTwo < *powerOne){
@ -532,7 +579,8 @@ void doAdd (double* operandOne, int* powerOne,
multiple in scientific number format
----------------------------------------------------------------------- */
void doMultiple(double* operandOne, int* powerOne,
double operandTwo, int powerTwo){
double operandTwo, int powerTwo)
{
(*operandOne) *= operandTwo;
(*powerOne) += powerTwo;
}
@ -540,12 +588,14 @@ void doMultiple(double* operandOne, int* powerOne,
/* -----------------------------------------------------------------------
Handles all one operand calculations
----------------------------------------------------------------------- */
void oneOperand(void){
void oneOperand(void)
{
int k = 0;
if (buttonGroup == basicButtons){
switch(CAL_BUTTON){
case btn_sqr:
if (result<0) calStatus = cal_error;
if (result<0)
calStatus = cal_error;
else{
if (power%2 == 1){
result = (mySqrt(result*10))/10;
@ -565,7 +615,8 @@ void oneOperand(void){
break;
case btn_rec:
if (result==0) calStatus = cal_error;
if (result==0)
calStatus = cal_error;
else{
power = -power;
result = 1/result;
@ -586,10 +637,17 @@ void oneOperand(void){
transcendFunc("cos", &result, &power);
break;
case sci_fac:
if (power<0 || power>8 || result<0 ) calStatus = cal_error;
else if(result == 0) {result = 1; power = 0; }
if (power<0 || power>8 || result<0 )
calStatus = cal_error;
else if(result == 0) {
result = 1;
power = 0;
}
else{
while(power > 0) {result *= 10; power--;}
while(power > 0) {
result *= 10;
power--;
}
if ( ( result - (int)result) > MINIMUM )
calStatus = cal_error;
else {
@ -614,7 +672,8 @@ void oneOperand(void){
/* -----------------------------------------------------------------------
Handles all two operands calculations
----------------------------------------------------------------------- */
void twoOperands(void){
void twoOperands(void)
{
switch(oper){
case '-':
doAdd(&operand, &operandPower, -result, power);
@ -629,7 +688,8 @@ void twoOperands(void){
if ( ABS(result) > MINIMUM ){
doMultiple(&operand, &operandPower, 1/result, -power);
}
else calStatus = cal_error;
else
calStatus = cal_error;
break;
default: /* ' ' */
switchOperands(); /* counter switchOperands() below */
@ -646,26 +706,34 @@ void moveButton(void){
switch(btn){
case BUTTON_LEFT:
case BUTTON_LEFT | BUTTON_REPEAT:
if (n == 0) n = 4;
else n--;
if (n == 0)
n = 4;
else
n--;
break;
case BUTTON_RIGHT:
case BUTTON_RIGHT | BUTTON_REPEAT:
if (n == 4) n = 0;
else n++;
if (n == 4)
n = 0;
else
n++;
break;
case BUTTON_UP:
case BUTTON_UP | BUTTON_REPEAT:
if (m == 0) m = 4;
else m--;
if (m == 0)
m = 4;
else
m--;
break;
case BUTTON_DOWN:
case BUTTON_DOWN | BUTTON_REPEAT:
if (m == 4) m = 0;
else m++;
if (m == 4)
m = 0;
else
m++;
break;
}
@ -685,14 +753,15 @@ void moveButton(void){
Y_1_POS + m*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
int group = {basicButtons, sciButtons}
----------------------------------------------------------------------- */
void printButtonGroups(int group){
void printButtonGroups(int group)
{
int i,j,w,h;
for (i = 0; i < 5; i++){
for (j = 3; j <= 4; j++){
@ -725,7 +794,8 @@ void printButtonGroups(int group){
/* -----------------------------------------------------------------------
flash the button pressed
----------------------------------------------------------------------- */
void flashButton(int b){
void flashButton(int b)
{
int i = b/5; int j = b - i*5;
int 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]
----------------------------------------------------------------------- */
void deleteAnimation(int pos){
void deleteAnimation(int pos)
{
int k;
if (pos<1 || pos >18) return;
if (pos<1 || pos >18)
return;
pos--;
rb->lcd_fillrect(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 1, let it be 0.1 and power++;
----------------------------------------------------------------------- */
void formatResult(void){
void formatResult(void)
{
int resultsign = SIGN(result);
result = ABS(result);
if(result > MINIMUM ){ /* doesn't check power, might have problem
@ -785,21 +858,31 @@ void formatResult(void){
if (result<1){
while( (int)(result*10) == 0 ){
result *= 10; power--; modifier *= 10;
result *= 10;
power--;
modifier *= 10;
}
}
else{ /* result >= 1 */
while( (int)result != 0 ){
result /= 10; power++; modifier /= 10;
result /= 10;
power++;
modifier /= 10;
}
} /* if result<1 */
if (result > (1-MINIMUM)){
result = 0.1; power++; modifier /= 10;
result = 0.1;
power++;
modifier /= 10;
}
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 begining
----------------------------------------------------------------------- */
void result2typingbuf(void){
void result2typingbuf(void)
{
bool haveDot = false;
char tempchar = 0;
int k;
double tempresult = ABS(result); /* positive num makes things simple */
int temppower;
if(SCIENTIFIC_FORMAT) temppower = 1; /* output x.xxxx format */
else temppower = power;
if(SCIENTIFIC_FORMAT)
temppower = 1; /* output x.xxxx format */
else
temppower = power;
double tempmodifier = 1;
int count;