Skip to content

Commit 942200f

Browse files
Update to use reference, not pointer. Improve documentation.
1 parent b3a6db6 commit 942200f

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

ArduinoPrintf.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
#include "ArduinoPrintf.h"
22
#include "Arduino.h"
33

4-
static Print* print_instance = &Serial;
4+
static Print& print_instance = Serial;
55

6-
void printf_init(Print* PrintClass)
6+
void printf_init(Print& PrintClass)
77
{
8-
if(PrintClass)
9-
{
10-
print_instance = PrintClass;
11-
}
8+
print_instance = PrintClass;
129
}
1310

11+
1412
// If you use the default printf() implementation, this function will route the output
1513
// to the Serial class
1614
extern "C" __attribute__((weak)) void _putchar(char character)
1715
{
18-
print_instance->print(character);
16+
print_instance.print(character);
1917
}

ArduinoPrintf.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
#include "Print.h"
55
#include "printf.h"
66

7-
// In Setup(), you must initialize printf with a serial class.
8-
// You must also configure the Serial interface in setup() and call begin().
9-
void printf_init(Print* StreamClass = nullptr);
7+
// In Setup(), you must initialize printf with a Print class if you don't want
8+
// to use the default Serial object. If you want the default behavior, calling this
9+
// function is not necessary.
10+
//
11+
// The caller is responsible for configure the Serial interface in setup() and calling
12+
// Serial.begin().
13+
void printf_init(Print& StreamClass);
1014

1115
#endif //ARDUINO_PRINTF_H_

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ This library adds support for the `printf()` function to Arduino projects. This
44

55
## Using the Library
66

7+
To use this library in your Arduino project, you need to include the header:
8+
9+
```
10+
#include ArduinoPrintf.h
11+
```
12+
713
By default, the library can be used without any special initialization. The `Serial` object is the default output target. You must still initialize the `Serial` object in `setup()`, the library will not do this for you.
814

915
You can specify any class derived from the `Print` base class for use with `printf()`. To change the output class, use the `printf_init` function in `setup()`:
1016

1117
```
12-
printf_init(&Serial1);
18+
printf_init(Serial1);
1319
Serial1.begin(115200);
1420
```
1521

@@ -36,6 +42,10 @@ void _putchar(char character)
3642

3743
And your prototype will be used instead of the library's version.
3844

45+
## Advanced Use
46+
47+
You can include `printf.h` directly and supply your own implementation of `_putchar`. This approach is useful if you want to use the library in a test suite (skipping Arduino SDK headers).
48+
3949
## Disabling Specific Formats
4050

4151
If memory footprint is critical, floating point, exponential and 'long long' support and can be turned off via the `PRINTF_DISABLE_SUPPORT_FLOAT`, `PRINTF_DISABLE_SUPPORT_EXPONENTIAL` and `PRINTF_DISABLE_SUPPORT_LONG_LONG` compiler switches. You must define these symbols in the build system.

examples/specify_print_class/specify_print_class.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
void setup() {
44
// Specify the print class to use with printf().
55
// Any class derived from Print will work.
6-
printf_init(&Serial1);
6+
printf_init(Serial1);
77

88
// But the important detail: you are responsible for initializing the interface!
99
Serial1.begin(115200);

0 commit comments

Comments
 (0)