2014-02-03 05:12:20 +00:00
|
|
|
/* Copyright (C) 2011-2014 by Jacob Alexander
|
2014-01-20 00:40:36 +00:00
|
|
|
*
|
2011-09-29 06:25:51 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2014-01-20 00:40:36 +00:00
|
|
|
*
|
2011-09-29 06:25:51 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2014-01-20 00:40:36 +00:00
|
|
|
*
|
2011-09-29 06:25:51 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-09-30 05:22:19 +00:00
|
|
|
// ----- Includes -----
|
|
|
|
|
2011-09-29 06:25:51 +00:00
|
|
|
// Compiler Includes
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
// Project Includes
|
|
|
|
#include "print.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-09-30 05:22:19 +00:00
|
|
|
// ----- Functions -----
|
|
|
|
|
2011-09-29 06:25:51 +00:00
|
|
|
// Multiple string Output
|
2014-03-31 08:07:48 +00:00
|
|
|
void printstrs( char* first, ... )
|
2011-09-29 06:25:51 +00:00
|
|
|
{
|
|
|
|
// Initialize the variadic function parameter list
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
// Get the first parameter
|
|
|
|
va_start( ap, first );
|
|
|
|
char *cur = first;
|
|
|
|
|
|
|
|
// Loop through the variadic list until "\0\0\0" is found
|
|
|
|
while ( !( cur[0] == '\0' && cur[1] == '\0' && cur[2] == '\0' ) )
|
|
|
|
{
|
|
|
|
// Print out the given string
|
2014-04-06 18:49:27 +00:00
|
|
|
Output_putstr( cur );
|
2011-09-29 06:25:51 +00:00
|
|
|
|
|
|
|
// Get the next argument ready
|
|
|
|
cur = va_arg( ap, char* );
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end( ap ); // Not required, but good practice
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print a constant string
|
2014-03-31 08:07:48 +00:00
|
|
|
void _print( const char* s )
|
2011-09-29 06:25:51 +00:00
|
|
|
{
|
2013-01-27 03:30:36 +00:00
|
|
|
#if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
|
2014-03-31 08:07:48 +00:00
|
|
|
// Pull string out of flash
|
2011-09-29 06:25:51 +00:00
|
|
|
char c;
|
2014-03-31 08:07:48 +00:00
|
|
|
while ( ( c = pgm_read_byte( s++ ) ) != '\0' )
|
2011-09-29 06:25:51 +00:00
|
|
|
{
|
2014-04-06 18:49:27 +00:00
|
|
|
Output_putchar( c );
|
2011-09-29 06:25:51 +00:00
|
|
|
}
|
2015-06-14 03:42:12 +00:00
|
|
|
#elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
|
2014-04-06 18:49:27 +00:00
|
|
|
Output_putstr( (char*)s );
|
2013-01-27 03:30:36 +00:00
|
|
|
#endif
|
2011-09-29 06:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-14 00:11:40 +00:00
|
|
|
// Number Printing Functions
|
|
|
|
void printInt8( uint8_t in )
|
|
|
|
{
|
|
|
|
// Max number of characters is 3 + 1 for null
|
|
|
|
char tmpStr[4];
|
|
|
|
|
|
|
|
// Convert number
|
|
|
|
int8ToStr( in, tmpStr );
|
|
|
|
|
|
|
|
// Print number
|
|
|
|
dPrintStr( tmpStr );
|
|
|
|
}
|
|
|
|
|
|
|
|
void printInt16( uint16_t in )
|
|
|
|
{
|
|
|
|
// Max number of characters is 5 + 1 for null
|
|
|
|
char tmpStr[6];
|
|
|
|
|
|
|
|
// Convert number
|
|
|
|
int16ToStr( in, tmpStr );
|
|
|
|
|
|
|
|
// Print number
|
|
|
|
dPrintStr( tmpStr );
|
|
|
|
}
|
|
|
|
|
2014-02-02 08:03:51 +00:00
|
|
|
void printInt32( uint32_t in )
|
|
|
|
{
|
|
|
|
// Max number of characters is 10 + 1 for null
|
|
|
|
char tmpStr[11];
|
|
|
|
|
|
|
|
// Convert number
|
|
|
|
int32ToStr( in, tmpStr );
|
|
|
|
|
|
|
|
// Print number
|
|
|
|
dPrintStr( tmpStr );
|
|
|
|
}
|
|
|
|
|
2013-04-14 00:11:40 +00:00
|
|
|
void printHex_op( uint16_t in, uint8_t op )
|
|
|
|
{
|
|
|
|
// With an op of 1, the max number of characters is 6 + 1 for null
|
|
|
|
// e.g. "0xFFFF\0"
|
|
|
|
// op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
|
|
|
|
char tmpStr[7];
|
|
|
|
|
|
|
|
// Convert number
|
|
|
|
hexToStr_op( in, tmpStr, op );
|
|
|
|
|
|
|
|
// Print number
|
|
|
|
dPrintStr( tmpStr );
|
|
|
|
}
|
|
|
|
|
2014-07-15 07:28:12 +00:00
|
|
|
void printHex32_op( uint32_t in, uint8_t op )
|
|
|
|
{
|
|
|
|
// With an op of 1, the max number of characters is 6 + 1 for null
|
|
|
|
// e.g. "0xFFFF\0"
|
|
|
|
// op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
|
|
|
|
char tmpStr[7];
|
|
|
|
|
|
|
|
// Convert number
|
|
|
|
hex32ToStr_op( in, tmpStr, op );
|
|
|
|
|
|
|
|
// Print number
|
|
|
|
dPrintStr( tmpStr );
|
|
|
|
}
|
|
|
|
|
2013-04-14 00:11:40 +00:00
|
|
|
|
2011-09-29 06:25:51 +00:00
|
|
|
|
|
|
|
// String Functions
|
|
|
|
void int8ToStr( uint8_t in, char* out )
|
|
|
|
{
|
|
|
|
// Position and sign containers
|
|
|
|
uint8_t pos;
|
|
|
|
pos = 0;
|
|
|
|
|
|
|
|
// Evaluate through digits as decimal
|
|
|
|
do
|
|
|
|
{
|
|
|
|
out[pos++] = in % 10 + '0';
|
|
|
|
}
|
|
|
|
while ( (in /= 10) > 0 );
|
|
|
|
|
|
|
|
// Append null
|
|
|
|
out[pos] = '\0';
|
|
|
|
|
|
|
|
// Reverse the string to the correct order
|
|
|
|
revsStr(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void int16ToStr( uint16_t in, char* out )
|
|
|
|
{
|
|
|
|
// Position and sign containers
|
|
|
|
uint16_t pos;
|
|
|
|
pos = 0;
|
|
|
|
|
|
|
|
// Evaluate through digits as decimal
|
|
|
|
do
|
|
|
|
{
|
|
|
|
out[pos++] = in % 10 + '0';
|
|
|
|
}
|
|
|
|
while ( (in /= 10) > 0 );
|
|
|
|
|
|
|
|
// Append null
|
|
|
|
out[pos] = '\0';
|
|
|
|
|
|
|
|
// Reverse the string to the correct order
|
|
|
|
revsStr(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-02 08:03:51 +00:00
|
|
|
void int32ToStr( uint32_t in, char* out )
|
|
|
|
{
|
|
|
|
// Position and sign containers
|
|
|
|
uint32_t pos;
|
|
|
|
pos = 0;
|
|
|
|
|
|
|
|
// Evaluate through digits as decimal
|
|
|
|
do
|
|
|
|
{
|
|
|
|
out[pos++] = in % 10 + '0';
|
|
|
|
}
|
|
|
|
while ( (in /= 10) > 0 );
|
|
|
|
|
|
|
|
// Append null
|
|
|
|
out[pos] = '\0';
|
|
|
|
|
|
|
|
// Reverse the string to the correct order
|
|
|
|
revsStr(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 06:25:51 +00:00
|
|
|
void hexToStr_op( uint16_t in, char* out, uint8_t op )
|
|
|
|
{
|
|
|
|
// Position container
|
|
|
|
uint16_t pos = 0;
|
|
|
|
|
|
|
|
// Evaluate through digits as hex
|
|
|
|
do
|
|
|
|
{
|
|
|
|
uint16_t cur = in % 16;
|
2014-07-15 07:28:12 +00:00
|
|
|
out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
|
|
|
|
}
|
|
|
|
while ( (in /= 16) > 0 );
|
|
|
|
|
|
|
|
// Output formatting options
|
|
|
|
switch ( op )
|
|
|
|
{
|
|
|
|
case 1: // Add 0x
|
|
|
|
out[pos++] = 'x';
|
|
|
|
out[pos++] = '0';
|
|
|
|
break;
|
|
|
|
case 2: // 8-bit padding
|
|
|
|
case 4: // 16-bit padding
|
|
|
|
while ( pos < op )
|
|
|
|
out[pos++] = '0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append null
|
|
|
|
out[pos] = '\0';
|
|
|
|
|
|
|
|
// Reverse the string to the correct order
|
|
|
|
revsStr(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void hex32ToStr_op( uint32_t in, char* out, uint8_t op )
|
|
|
|
{
|
|
|
|
// Position container
|
|
|
|
uint32_t pos = 0;
|
|
|
|
|
|
|
|
// Evaluate through digits as hex
|
|
|
|
do
|
|
|
|
{
|
|
|
|
uint32_t cur = in % 16;
|
2011-09-29 06:25:51 +00:00
|
|
|
out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
|
|
|
|
}
|
|
|
|
while ( (in /= 16) > 0 );
|
|
|
|
|
|
|
|
// Output formatting options
|
|
|
|
switch ( op )
|
|
|
|
{
|
|
|
|
case 1: // Add 0x
|
|
|
|
out[pos++] = 'x';
|
|
|
|
out[pos++] = '0';
|
|
|
|
break;
|
|
|
|
case 2: // 8-bit padding
|
|
|
|
case 4: // 16-bit padding
|
|
|
|
while ( pos < op )
|
|
|
|
out[pos++] = '0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append null
|
|
|
|
out[pos] = '\0';
|
|
|
|
|
|
|
|
// Reverse the string to the correct order
|
|
|
|
revsStr(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void revsStr( char* in )
|
|
|
|
{
|
|
|
|
// Iterators
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
// Temp storage
|
|
|
|
char c;
|
|
|
|
|
|
|
|
// Loop through the string, and reverse the order of the characters
|
|
|
|
for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
|
|
|
|
{
|
|
|
|
c = in[i];
|
|
|
|
in[i] = in[j];
|
|
|
|
in[j] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t lenStr( char* in )
|
|
|
|
{
|
|
|
|
// Iterator
|
|
|
|
char *pos;
|
|
|
|
|
|
|
|
// Loop until null is found
|
|
|
|
for ( pos = in; *pos; pos++ );
|
|
|
|
|
|
|
|
// Return the difference between the pointers of in and pos (which is the string length)
|
|
|
|
return (pos - in);
|
|
|
|
}
|
|
|
|
|
2014-01-22 08:38:53 +00:00
|
|
|
|
2014-01-24 11:01:09 +00:00
|
|
|
int16_t eqStr( char* str1, char* str2 )
|
2014-01-22 08:38:53 +00:00
|
|
|
{
|
|
|
|
// Scan each string for NULLs and whether they are the same
|
|
|
|
while( *str1 != '\0' && *str1++ == *str2++ );
|
|
|
|
|
2014-01-24 11:01:09 +00:00
|
|
|
// If the strings are still identical (i.e. both NULL), then return -1, otherwise current *str1
|
|
|
|
// If *str1 is 0, then str1 ended (and str1 is "like" str2), otherwise strings are different
|
|
|
|
return *--str1 == *--str2 ? -1 : *++str1;
|
2014-01-22 08:38:53 +00:00
|
|
|
}
|
|
|
|
|
2014-08-16 19:07:25 +00:00
|
|
|
int numToInt( char* in )
|
2014-02-03 08:41:57 +00:00
|
|
|
{
|
|
|
|
// Pointers to the LSD (Least Significant Digit) and MSD
|
|
|
|
char* lsd = in;
|
|
|
|
char* msd = in;
|
|
|
|
|
|
|
|
int total = 0;
|
|
|
|
int sign = 1; // Default to positive
|
2014-08-16 19:07:25 +00:00
|
|
|
uint8_t base = 10; // Use base 10 by default TODO Add support for bases other than 10 and 16
|
2014-02-03 08:41:57 +00:00
|
|
|
|
|
|
|
// Scan the string once to determine the length
|
|
|
|
while ( *lsd != '\0' )
|
|
|
|
{
|
|
|
|
// Check for positive/negative
|
|
|
|
switch ( *lsd++ )
|
|
|
|
{
|
|
|
|
// Fall through is intentional, only do something on negative, ignore the rest
|
|
|
|
// Update the MSD to remove leading spaces and signs
|
|
|
|
case '-': sign = -1;
|
|
|
|
case '+':
|
|
|
|
case ' ':
|
|
|
|
msd = lsd;
|
|
|
|
break;
|
2014-08-16 19:07:25 +00:00
|
|
|
case 'x': // Hex Mode
|
|
|
|
base = 0x10;
|
|
|
|
msd = lsd;
|
|
|
|
break;
|
2014-02-03 08:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 19:07:25 +00:00
|
|
|
// Process string depending on which base
|
|
|
|
switch ( base )
|
|
|
|
{
|
|
|
|
case 10: // Decimal
|
|
|
|
// Rescan the string from the LSD to MSD to convert it to a decimal number
|
|
|
|
for ( unsigned int digit = 1; lsd > msd ; digit *= 10 )
|
|
|
|
total += ( (*--lsd) - '0' ) * digit;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x10: // Hex
|
|
|
|
// Rescan the string from the LSD to MSD to convert it to a hexadecimal number
|
|
|
|
for ( unsigned int digit = 1; lsd > msd ; digit *= 0x10 )
|
|
|
|
{
|
|
|
|
if ( *--lsd <= '9' ) total += ( *lsd - '0' ) * digit;
|
|
|
|
else if ( *lsd <= 'F' ) total += ( *lsd - 'A' + 10 ) * digit;
|
|
|
|
else if ( *lsd <= 'f' ) total += ( *lsd - 'a' + 10 ) * digit;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-02-03 08:41:57 +00:00
|
|
|
|
|
|
|
// Propagate sign and return
|
|
|
|
return total * sign;
|
|
|
|
}
|
|
|
|
|