Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Enabling printf style output method with variadic arguments#29

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
aentinger wants to merge1 commit intoarduino:masterfromaentinger:feature/Serial.printf
Closed
Show file tree
Hide file tree
Changes fromall 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
29 changes: 29 additions & 0 deletionsapi/Print.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -144,6 +144,15 @@ size_t Print::print(const Printable& x)
return x.printTo(*this);
}

size_t Print::printf(char const * fmt, ...)
{
va_list args;
va_start(args, fmt);
size_t const n = vprintf(fmt, args);
va_end(args);
return n;
}

size_t Print::println(void)
{
return write("\r\n");
Expand DownExpand Up@@ -233,6 +242,16 @@ size_t Print::println(const Printable& x)
return n;
}

size_t Print::printfln(char const * fmt, ...)
{
va_list args;
va_start(args, fmt);
size_t n = vprintf(fmt, args);
va_end(args);
n += println();
return n;
}

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

size_t Print::printNumber(unsigned long n, uint8_t base)
Expand DownExpand Up@@ -374,3 +393,13 @@ size_t Print::printFloat(double number, uint8_t digits)

return n;
}

size_t Print::vprintf(char const * fmt, va_list args)
{
static size_t const MSG_BUF_SIZE = 64;
char msg_buf[MSG_BUF_SIZE] = {0};

int const length = vsnprintf(msg_buf, MSG_BUF_SIZE, fmt, args);

return write(msg_buf, length);
}
3 changes: 3 additions & 0 deletionsapi/Print.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,6 +36,7 @@ class Print
size_t printNumber(unsigned long, uint8_t);
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, uint8_t);
size_t vprintf(char const * fmt, va_list args);
protected:
void setWriteError(int err = 1) { write_error = err; }
public:
Expand DownExpand Up@@ -67,6 +68,7 @@ class Print
size_t print(unsigned long long, int = DEC);
size_t print(double, int = 2);
size_t print(const Printable&);
size_t printf(char const * fmt, ...);

size_t println(const __FlashStringHelper *);
size_t println(const String &s);
Expand All@@ -82,5 +84,6 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printfln(char const * fmt, ...);
};


[8]ページ先頭

©2009-2025 Movatter.jp