]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Debug/print/print.c
522f6f4062127c6e593d2ff4bb08a2aeff3d22f9
[kiibohd-controller.git] / Debug / print / print.c
1 /* Copyright (C) 2011-2014 by Jacob Alexander
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 // ----- Includes -----
23
24 // Compiler Includes
25 #include <stdarg.h>
26
27 // Project Includes
28 #include "print.h"
29
30
31
32 // ----- Functions -----
33
34 // Multiple string Output
35 void printstrs( char* first, ... )
36 {
37         // Initialize the variadic function parameter list
38         va_list ap;
39
40         // Get the first parameter
41         va_start( ap, first );
42         char *cur = first;
43
44         // Loop through the variadic list until "\0\0\0" is found
45         while ( !( cur[0] == '\0' && cur[1] == '\0' && cur[2] == '\0' ) )
46         {
47                 // Print out the given string
48                 Output_putstr( cur );
49
50                 // Get the next argument ready
51                 cur = va_arg( ap, char* );
52         }
53
54         va_end( ap ); // Not required, but good practice
55 }
56
57 // Print a constant string
58 void _print( const char* s )
59 {
60 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
61         // Pull string out of flash
62         char c;
63         while ( ( c = pgm_read_byte( s++ ) ) != '\0' )
64         {
65                 Output_putchar( c );
66         }
67 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
68         Output_putstr( (char*)s );
69 #endif
70 }
71
72
73
74 // Number Printing Functions
75 void printInt8( uint8_t in )
76 {
77         // Max number of characters is 3 + 1 for null
78         char tmpStr[4];
79
80         // Convert number
81         int8ToStr( in, tmpStr );
82
83         // Print number
84         dPrintStr( tmpStr );
85 }
86
87 void printInt16( uint16_t in )
88 {
89         // Max number of characters is 5 + 1 for null
90         char tmpStr[6];
91
92         // Convert number
93         int16ToStr( in, tmpStr );
94
95         // Print number
96         dPrintStr( tmpStr );
97 }
98
99 void printInt32( uint32_t in )
100 {
101         // Max number of characters is 10 + 1 for null
102         char tmpStr[11];
103
104         // Convert number
105         int32ToStr( in, tmpStr );
106
107         // Print number
108         dPrintStr( tmpStr );
109 }
110
111 void printHex_op( uint16_t in, uint8_t op )
112 {
113         // With an op of 1, the max number of characters is 6 + 1 for null
114         // e.g. "0xFFFF\0"
115         // op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
116         char tmpStr[7];
117
118         // Convert number
119         hexToStr_op( in, tmpStr, op );
120
121         // Print number
122         dPrintStr( tmpStr );
123 }
124
125 void printHex32_op( uint32_t in, uint8_t op )
126 {
127         // With an op of 1, the max number of characters is 6 + 1 for null
128         // e.g. "0xFFFF\0"
129         // op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
130         char tmpStr[7];
131
132         // Convert number
133         hex32ToStr_op( in, tmpStr, op );
134
135         // Print number
136         dPrintStr( tmpStr );
137 }
138
139
140
141 // String Functions
142 void int8ToStr( uint8_t in, char* out )
143 {
144         // Position and sign containers
145         uint8_t pos;
146         pos = 0;
147
148         // Evaluate through digits as decimal
149         do
150         {
151                 out[pos++] = in % 10 + '0';
152         }
153         while ( (in /= 10) > 0 );
154
155         // Append null
156         out[pos] = '\0';
157
158         // Reverse the string to the correct order
159         revsStr(out);
160 }
161
162
163 void int16ToStr( uint16_t in, char* out )
164 {
165         // Position and sign containers
166         uint16_t pos;
167         pos = 0;
168
169         // Evaluate through digits as decimal
170         do
171         {
172                 out[pos++] = in % 10 + '0';
173         }
174         while ( (in /= 10) > 0 );
175
176         // Append null
177         out[pos] = '\0';
178
179         // Reverse the string to the correct order
180         revsStr(out);
181 }
182
183
184 void int32ToStr( uint32_t in, char* out )
185 {
186         // Position and sign containers
187         uint32_t pos;
188         pos = 0;
189
190         // Evaluate through digits as decimal
191         do
192         {
193                 out[pos++] = in % 10 + '0';
194         }
195         while ( (in /= 10) > 0 );
196
197         // Append null
198         out[pos] = '\0';
199
200         // Reverse the string to the correct order
201         revsStr(out);
202 }
203
204
205 void hexToStr_op( uint16_t in, char* out, uint8_t op )
206 {
207         // Position container
208         uint16_t pos = 0;
209
210         // Evaluate through digits as hex
211         do
212         {
213                 uint16_t cur = in % 16;
214                 out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
215         }
216         while ( (in /= 16) > 0 );
217
218         // Output formatting options
219         switch ( op )
220         {
221         case 1: // Add 0x
222                 out[pos++] = 'x';
223                 out[pos++] = '0';
224                 break;
225         case 2: //  8-bit padding
226         case 4: // 16-bit padding
227                 while ( pos < op )
228                         out[pos++] = '0';
229                 break;
230         }
231
232         // Append null
233         out[pos] = '\0';
234
235         // Reverse the string to the correct order
236         revsStr(out);
237 }
238
239
240 void hex32ToStr_op( uint32_t in, char* out, uint8_t op )
241 {
242         // Position container
243         uint32_t pos = 0;
244
245         // Evaluate through digits as hex
246         do
247         {
248                 uint32_t cur = in % 16;
249                 out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
250         }
251         while ( (in /= 16) > 0 );
252
253         // Output formatting options
254         switch ( op )
255         {
256         case 1: // Add 0x
257                 out[pos++] = 'x';
258                 out[pos++] = '0';
259                 break;
260         case 2: //  8-bit padding
261         case 4: // 16-bit padding
262                 while ( pos < op )
263                         out[pos++] = '0';
264                 break;
265         }
266
267         // Append null
268         out[pos] = '\0';
269
270         // Reverse the string to the correct order
271         revsStr(out);
272 }
273
274
275 void revsStr( char* in )
276 {
277         // Iterators
278         int i, j;
279
280         // Temp storage
281         char c;
282
283         // Loop through the string, and reverse the order of the characters
284         for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
285         {
286                 c = in[i];
287                 in[i] = in[j];
288                 in[j] = c;
289         }
290 }
291
292
293 uint16_t lenStr( char* in )
294 {
295         // Iterator
296         char *pos;
297
298         // Loop until null is found
299         for ( pos = in; *pos; pos++ );
300
301         // Return the difference between the pointers of in and pos (which is the string length)
302         return (pos - in);
303 }
304
305
306 int16_t eqStr( char* str1, char* str2 )
307 {
308         // Scan each string for NULLs and whether they are the same
309         while( *str1 != '\0' && *str1++ == *str2++ );
310
311         // If the strings are still identical (i.e. both NULL), then return -1, otherwise current *str1
312         // If *str1 is 0, then str1 ended (and str1 is "like" str2), otherwise strings are different
313         return *--str1 == *--str2 ? -1 : *++str1;
314 }
315
316 int numToInt( char* in )
317 {
318         // Pointers to the LSD (Least Significant Digit) and MSD
319         char* lsd = in;
320         char* msd = in;
321
322         int total = 0;
323         int sign = 1; // Default to positive
324         uint8_t base = 10; // Use base 10 by default TODO Add support for bases other than 10 and 16
325
326         // Scan the string once to determine the length
327         while ( *lsd != '\0' )
328         {
329                 // Check for positive/negative
330                 switch ( *lsd++ )
331                 {
332                 // Fall through is intentional, only do something on negative, ignore the rest
333                 // Update the MSD to remove leading spaces and signs
334                 case '-': sign = -1;
335                 case '+':
336                 case ' ':
337                         msd = lsd;
338                         break;
339                 case 'x': // Hex Mode
340                         base = 0x10;
341                         msd = lsd;
342                         break;
343                 }
344         }
345
346         // Process string depending on which base
347         switch ( base )
348         {
349         case 10: // Decimal
350                 // Rescan the string from the LSD to MSD to convert it to a decimal number
351                 for ( unsigned int digit = 1; lsd > msd ; digit *= 10 )
352                         total += ( (*--lsd) - '0' ) * digit;
353                 break;
354
355         case 0x10: // Hex
356                 // Rescan the string from the LSD to MSD to convert it to a hexadecimal number
357                 for ( unsigned int digit = 1; lsd > msd ; digit *= 0x10 )
358                 {
359                         if    ( *--lsd <= '9' ) total += ( *lsd - '0' ) * digit;
360                         else if ( *lsd <= 'F' ) total += ( *lsd - 'A' + 10 ) * digit;
361                         else if ( *lsd <= 'f' ) total += ( *lsd - 'a' + 10 ) * digit;
362                 }
363                 break;
364         }
365
366         // Propagate sign and return
367         return total * sign;
368 }
369