Skip to content

Add printf to print class #780

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

Merged
merged 6 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cores/arduino/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,26 @@ size_t Print::println(const Printable &x)
return n;
}

void Print::printf(const char *format, ...)
{
char buf[PRINTF_BUFFER];
va_list ap;
va_start(ap, format);
vsnprintf(buf, sizeof(buf), format, ap);
write(buf);
va_end(ap);
}

void Print::printf(const __FlashStringHelper *format, ...)
{
char buf[PRINTF_BUFFER];
va_list ap;
va_start(ap, format);
vsnprintf_P(buf, sizeof(buf), (const char *)format, ap);
write(buf);
va_end(ap);
}

// Private Methods /////////////////////////////////////////////////////////////

size_t Print::printNumber(unsigned long n, uint8_t base)
Expand Down
8 changes: 8 additions & 0 deletions cores/arduino/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <inttypes.h>
#include <stdio.h> // for size_t
#include <stdarg.h> // for printf

#include "WString.h"
#include "Printable.h"
Expand All @@ -31,6 +32,10 @@
#define OCT 8
#define BIN 2

#ifndef PRINTF_BUFFER
#define PRINTF_BUFFER 80
#endif

// uncomment next line to support printing of 64 bit ints.
#define SUPPORT_LONGLONG

Expand Down Expand Up @@ -103,6 +108,9 @@ class Print {
void println(uint64_t, uint8_t = DEC);
void print(uint64_t, uint8_t = DEC);
#endif

void printf(const char *format, ...);
void printf(const __FlashStringHelper *format, ...);
};

#endif