-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReceiveIR.ino
202 lines (159 loc) · 8.08 KB
/
ReceiveIR.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CreateIRmessage() // TODO: Currently not checking for valid -2mS breaks !!!!
{
if (irPacketLength < 6) // The message was incomplete.
{
decodedIRmessage.type = 's';
decodedIRmessage.newMessage = TRUE;
return;
}
if (messageIR[1] == 3 && messageIR[2] == -6); // We have a good header.
else
{
decodedIRmessage.type = 'h';
decodedIRmessage.newMessage = TRUE;
return;
}
////---------------------------------------------------------------------------------------------------------
// Set the message type via the number of bits received (and 1/9th bit of Packet/Checksum)
if (irPacketLength > 20 && messageIR[3] == 3 && messageIR[5] == 0) decodedIRmessage.type = 'P'; // Packet
else if (irPacketLength > 20 && messageIR[3] == 3 && messageIR[5] == 1) decodedIRmessage.type = 'C'; // Checksum
else if (irPacketLength > 18 && messageIR[3] == 3 && irPacketLength < 21) decodedIRmessage.type = 'D'; // Data
else if (irPacketLength > 16 && messageIR[3] == 3 && irPacketLength < 19) decodedIRmessage.type = 'T'; // Tag
else if (irPacketLength > 12 && messageIR[3] == 6 && irPacketLength < 15) decodedIRmessage.type = 'B'; // Beacon - only beacons have 3/6/6 header !!
else {
decodedIRmessage.type = 'x';
decodedIRmessage.newMessage = TRUE;
return;
}
////---------------------------------------------------------------------------------------------------------
// Set the message length based on the type
byte messageLength = 0;
if (decodedIRmessage.type == 'T') messageLength = 17; // Long Break [0] + 3 header [1,2,3] + break [4] + 7 bits,breaks [5,7,9,11,13,15,17]
else if (decodedIRmessage.type == 'B') messageLength = 13; // Long Break [0] + 3 header [1,2,3] + break [4] + 5 bits,breaks [5,7,9,11,13]
else if (decodedIRmessage.type == 'P') messageLength = 21; // Long Break [0] + 3 header [1,2,3] + break [4] + 9 bits,breaks [5,7,9,11,13,15,17,19,21]
else if (decodedIRmessage.type == 'D') messageLength = 19; // Long Break [0] + 3 header [1,2,3] + break [4] + 8 bits,breaks [5,7,9,11,13,15,17,19]
else if (decodedIRmessage.type == 'C') messageLength = 21; // Long Break [0] + 3 header [1,2,3] + break [4] + 9 bits,breaks [5,7,9,11,13,15,17,19,21]
////---------------------------------------------------------------------------------------------------------
// Push the data into the dataPacket
for (int i = 5; i<=messageLength; i+=2)
{
decodedIRmessage.rawDataPacket = decodedIRmessage.rawDataPacket << 1;
decodedIRmessage.rawDataPacket = decodedIRmessage.rawDataPacket + (messageIR [i]);
}
////---------------------------------------------------------------------------------------------------------
// Tidy up and go home
decodedIRmessage.newMessage = TRUE; // Set the flag to say there is a new message to decode.
digitalWrite(DE_BUG_TIMING_PIN, LOW);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool DecodeIR()
{
if (decodedIRmessage.newMessage == FALSE) return FALSE;
if (decodedIRmessage.type == 'T')
{
// Find TeamID of tagger
decodedIRmessage.TeamID = decodedIRmessage.rawDataPacket & B01100000; // TeamID = 1 thru 3 (0 = NoTeams)
decodedIRmessage.TeamID = decodedIRmessage.TeamID >> 5;
// Find PlayerID
decodedIRmessage.PlayerID = (decodedIRmessage.rawDataPacket & B00011100); // PlayerID = 1 thru 8
decodedIRmessage.PlayerID = (decodedIRmessage.PlayerID >> 2) + 1;
// Find tag Power
decodedIRmessage.ShotStrength = (decodedIRmessage.rawDataPacket & B00000011)+1; // Tag strength = 1 to 4
}
//TODO: Check for a bad 3/6 Tag packet and then flag as a near miss !!
else if (decodedIRmessage.type == 'B')
{
// Find TeamID of Beacon
decodedIRmessage.TeamID = decodedIRmessage.rawDataPacket & B11000; // TeamID = 1 thru 3 (0 = NoTeams)
decodedIRmessage.TeamID = decodedIRmessage.TeamID >> 3;
// Check the TagBeacon flag
decodedIRmessage.TagReceivedBeacon = (decodedIRmessage.rawDataPacket & B00100); // Sets flag for whether the beacon was sent because of receiving a tag.
decodedIRmessage.TagReceivedBeacon = (decodedIRmessage.TagReceivedBeacon >> 2);
// Find tag Power
decodedIRmessage.ShotStrength = (decodedIRmessage.rawDataPacket & B00011)+1; // Tag strength = 1 to 4 - MUST be Zero if TagBeacon flag above is FALSE.
//TODO: Implement Zone Beacons and LTAR beacons www.wiki.lazerswarm.com/wiki/Beacon_Signature.html
// (if TeamID = 0 in a hosted game, then it is a Medic Beacon)
}
else if (decodedIRmessage.type == 'P')
{
byteCount = 0;
decodedIRmessage.PacketByte = decodedIRmessage.rawDataPacket & B11111111;
CheckSumRx = 0;
decodedIRmessage.CheckSumOK == FALSE;
}
else if (decodedIRmessage.type == 'D')
{
byteCount++;
decodedIRmessage.DataByte = decodedIRmessage.rawDataPacket & B11111111;
}
else if (decodedIRmessage.type == 'C')
{
decodedIRmessage.CheckSumRxByte = decodedIRmessage.rawDataPacket & B11111111;
}
else if (decodedIRmessage.type == 's') badMessage_CountISRshortPacket++;
else if (decodedIRmessage.type == 'x') badMessage_InvalidPacketType++;
else if (decodedIRmessage.type == 'h') badMessage_non3_6Header++;
decodedIRmessage.newMessage = FALSE;
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ProcessRxPacket()
{
CheckSumRx = 0;
CheckSumRx = CheckSumRx + decodedIRmessage.PacketByte;
gameIDmatch = FALSE;
taggerID = 0;
AckPlayerAssignOK = FALSE;
switch (decodedIRmessage.PacketByte) // Currently only implementing messages relating to Hosting and ignoring others.
{
case 0x10:
decodedIRmessage.PacketName = "RequestJoinGame";
Serial.print(F("\nRx'd RequestJoinGame : "));
break;
case 0x11:
decodedIRmessage.PacketName = "AckPlayerAssign";
Serial.print(F("\nAckPlayerAssign"));
break;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ProcessRxDataByte()
{
CheckSumRx = CheckSumRx + decodedIRmessage.rawDataPacket;
if (decodedIRmessage.PacketName == "RequestJoinGame") ActionRequestJoinGameDataByte();
if (decodedIRmessage.PacketName == "AckPlayerAssign") ActionAcknowledgePlayerAssignDataByte();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ProcessRxCheckSum()
{
if (CheckSumRx == decodedIRmessage.CheckSumRxByte) decodedIRmessage.CheckSumOK = TRUE;
else decodedIRmessage.CheckSumOK = FALSE;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
void PrintIR(char mode)
{
if (mode == 'X')
{
Serial.print(F("\nPrintIR(ext): "));
for (int i = 0; i<= ARRAY_LENGTH; i++)
{
if (messageIR[i] != 42)
{
Serial.print(messageIR [i]);
Serial.print(F(", "));
}
}
}
else if (mode == 'S')
{
Serial.print(F("\nPrintIR: "));
Serial.print(decodedIRmessage.type);
Serial.print(F(", "));
Serial.print(decodedIRmessage.rawDataPacket, BIN);
Serial.println();
}
if (decodedIRmessage.type == 'C') Serial.println(F("-------------------"));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////