]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Debug/print/print.c
Adding basic Tab completion.
[kiibohd-controller.git] / Debug / print / print.c
1 /* Copyright (C) 2011-2013 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 // USB HID String Output
35 void usb_debug_putstr( char* s )
36 {
37 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
38         while ( *s != '\0' )
39                 usb_debug_putchar( *s++ );
40 #elif defined(_mk20dx128_) // ARM
41         // Count characters until NULL character, then send the amount counted
42         uint32_t count = 0;
43         while ( s[count] != '\0' )
44                 count++;
45
46         usb_serial_write( s, count );
47 #endif
48 }
49
50 // Multiple string Output
51 void usb_debug_putstrs( char* first, ... )
52 {
53         // Initialize the variadic function parameter list
54         va_list ap;
55
56         // Get the first parameter
57         va_start( ap, first );
58         char *cur = first;
59
60         // Loop through the variadic list until "\0\0\0" is found
61         while ( !( cur[0] == '\0' && cur[1] == '\0' && cur[2] == '\0' ) )
62         {
63                 // Print out the given string
64                 usb_debug_putstr( cur );
65
66                 // Get the next argument ready
67                 cur = va_arg( ap, char* );
68         }
69
70         va_end( ap ); // Not required, but good practice
71 }
72
73 // Print a constant string
74 void _print(const char *s)
75 {
76 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
77         char c;
78
79         // Acquire the character from flash, and print it, as long as it's not NULL
80         // Also, if a newline is found, print a carrige return as well
81         while ( ( c = pgm_read_byte(s++) ) != '\0' )
82         {
83                 if ( c == '\n' )
84                         usb_debug_putchar('\r');
85                 usb_debug_putchar(c);
86         }
87 #elif defined(_mk20dx128_) // ARM
88         usb_debug_putstr( (char*)s );
89 #endif
90 }
91
92
93
94 // Number Printing Functions
95 void printInt8( uint8_t in )
96 {
97         // Max number of characters is 3 + 1 for null
98         char tmpStr[4];
99
100         // Convert number
101         int8ToStr( in, tmpStr );
102
103         // Print number
104         dPrintStr( tmpStr );
105 }
106
107 void printInt16( uint16_t in )
108 {
109         // Max number of characters is 5 + 1 for null
110         char tmpStr[6];
111
112         // Convert number
113         int16ToStr( in, tmpStr );
114
115         // Print number
116         dPrintStr( tmpStr );
117 }
118
119 void printHex_op( uint16_t in, uint8_t op )
120 {
121         // With an op of 1, the max number of characters is 6 + 1 for null
122         // e.g. "0xFFFF\0"
123         // op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
124         char tmpStr[7];
125
126         // Convert number
127         hexToStr_op( in, tmpStr, op );
128
129         // Print number
130         dPrintStr( tmpStr );
131 }
132
133
134
135 // String Functions
136 void int8ToStr( uint8_t in, char* out )
137 {
138         // Position and sign containers
139         uint8_t pos;
140         pos = 0;
141
142         // Evaluate through digits as decimal
143         do
144         {
145                 out[pos++] = in % 10 + '0';
146         }
147         while ( (in /= 10) > 0 );
148
149         // Append null
150         out[pos] = '\0';
151
152         // Reverse the string to the correct order
153         revsStr(out);
154 }
155
156
157 void int16ToStr( uint16_t in, char* out )
158 {
159         // Position and sign containers
160         uint16_t pos;
161         pos = 0;
162
163         // Evaluate through digits as decimal
164         do
165         {
166                 out[pos++] = in % 10 + '0';
167         }
168         while ( (in /= 10) > 0 );
169
170         // Append null
171         out[pos] = '\0';
172
173         // Reverse the string to the correct order
174         revsStr(out);
175 }
176
177
178 void hexToStr_op( uint16_t in, char* out, uint8_t op )
179 {
180         // Position container
181         uint16_t pos = 0;
182
183         // Evaluate through digits as hex
184         do
185         {
186                 uint16_t cur = in % 16;
187                 out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
188         }
189         while ( (in /= 16) > 0 );
190
191         // Output formatting options
192         switch ( op )
193         {
194         case 1: // Add 0x
195                 out[pos++] = 'x';
196                 out[pos++] = '0';
197                 break;
198         case 2: //  8-bit padding
199         case 4: // 16-bit padding
200                 while ( pos < op )
201                         out[pos++] = '0';
202                 break;
203         }
204
205         // Append null
206         out[pos] = '\0';
207
208         // Reverse the string to the correct order
209         revsStr(out);
210 }
211
212
213 void revsStr( char* in )
214 {
215         // Iterators
216         int i, j;
217
218         // Temp storage
219         char c;
220
221         // Loop through the string, and reverse the order of the characters
222         for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
223         {
224                 c = in[i];
225                 in[i] = in[j];
226                 in[j] = c;
227         }
228 }
229
230
231 uint16_t lenStr( char* in )
232 {
233         // Iterator
234         char *pos;
235
236         // Loop until null is found
237         for ( pos = in; *pos; pos++ );
238
239         // Return the difference between the pointers of in and pos (which is the string length)
240         return (pos - in);
241 }
242
243
244 int16_t eqStr( char* str1, char* str2 )
245 {
246         // Scan each string for NULLs and whether they are the same
247         while( *str1 != '\0' && *str1++ == *str2++ );
248
249         // If the strings are still identical (i.e. both NULL), then return -1, otherwise current *str1
250         // If *str1 is 0, then str1 ended (and str1 is "like" str2), otherwise strings are different
251         return *--str1 == *--str2 ? -1 : *++str1;
252 }
253