-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSensors.cpp
63 lines (55 loc) · 1.47 KB
/
Sensors.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
58
59
60
61
62
63
#include "Sensors.h"
#include "Arduino.h"
const int MAX_COMMAND = 32;
const int MAX_ITEM = 32;
void Sensors::sendUpdates() {
for(unsigned char i = 0; i < numSensors; i++) {
sensors[i]->printStatus(stream);
}
}
void Sensors::processCommand(const char* const item, const char* const command) {
for(unsigned char i = 0; i < numActors; i++) {
if(strncmp(item, actors[i]->getName(), MAX_ITEM) == 0) {
actors[i]->setCommand(command);
actors[i]->printStatus(stream);
break;
}
}
}
void Sensors::updateSensors() {
for(int i = 0; i < numActors; i++) {
actors[i]->execute();
}
}
int Sensors::readBlocking() {
while(stream.available() == 0);
return stream.read();
}
void Sensors::process() {
if(stream.available() > 0) {
char item[MAX_ITEM];
memset(item, '\0', sizeof(item));
int i = 0;
for(; i < MAX_ITEM; i++) {
item[i] = readBlocking();
if(item[i] == '=') {
item[i] = '\0';
break;
}
}
//stream.println(item);
char command[MAX_COMMAND];
memset(command, '\0', sizeof(command));
for(i = 0; i < MAX_COMMAND; i++) {
command[i] = readBlocking();
if(command[i] == '\n') {
command[i] = '\0';
break;
}
}
//stream.println(command);
processCommand(item, command);
}
updateSensors();
sendUpdates();
}