3rd year group project. Electronic and Electrical Engineering. Heriot-Watt University. This is the code for the mbed for the Automatic Little Object Organiser (ALOO).

Dependencies:   MCP23017 TCS3472_I2C WattBob_TextLCD mbed

commander.cpp

Committer:
lordlycastle
Date:
2015-11-16
Revision:
5:6ef2d954fca3
Parent:
3:843b830ee8bd
Child:
6:98fe30430194

File content as of revision 5:6ef2d954fca3:

#include <string>
#include <iostream>
#include "commander.h"
#include "globals.h"

//enum Type { INVALID = -1, QUERY = 0, SET, REPLY };
//enum CObject { MBED = 0, COLOUR_SENSOR, SERVOS };

/** Listen serial port for either of the command prefix i.e. '?' or '!' or '=' 
    @return -1:not correct prefix, otherwise 0:QUERY, 1:SET, 2:REPLY*/
Type listenForCommand(){
    char commandType;
    pc.printf("In listen");
    commandType = pc.getc();
    
    if (commandType == '?'){
        return QUERY;
    }
    else if (commandType == '!'){
        return SET;
    }
    else if (commandType == '='){
        return REPLY;
    }
    else{
        return INVALID;
    }
}

void recieveCommand(Command *cmd, Type type){
//    pc.printf("In recieve command\n");
    
    switch (type) {
        case QUERY:
            cmd->inputString[0] = '?';
            cmd->commandType[0] = '?';
            break;
        case SET:
            cmd->inputString[0] = '!';
            cmd->commandType[0] = '!';
            break;
        case REPLY:
            cmd->inputString[0] = ':';
            cmd->commandType[0] = ':';
            break;
        default:
            break;
    }
    
    char nextChar;
    
    int commandObjectIdx = 0;
    bool recordCommandObject = false;
    int commandStringIdx = 0;
    bool recordCommandString = false;
    
    for (int i = 1; i < gCommandBufferSize; i++){
        nextChar = pc.getc();
        cmd->inputString[i] = nextChar;
        
        if (nextChar == gCommandTerminator){
            pc.printf("nextChar == Terminator\r\n");
            pc.printf("%s\r\n", cmd->inputString);
//            cmd->commandString[commandStringIdx - 1] = '\0';
            break;
        }
        
        if (recordCommandObject){
            cmd->commandObject[commandObjectIdx] = nextChar;
            commandObjectIdx++;
        } else if (recordCommandString) {
            cmd->commandString[commandStringIdx] = nextChar;
            commandStringIdx++;
        }
        
        if (nextChar == '<')
            recordCommandObject = true;
        else if (nextChar == '>'){
            recordCommandObject = false;
            cmd->commandObject[commandObjectIdx - 1] = '\0';
            recordCommandString = true;
        }
    }
    
    if (cmd->commandObject == "mbed")
        cmd->objectEnum = MBED;
    else if (cmd->commandObject == "colour_sensor")
        cmd->objectEnum = COLOUR_SENSOR;
}

void decodeCommand(Command *cmd)
{
    switch (cmd->typeEnum) {
        case QUERY: {
            switch (cmd->objectEnum) {
                case MBED: {
                    string commandString = cmd->commandString;
                    const string init = "init";
                    if (cmd->commandString == "init") {
                        // Convert gParity from Pairty to string
                        pc.printf("%s<%s>baudRate:%f,parity:none,stopbits:%i;\r\n", cmd->commandType, cmd->commandObject, gBaudRate, gStopBits);
                    }
                    break;
                }
                default:
                    pc.printf("Going to 2rd default\r\n");
                    break;
            }
            break;
        }
        default:
            pc.printf("Going to 1rd default\r\n");
            break;
    }
}

//string parseReply(Command *cmd){
//    
//}