![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
First version
Dependencies: mbed EthernetInterface mbed-rto
Interpreter.cpp@35:efdbfccf2678, 2018-05-02 (annotated)
- Committer:
- KlaasGovaerts
- Date:
- Wed May 02 06:53:40 2018 +0000
- Revision:
- 35:efdbfccf2678
- Parent:
- 30:915f6cb7ffa5
Added documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
KlaasGovaerts | 18:5ee34e60a31d | 1 | #include "Interpreter.h" |
KlaasGovaerts | 18:5ee34e60a31d | 2 | |
KlaasGovaerts | 35:efdbfccf2678 | 3 | /** |
KlaasGovaerts | 35:efdbfccf2678 | 4 | * @param queue. A pointer to the queue where the commands will be written. |
KlaasGovaerts | 35:efdbfccf2678 | 5 | */ |
KlaasGovaerts | 30:915f6cb7ffa5 | 6 | Interpreter::Interpreter(Queue<int,8>* queue){ |
KlaasGovaerts | 24:bf62c46acb3e | 7 | Interpreter::queue=queue; |
KlaasGovaerts | 18:5ee34e60a31d | 8 | } |
KlaasGovaerts | 18:5ee34e60a31d | 9 | |
KlaasGovaerts | 35:efdbfccf2678 | 10 | /** |
KlaasGovaerts | 35:efdbfccf2678 | 11 | * Interprets a text command, and push the result on the queue. |
KlaasGovaerts | 35:efdbfccf2678 | 12 | * Syntax: |
KlaasGovaerts | 35:efdbfccf2678 | 13 | * The number of the LED |
KlaasGovaerts | 35:efdbfccf2678 | 14 | * Dash (-) |
KlaasGovaerts | 35:efdbfccf2678 | 15 | * The orientation (left,right,up,down or ignore). |
KlaasGovaerts | 35:efdbfccf2678 | 16 | * Multiple LEDS can be set up in one command, by using a whitespace. |
KlaasGovaerts | 35:efdbfccf2678 | 17 | * Example: 0-left 1-right 2-up |
KlaasGovaerts | 35:efdbfccf2678 | 18 | * @param command A character array representing the command. |
KlaasGovaerts | 35:efdbfccf2678 | 19 | */ |
KlaasGovaerts | 18:5ee34e60a31d | 20 | void Interpreter::executeCommand(char* command){ |
KlaasGovaerts | 24:bf62c46acb3e | 21 | for(int i=0;i<8;i++){ |
KlaasGovaerts | 24:bf62c46acb3e | 22 | LED[i]=-1; |
KlaasGovaerts | 30:915f6cb7ffa5 | 23 | } //Set all default on -1 |
KlaasGovaerts | 24:bf62c46acb3e | 24 | |
KlaasGovaerts | 20:fe6a58e84929 | 25 | char *ID=strtok(command,"-"); |
KlaasGovaerts | 18:5ee34e60a31d | 26 | char *direction=strtok(NULL," "); |
KlaasGovaerts | 18:5ee34e60a31d | 27 | while((ID!=NULL)&&(direction!=NULL)){ |
KlaasGovaerts | 18:5ee34e60a31d | 28 | int IDNumber=atoi(ID); |
KlaasGovaerts | 18:5ee34e60a31d | 29 | int directionNumber=directionToNumber(direction); |
KlaasGovaerts | 18:5ee34e60a31d | 30 | if((IDNumber>=0)&&(IDNumber<=11)&&(directionNumber>=-1)&&(directionNumber<=3)){ |
KlaasGovaerts | 18:5ee34e60a31d | 31 | //lock->lock(); |
KlaasGovaerts | 18:5ee34e60a31d | 32 | LED[IDNumber]=directionNumber; |
KlaasGovaerts | 18:5ee34e60a31d | 33 | //lock->unlock(); |
KlaasGovaerts | 18:5ee34e60a31d | 34 | } |
KlaasGovaerts | 20:fe6a58e84929 | 35 | ID=strtok(NULL,"-"); |
KlaasGovaerts | 18:5ee34e60a31d | 36 | direction=strtok(NULL," "); |
KlaasGovaerts | 18:5ee34e60a31d | 37 | } |
KlaasGovaerts | 24:bf62c46acb3e | 38 | |
KlaasGovaerts | 24:bf62c46acb3e | 39 | for(int i=0;i<8;i++){ |
KlaasGovaerts | 24:bf62c46acb3e | 40 | queue->put(&LED[i]); |
KlaasGovaerts | 24:bf62c46acb3e | 41 | } |
KlaasGovaerts | 24:bf62c46acb3e | 42 | |
KlaasGovaerts | 24:bf62c46acb3e | 43 | for(int i=0;i<8;i++){ |
KlaasGovaerts | 24:bf62c46acb3e | 44 | printf("LED %i, richting: %i\r\n",i,LED[i]); |
KlaasGovaerts | 24:bf62c46acb3e | 45 | } |
KlaasGovaerts | 18:5ee34e60a31d | 46 | } |
KlaasGovaerts | 18:5ee34e60a31d | 47 | |
KlaasGovaerts | 35:efdbfccf2678 | 48 | /** |
KlaasGovaerts | 35:efdbfccf2678 | 49 | * Convert a direction such as left, right, up, down to the corresponding number (0, 1, 2 or 3). |
KlaasGovaerts | 35:efdbfccf2678 | 50 | * Invalid directions (or ignore) will return -1. |
KlaasGovaerts | 35:efdbfccf2678 | 51 | * @param command A character array representing the direction. |
KlaasGovaerts | 35:efdbfccf2678 | 52 | */ |
KlaasGovaerts | 18:5ee34e60a31d | 53 | int Interpreter::directionToNumber(char* direction){ |
KlaasGovaerts | 20:fe6a58e84929 | 54 | if(strcmp(direction,"left")==0) |
KlaasGovaerts | 18:5ee34e60a31d | 55 | return 0; |
KlaasGovaerts | 20:fe6a58e84929 | 56 | else if(strcmp(direction,"right")==0) |
KlaasGovaerts | 18:5ee34e60a31d | 57 | return 1; |
KlaasGovaerts | 20:fe6a58e84929 | 58 | else if(strcmp(direction,"up")==0) |
KlaasGovaerts | 18:5ee34e60a31d | 59 | return 2; |
KlaasGovaerts | 20:fe6a58e84929 | 60 | else if(strcmp(direction,"down")==0) |
KlaasGovaerts | 18:5ee34e60a31d | 61 | return 3; |
KlaasGovaerts | 18:5ee34e60a31d | 62 | else |
KlaasGovaerts | 18:5ee34e60a31d | 63 | return -1; |
KlaasGovaerts | 18:5ee34e60a31d | 64 | } |