-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBluetooth.ino
130 lines (119 loc) · 3.84 KB
/
Bluetooth.ino
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
String serialString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void initBluetooth() {
serialString.reserve(100);
}
void checkBtMessage() {
if (stringComplete) {
// Handle bluetooth message, then clear string
handleBluetoothMessage(serialString);
serialString = "";
stringComplete = false;
}
}
void handleBluetoothMessage(String msg) {
Serial.println(msg);
// Handle message
if (msg.equals("connect")) {
sendBoardStatus();
sendBatteryInfo();
sendTripInfo();
showAlert(0, 0, 0, 0, 0, 255, 10);
playConnectSE();
showAlert(0, 0, 255, 0, 0, 0, 200);
} else if (msg.equalsIgnoreCase("status=standby")) {
switchMode(STANDBY);
} else if (msg.equalsIgnoreCase("status=drive")) {
switchMode(DRIVING);
} else if (msg.equalsIgnoreCase("status=diagnostics")) {
switchMode(DIAGNOSTICS);
} else if (msg.equalsIgnoreCase("locked=true")) {
switchMode(LOCKED);
saveBoolean(EEPROM_LOCKED, true);
} else if (msg.equalsIgnoreCase("locked=false")) {
switchMode(STANDBY);
saveBoolean(EEPROM_LOCKED, false);
} else if (msg.equalsIgnoreCase("standbyLights=1")) {
showAlert(0, 0, 0, 0, 0, 255, 10);
standbyLights = true;
saveBoolean(EEPROM_STANDBY_LIGHTS, standbyLights);
showAlert(0, 0, 255, 0, 0, 0, 200);
} else if (msg.equalsIgnoreCase("standbyLights=0")) {
showAlert(0, 0, 0, 0, 0, 255, 10);
standbyLights = false;
saveBoolean(EEPROM_STANDBY_LIGHTS, standbyLights);
showAlert(0, 0, 255, 0, 0, 0, 200);
lightsOff();
} else if (msg.equalsIgnoreCase("drivingLights=1")) {
showAlert(0, 0, 0, 0, 0, 255, 10);
drivingLights = true;
saveBoolean(EEPROM_DRIVING_LIGHTS, drivingLights);
showAlert(0, 0, 255, 0, 0, 0, 200);
} else if (msg.equalsIgnoreCase("drivingLights=0")) {
showAlert(0, 0, 0, 0, 0, 255, 10);
drivingLights = false;
saveBoolean(EEPROM_DRIVING_LIGHTS, drivingLights);
showAlert(0, 0, 255, 0, 0, 0, 200);
} else if (msg.equalsIgnoreCase("endTrip=1")) { // ????
showAlert(0, 0, 0, 0, 0, 255, 10);
resetTrip();
showAlert(0, 0, 255, 0, 0, 0, 200);
} else if (msg.equalsIgnoreCase("calibrate=1")) {
showAlert(0, 0, 0, 255, 0, 0, 10);
mirrorLedSweep(0, 0, 0, 40, 0, 0, 255, 0, 0, 4, 1000);
mirrorLedSweep(255, 0, 0, 40, 0, 0, 0, 0, 0, 0.25, 1000);
runCalibration();
playCalibrationSE();
showAlert(255, 0, 0, 0, 0, 0, 200);
} else if (msg.startsWith("weight=")) {
if (mode == DIAGNOSTICS) {
String data = msg.substring(7); // data = "xx,xx"
int front = data.toInt();
int back = data.substring(data.indexOf(",")+ 1).toInt();
setVirtualWeight(front, back);
}
}
}
void sendBoardStatus() {
Serial.println("model=Momentum Alpha");
switchMode(mode); // <- Will print out current mode
}
void sendTripInfo() {
Serial.print("currenttrip=");
Serial.println(getCurrentTripInKmh());
Serial.print("totaltrip=");
Serial.println(getTotalTripInKmh());
}
void sendBatteryInfo() {
// Todo
Serial.println("battery=91");
}
void sendKmh() {
Serial.print("kmh=");
Serial.println(getSpeedInKmh());
}
void sendWeightSensorValues(int front, int back) {
Serial.print("sensor=");
Serial.print(front);
Serial.print(",");
Serial.println(back);
}
// Called automatically whenever there is serial data in the buffer
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
// Testing new bt handler:
handleBluetoothMessage(serialString);
serialString = "";
stringComplete = false;
} else {
// add it to the serialString:
serialString += inChar;
}
}
}