-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMate.cpp
126 lines (112 loc) · 3.32 KB
/
Mate.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
116
117
118
119
120
121
122
123
124
125
126
#include "Mate.hpp"
// Implementierung: OBJEKTE
extern Display d;
extern Player p;
extern Led led;
//extern Mate mate;
extern Pilot m;
extern Ultrasonic us;
Mate::Mate() {
}
void Mate::send(byte data){
byte t_data[1];
t_data[0] = data;
send(t_data, 1); // avoid mate
}
/*****************************************************
übertrage Daten an den Patner
@param data: Datenarray
@param numberOfElements: Länge des Arrays
*****************************************************/
void Mate::send(byte * data, byte numberOfElements) {
if (BLUETOOTH && input.switch_bluetooth) {
BLUETOOTH_SERIAL.write(START_MARKER);
for (byte i = 0; i < numberOfElements; i++) {
BLUETOOTH_SERIAL.write(constrain(data[i], 0, 253));
}
BLUETOOTH_SERIAL.write(END_MARKER);
}
}
/*****************************************************
empfange Daten an des Patners
- speichere diese im Cache
*****************************************************/
byte Mate::fetch() {
// returns length of incomming message
if (BLUETOOTH && input.switch_bluetooth) {
while (BLUETOOTH_SERIAL.available()) {
byte b = BLUETOOTH_SERIAL.read();
if (cacheIndex != 255) { // aktives Zuhören?
if (b == START_MARKER) {
cacheIndex = 0; // aktiviere Zuhören
for (byte i = 0; i < CACHE_SIZE; i++) {
cache[i] = 255; // überschreibe den Cache
}
} else if (b == END_MARKER) {
byte messageLength = cacheIndex;
cacheIndex = 255; // deaktiviere Zuhören
return messageLength; // Befehl empfangen!
} else {
if (cacheIndex >= CACHE_SIZE) {
cacheIndex = 255; // deaktiviere Zuhören
} else {
cache[cacheIndex] = b; // speichere in Cache
cacheIndex += 1; // speichere index
}
}
} else {
if (b == START_MARKER) {
cacheIndex = 0; // aktiviere Zuhören
for (byte i = 0; i < CACHE_SIZE; i++) {
cache[i] = 255; // überschreibe den Cache
}
}
}
}
}
return 0;
}
/*****************************************************
lese Nachrichten aus dem Cache aus
*****************************************************/
byte Mate::receive() {
byte messageLength = fetch(); // aktualisiere den Cache
if (messageLength == 3 && cache[0] == 'h') {
/*****************************************************
Sende einen Herzschlag mit Statusinformationen an den Partner
Byte Information mögliche Zustände
-----------------------------------------------------
0 Pakettyps Heartbeat(104)
1 Status+Rolle Aus(0+Status) / Torwart(1+Status) / Stürmer(2+Status)
2 Score Blind(0) / Bewertung(...)
*****************************************************/
role = cache[1] / 10;
state = cache[1] % 10;
score = cache[2];
responseTimer = millis();
}
if (messageLength > 0) {
return cache[0];
} else {
return 255;
}
}
bool Mate::getMotEn() {
return role > 0;
}
bool Mate::isKeeper() {
return role == 1;
}
bool Mate::isRusher() {
return role == 2;
}
byte Mate::getState() {
return state;
}
byte Mate::getScore() {
return score;
}
unsigned long Mate::timeout() {
if (millis() - responseTimer < 500) return 0;
else return millis() - responseTimer;
}