fasdf gfaha / Terminal
Committer:
UCSBRobotics
Date:
Thu Aug 02 02:15:06 2012 +0000
Revision:
4:4bcb955b81f3
Parent:
3:3c2cc2ea12a9
Child:
6:4269b879a2af
Explicitly initialized flag added in last commit

Who changed what in which revision?

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