-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dashboard.cpp
116 lines (109 loc) · 3.19 KB
/
Dashboard.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "Dashboard.h"
Vector<Controller> Dashboard::controllers;
String Dashboard::dashMessage = "";
bool Dashboard::isMessage = false;
char Dashboard::nextChar;
int Dashboard::controllerIndex;
int Dashboard::axisIndex = -1;
int Dashboard::buttonIndex = -1;
int Dashboard::POVIndex = -1;
float Dashboard::newVal;
String Dashboard::message = "";
bool Dashboard::logging = false;
String logMessage = "";
void Dashboard::begin(){
Serial.begin(57600);
}
void Dashboard::startLogging(){
logging = true;
}
void Dashboard::stopLogging(){
logging = false;
}
Controller& Dashboard::getController(int ID){
while (controllerIndex > controllers.getLength()){
controllers.add(Controller());
}
return controllers.get(ID);
}
String Dashboard::getMessage(){
return dashMessage;
}
void Dashboard::clearMessage(){
dashMessage = "";
}
void Dashboard::println(String toPrint){
Serial.println(toPrint);
}
void Dashboard::updateData(){
while (Serial.available() > 0){
nextChar = (char)Serial.read();
isMessage = isMessage || (message.length() == 0 && nextChar == 'M');
if (nextChar != '\n'){
message += nextChar;
if (!isMessage){
switch(nextChar){
case 'C':
controllerIndex = message.substring(0,
message.indexOf('C')).toInt();
message = "";
break;
case 'B':
buttonIndex = message.substring(0,
message.indexOf('B')).toInt();
message = "";
break;
case 'A':
axisIndex = message.substring(0,
message.indexOf('A')).toInt();
message = "";
break;
case 'P':
POVIndex = message.substring(0,
message.indexOf('P')).toInt();
message = "";
break;
case 'V':
newVal = message.substring(0,
message.indexOf('V')).toFloat();
message = "";
break;
}
}
}
else{
if (isMessage){
message = message.substring(1);
dashMessage = "Arduino recieved: "+message;
}
else{
while (controllerIndex >= controllers.getLength()){
controllers.add(Controller());
}
if (axisIndex > -1){
controllers.get(controllerIndex).setAxis(axisIndex, newVal);
}
else if (buttonIndex > -1){
controllers.get(controllerIndex).setButton(buttonIndex, newVal);
}
else{
controllers.get(controllerIndex).setPOV(POVIndex, newVal);
}
if (logging){
logMessage = "Controller: "+String(controllerIndex)+"\n";
if (axisIndex > -1) logMessage += "Axis: "+String(axisIndex)+"\n";
else if (buttonIndex > -1) logMessage += "Button: "+String(buttonIndex)+"\n";
else logMessage += "POV: "+String(POVIndex)+"\n";
logMessage += "Value: "+String(newVal, 3);
Dashboard::println("");
Dashboard::println(logMessage);
}
}
isMessage = false;
axisIndex = -1;
buttonIndex = -1;
POVIndex = -1;
message = "";
}
}
}