View Single Post
Old 11-07-2009, 05:49 AM   #1 (permalink)
Sebastian
EcoModding Lurker
 
Join Date: Oct 2009
Location: Austria
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Improved char* format(..) function

I modified the MPGuino for motorcycle use, and I also added a temperature sensor. Next thing I recognised: format() function doesn't support negative values ^^ Of course, they are not needed in original MPGuino.
If somebody also wants to add meters etc that can return negative numbers, here is my improved one. Beyond support of negatives, I changed:
.) no leading 0 anymore. I don't like the "005.99" Display, so now it shows " 5.99".
.) a second byte parameter gives you control over the amound of post decimal positions. Or, to be more precise, the maxmimum amount you want to be displayed. I don't think it's useful to show the value of a temperatur sensor in "00.00" if it has a precision of +-0.25°C! Also, speed with 2 post decimal positions seems wired to me.
Of course you have to modifiy the screen functions to use this. You also may want to create a format function without "maxdec". As I know, Arduino supports overloading funcions, so that shouldn't be a problem.

Code:
char* format(long num, byte maxdec){ //gets the *1000 value and max post decimal positions
  //num = 9125;
  fBuff[0] = 0; //start with defined condition
  fBuff[1] = 0;
  fBuff[2] = 0;
  fBuff[3] = 0;
  fBuff[4] = 0;
  fBuff[5] = 0;
  fBuff[6] = 0;
  
  if (num >= 0){ //is num positive?
    byte dp = 3;

    while(num > 999999){
      num /= 10;
      dp++;
      if( dp == 5 ) break; // We'll lose the top numbers like an odometer
    }
    if(dp == 5){
      dp = 99; // We don't need a decimal point here.
    }else{ //see if we have to cut decimals because of maxdec
       while(5 - dp > maxdec){
         num /= 10;
         dp++;
       }
       if (dp == 5){
         dp = 99;
       }
    }

  // Round off the non-printed value.
    if((num % 10) > 4)
      num += 10;
    num /= 10;
  
    byte x = 6;
    while(x > 0){
      x--;
      if(x==dp){ //time to poke in the decimal point?
        fBuff[x]='.';
      }else if(num == 0 && (fBuff[x+1] < 58 && fBuff[x+1] > 47 || fBuff[x+1] == ' ')){ //have we reached end of number?
        fBuff[x]= ' ';
      }else{
        fBuff[x]= '0' + (num % 10);//poke the ascii character for the digit.
        num /= 10;
      } 
    }
  }else{ //num is negative!
    byte dp = 4; //we loose the last digit because leading "-"
    num /= 10; //shift to dp=4
    boolean minussign = false;

    while(num < -99999){
      num /= 10;
      dp++;
      if( dp == 5 ) break; // We'll lose the top numbers like an odometer
    }
    if(dp == 5){
      dp = 99; // We don't need a decimal point here.
    }else{ //see if we have to cut decimals because of maxdec
       while(5 - dp > maxdec){
         num /= 10;
         dp++;
       }
       if (dp == 5){
         dp = 99;
       }
    }

  // Round off the non-printed value.
    if((num % 10) < -4) //% returnes - values!!
      num -= 10;
    num /= 10;
  
    byte x = 6;
    while(x > 1){
      x--;
      if(x==dp){ //time to poke in the decimal point?
        fBuff[x]='.';
      }else if (num == 0 && fBuff[x+1] != '.'){ //have we reached end of number?
        if (minussign == false){
          fBuff[x]= '-';
          minussign = true;
        }else{
          fBuff[x]= ' ';
        }
      }else{
        fBuff[x]= '0' - (num % 10);//poke the ascii character for the digit.
        num /= 10;
      } 
    }
    if (minussign == false){ //no sign until yet?
      fBuff[0]= '-';
    }
  }
  //fBuff[6] = 0; //end string identifier
  return fBuff;
}
PS: Yes, maybe it could be shorter. But it works

Edit: little code change, there was a bug in it. Someone knows why fBuff has to be initialised as a global variable? I tried to define it in format(), and it messed up the display.


Last edited by Sebastian; 11-07-2009 at 07:15 AM..
  Reply With Quote
The Following User Says Thank You to Sebastian For This Useful Post:
CoastRider (11-10-2009)