First version

Dependencies:   mbed EthernetInterface mbed-rto

Interpreter.cpp

Committer:
KlaasGovaerts
Date:
2018-05-02
Revision:
35:efdbfccf2678
Parent:
30:915f6cb7ffa5

File content as of revision 35:efdbfccf2678:

#include "Interpreter.h"

/**
* @param queue. A pointer to the queue where the commands will be written.
*/
Interpreter::Interpreter(Queue<int,8>* queue){
    Interpreter::queue=queue;
}

/**
* Interprets a text command, and push the result on the queue.
* Syntax:
* The number of the LED
* Dash (-)
* The orientation (left,right,up,down or ignore).
* Multiple LEDS can be set up in one command, by using a whitespace.
* Example: 0-left 1-right 2-up
* @param command A character array representing the command.
*/
void Interpreter::executeCommand(char* command){
    for(int i=0;i<8;i++){
        LED[i]=-1;
    } //Set all default on -1
    
    char *ID=strtok(command,"-");
    char *direction=strtok(NULL," ");
    while((ID!=NULL)&&(direction!=NULL)){
        int IDNumber=atoi(ID);
        int directionNumber=directionToNumber(direction);
        if((IDNumber>=0)&&(IDNumber<=11)&&(directionNumber>=-1)&&(directionNumber<=3)){
            //lock->lock();
            LED[IDNumber]=directionNumber;
            //lock->unlock();
        }
    ID=strtok(NULL,"-");
    direction=strtok(NULL," ");
    }
    
    for(int i=0;i<8;i++){
        queue->put(&LED[i]);
    }
    
    for(int i=0;i<8;i++){
        printf("LED %i, richting: %i\r\n",i,LED[i]);
    }
}

/**
* Convert a direction such as left, right, up, down to the corresponding number (0, 1, 2 or 3).
* Invalid directions (or ignore) will return -1.
* @param command A character array representing the direction.
*/
int Interpreter::directionToNumber(char* direction){
    if(strcmp(direction,"left")==0)
        return 0;
    else if(strcmp(direction,"right")==0)
        return 1;
    else if(strcmp(direction,"up")==0)
        return 2;
    else if(strcmp(direction,"down")==0)
        return 3;
    else 
        return -1;
}