|
| 1 | +# Minimal printf and snprintf |
| 2 | + |
| 3 | +Library supports both printf and snprintf in 1252 bytes of flash. |
| 4 | + |
| 5 | +Prints directly to stdio/UART without using malloc. All flags and precision modifiers are ignored. |
| 6 | +Floating point is disabled by default. |
| 7 | +Printing to a FILE stream is enabled by default. |
| 8 | + |
| 9 | +Supports: |
| 10 | +* %d: signed integer [h, hh, (none), l, ll, z, j, t]. |
| 11 | +* %i: signed integer [h, hh, (none), l, ll, z, j, t]. |
| 12 | +* %u: unsigned integer [h, hh, (none), l, ll, z, j, t]. |
| 13 | +* %x: unsigned integer [h, hh, (none), l, ll, z, j, t], printed as hexadecimal number (e.g., ff). |
| 14 | +* %X: unsigned integer [h, hh, (none), l, ll, z, j, t], printed as hexadecimal number (e.g., FF). |
| 15 | +* %f: floating point (disabled by default). |
| 16 | +* %F: floating point (disabled by default, treated as %f). |
| 17 | +* %g: floating point (disabled by default, treated as %f). |
| 18 | +* %G: floating point (disabled by default, treated as %f). |
| 19 | +* %c: character. |
| 20 | +* %s: string. |
| 21 | +* %p: pointer (e.g. 0x00123456). |
| 22 | + |
| 23 | +Unrecognized format specifiers are treated as ordinary characters. |
| 24 | + |
| 25 | +Floating point support: |
| 26 | +* Floating point is disabled by default. |
| 27 | +* All floating points are treated as %f. |
| 28 | +* No support for inf, infinity or nan |
| 29 | + |
| 30 | +To replace the standard implementations of the printf functions with the ones in this library: |
| 31 | + |
| 32 | +* Add the library to your project. |
| 33 | +* Compile with mbed-cli using one of the custom profiles in the `profiles/` subdirectory. For |
| 34 | + example, to compile in release mode: |
| 35 | + |
| 36 | +``` |
| 37 | +$ mbed compile -t <toolchain> -m <target> --profile mbed-printf/profiles/release.json |
| 38 | +``` |
| 39 | + |
| 40 | +## Enabling floating point, FILE stream, 64 bit integers, new line conversion, and setting baud rate |
| 41 | + |
| 42 | +In mbed_app.json: |
| 43 | + |
| 44 | +``` |
| 45 | + "target_overrides": { |
| 46 | + "*": { |
| 47 | + "platform.stdio-baud-rate": 115200, |
| 48 | + "platform.stdio-convert-newlines": false, |
| 49 | + "minimal-printf.enable-file-stream": true, |
| 50 | + "minimal-printf.enable-floating-point": true, |
| 51 | + "minimal-printf.set-floating-point-max-decimals": 6, |
| 52 | + "minimal-printf.enable-64-bit": true |
| 53 | + } |
| 54 | + } |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | +## Size comparison |
| 59 | + |
| 60 | +Example application compiled with minimal mbed OS (no RTOS) using this .mbedignore: |
| 61 | + |
| 62 | +``` |
| 63 | +mbed-os/rtos/* |
| 64 | +mbed-os/features/FEATURE_LWIP/* |
| 65 | +mbed-os/features/FEATURE_CLIENT/* |
| 66 | +mbed-os/features/FEATURE_COMMON_PAL/* |
| 67 | +mbed-os/features/FEATURE_UVISOR/* |
| 68 | +mbed-os/features/frameworks/* |
| 69 | +mbed-os/features/net/* |
| 70 | +mbed-os/features/netsocket/* |
| 71 | +mbed-os/features/storage/* |
| 72 | +mbed-os/events/* |
| 73 | +``` |
| 74 | + |
| 75 | +### Example application |
| 76 | +``` |
| 77 | +#include "mbed.h" |
| 78 | +#include "mbed_printf.h" |
| 79 | +#include <limits.h> |
| 80 | +
|
| 81 | +int main() |
| 82 | +{ |
| 83 | + char buffer[1000]; |
| 84 | + int result; |
| 85 | +
|
| 86 | + double pi = 3.14159265359; |
| 87 | +
|
| 88 | +#if 0 |
| 89 | + result = printf("hello world %d %u %X %p %s %2.5f %% %\r\n", LONG_MAX, ULONG_MAX, UCHAR_MAX, buffer, "muh", pi); |
| 90 | + printf("results: %d\r\n", result); |
| 91 | +
|
| 92 | + result = snprintf(buffer, 1000, "hello world %d %u %X %p %s %2.5f %% %\r\n", LONG_MIN, 0, 0, buffer, "muh", -1*pi); |
| 93 | + printf("%s\r\n", buffer); |
| 94 | +
|
| 95 | + printf("results: %d\r\n", result); |
| 96 | +
|
| 97 | +#else |
| 98 | + result = mbed_printf("hello world %ld %llu %02X %p %s %2.5f %% %\r\n", LONG_MAX, ULONG_MAX, UCHAR_MAX, buffer, "muh", pi); |
| 99 | + mbed_printf("results: %d\r\n", result); |
| 100 | +
|
| 101 | + result = mbed_snprintf(buffer, 1000, "hello world %d %u %X %p %s %2.5f %% %\r\n", LONG_MIN, 0, 0, buffer, "muh", -1*pi); |
| 102 | + mbed_printf("%s\r\n", buffer); |
| 103 | +
|
| 104 | + mbed_printf("results: %d\r\n", result); |
| 105 | +#endif |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### Full application size on K64F/GCC |
| 110 | + |
| 111 | +| | Floating point | 64 bit integers | Flash | RAM | |
| 112 | +| - | - | - | - | - | |
| 113 | +| mbed-printf | | | 7772 | 2752 | |
| 114 | +| mbed-printf | | X | 8708 | 2752 | |
| 115 | +| mbed-printf | X | | 10368 | 2752 | |
| 116 | +| mbed-printf | X | X | 11360 | 2752 | |
| 117 | +| std printf | X | X | 37354 | 5364 | |
0 commit comments