X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=Debug%2Fcli%2Fcli.c;h=03fb3ca44b2924888d3b8247e124d7f422123364;hb=a824a0fe52b1c2c3a88b3590d0513f63a334333a;hp=e91046903cd32f6d2bda1d669bef67a3e46f941a;hpb=aeaaaaa468e95e732f12725f87f9a4fce70b5de0;p=kiibohd-controller.git diff --git a/Debug/cli/cli.c b/Debug/cli/cli.c index e910469..03fb3ca 100644 --- a/Debug/cli/cli.c +++ b/Debug/cli/cli.c @@ -35,6 +35,7 @@ // ----- Variables ----- // Basic command dictionary +CLIDict_Entry( clear, "Clear the screen."); CLIDict_Entry( cliDebug, "Enables/Disables hex output of the most recent cli input." ); CLIDict_Entry( help, "You're looking at it :P" ); CLIDict_Entry( led, "Enables/Disables indicator LED. Try a couple times just in case the LED is in an odd state.\r\n\t\t\033[33mWarning\033[0m: May adversely affect some modules..." ); @@ -44,6 +45,7 @@ CLIDict_Entry( restart, "Sends a software restart, should be similar to powerin CLIDict_Entry( version, "Version information about this firmware." ); CLIDict_Def( basicCLIDict, "General Commands" ) = { + CLIDict_Item( clear ), CLIDict_Item( cliDebug ), CLIDict_Item( help ), CLIDict_Item( led ), @@ -70,6 +72,11 @@ inline void CLI_init() // Reset the Line Buffer CLILineBufferCurrent = 0; + // History starts empty + CLIHistoryHead = 0; + CLIHistoryCurrent = 0; + CLIHistoryTail = 0; + // Set prompt prompt(); @@ -142,15 +149,41 @@ void CLI_process() // Check for control characters switch ( CLILineBuffer[prev_buf_pos] ) { - case 0x0D: // Enter + // Enter + case 0x0A: // LF + case 0x0D: // CR CLILineBuffer[CLILineBufferCurrent - 1] = ' '; // Replace Enter with a space (resolves a bug in args) // Remove the space if there is no command if ( CLILineBufferCurrent == 1 ) + { CLILineBufferCurrent--; + } + else + { + // Only do command-related stuff if there was actually a command + // Avoids clogging command history with blanks + + // Process the current line buffer + CLI_commandLookup(); + + // Add the command to the history + CLI_saveHistory( CLILineBuffer ); - // Process the current line buffer - CLI_commandLookup(); + // Keep the array circular, discarding the older entries + if ( CLIHistoryTail < CLIHistoryHead ) + CLIHistoryHead = ( CLIHistoryHead + 1 ) % CLIMaxHistorySize; + CLIHistoryTail++; + if ( CLIHistoryTail == CLIMaxHistorySize ) + { + CLIHistoryTail = 0; + CLIHistoryHead = 1; + } + + CLIHistoryCurrent = CLIHistoryTail; // 'Up' starts at the last item + CLI_saveHistory( NULL ); // delete the old temp buffer + + } // Reset the buffer CLILineBufferCurrent = 0; @@ -173,9 +206,38 @@ void CLI_process() // Doesn't look like it will happen *that* often, so not handling it for now -HaaTa return; - case 0x1B: // Esc - // Check for escape sequence - // TODO + case 0x1B: // Esc / Escape codes + // Check for other escape sequence + + // \e[ is an escape code in vt100 compatible terminals + if ( CLILineBufferCurrent >= prev_buf_pos + 3 + && CLILineBuffer[ prev_buf_pos ] == 0x1B + && CLILineBuffer[ prev_buf_pos + 1] == 0x5B ) + { + // Arrow Keys: A (0x41) = Up, B (0x42) = Down, C (0x43) = Right, D (0x44) = Left + + if ( CLILineBuffer[ prev_buf_pos + 2 ] == 0x41 ) // Hist prev + { + if ( CLIHistoryCurrent == CLIHistoryTail ) + { + // Is first time pressing arrow. Save the current buffer + CLILineBuffer[ prev_buf_pos ] = '\0'; + CLI_saveHistory( CLILineBuffer ); + } + + // Grab the previus item from the history if there is one + if ( RING_PREV( CLIHistoryCurrent ) != RING_PREV( CLIHistoryHead ) ) + CLIHistoryCurrent = RING_PREV( CLIHistoryCurrent ); + CLI_retreiveHistory( CLIHistoryCurrent ); + } + if ( CLILineBuffer[ prev_buf_pos + 2 ] == 0x42 ) // Hist next + { + // Grab the next item from the history if it exists + if ( RING_NEXT( CLIHistoryCurrent ) != RING_NEXT( CLIHistoryTail ) ) + CLIHistoryCurrent = RING_NEXT( CLIHistoryCurrent ); + CLI_retreiveHistory( CLIHistoryCurrent ); + } + } return; case 0x08: @@ -346,10 +408,63 @@ inline void CLI_tabCompletion() } } +inline int CLI_wrap( int kX, int const kLowerBound, int const kUpperBound ) +{ + int range_size = kUpperBound - kLowerBound + 1; + + if ( kX < kLowerBound ) + kX += range_size * ((kLowerBound - kX) / range_size + 1); + + return kLowerBound + (kX - kLowerBound) % range_size; +} + +inline void CLI_saveHistory( char *buff ) +{ + if ( buff == NULL ) + { + //clear the item + CLIHistoryBuffer[ CLIHistoryTail ][ 0 ] = '\0'; + return; + } + + // Copy the line to the history + int i; + for (i = 0; i < CLILineBufferCurrent; i++) + { + CLIHistoryBuffer[ CLIHistoryTail ][ i ] = CLILineBuffer[ i ]; + } +} + +void CLI_retreiveHistory( int index ) +{ + char *histMatch = CLIHistoryBuffer[ index ]; + + // Reset the buffer + CLILineBufferCurrent = 0; + + // Reprint the prompt (automatically clears the line) + prompt(); + + // Display the command + dPrint( histMatch ); + + // There are no index counts, so just copy the whole string to the input buffe + CLILineBufferCurrent = 0; + while ( *histMatch != '\0' ) + { + CLILineBuffer[ CLILineBufferCurrent++ ] = *histMatch++; + } +} + // ----- CLI Command Functions ----- +void cliFunc_clear( char* args) +{ + print("\033[2J\033[H\r"); // Erases the whole screen +} + void cliFunc_cliDebug( char* args ) { // Toggle Hex Debug Mode @@ -433,5 +548,15 @@ void cliFunc_version( char* args ) print( " \033[1mCPU:\033[0m " CLI_CPU NL ); print( " \033[1mDevice:\033[0m " CLI_Device NL ); print( " \033[1mModules:\033[0m " CLI_Modules NL ); +#if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) + print( " \033[1mUnique Id:\033[0m " ); + printHex32_op( SIM_UIDH, 4 ); + printHex32_op( SIM_UIDMH, 4 ); + printHex32_op( SIM_UIDML, 4 ); + printHex32_op( SIM_UIDL, 4 ); +#elif defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) +#else +#error "No unique id defined." +#endif }