1 /////////////////////////////////////////////////////////////////////////////// 2 // \author (c) Marco Paland (info@paland.com) 3 // 2014-2019, PALANDesign Hannover, Germany 4 // 5 // \license The MIT License (MIT) 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 // 25 // \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on 26 // embedded systems with a very limited resources. 27 // Use this instead of bloated standard/newlib printf. 28 // These routines are thread safe and reentrant. 29 // 30 /////////////////////////////////////////////////////////////////////////////// 31 32 #ifndef _PICO_PRINTF_H 33 #define _PICO_PRINTF_H 34 35 /** \file printf.h 36 * \defgroup pico_printf pico_printf 37 * 38 * \brief Compact replacement for printf by Marco Paland (info@paland.com) 39 */ 40 41 #ifdef __cplusplus 42 extern "C" { 43 #endif 44 45 #include "pico.h" 46 #include <stdio.h> 47 #include <stdarg.h> 48 49 // PICO_CONFIG: PICO_PRINTF_ALWAYS_INCLUDED, Whether to always include printf code even if only called weakly (by panic), type=bool, default=1 in debug build 0 otherwise, group=pico_printf 50 #ifndef PICO_PRINTF_ALWAYS_INCLUDED 51 #ifndef NDEBUG 52 #define PICO_PRINTF_ALWAYS_INCLUDED 1 53 #else 54 #define PICO_PRINTF_ALWAYS_INCLUDED 0 55 #endif 56 #endif 57 58 #if LIB_PICO_PRINTF_PICO 59 // weak raw printf may be a puts if printf has not been called, 60 // so that we can support gc of printf when it isn't called 61 // 62 // it is called raw to distinguish it from the regular printf which 63 // is in stdio.c and does mutex protection 64 #if !PICO_PRINTF_ALWAYS_INCLUDED 65 bool __printflike(1, 0) weak_raw_printf(const char *fmt, ...); 66 bool weak_raw_vprintf(const char *fmt, va_list args); 67 #else 68 #define weak_raw_printf(...) ({printf(__VA_ARGS__); true;}) 69 #define weak_raw_vprintf(fmt,va) ({vprintf(fmt,va); true;}) 70 #endif 71 72 /** 73 * \brief printf with output function 74 * You may use this as dynamic alternative to printf() with its fixed _putchar() output 75 * \param out An output function which takes one character and an argument pointer 76 * \param arg An argument pointer for user data passed to output function 77 * \param format A string that specifies the format of the output 78 * \return The number of characters that are sent to the output function, not counting the terminating null character 79 */ 80 int vfctprintf(void (*out)(char character, void *arg), void *arg, const char *format, va_list va); 81 82 #else 83 84 #define weak_raw_printf(...) ({printf(__VA_ARGS__); true;}) 85 #define weak_raw_vprintf(fmt,va) ({vprintf(fmt,va); true;}) 86 87 #endif 88 89 #ifdef __cplusplus 90 } 91 #endif 92 93 #endif // _PRINTF_H_ 94