Skip to content

Commit

Permalink
Merge pull request #112 from MCUdude/master
Browse files Browse the repository at this point in the history
Add printf to print class
  • Loading branch information
SpenceKonde authored Nov 15, 2019
2 parents 3d04936 + 966282a commit 528b809
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
29 changes: 29 additions & 0 deletions megaavr/cores/arduino/api/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,35 @@ size_t Print::println(const Printable& x)
return n;
}

// Custom implementation of printf borrowed from the teensy core files
static int16_t printf_putchar(char c, FILE *fp)
{
((class Print *)(fdev_get_udata(fp)))->write((uint8_t)c);
return 0;
}

int16_t Print::printf(const char *format, ...)
{
FILE f;
va_list ap;

fdev_setup_stream(&f, printf_putchar, NULL, _FDEV_SETUP_WRITE);
fdev_set_udata(&f, this);
va_start(ap, format);
return vfprintf(&f, format, ap);
}

int16_t Print::printf(const __FlashStringHelper *format, ...)
{
FILE f;
va_list ap;

fdev_setup_stream(&f, printf_putchar, NULL, _FDEV_SETUP_WRITE);
fdev_set_udata(&f, this);
va_start(ap, format);
return vfprintf_P(&f, (const char *)format, ap);
}

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

size_t Print::printNumber(unsigned long n, uint8_t base)
Expand Down
3 changes: 3 additions & 0 deletions megaavr/cores/arduino/api/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,8 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);

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

0 comments on commit 528b809

Please sign in to comment.