First off, I notice you're using a different format utility, it looks like it combines both intformat and format, and takes a single parameter, int., which looks like it could be a decimal place parameter.
I tried pasting my old format routine, but it uses a c primitive, 'byte' which is either not represented in c++ or has been replaced by superior functions. Rather than fix my the old intformat routine by re-creating byte, I'd rather learn to use yours.
Problem is, as a functionally illiterate developer, I have to run through a number of steps to figure out how yours works. From
Code:
char * format(unsigned long num, uint8_t ndp)
it looks like in addition to the long (uh oh, that's going to be a problem for calculating negative safe fuel remaining, but that's easy enough to fix), it takes and unsigned short, essentially being a byte. But, there's the ndp ..... [error, unrecognized], so I'd step through passing it a number of values and parameters until it did what I expected.
Also, since format takes a long, what do you do to format strings? I'll admit I haven't yet looked for strings, I'm just working on hijacking your serial out string to send it as json.
At one point I experimented with three functions jsonBegin(), jsonPair() and jsonEnd(), so that I could simply call the start, randomly pass keys at any point in the code, then finish. It ended up being easier to create the serialSend monstrosity where it's all hard coded. I despise hard coding more than anyone, but to get Frankenstein standing I had to abandon some of my principles.
Old format function:
Code:
//--------------------------------------------------------
char* intformat(unsigned long num, byte numlength=6)
{
static char fBuff[7]; //used by format
//if number doesn't fit, show 9999 (92 bytes)
if(numlength==6 && num > 999999999){
num = 999999000ul;
}
else if(numlength==5 && num > 99999999){
num = 99999000ul;
}
else if(numlength==4 && num > 9999999){
num = 9999000ul;
}
num /= 100;
// Round off the non-printed value.
if((num % 10) > 4) num += 10;
num /= 10;
byte x = numlength;
fBuff[x] = 0;//end string with \0
while(x > 0){
x--;
fBuff[x]= '0' + (num % 10);//poke the ascii character for the digit.
num /= 10;
}
//replace leading 0 with ' '
boolean leadnulls = true;
while(x < numlength-1 && leadnulls){
fBuff[x]=='0' ? fBuff[x]=' ' : leadnulls = false;
x++;
}
return fBuff;
}