Archived
1
0

Changing decToInt to numToInt (adds support for Hex number interpreter)

- CLI now works with hex or decimal numbers
- Hex requires 0x (technically just x would work too)
This commit is contained in:
Jacob Alexander 2014-08-16 12:07:25 -07:00
parent 662d1f557f
commit d6d792fdf9
10 changed files with 54 additions and 31 deletions

View File

@ -313,7 +313,7 @@ int16_t eqStr( char* str1, char* str2 )
return *--str1 == *--str2 ? -1 : *++str1;
}
int decToInt( char* in )
int numToInt( char* in )
{
// Pointers to the LSD (Least Significant Digit) and MSD
char* lsd = in;
@ -321,6 +321,7 @@ int decToInt( char* in )
int total = 0;
int sign = 1; // Default to positive
uint8_t base = 10; // Use base 10 by default TODO Add support for bases other than 10 and 16
// Scan the string once to determine the length
while ( *lsd != '\0' )
@ -335,12 +336,32 @@ int decToInt( char* in )
case ' ':
msd = lsd;
break;
case 'x': // Hex Mode
base = 0x10;
msd = lsd;
break;
}
}
// 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;
}
// Propagate sign and return
return total * sign;

View File

@ -114,7 +114,7 @@ void hex32ToStr_op( uint32_t in, char* out, uint8_t op );
void revsStr ( char* in );
uint16_t lenStr ( char* in );
int16_t eqStr ( char* str1, char* str2 ); // Returns -1 if identical, last character of str1 comparison (0 if str1 is like str2)
int decToInt ( char* in ); // Returns the int representation of a string
int numToInt ( char* in ); // Returns the int representation of a string
#endif

View File

@ -221,6 +221,7 @@ Guide_TM( 0 ) = { 1, 0x00, 0x01, 0x73, 0 };
Guide_TM( 1 ) = { 1, 0x00, 0x01, 0x73, 1, 0x00, 0x01, 0x75, 0 };
Guide_TM( 2 ) = { 2, 0x00, 0x01, 0x73, 0x00, 0x01, 0x74, 0 };
Guide_TM( 3 ) = { 1, 0x00, 0x01, 0x76, 0 };
Guide_TM( 4 ) = { 1, 0x00, 0x01, 0x77, 0 };
// -- Trigger Macro List
@ -235,6 +236,7 @@ TriggerMacro TriggerMacroList[] = {
Define_TM( 1, 1 ),
Define_TM( 2, 2 ),
Define_TM( 3, 3 ),
Define_TM( 4, 0 ),
};
@ -379,7 +381,7 @@ Define_TL( default, 0x73 ) = { 3, 0, 1, 2 };
Define_TL( default, 0x74 ) = { 1, 2 };
Define_TL( default, 0x75 ) = { 1, 1 };
Define_TL( default, 0x76 ) = { 1, 3 };
Define_TL( default, 0x77 ) = { 0 };
Define_TL( default, 0x77 ) = { 1, 4 };
Define_TL( default, 0x78 ) = { 0 };
Define_TL( default, 0x79 ) = { 0 };
Define_TL( default, 0x7A ) = { 0 };

View File

@ -784,7 +784,7 @@ void cliFunc_capSelect( char* args )
// Keyboard Capability
case 'K':
// Determine capability index
cap = decToInt( &arg1Ptr[1] );
cap = numToInt( &arg1Ptr[1] );
// Lookup the number of args
totalArgs += CapabilitiesList[ cap ].argCount;
@ -793,7 +793,7 @@ void cliFunc_capSelect( char* args )
// Because allocating memory isn't doable, and the argument count is arbitrary
// The argument pointer is repurposed as the argument list (much smaller anyways)
argSet[ argSetCount++ ] = (uint8_t)decToInt( arg1Ptr );
argSet[ argSetCount++ ] = (uint8_t)numToInt( arg1Ptr );
// Once all the arguments are prepared, call the keyboard capability function
if ( argSetCount == totalArgs )
@ -838,7 +838,7 @@ void cliFunc_keyHold( char* args )
{
// Scancode
case 'S':
Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
break;
}
}
@ -866,7 +866,7 @@ void cliFunc_keyPress( char* args )
{
// Scancode
case 'S':
Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
break;
}
}
@ -894,7 +894,7 @@ void cliFunc_keyRelease( char* args )
{
// Scancode
case 'S':
Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
break;
}
}
@ -956,11 +956,11 @@ void cliFunc_layerState( char* args )
if ( arg1Ptr[0] != 'L' )
return;
arg1 = (uint8_t)decToInt( &arg1Ptr[1] );
arg1 = (uint8_t)numToInt( &arg1Ptr[1] );
break;
// Second argument (e.g. 4)
case 1:
arg2 = (uint8_t)decToInt( arg1Ptr );
arg2 = (uint8_t)numToInt( arg1Ptr );
// Display operation (to indicate that it worked)
print( NL );
@ -1223,11 +1223,11 @@ void cliFunc_macroShow( char* args )
{
// Indexed Trigger Macro
case 'T':
macroDebugShowTrigger( decToInt( &arg1Ptr[1] ) );
macroDebugShowTrigger( numToInt( &arg1Ptr[1] ) );
break;
// Indexed Result Macro
case 'R':
macroDebugShowResult( decToInt( &arg1Ptr[1] ) );
macroDebugShowResult( numToInt( &arg1Ptr[1] ) );
break;
}
}
@ -1242,7 +1242,7 @@ void cliFunc_macroStep( char* args )
CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
// Default to 1, if no argument given
unsigned int count = (unsigned int)decToInt( arg1Ptr );
unsigned int count = (unsigned int)numToInt( arg1Ptr );
if ( count == 0 )
count = 1;

View File

@ -276,7 +276,7 @@ void cliFunc_setKeys( char* args )
break;
// Add the USB code to be sent
USBKeys_ArrayCLI[USBKeys_SentCLI] = decToInt( arg1Ptr );
USBKeys_ArrayCLI[USBKeys_SentCLI] = numToInt( arg1Ptr );
}
}
@ -289,6 +289,6 @@ void cliFunc_setMod( char* args )
char* arg2Ptr;
CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
USBKeys_ModifiersCLI = decToInt( arg1Ptr );
USBKeys_ModifiersCLI = numToInt( arg1Ptr );
}

View File

@ -214,7 +214,7 @@ void cliFunc_setKeys( char* args )
break;
// Add the USB code to be sent
USBKeys_ArrayCLI[USBKeys_SentCLI] = decToInt( arg1Ptr );
USBKeys_ArrayCLI[USBKeys_SentCLI] = numToInt( arg1Ptr );
}
}
@ -227,6 +227,6 @@ void cliFunc_setMod( char* args )
char* arg2Ptr;
CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
USBKeys_ModifiersCLI = decToInt( arg1Ptr );
USBKeys_ModifiersCLI = numToInt( arg1Ptr );
}

View File

@ -278,7 +278,7 @@ void cliFunc_setKeys( char* args )
break;
// Add the USB code to be sent
USBKeys_ArrayCLI[USBKeys_SentCLI] = decToInt( arg1Ptr );
USBKeys_ArrayCLI[USBKeys_SentCLI] = numToInt( arg1Ptr );
}
}
@ -291,6 +291,6 @@ void cliFunc_setMod( char* args )
char* arg2Ptr;
CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
USBKeys_ModifiersCLI = decToInt( arg1Ptr );
USBKeys_ModifiersCLI = numToInt( arg1Ptr );
}

View File

@ -176,7 +176,7 @@ void cliFunc_adc( char* args )
CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
// Set the ADC Channel
uint8_t channel = decToInt( arg1Ptr );
uint8_t channel = numToInt( arg1Ptr );
__disable_irq();
ADC0_SC1A = channel;
__enable_irq();
@ -187,7 +187,7 @@ void cliFunc_adc( char* args )
int displayedADC = 1; // Default to 1 read
if ( arg1Ptr ) // If there is an argument, use that instead
{
displayedADC = decToInt( arg1Ptr );
displayedADC = numToInt( arg1Ptr );
}
// Poll ADC until it gets a value, making sure to serve interrupts on each attempt
@ -232,7 +232,7 @@ void cliFunc_adcInit( char* args )
ADC0_SC3 = 0;
// Select bit resolution
int bitResolution = decToInt( arg1Ptr );
int bitResolution = numToInt( arg1Ptr );
switch ( bitResolution )
{
case 8: // 8-bit
@ -260,7 +260,7 @@ void cliFunc_adcInit( char* args )
// Select Vref
CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
int vRef = decToInt( arg1Ptr );
int vRef = numToInt( arg1Ptr );
switch ( vRef )
{
case 0: // 1.2V internal Vref
@ -276,7 +276,7 @@ void cliFunc_adcInit( char* args )
// Hardware averaging (and start calibration)
CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
int hardwareAvg = decToInt( arg1Ptr );
int hardwareAvg = numToInt( arg1Ptr );
switch ( hardwareAvg )
{
case 0: // No hardware averaging
@ -342,7 +342,7 @@ void cliFunc_dac( char* args )
char* arg2Ptr;
CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
int dacOut = decToInt( arg1Ptr );
int dacOut = numToInt( arg1Ptr );
// Make sure the value is between 0 and 4096, otherwise ignore
if ( dacOut >= 0 && dacOut <= 4095 )
@ -361,7 +361,7 @@ void cliFunc_dacVref( char* args )
char* arg2Ptr;
CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
switch ( decToInt( arg1Ptr ) )
switch ( numToInt( arg1Ptr ) )
{
case 0:
DAC0_C0 = DAC_C0_DACEN; // 1.2V Vref is DACREF_1

View File

@ -1027,7 +1027,7 @@ void cliFunc_senseDebug( char* args )
// If there was an argument, use that instead
if ( *arg1Ptr != '\0' )
{
senseDebugCount = decToInt( arg1Ptr );
senseDebugCount = numToInt( arg1Ptr );
}
}

View File

@ -444,7 +444,7 @@ void cliFunc_matrixState ( char* args )
if ( arg1Ptr[0] != '\0' )
{
matrixDebugStateCounter = (uint16_t)decToInt( arg1Ptr );
matrixDebugStateCounter = (uint16_t)numToInt( arg1Ptr );
}
}