fasdf gfaha / Terminal
Committer:
UCSBRobotics
Date:
Wed Aug 01 23:51:08 2012 +0000
Revision:
2:e47cc8c92b3f
Parent:
0:6b2bae4e0481
Child:
3:3c2cc2ea12a9
Changed default input buffer size and removed output buffer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
UCSBRobotics 0:6b2bae4e0481 1 #include "Terminal.h"
UCSBRobotics 0:6b2bae4e0481 2 #include "mbed.h"
UCSBRobotics 0:6b2bae4e0481 3 #include <cctype>
UCSBRobotics 0:6b2bae4e0481 4 #include <cstring>
UCSBRobotics 0:6b2bae4e0481 5
UCSBRobotics 0:6b2bae4e0481 6
UCSBRobotics 0:6b2bae4e0481 7
UCSBRobotics 0:6b2bae4e0481 8 Terminal::Terminal(PinName tx, PinName rx, int baudrate) : serial(tx, rx)
UCSBRobotics 0:6b2bae4e0481 9 {
UCSBRobotics 0:6b2bae4e0481 10 // Initialize certain values
UCSBRobotics 0:6b2bae4e0481 11 numCommands = 0;
UCSBRobotics 0:6b2bae4e0481 12 for (int i = 0; i < NUM_COMMANDS_MAX; i++)
UCSBRobotics 0:6b2bae4e0481 13 {
UCSBRobotics 0:6b2bae4e0481 14 cmdList[i].stringLength = 0;
UCSBRobotics 0:6b2bae4e0481 15 }
UCSBRobotics 0:6b2bae4e0481 16
UCSBRobotics 0:6b2bae4e0481 17 // Set up serial connection
UCSBRobotics 0:6b2bae4e0481 18 serial.baud(baudrate);
UCSBRobotics 0:6b2bae4e0481 19 serial.attach(this, &Terminal::receive, Serial::RxIrq);
UCSBRobotics 0:6b2bae4e0481 20 serial.printf("\n> ");
UCSBRobotics 0:6b2bae4e0481 21 }
UCSBRobotics 0:6b2bae4e0481 22
UCSBRobotics 0:6b2bae4e0481 23
UCSBRobotics 0:6b2bae4e0481 24
UCSBRobotics 0:6b2bae4e0481 25 void Terminal::addCommand(const char* cmdString, void (*fpointer)(Serial&, const char*) )
UCSBRobotics 0:6b2bae4e0481 26 {
UCSBRobotics 0:6b2bae4e0481 27 if (numCommands < NUM_COMMANDS_MAX)
UCSBRobotics 0:6b2bae4e0481 28 {
UCSBRobotics 0:6b2bae4e0481 29 strncpy(cmdList[numCommands].cmdString, cmdString, INPUT_BUFFER_MAX);
UCSBRobotics 0:6b2bae4e0481 30 cmdList[numCommands].cmdString[INPUT_BUFFER_MAX - 1] = '\0'; // Make sure that the command string is null terminated
UCSBRobotics 0:6b2bae4e0481 31 cmdList[numCommands].stringLength = strlen(cmdList[numCommands].cmdString);
UCSBRobotics 0:6b2bae4e0481 32 cmdList[numCommands].fpointer = fpointer;
UCSBRobotics 0:6b2bae4e0481 33 numCommands++;
UCSBRobotics 0:6b2bae4e0481 34 }
UCSBRobotics 0:6b2bae4e0481 35 else
UCSBRobotics 0:6b2bae4e0481 36 {
UCSBRobotics 0:6b2bae4e0481 37 serial.printf("error: too many commands");
UCSBRobotics 0:6b2bae4e0481 38 }
UCSBRobotics 0:6b2bae4e0481 39 }
UCSBRobotics 0:6b2bae4e0481 40
UCSBRobotics 0:6b2bae4e0481 41
UCSBRobotics 0:6b2bae4e0481 42
UCSBRobotics 0:6b2bae4e0481 43 void Terminal::receive()
UCSBRobotics 0:6b2bae4e0481 44 {
UCSBRobotics 0:6b2bae4e0481 45 char c = serial.getc();
UCSBRobotics 0:6b2bae4e0481 46 int len = strlen(inputBuffer);
UCSBRobotics 0:6b2bae4e0481 47
UCSBRobotics 0:6b2bae4e0481 48 if (isprint(c))
UCSBRobotics 0:6b2bae4e0481 49 {
UCSBRobotics 0:6b2bae4e0481 50 if (len < INPUT_BUFFER_MAX - 1)
UCSBRobotics 0:6b2bae4e0481 51 {
UCSBRobotics 0:6b2bae4e0481 52 inputBuffer[len] = c;
UCSBRobotics 0:6b2bae4e0481 53 inputBuffer[len + 1] = '\0';
UCSBRobotics 0:6b2bae4e0481 54 serial.putc(c);
UCSBRobotics 0:6b2bae4e0481 55 }
UCSBRobotics 0:6b2bae4e0481 56 }
UCSBRobotics 2:e47cc8c92b3f 57 else if (c == '\b' || c == 127) // Backspace
UCSBRobotics 0:6b2bae4e0481 58 {
UCSBRobotics 0:6b2bae4e0481 59 if (len > 0)
UCSBRobotics 0:6b2bae4e0481 60 {
UCSBRobotics 0:6b2bae4e0481 61 inputBuffer[len - 1] = '\0';
UCSBRobotics 0:6b2bae4e0481 62 serial.printf("\b \b");
UCSBRobotics 0:6b2bae4e0481 63 }
UCSBRobotics 0:6b2bae4e0481 64 }
UCSBRobotics 0:6b2bae4e0481 65 else if (c == '\n')
UCSBRobotics 0:6b2bae4e0481 66 {
UCSBRobotics 0:6b2bae4e0481 67 serial.putc('\n');
UCSBRobotics 0:6b2bae4e0481 68
UCSBRobotics 2:e47cc8c92b3f 69 // Try to match the input string to a command, and call the associated function if a match is found
UCSBRobotics 0:6b2bae4e0481 70 for (int i = 0; i < NUM_COMMANDS_MAX; i++)
UCSBRobotics 0:6b2bae4e0481 71 {
UCSBRobotics 0:6b2bae4e0481 72 if (cmdList[i].stringLength && !strncmp(inputBuffer, cmdList[i].cmdString, cmdList[i].stringLength))
UCSBRobotics 0:6b2bae4e0481 73 {
UCSBRobotics 0:6b2bae4e0481 74 cmdList[i].fpointer(serial, inputBuffer);
UCSBRobotics 2:e47cc8c92b3f 75 goto foundCommand;
UCSBRobotics 0:6b2bae4e0481 76 }
UCSBRobotics 0:6b2bae4e0481 77 }
UCSBRobotics 0:6b2bae4e0481 78
UCSBRobotics 2:e47cc8c92b3f 79 // No match was found
UCSBRobotics 2:e47cc8c92b3f 80 serial.printf("unrecognized command");
UCSBRobotics 2:e47cc8c92b3f 81
UCSBRobotics 2:e47cc8c92b3f 82 foundCommand:
UCSBRobotics 2:e47cc8c92b3f 83 serial.printf("\n> ");
UCSBRobotics 0:6b2bae4e0481 84 inputBuffer[0] = '\0'; // Clear the input buffer
UCSBRobotics 0:6b2bae4e0481 85 }
UCSBRobotics 0:6b2bae4e0481 86 }