Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: Terminal.cpp
- Revision:
- 3:3c2cc2ea12a9
- Parent:
- 2:e47cc8c92b3f
- Child:
- 4:4bcb955b81f3
diff -r e47cc8c92b3f -r 3c2cc2ea12a9 Terminal.cpp --- a/Terminal.cpp Wed Aug 01 23:51:08 2012 +0000 +++ b/Terminal.cpp Thu Aug 02 02:08:48 2012 +0000 @@ -1,86 +1,88 @@ -#include "Terminal.h" -#include "mbed.h" -#include <cctype> -#include <cstring> - - - -Terminal::Terminal(PinName tx, PinName rx, int baudrate) : serial(tx, rx) -{ - // Initialize certain values - numCommands = 0; - for (int i = 0; i < NUM_COMMANDS_MAX; i++) - { - cmdList[i].stringLength = 0; - } - - // Set up serial connection - serial.baud(baudrate); - serial.attach(this, &Terminal::receive, Serial::RxIrq); - serial.printf("\n> "); -} - - - -void Terminal::addCommand(const char* cmdString, void (*fpointer)(Serial&, const char*) ) -{ - if (numCommands < NUM_COMMANDS_MAX) - { - strncpy(cmdList[numCommands].cmdString, cmdString, INPUT_BUFFER_MAX); - cmdList[numCommands].cmdString[INPUT_BUFFER_MAX - 1] = '\0'; // Make sure that the command string is null terminated - cmdList[numCommands].stringLength = strlen(cmdList[numCommands].cmdString); - cmdList[numCommands].fpointer = fpointer; - numCommands++; - } - else - { - serial.printf("error: too many commands"); - } -} - - - -void Terminal::receive() -{ - char c = serial.getc(); - int len = strlen(inputBuffer); - - if (isprint(c)) - { - if (len < INPUT_BUFFER_MAX - 1) - { - inputBuffer[len] = c; - inputBuffer[len + 1] = '\0'; - serial.putc(c); - } - } - else if (c == '\b' || c == 127) // Backspace - { - if (len > 0) - { - inputBuffer[len - 1] = '\0'; - serial.printf("\b \b"); - } - } - else if (c == '\n') - { - serial.putc('\n'); - - // Try to match the input string to a command, and call the associated function if a match is found - for (int i = 0; i < NUM_COMMANDS_MAX; i++) - { - if (cmdList[i].stringLength && !strncmp(inputBuffer, cmdList[i].cmdString, cmdList[i].stringLength)) - { - cmdList[i].fpointer(serial, inputBuffer); - goto foundCommand; - } - } - - // No match was found - serial.printf("unrecognized command"); - - foundCommand: - serial.printf("\n> "); - inputBuffer[0] = '\0'; // Clear the input buffer - } +#include "Terminal.h" +#include "mbed.h" +#include <cctype> +#include <cstring> + + + +Terminal::Terminal(PinName tx, PinName rx, int baudrate) : serial(tx, rx) +{ + // Initialize certain values + numCommands = 0; + for (int i = 0; i < NUM_COMMANDS_MAX; i++) + { + cmdList[i].stringLength = 0; + } + + // Set up serial connection + serial.baud(baudrate); + serial.attach(this, &Terminal::receive, Serial::RxIrq); + serial.printf("\n> "); +} + + + +void Terminal::addCommand(const char* cmdString, void (*fpointer)(Serial&, const char*) ) +{ + if (numCommands < NUM_COMMANDS_MAX) + { + strncpy(cmdList[numCommands].cmdString, cmdString, INPUT_BUFFER_MAX); + cmdList[numCommands].cmdString[INPUT_BUFFER_MAX - 1] = '\0'; // Make sure that the command string is null terminated + cmdList[numCommands].stringLength = strlen(cmdList[numCommands].cmdString); + cmdList[numCommands].fpointer = fpointer; + numCommands++; + } + else + { + serial.printf("error: too many commands"); + } +} + + + +void Terminal::receive() +{ + char c = serial.getc(); + int len = strlen(inputBuffer); + + if (isprint(c)) + { + if (len < INPUT_BUFFER_MAX - 1) + { + inputBuffer[len] = c; + inputBuffer[len + 1] = '\0'; + serial.putc(c); + } + } + else if (c == '\b' || c == 127) // Backspace + { + if (len > 0) + { + inputBuffer[len - 1] = '\0'; + serial.printf("\b \b"); + } + } + else if (c == '\n') + { + serial.putc('\n'); + + bool matchFound; + + // Try to match the input string to a command, and call the associated function if a match is found + for (int i = 0; i < NUM_COMMANDS_MAX; i++) + { + if (cmdList[i].stringLength && !strncmp(inputBuffer, cmdList[i].cmdString, cmdList[i].stringLength)) + { + cmdList[i].fpointer(serial, inputBuffer); + matchFound = true; + break; + } + } + + // No match was found + if (!matchFound) serial.printf("unrecognized command"); + + serial.printf("\n> "); + inputBuffer[0] = '\0'; // Clear the input buffer + } } \ No newline at end of file