Go Back   EcoModder Forum > EcoModding > Instrumentation > OpenGauge / MPGuino FE computer
Register Now
 Register Now
 

Reply  Post New Thread
 
Submit Tools LinkBack Thread Tools
Old 11-07-2009, 04:49 AM   #1 (permalink)
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.25C! 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 06:15 AM..
  Reply With Quote
The Following User Says Thank You to Sebastian For This Useful Post:
CoastRider (11-10-2009)
Alt Today
Popular topics

Other popular topics in this forum...

   
Old 11-09-2009, 07:18 PM   #2 (permalink)
EcoModding Apprentice
 
meelis11's Avatar
 
Join Date: Feb 2009
Location: Estonia
Posts: 199

Green frog - '97 Audi A4 Avant 1.9TDI 81kW
Diesel
90 day: 43.1 mpg (US)
Thanks: 19
Thanked 40 Times in 28 Posts
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
  Reply With Quote
Old 11-10-2009, 02:55 AM   #3 (permalink)
EcoModding Apprentice
 
Join Date: Aug 2009
Location: terra firma
Posts: 138
Thanks: 4
Thanked 24 Times in 22 Posts
Quote:
Originally Posted by Sebastian View Post
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.
  Reply With Quote
Old 11-10-2009, 09:10 AM   #4 (permalink)
EcoModding Lurker
 
Join Date: Aug 2009
Location: Central PA
Posts: 90

Draco - '89 Ford F150 ext cab shrt bed XLT Lariat
90 day: 16.45 mpg (US)
Thanks: 12
Thanked 0 Times in 0 Posts
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.

  Reply With Quote
Reply  Post New Thread


Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
AutoSpeed article: spoofing the intake air temp for slightly improved MPG MetroMPG EcoModding Central 14 09-30-2014 01:33 PM
Format for storing values on a memory stick jonoxer OpenGauge / MPGuino FE computer 4 09-26-2009 11:28 PM
1990 Geo Metro hatchback; improved mpg 90metro Success Stories 9 04-07-2009 09:25 PM
DIY - Lowering for Both Form and Function SVOboy DIY / How-to 6 09-19-2008 08:28 AM



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