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-01
- Revision:
- 2:e47cc8c92b3f
- Parent:
- 0:6b2bae4e0481
- Child:
- 3:3c2cc2ea12a9
File content as of revision 2:e47cc8c92b3f:
#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 } }