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 // Multiple string Output
35 void printstrs( char* first, ... )
37 // Initialize the variadic function parameter list
40 // Get the first parameter
41 va_start( ap, first );
44 // Loop through the variadic list until "\0\0\0" is found
45 while ( !( cur[0] == '\0' && cur[1] == '\0' && cur[2] == '\0' ) )
47 // Print out the given string
50 // Get the next argument ready
51 cur = va_arg( ap, char* );
54 va_end( ap ); // Not required, but good practice
57 // Print a constant string
58 void _print( const char* s )
60 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
61 // Pull string out of flash
63 while ( ( c = pgm_read_byte( s++ ) ) != '\0' )
67 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
68 Output_putstr( (char*)s );
74 // Number Printing Functions
75 void printInt8( uint8_t in )
77 // Max number of characters is 3 + 1 for null
81 int8ToStr( in, tmpStr );
87 void printInt16( uint16_t in )
89 // Max number of characters is 5 + 1 for null
93 int16ToStr( in, tmpStr );
99 void printInt32( uint32_t in )
101 // Max number of characters is 10 + 1 for null
105 int32ToStr( in, tmpStr );
111 void printHex_op( uint16_t in, uint8_t op )
113 // With an op of 1, the max number of characters is 6 + 1 for null
115 // op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
119 hexToStr_op( in, tmpStr, op );
128 void int8ToStr( uint8_t in, char* out )
130 // Position and sign containers
134 // Evaluate through digits as decimal
137 out[pos++] = in % 10 + '0';
139 while ( (in /= 10) > 0 );
144 // Reverse the string to the correct order
149 void int16ToStr( uint16_t in, char* out )
151 // Position and sign containers
155 // Evaluate through digits as decimal
158 out[pos++] = in % 10 + '0';
160 while ( (in /= 10) > 0 );
165 // Reverse the string to the correct order
170 void int32ToStr( uint32_t in, char* out )
172 // Position and sign containers
176 // Evaluate through digits as decimal
179 out[pos++] = in % 10 + '0';
181 while ( (in /= 10) > 0 );
186 // Reverse the string to the correct order
191 void hexToStr_op( uint16_t in, char* out, uint8_t op )
193 // Position container
196 // Evaluate through digits as hex
199 uint16_t cur = in % 16;
200 out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
202 while ( (in /= 16) > 0 );
204 // Output formatting options
211 case 2: // 8-bit padding
212 case 4: // 16-bit padding
221 // Reverse the string to the correct order
226 void revsStr( char* in )
234 // Loop through the string, and reverse the order of the characters
235 for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
244 uint16_t lenStr( char* in )
249 // Loop until null is found
250 for ( pos = in; *pos; pos++ );
252 // Return the difference between the pointers of in and pos (which is the string length)
257 int16_t eqStr( char* str1, char* str2 )
259 // Scan each string for NULLs and whether they are the same
260 while( *str1 != '\0' && *str1++ == *str2++ );
262 // If the strings are still identical (i.e. both NULL), then return -1, otherwise current *str1
263 // If *str1 is 0, then str1 ended (and str1 is "like" str2), otherwise strings are different
264 return *--str1 == *--str2 ? -1 : *++str1;
267 int decToInt( char* in )
269 // Pointers to the LSD (Least Significant Digit) and MSD
274 int sign = 1; // Default to positive
276 // Scan the string once to determine the length
277 while ( *lsd != '\0' )
279 // Check for positive/negative
282 // Fall through is intentional, only do something on negative, ignore the rest
283 // Update the MSD to remove leading spaces and signs
292 // Rescan the string from the LSD to MSD to convert it to a decimal number
293 for ( unsigned int digit = 1; lsd > msd ; digit *= 10 )
294 total += ( (*--lsd) - '0' ) * digit;
296 // Propagate sign and return