1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-12 01:53:07 +03:00

Merge remote-tracking branch 'remotes/esp8266/esp8266' into esp8266

This commit is contained in:
Markus Sattler
2015-07-15 13:58:30 +02:00
4 changed files with 39 additions and 18 deletions

View File

@ -149,49 +149,58 @@ char* ultoa(unsigned long value, char* result, int base) {
char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
if(isnan(number)) {
if (isnan(number)) {
strcpy(s, "nan");
return s;
}
if(isinf(number)) {
if (isinf(number)) {
strcpy(s, "inf");
return s;
}
if(number > 4294967040.0 || number < -4294967040.0) {
if (number > 4294967040.0 || number < -4294967040.0) {
strcpy(s, "ovf");
return s;
}
char* out = s;
int signInt_Part = 1;
// Handle negative numbers
if(number < 0.0) {
*out = '-';
++out;
if (number < 0.0) {
signInt_Part = -1;
number = -number;
}
// calc left over digits
if (prec > 0)
{
width -= (prec + 1);
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for(uint8_t i = 0; i < prec; ++i)
for (uint8_t i = 0; i < prec; ++i)
rounding /= 10.0;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long) number;
double remainder = number - (double) int_part;
out += sprintf(out, "%ld", int_part);
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
out += sprintf(out, "%*ld", width, int_part * signInt_Part);
// Print the decimal point, but only if there are digits beyond
if(prec > 0) {
if (prec > 0) {
*out = '.';
++out;
}
for (unsigned char decShift = prec; decShift > 0; decShift--) {
remainder *= 10.0;
for (unsigned char decShift = prec; decShift > 0; decShift--) {
remainder *= 10.0;
}
sprintf(out, "%0*d", prec, (int)remainder);
}
sprintf(out, "%0*d", prec, (int)remainder);
return s;
}

View File

@ -135,6 +135,17 @@ int printf_P(const char* formatP, ...) {
return ret;
}
int sprintf_P(char* str, const char* formatP, ...) {
int ret;
va_list arglist;
va_start(arglist, formatP);
ret = vsnprintf_P(str, SIZE_IRRELEVANT, formatP, arglist);
va_end(arglist);
return ret;
}
int snprintf_P(char* str, size_t strSize, const char* formatP, ...) {
int ret;
va_list arglist;

View File

@ -50,7 +50,8 @@ size_t strnlen_P(const char *s, size_t size);
#define strlen_P(strP) strnlen_P((strP), SIZE_IRRELEVANT)
int printf_P(const char *formatP, ...) __attribute__ ((format (printf, 1, 2)));
int snprintf_P(char *str, size_t strSize, const char *formatP, ...) __attribute__ ((format (printf, 3, 4)));
int sprintf_P(char *str, const char *formatP, ...) __attribute__((format(printf, 2, 3)));
int snprintf_P(char *str, size_t strSize, const char *formatP, ...) __attribute__((format(printf, 3, 4)));
int vsnprintf_P(char *str, size_t strSize, const char *formatP, va_list ap) __attribute__ ((format (printf, 3, 0)));
// flash memory must be read using 32 bit aligned addresses else a processor