EcoModder.com

EcoModder.com (https://ecomodder.com/forum/)
-   OpenGauge / MPGuino FE computer (https://ecomodder.com/forum/opengauge-mpguino-fe-computer.html)
-   -   Improved char* format(..) function (https://ecomodder.com/forum/showthread.php/improved-char-format-function-10898.html)

Sebastian 11-07-2009 04:49 AM

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.

meelis11 11-09-2009 07:18 PM

Hey,
if you define function prototype with default value, then you can call function normally when 2 decimal places is needed:

Quote:

char* format(long num, byte maxdec=2){
//...
}

//calling function
a = format(b); //2 decimal places
a = format(b, 1); //1 decimal places
a = format(b, 0); //0 decimal places = integer

nickdigger 11-10-2009 02:55 AM

Quote:

Originally Posted by Sebastian (Post 138301)
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.

By making fBuff global, you don't need to pass the pointer each time you call format().

I also have a modified format() in the Code Hacks wiki, that you can play with. It might be a little tighter, I dont know. It doesnt support negative numbers, but does have leading blanks instead of zeros, and has flexible decimal places. It might not be easy to find, as it's only a small part of a huge hack.

CoastRider 11-10-2009 09:10 AM

Last night driving home admiring my new device, I was thinking the display could be a tad bigger, now I just realized all I need to do is get rid of the extra numbers!

I haven't got a clue about programming though.


All times are GMT -4. The time now is 01:26 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Content Relevant URLs by vBSEO 3.5.2
All content copyright EcoModder.com