diff --git a/libraries/AP_HAL/Util.cpp b/libraries/AP_HAL/Util.cpp index bdd18400de..2f9d25f6ee 100644 --- a/libraries/AP_HAL/Util.cpp +++ b/libraries/AP_HAL/Util.cpp @@ -24,7 +24,7 @@ int AP_HAL::Util::snprintf(char* str, size_t size, const char *format, ...) { va_list ap; va_start(ap, format); - int res = this->vsnprintf(str, size, format, ap); + int res = vsnprintf(str, size, format, ap); va_end(ap); return res; } @@ -33,7 +33,7 @@ int AP_HAL::Util::snprintf_P(char* str, size_t size, const prog_char_t *format, { va_list ap; va_start(ap, format); - int res = this->vsnprintf_P(str, size, format, ap); + int res = vsnprintf_P(str, size, format, ap); va_end(ap); return res; } @@ -44,8 +44,9 @@ int AP_HAL::Util::vsnprintf(char* str, size_t size, const char *format, va_list BufferPrinter buf(str, size); print_vprintf(&buf, 0, format, ap); // null terminate if possible + int ret = buf._offs; buf.write(0); - return (int) buf._offs; + return ret; } int AP_HAL::Util::vsnprintf_P(char* str, size_t size, const prog_char_t *format, @@ -53,7 +54,8 @@ int AP_HAL::Util::vsnprintf_P(char* str, size_t size, const prog_char_t *format, { BufferPrinter buf(str, size); print_vprintf(&buf, 1,(const char*) format, ap); + int ret = buf._offs; // null terminate if possible buf.write(0); - return (int) buf._offs; + return ret; } diff --git a/libraries/AP_HAL/examples/Printf/Printf.pde b/libraries/AP_HAL/examples/Printf/Printf.pde index 7d3c0749e4..be5e6c1f97 100644 --- a/libraries/AP_HAL/examples/Printf/Printf.pde +++ b/libraries/AP_HAL/examples/Printf/Printf.pde @@ -35,17 +35,23 @@ static const struct { { "%f", 10.6f, "10.60000" }, { "%f", 1020.4f, "1020.400" }, { "%f", 1030.6f, "1030.600" }, - { "%f", 10304052.6f, "1.0304053e+07" }, - { "%f", 103040501.6f, "1.0304050e+08" }, - { "%f", 1030405023.6f, "1.0304050e+09" }, + { "%f", 10.123456f, "10.12346" }, + { "%f", 102.123456f, "102.1235" }, + { "%f", 1020.123456f, "1020.123" }, + { "%.6f", 10.123456f, "10.12346" }, + { "%.6f", 102.123456f, "102.1235" }, + { "%.6f", 1020.123456f, "1020.123" }, + { "%f", 10304052.6f, "1.030405e+07" }, + { "%f", 103040501.6f, "1.030405e+08" }, + { "%f", 1030405023.6f, "1.030405e+09" }, { "%f", -1030.6f, "-1030.600" }, - { "%f", -10304052.6f, "-1.0304053e+07" }, - { "%f", -103040501.6f, "-1.0304050e+08" }, - { "%f", -1030405023.6f, "-1.0304050e+09" }, - { "%e", 103040501.6f, "1.0304050e+08" }, - { "%g", 103040501.6f, "1.030405e+08" }, - { "%e", -103040501.6f, "-1.0304050e+08" }, - { "%g", -103040501.6f, "-1.030405e+08" }, + { "%f", -10304052.6f, "-1.030405e+07" }, + { "%f", -103040501.6f, "-1.030405e+08" }, + { "%f", -1030405023.6f, "-1.030405e+09" }, + { "%e", 103040501.6f, "1.030405e+08" }, + { "%g", 103040501.6f, "1.03041e+08" }, + { "%e", -103040501.6f, "-1.030405e+08" }, + { "%g", -103040501.6f, "-1.03041e+08" }, { "%.0f", 10.4f, "10" }, { "%.0f", 10.6f, "11" }, { "%.1f", 10.4f, "10.4" }, @@ -59,7 +65,7 @@ static void test_printf(void) uint8_t failures = 0; hal.console->printf("Running printf tests\n"); for (i=0; isnprintf(buf, sizeof(buf), float_tests[i].fmt, float_tests[i].v); + int ret = hal.util->snprintf(buf, sizeof(buf), float_tests[i].fmt, float_tests[i].v); if (strcmp(buf, float_tests[i].result) != 0) { hal.console->printf("Failed float_tests[%u] '%s' -> '%s' should be '%s'\n", (unsigned)i, @@ -68,6 +74,14 @@ static void test_printf(void) float_tests[i].result); failures++; } + if (ret != strlen(float_tests[i].result)) { + hal.console->printf("Failed float_tests[%u] ret=%d/%d '%s' should be '%s'\n", + (unsigned)i, + ret, (int)strlen(float_tests[i].result), + float_tests[i].fmt, + float_tests[i].result); + failures++; + } } hal.console->printf("%u failures\n", (unsigned)failures); } diff --git a/libraries/AP_HAL/utility/print_vprintf.cpp b/libraries/AP_HAL/utility/print_vprintf.cpp index 79f0603bd5..6cae4141e3 100644 --- a/libraries/AP_HAL/utility/print_vprintf.cpp +++ b/libraries/AP_HAL/utility/print_vprintf.cpp @@ -173,7 +173,7 @@ void print_vprintf (AP_HAL::Print *s, unsigned char in_progmem, const char *fmt, flt_oper: float value = va_arg(ap,double); if (!(flags & FL_PREC)) - prec = 7; + prec = 6; flags &= ~(FL_FLTEXP | FL_FLTFIX); if (c == 'e') { flags |= FL_FLTEXP; @@ -284,7 +284,7 @@ void print_vprintf (AP_HAL::Print *s, unsigned char in_progmem, const char *fmt, s->write('.'); flags = (n <= exp && n > exp - ndigs) ? buf[exp - n + 1] : '0'; - if (--n < -prec) + if (--n < -prec || flags == 0) break; s->write(flags); } while (1); @@ -294,7 +294,7 @@ void print_vprintf (AP_HAL::Print *s, unsigned char in_progmem, const char *fmt, { flags = '1'; } - s->write(flags); + if (flags) s->write(flags); } else { /* 'e(E)' format */