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

Add printf support to the Arduino SDK

License

NotificationsYou must be signed in to change notification settings

embeddedartistry/arduino-printf

Repository files navigation

This library adds support for theprintf() function to Arduino projects. This code leverages theembeddedartistry/printf library (a fork ofeyalroz/printf, which is designed for use in embedded systems. For more information about what is available, please refer to theparent library documentation.

What This Library Provides

This library provides a standalone implementation for the following functions:

  • printf()
  • sprintf() andsnprintf()
  • vprintf() andvsnprintf()

Project Target

This library aims to offer a completeprintf() solution while maintaining low storage and RAM requirements.This iscritical for MCUs with limited storage and RAM. This project is ideal forAVR based MCUs like theArduino Uno and it's siblings.

ESP8266 and ESP32

The Arduino implementations for theESP8266 andESP32 already include aprintf() implementationas part of the base library. You do not need this library for those platforms.

Using the Library

To use this library in your Arduino project, you need to include the header:

#include <LibPrintf.h>void setup() {    Serial.begin(115200);}

By default, the library can be used without any special initialization. Anyprintf() calls will be output usingthe ArduinoSerial interface. If you need to use a different interface, callprintf_init.

If you only want to uses[n]printf, then you do not need to initialize the library.

Advanced Usage

Seeadvanced_usage.md.

Configuration

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

  • PRINTF_DISABLE_ALL
    • Remove allprintf 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
  • PRINTF_FTOA_BUFFER_SIZE (unsigned integer)
    • 'ftoa' conversion buffer size, this must be big enough to hold one converted float number including padded zeros (dynamically created on stack)
    • Default: 32 bytes
  • PRINTF_DISABLE_SUPPORT_FLOAT
    • support for the floating point type (%f)
  • PRINTF_DISABLE_SUPPORT_EXPONENTIAL
    • support for exponential floating point notation (%e/%g)
    • Default: active
  • PRINTF_DEFAULT_FLOAT_PRECISION (unsigned integer)
  • PRINTF_MAX_FLOAT (float value)
    • define the largest float suitable to print with %f
    • Default: active
    • 1e9
  • PRINTF_DISABLE_SUPPORT_LONG_LONG
    • support for the long long types (%llu or %p)
    • Default: active
  • PRINTF_DISABLE_SUPPORT_PTRDIFF_T
    • support for the ptrdiff_t type (%t)
    • Default: active

For AVR chips, the library will automatically setPRINTF_DISABLE_SUPPORT_EXPONENTIAL andPRINTF_DISABLE_SUPPORT_LONG_LONG. You can re-enable these settings by definingPRINTF_PREVENT_DEFAULT_AVR_SETTINGS.

Because these settings control behavior in the source file, they cannot be defined in the sketch. You must adjust the compilation commands for your project in order for the changes to take effect.

If you're using a Makefile or other build system, you'd use the-D flag (e.g.,-DPRINTF_DISABLE_SUPPORT_EXPONENTIAL) to the library build target. For Arduino IDE, the flags need to be added to the compiler.extra_flags property inplatform.txt orplatform.local.txt. You would need to restart the IDE for the changes to take effect.

Here are comparisons for a simple test sketch showing the overall sketch size for different configurations:

TypeBytes
No Serial1606
All options enabled9476
Disable long long and exponential6328
Disable long long, float, and exponential4256

Examples

Multiple examples are provided with this library in theexamples/ folder.

  • Default Usage
    • Without any initialization,Serial will be the default output forprintf()
    • This example initializes theSerial class and prints in a loop
  • Specify Print Class
    • Any class derived from thePrint base class can be used with theArduino Printf library
    • This example initializesprintf withSerial1 instead ofSerial
  • Override Putchar
    • This example overridesputchar_() and adds a space in between every letter
    • You can implement any kind of logic withinputchar_() that you like, such as outputting information to multiple ports

About

Add printf support to the Arduino SDK

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp