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.
Terminal.cpp
- Committer:
- UCSBRobotics
- Date:
- 2012-08-02
- Revision:
- 4:4bcb955b81f3
- Parent:
- 3:3c2cc2ea12a9
- Child:
- 6:4269b879a2af
File content as of revision 4:4bcb955b81f3:
#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 = false; // 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 } }