1 /* Copyright (C) 2011-2014 by Jacob Alexander
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 // ----- Includes -----
32 // ----- Functions -----
34 // USB HID String Output
35 void usb_debug_putstr( char* s )
37 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
39 usb_debug_putchar( *s++ );
40 #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
41 // Count characters until NULL character, then send the amount counted
43 while ( s[count] != '\0' )
46 usb_serial_write( s, count );
50 // Multiple string Output
51 void usb_debug_putstrs( char* first, ... )
53 // Initialize the variadic function parameter list
56 // Get the first parameter
57 va_start( ap, first );
60 // Loop through the variadic list until "\0\0\0" is found
61 while ( !( cur[0] == '\0' && cur[1] == '\0' && cur[2] == '\0' ) )
63 // Print out the given string
64 usb_debug_putstr( cur );
66 // Get the next argument ready
67 cur = va_arg( ap, char* );
70 va_end( ap ); // Not required, but good practice
73 // Print a constant string
74 void _print(const char *s)
76 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
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' )
84 usb_debug_putchar('\r');
87 #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
88 usb_debug_putstr( (char*)s );
94 // Number Printing Functions
95 void printInt8( uint8_t in )
97 // Max number of characters is 3 + 1 for null
101 int8ToStr( in, tmpStr );
107 void printInt16( uint16_t in )
109 // Max number of characters is 5 + 1 for null
113 int16ToStr( in, tmpStr );
119 void printInt32( uint32_t in )
121 // Max number of characters is 10 + 1 for null
125 int32ToStr( in, tmpStr );
131 void printHex_op( uint16_t in, uint8_t op )
133 // With an op of 1, the max number of characters is 6 + 1 for null
135 // op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
139 hexToStr_op( in, tmpStr, op );
148 void int8ToStr( uint8_t in, char* out )
150 // Position and sign containers
154 // Evaluate through digits as decimal
157 out[pos++] = in % 10 + '0';
159 while ( (in /= 10) > 0 );
164 // Reverse the string to the correct order
169 void int16ToStr( uint16_t in, char* out )
171 // Position and sign containers
175 // Evaluate through digits as decimal
178 out[pos++] = in % 10 + '0';
180 while ( (in /= 10) > 0 );
185 // Reverse the string to the correct order
190 void int32ToStr( uint32_t in, char* out )
192 // Position and sign containers
196 // Evaluate through digits as decimal
199 out[pos++] = in % 10 + '0';
201 while ( (in /= 10) > 0 );
206 // Reverse the string to the correct order
211 void hexToStr_op( uint16_t in, char* out, uint8_t op )
213 // Position container
216 // Evaluate through digits as hex
219 uint16_t cur = in % 16;
220 out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
222 while ( (in /= 16) > 0 );
224 // Output formatting options
231 case 2: // 8-bit padding
232 case 4: // 16-bit padding
241 // Reverse the string to the correct order
246 void revsStr( char* in )
254 // Loop through the string, and reverse the order of the characters
255 for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
264 uint16_t lenStr( char* in )
269 // Loop until null is found
270 for ( pos = in; *pos; pos++ );
272 // Return the difference between the pointers of in and pos (which is the string length)
277 int16_t eqStr( char* str1, char* str2 )
279 // Scan each string for NULLs and whether they are the same
280 while( *str1 != '\0' && *str1++ == *str2++ );
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;