Skip to content

Feature request: printf padding/floating point #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
adamgarbo opened this issue Feb 25, 2021 · 4 comments
Closed

Feature request: printf padding/floating point #361

adamgarbo opened this issue Feb 25, 2021 · 4 comments
Milestone

Comments

@adamgarbo
Copy link
Contributor

adamgarbo commented Feb 25, 2021

Hi there,

In v2.x of the Apollo3 Core, Mbed OS is responsible for managing sprintf/printf (#239). However, it appears that padding is no longer enabled. A simple example code snippet to print the date and time:

Serial.printf("20%02d-%02d-%02d %02d:%02d:%02d.%03d\n",
    RTC.year, RTC.month, RTC.dayOfMonth,
    RTC.hour, RTC.minute, RTC.seconds, RTC.hundredths);

Produces the following output:

  • 2020-6-3 13:9:3.1

But should have a number of zeros padding the values:

  • 2020-06-03 13:09:03.100

From what I can gather, this is due to the fact the Mbed core by default uses the minimal printf library to increase memory savings:
https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md#usage

It would be great to have padding reenabled, as well as the ability to print floating-point values. Given the specifications of the Artemis, I believe the additional Flash/RAM requirements should be trivial?

Cheers,
Adam

@jerabaul29
Copy link

I usually include something like this in my projects, can this help?

#ifndef HELPERS
#define HELPERS

#include "Arduino.h"
#include "stdarg.h"

//--------------------------------------------------------------------------------
constexpr size_t serial_printf_max_buffer = 256;

// variadic serial print using printf formatting
void serialPrintf(const char *fmt, ...);


#endif

and the implementation:

#include "helpers.h"

void serialPrintf(const char *fmt, ...) {
  /* Buffer for storing the formatted data */
  char buff[serial_printf_max_buffer];
  /* pointer to the variable arguments list */
  va_list pargs;
  /* Initialise pargs to point to the first optional argument */
  va_start(pargs, fmt);
  /* create the formatted data and store in buff */
  vsnprintf(buff, serial_printf_max_buffer, fmt, pargs);
  va_end(pargs);
  Serial.print(buff);
}

@jerabaul29
Copy link

(slightly adapted from a recipe found some time ago on a public github).

@adamgarbo
Copy link
Contributor Author

Thanks @jerabaul29,

In Mbed OS, it's very simple to enable std-printf or mbed-printf with support for floating-point. For example, mbed_json.app just needs to have the following code added to enable standard printf.

    "target_overrides": {
        "*": {
            "target.printf_lib": "std"
        }
    }

Cheers,
Adam

@Wenn0101 Wenn0101 added this to the v2.1.0 milestone Mar 2, 2021
@Wenn0101
Copy link
Contributor

Wenn0101 commented Mar 2, 2021

We have more than issue tracking this. closing in favor of #278

@Wenn0101 Wenn0101 closed this as completed Mar 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants