diff options
Diffstat (limited to 'tmk_core/common/chibios/printf.c')
-rw-r--r-- | tmk_core/common/chibios/printf.c | 233 |
1 files changed, 0 insertions, 233 deletions
diff --git a/tmk_core/common/chibios/printf.c b/tmk_core/common/chibios/printf.c deleted file mode 100644 index a99752bb3..000000000 --- a/tmk_core/common/chibios/printf.c +++ /dev/null | |||
@@ -1,233 +0,0 @@ | |||
1 | /* | ||
2 | * found at: http://www.sparetimelabs.com/tinyprintf/tinyprintf.php | ||
3 | * and: http://www.sparetimelabs.com/printfrevisited/printfrevisited.php | ||
4 | */ | ||
5 | |||
6 | /* | ||
7 | File: printf.c | ||
8 | |||
9 | Copyright (C) 2004 Kustaa Nyholm | ||
10 | |||
11 | This library is free software; you can redistribute it and/or | ||
12 | modify it under the terms of the GNU Lesser General Public | ||
13 | License as published by the Free Software Foundation; either | ||
14 | version 2.1 of the License, or (at your option) any later version. | ||
15 | |||
16 | This library is distributed in the hope that it will be useful, | ||
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
19 | Lesser General Public License for more details. | ||
20 | |||
21 | You should have received a copy of the GNU Lesser General Public | ||
22 | License along with this library; if not, write to the Free Software | ||
23 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
24 | |||
25 | */ | ||
26 | |||
27 | #include "printf.h" | ||
28 | |||
29 | typedef void (*putcf)(void*, char); | ||
30 | static putcf stdout_putf; | ||
31 | static void* stdout_putp; | ||
32 | |||
33 | // this adds cca 400 bytes | ||
34 | #define PRINTF_LONG_SUPPORT | ||
35 | |||
36 | #ifdef PRINTF_LONG_SUPPORT | ||
37 | |||
38 | static void uli2a(unsigned long int num, unsigned int base, int uc, char* bf) { | ||
39 | int n = 0; | ||
40 | unsigned int d = 1; | ||
41 | while (num / d >= base) d *= base; | ||
42 | while (d != 0) { | ||
43 | int dgt = num / d; | ||
44 | num %= d; | ||
45 | d /= base; | ||
46 | if (n || dgt > 0 || d == 0) { | ||
47 | *bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10); | ||
48 | ++n; | ||
49 | } | ||
50 | } | ||
51 | *bf = 0; | ||
52 | } | ||
53 | |||
54 | static void li2a(long num, char* bf) { | ||
55 | if (num < 0) { | ||
56 | num = -num; | ||
57 | *bf++ = '-'; | ||
58 | } | ||
59 | uli2a(num, 10, 0, bf); | ||
60 | } | ||
61 | |||
62 | #endif | ||
63 | |||
64 | static void ui2a(unsigned int num, unsigned int base, int uc, char* bf) { | ||
65 | int n = 0; | ||
66 | unsigned int d = 1; | ||
67 | while (num / d >= base) d *= base; | ||
68 | while (d != 0) { | ||
69 | int dgt = num / d; | ||
70 | num %= d; | ||
71 | d /= base; | ||
72 | if (n || dgt > 0 || d == 0) { | ||
73 | *bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10); | ||
74 | ++n; | ||
75 | } | ||
76 | } | ||
77 | *bf = 0; | ||
78 | } | ||
79 | |||
80 | static void i2a(int num, char* bf) { | ||
81 | if (num < 0) { | ||
82 | num = -num; | ||
83 | *bf++ = '-'; | ||
84 | } | ||
85 | ui2a(num, 10, 0, bf); | ||
86 | } | ||
87 | |||
88 | static int a2d(char ch) { | ||
89 | if (ch >= '0' && ch <= '9') | ||
90 | return ch - '0'; | ||
91 | else if (ch >= 'a' && ch <= 'f') | ||
92 | return ch - 'a' + 10; | ||
93 | else if (ch >= 'A' && ch <= 'F') | ||
94 | return ch - 'A' + 10; | ||
95 | else | ||
96 | return -1; | ||
97 | } | ||
98 | |||
99 | static char a2i(char ch, const char** src, int base, int* nump) { | ||
100 | const char* p = *src; | ||
101 | int num = 0; | ||
102 | int digit; | ||
103 | while ((digit = a2d(ch)) >= 0) { | ||
104 | if (digit > base) break; | ||
105 | num = num * base + digit; | ||
106 | ch = *p++; | ||
107 | } | ||
108 | *src = p; | ||
109 | *nump = num; | ||
110 | return ch; | ||
111 | } | ||
112 | |||
113 | static void putchw(void* putp, putcf putf, int n, char z, char* bf) { | ||
114 | char fc = z ? '0' : ' '; | ||
115 | char ch; | ||
116 | char* p = bf; | ||
117 | while (*p++ && n > 0) n--; | ||
118 | while (n-- > 0) putf(putp, fc); | ||
119 | while ((ch = *bf++)) putf(putp, ch); | ||
120 | } | ||
121 | |||
122 | void tfp_format(void* putp, putcf putf, const char* fmt, va_list va) { | ||
123 | // This used to handle max of 12, but binary support jumps this to at least 32 | ||
124 | char bf[36]; | ||
125 | |||
126 | char ch; | ||
127 | |||
128 | while ((ch = *(fmt++))) { | ||
129 | if (ch != '%') | ||
130 | putf(putp, ch); | ||
131 | else { | ||
132 | char lz = 0; | ||
133 | #ifdef PRINTF_LONG_SUPPORT | ||
134 | char lng = 0; | ||
135 | #endif | ||
136 | int w = 0; | ||
137 | ch = *(fmt++); | ||
138 | if (ch == '0') { | ||
139 | ch = *(fmt++); | ||
140 | lz = 1; | ||
141 | } | ||
142 | if (ch >= '0' && ch <= '9') { | ||
143 | ch = a2i(ch, &fmt, 10, &w); | ||
144 | } | ||
145 | #ifdef PRINTF_LONG_SUPPORT | ||
146 | if (ch == 'l') { | ||
147 | ch = *(fmt++); | ||
148 | lng = 1; | ||
149 | } | ||
150 | #endif | ||
151 | switch (ch) { | ||
152 | case 0: | ||
153 | goto abort; | ||
154 | case 'u': { | ||
155 | #ifdef PRINTF_LONG_SUPPORT | ||
156 | if (lng) | ||
157 | uli2a(va_arg(va, unsigned long int), 10, 0, bf); | ||
158 | else | ||
159 | #endif | ||
160 | ui2a(va_arg(va, unsigned int), 10, 0, bf); | ||
161 | putchw(putp, putf, w, lz, bf); | ||
162 | break; | ||
163 | } | ||
164 | case 'd': { | ||
165 | #ifdef PRINTF_LONG_SUPPORT | ||
166 | if (lng) | ||
167 | li2a(va_arg(va, unsigned long int), bf); | ||
168 | else | ||
169 | #endif | ||
170 | i2a(va_arg(va, int), bf); | ||
171 | putchw(putp, putf, w, lz, bf); | ||
172 | break; | ||
173 | } | ||
174 | case 'x': | ||
175 | case 'X': | ||
176 | #ifdef PRINTF_LONG_SUPPORT | ||
177 | if (lng) | ||
178 | uli2a(va_arg(va, unsigned long int), 16, (ch == 'X'), bf); | ||
179 | else | ||
180 | #endif | ||
181 | ui2a(va_arg(va, unsigned int), 16, (ch == 'X'), bf); | ||
182 | putchw(putp, putf, w, lz, bf); | ||
183 | break; | ||
184 | case 'c': | ||
185 | putf(putp, (char)(va_arg(va, int))); | ||
186 | break; | ||
187 | case 's': | ||
188 | putchw(putp, putf, w, 0, va_arg(va, char*)); | ||
189 | break; | ||
190 | case 'b': | ||
191 | #ifdef PRINTF_LONG_SUPPORT | ||
192 | if (lng) | ||
193 | uli2a(va_arg(va, unsigned long int), 2, 0, bf); | ||
194 | else | ||
195 | #endif | ||
196 | ui2a(va_arg(va, unsigned int), 2, 0, bf); | ||
197 | putchw(putp, putf, w, lz, bf); | ||
198 | break; | ||
199 | case '%': | ||
200 | putf(putp, ch); | ||
201 | default: | ||
202 | break; | ||
203 | } | ||
204 | } | ||
205 | } | ||
206 | abort:; | ||
207 | } | ||
208 | |||
209 | void init_printf(void* putp, void (*putf)(void*, char)) { | ||
210 | stdout_putf = putf; | ||
211 | stdout_putp = putp; | ||
212 | } | ||
213 | |||
214 | int tfp_printf(const char* fmt, ...) { | ||
215 | va_list va; | ||
216 | va_start(va, fmt); | ||
217 | tfp_format(stdout_putp, stdout_putf, fmt, va); | ||
218 | va_end(va); | ||
219 | |||
220 | return 1; | ||
221 | } | ||
222 | |||
223 | static void putcp(void* p, char c) { *(*((char**)p))++ = c; } | ||
224 | |||
225 | int tfp_sprintf(char* s, const char* fmt, ...) { | ||
226 | va_list va; | ||
227 | va_start(va, fmt); | ||
228 | tfp_format(&s, putcp, fmt, va); | ||
229 | putcp(&s, 0); | ||
230 | va_end(va); | ||
231 | |||
232 | return 1; | ||
233 | } | ||