Skip to content

consider this printf implementation with stdio fopencookie function #4713

Closed

Description

@JAndrassy
ssize_t adapterWrite(void* p, const char *buf, size_t n) {
  return ((Print*) p) -> write(buf, n);
}

FILE* openAdapter(void *p) {
  cookie_io_functions_t fncs; 
  fncs.write = adapterWrite; 
  fncs.close = NULL; 
  return fopencookie(p, "w", fncs); 
}

size_t Print::printf(const char *fmt, ...) {
  va_list args;
  va_start(args, fmt);
  FILE* adapter = openAdapter(this);
  size_t len = vfprintf(adapter, fmt, args);
  fclose(adapter);
  va_end(args);
  return len;
}

size_t Print::printf(const __FlashStringHelper *fmt, ...) {
  size_t fmtLen = strlen_P((PGM_P) fmt);
  char format[fmtLen + 1];
  strcpy_P(format, (PGM_P) fmt);
  va_list args;
  va_start(args, fmt);
  FILE* adapter = openAdapter(this);
  size_t len = vfprintf(adapter, format, args);
  fclose(adapter);
  va_end(args);
  return len;
}

Activity

  1. earlephilhower commented on Jan 16, 2019

    @earlephilhower
    Collaborator

    The suggested code snippet doesn't have any explanation or context. Nothing to do here. If you have a need for this, I suggest making a PR with an example or some explanation. Closing as an issue.

  2. devyte commented on Jan 17, 2019

    @devyte
    Collaborator

    @JAndrassy I also don't understand the intent here. If you provide a more detailed explanation and examples we can discus it.

  3. JAndrassy commented on Jan 17, 2019

    @JAndrassy
    ContributorAuthor

    current implementation of printf in Print in esp8266 core calls vsnprintf twice and uses dynamic allocation of buffer, if the resulting string doesn't fit into 64 bytes. I would guess that a lot of printf uses have the result larger then 64 characters.

    the proposed implementation writes to output the result of formatting directly with write(byte).

    I use this implementation in StreamLib , a library for all architectures. This printf implementation works for xtensa and for ARM. For AVR the implementation with the same concept is slightly different. I use the StreamLib in my project with many different printfs. The project ran months on esp8266. Now it is on M0.

    In AVR core this way of implementation of printf was proposed but not merged yet.

  4. u48 commented on Feb 9, 2019

    @u48

    Hi.

    The solution is correct, it will work fine.

    I have already added a completely similar way of printf() in arduino STM32.
    Works on STM32, ESP8266, DUE, Raspbery.

    In my profile there is a fork Arduino Stm32. In the branch for Stm32F1 there is already a ready source print.с

    Bonus, for Arduino Uno, too, there is a similar solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions