]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Debug/print/print.c
Finished USB for Teensy 3.1 (Now 3.1 compatible!)
[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 // 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_) || defined(_mk20dx256_) // 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_) || defined(_mk20dx256_) // 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 printInt32( uint32_t in )
120 {
121         // Max number of characters is 10 + 1 for null
122         char tmpStr[11];
123
124         // Convert number
125         int32ToStr( in, tmpStr );
126
127         // Print number
128         dPrintStr( tmpStr );
129 }
130
131 void printHex_op( uint16_t in, uint8_t op )
132 {
133         // With an op of 1, the max number of characters is 6 + 1 for null
134         // e.g. "0xFFFF\0"
135         // op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
136         char tmpStr[7];
137
138         // Convert number
139         hexToStr_op( in, tmpStr, op );
140
141         // Print number
142         dPrintStr( tmpStr );
143 }
144
145
146
147 // String Functions
148 void int8ToStr( uint8_t in, char* out )
149 {
150         // Position and sign containers
151         uint8_t pos;
152         pos = 0;
153
154         // Evaluate through digits as decimal
155         do
156         {
157                 out[pos++] = in % 10 + '0';
158         }
159         while ( (in /= 10) > 0 );
160
161         // Append null
162         out[pos] = '\0';
163
164         // Reverse the string to the correct order
165         revsStr(out);
166 }
167
168
169 void int16ToStr( uint16_t in, char* out )
170 {
171         // Position and sign containers
172         uint16_t pos;
173         pos = 0;
174
175         // Evaluate through digits as decimal
176         do
177         {
178                 out[pos++] = in % 10 + '0';
179         }
180         while ( (in /= 10) > 0 );
181
182         // Append null
183         out[pos] = '\0';
184
185         // Reverse the string to the correct order
186         revsStr(out);
187 }
188
189
190 void int32ToStr( uint32_t in, char* out )
191 {
192         // Position and sign containers
193         uint32_t pos;
194         pos = 0;
195
196         // Evaluate through digits as decimal
197         do
198         {
199                 out[pos++] = in % 10 + '0';
200         }
201         while ( (in /= 10) > 0 );
202
203         // Append null
204         out[pos] = '\0';
205
206         // Reverse the string to the correct order
207         revsStr(out);
208 }
209
210
211 void hexToStr_op( uint16_t in, char* out, uint8_t op )
212 {
213         // Position container
214         uint16_t pos = 0;
215
216         // Evaluate through digits as hex
217         do
218         {
219                 uint16_t cur = in % 16;
220                 out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
221         }
222         while ( (in /= 16) > 0 );
223
224         // Output formatting options
225         switch ( op )
226         {
227         case 1: // Add 0x
228                 out[pos++] = 'x';
229                 out[pos++] = '0';
230                 break;
231         case 2: //  8-bit padding
232         case 4: // 16-bit padding
233                 while ( pos < op )
234                         out[pos++] = '0';
235                 break;
236         }
237
238         // Append null
239         out[pos] = '\0';
240
241         // Reverse the string to the correct order
242         revsStr(out);
243 }
244
245
246 void revsStr( char* in )
247 {
248         // Iterators
249         int i, j;
250
251         // Temp storage
252         char c;
253
254         // Loop through the string, and reverse the order of the characters
255         for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
256         {
257                 c = in[i];
258                 in[i] = in[j];
259                 in[j] = c;
260         }
261 }
262
263
264 uint16_t lenStr( char* in )
265 {
266         // Iterator
267         char *pos;
268
269         // Loop until null is found
270         for ( pos = in; *pos; pos++ );
271
272         // Return the difference between the pointers of in and pos (which is the string length)
273         return (pos - in);
274 }
275
276
277 int16_t eqStr( char* str1, char* str2 )
278 {
279         // Scan each string for NULLs and whether they are the same
280         while( *str1 != '\0' && *str1++ == *str2++ );
281
282         // If the strings are still identical (i.e. both NULL), then return -1, otherwise current *str1
283         // If *str1 is 0, then str1 ended (and str1 is "like" str2), otherwise strings are different
284         return *--str1 == *--str2 ? -1 : *++str1;
285 }
286