Skip to content

Commit

Permalink
Add ability to remove all printf-related functions from a program.
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
phillipjohnston committed Jun 7, 2022
1 parent c830d06 commit 29b0042
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ See [advanced_usage.md](advanced_usage.md).

If memory footprint is critical, you can disable library features using compiler definitions. Available controls are:

* `PRINTF_DISABLE_ALL`
- Remove all `printf` calls from the program
* `PRINTF_NTOA_BUFFER_SIZE` (unsigned integer)
* 'ntoa' conversion buffer size, this must be big enough to hold one converted numeric number including padded zeros (dynamically created on stack)
* Default: 32 bytes
Expand Down
4 changes: 4 additions & 0 deletions src/LibPrintf.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "LibPrintf.h"

#ifndef PRINTF_DISABLE_ALL
#include "Arduino.h"

#ifdef __AVR__
Expand All @@ -25,3 +27,5 @@ extern "C" __attribute__((weak)) void putchar_(char character)
{
print_instance->print(character);
}

#endif // PRINTF_DISABLE_ALL
16 changes: 15 additions & 1 deletion src/LibPrintf.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
#ifndef ARDUINO_PRINTF_H_
#define ARDUINO_PRINTF_H_

#ifdef PRINTF_DISABLE_ALL

#define printf(...)
#define sprintf(...)
#define vsprintf(...)
#define snprintf(...)
#define vsnprintf(...)
#define vprintf(...)

#define printf_init(...)

#else // printf is enabled

#include "Print.h"
#include "../extras/printf/printf.h"

Expand All @@ -24,6 +37,7 @@
void printf_init(arduino::Print& StreamClass);
#else
void printf_init(Print& StreamClass);
#endif
#endif // __AVR_ATmega4809__

#endif // PRINTF_DISABLE_ALL
#endif //ARDUINO_PRINTF_H_

0 comments on commit 29b0042

Please sign in to comment.