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:
- 0:6b2bae4e0481
- Child:
- 2:e47cc8c92b3f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Terminal.cpp Wed Aug 01 22:26:52 2012 +0000 @@ -0,0 +1,81 @@ +#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 == 127) // Backspace + { + if (len > 0) + { + inputBuffer[len - 1] = '\0'; + serial.printf("\b \b"); + } + } + else if (c == '\n') + { + serial.putc('\n'); + + 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); + break; + } + } + + serial.putc('\n'); + inputBuffer[0] = '\0'; // Clear the input buffer + } +} \ No newline at end of file