-
Notifications
You must be signed in to change notification settings - Fork 0
/
miniprintf.h
123 lines (87 loc) · 3.36 KB
/
miniprintf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* Minimal printf() facility for MCUs
* Warren W. Gay VE3WWG, Sun Feb 12 2017
*
* This work is placed into the public domain. No warranty, or guarantee
* is expressed or implied. When uou use this source code, you do so
* with full responsibility.
*/
#ifndef MINIPRINTF_H
#define MINIPRINTF_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
int mini_vprintf_cooked(void (*putc)(char),const char *format,va_list args);
int mini_vprintf_uncooked(void (*putc)(char),const char *format,va_list args);
int mini_snprintf(char *buf,unsigned maxbuf,const char *format,...)
__attribute((format(printf,3,4)));
#ifdef __cplusplus
}
#endif
#endif // MINIPRINTF_H
#if 0
//////////////////////////////////////////////////////////////////////
// From the README file:
//////////////////////////////////////////////////////////////////////
Tested and estimated to require about 640 bytes of code for STM32F103C8T6. Should
be usable on any MCU platform that supports:
#include <stdarg.h>
#include <string.h>
SUPPORT:
Each format item %+0wd, %0wx and for strings %-ws, the following
applies:
+ Optional: Indicates sign should always print (d and x)
- Optional: Indicates field should be left justified (s)
0 Optional: Pad with leading zeros (d and x)
w Optional: Decimal field width
Formats %c, %d, %x and %s are supported (only). '%%' prints as '%'.
Floating point is not supported, keeping this library minimal.
FORMAT EXAMPLES:
%+05d '+00009' int is 9.
%d '9'
%03d '009'
%04x '001F' int is 31
%x '1F'
%-9s 'abc ' string was 'abc'
%9s ' abc'
%s 'abc'
STRING FORMATTING:
int mini_snprintf(char *buf,unsigned maxbuf,const char *format,...);
See standard snprintf(3). Note that the output is null terminated
when the buffer size permits.
DEVICE FORMATTING HOWTO:
int mini_vprintf_cooked(void (*putc)(char),const char *format,va_list args);
int mini_vprintf_uncooked(void (*putc)(char),const char *format,va_list args);
(0) Decide: cooked or uncooked output?
COOKED means that a CR is sent after every LF is sent out,
like UNIX terminal output.
UNCOOKED means no CR processing is performed. Like snprintf,
what you format is what you get.
(1) Declare your own putc function, something like:
static void uart_putc(char ch) {
usart_send_blocking(USART1,ch); // libopencm3
}
(2) Declare your own printf function:
int uart_printf(const char *format,...)
__attribute((format(printf,1,2)));
int uart_printf(const char *format,...) {
va_list args;
int rc;
va_start(args,format);
rc = mini_vprintf_cooked(uart_putc,format,args);
va_end(args);
return rc;
}
The attribute clause is optional, but when provided can only
appear in the function prototype. It allows the compiler to
check that you have appropriate arguments for each format item.
(3) Use it:
int flea_count = 45;
uart_printf("My dog has %d fleas.\n",flea_count);
NOTES:
1. Stack usage is minimal (perhaps 256 bytes).
2. No malloc/realloc/free calls (no heap usage)
3. Re-entrant (no static storage used)
4. Compromizes favoured smaller code over speed.
#endif
/* End miniprintf.h */