-
Notifications
You must be signed in to change notification settings - Fork 2
/
Controller.cpp
57 lines (47 loc) · 1.25 KB
/
Controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Controller.cpp
*
* Created on: 5 Jun 2012
* Author: fletcherb
*/
#include "Controller.h"
Controller::Controller(HardwareSerial* iSerial) {
Serial = iSerial;
Serial->begin(9600);
Serial->println("Controller is Starting...");
commandInstruction = "";
}
void Controller::setCommandArray(Command *inCommand, int inArraySize){
command = inCommand;
commandArraySize = inArraySize;
Serial->print("Setting Command array: array size: ");
Serial->println(inArraySize);
}
Controller::~Controller() {
}
void Controller::loop() {
if (Serial->available()>0){
char i = Serial->read();
if (i!=10){
commandInstruction += i;
} else {
bool runCommand = false;
for (int i = 0; i< commandArraySize; i++){
int locOfSpace = commandInstruction.indexOf(' ');
String parameter = commandInstruction.substring(locOfSpace);
if (commandInstruction.startsWith(command[i].getInstruction())){
command[i].execute(parameter);
runCommand = true;
}
}
if (!runCommand){
unknownCommand(commandInstruction);
}
commandInstruction = "";
}
}
}
void Controller::unknownCommand(String instruction){
Serial->print("Unknown command: ");
Serial->println(instruction);
}