Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / common / printf.h
blobd143c5afbe1c14803e27f20680acf03a0457c00a
1 /*
2 File: printf.h
4 Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs
6 All rights reserved.
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
11 Redistributions of source code must retain the above copyright notice, this list
12 of conditions and the following disclaimer.
14 Redistributions in binary form must reproduce the above copyright notice, this
15 list of conditions and the following disclaimer in the documentation and/or other
16 materials provided with the distribution.
18 Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its
19 contributors may be used to endorse or promote products derived from this software
20 without specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
28 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 OF SUCH DAMAGE.
33 ----------------------------------------------------------------------
35 This library is realy just two files: 'printf.h' and 'printf.c'.
37 They provide a simple and small (+200 loc) printf functionality to
38 be used in embedded systems.
40 I've found them so usefull in debugging that I do not bother with a
41 debugger at all.
43 They are distributed in source form, so to use them, just compile them
44 into your project.
46 Two printf variants are provided: printf and sprintf.
48 The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'.
50 Zero padding and field width are also supported.
52 If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the
53 long specifier is also
54 supported. Note that this will pull in some long math routines (pun intended!)
55 and thus make your executable noticably longer.
57 The memory foot print of course depends on the target cpu, compiler and
58 compiler options, but a rough guestimate (based on a H8S target) is about
59 1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space.
60 Not too bad. Your milage may vary. By hacking the source code you can
61 get rid of some hunred bytes, I'm sure, but personally I feel the balance of
62 functionality and flexibility versus code size is close to optimal for
63 many embedded systems.
65 To use the printf you need to supply your own character output function,
66 something like :
68 void putc ( void* p, char c)
70 while (!SERIAL_PORT_EMPTY) ;
71 SERIAL_PORT_TX_REGISTER = c;
74 Before you can call printf you need to initialize it to use your
75 character output function with something like:
77 init_printf(NULL,putc);
79 Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc',
80 the NULL (or any pointer) you pass into the 'init_printf' will eventually be
81 passed to your 'putc' routine. This allows you to pass some storage space (or
82 anything realy) to the character output function, if necessary.
83 This is not often needed but it was implemented like that because it made
84 implementing the sprintf function so neat (look at the source code).
86 The code is re-entrant, except for the 'init_printf' function, so it
87 is safe to call it from interrupts too, although this may result in mixed output.
88 If you rely on re-entrancy, take care that your 'putc' function is re-entrant!
90 The printf and sprintf functions are actually macros that translate to
91 'tfp_printf' and 'tfp_sprintf'. This makes it possible
92 to use them along with 'stdio.h' printf's in a single source file.
93 You just need to undef the names before you include the 'stdio.h'.
94 Note that these are not function like macros, so if you have variables
95 or struct members with these names, things will explode in your face.
96 Without variadic macros this is the best we can do to wrap these
97 functions. If it is a problem just give up the macros and use the
98 functions directly or rename them.
100 For further details see source code.
102 regs Kusti, 23.10.2004
105 #pragma once
107 #include <stdarg.h>
109 typedef void (*putcf) (void *, char);
110 extern putcf stdout_putf;
111 extern void *stdout_putp;
113 void init_printf(void *putp, void (*putf) (void *, char));
115 int tfp_sprintf(char *s, const char *fmt, ...);
116 int tfp_format(void *putp, void (*putf) (void *, char), const char *fmt, va_list va);