forked from braidstag/arduino-milestag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.ino
151 lines (137 loc) · 4.44 KB
/
serial.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
char serialReadBuffer[64];
byte serialReadBufferOffset = 0;
void checkSerial() {
int byteRead = Serial.read();
if (byteRead == -1) {
serialRead = false;
}
else {
serialRead = true;
serialReadBuffer[serialReadBufferOffset] = byteRead;
serialReadBufferOffset++;
if (serialReadBufferOffset >= 64) {
serialReadBufferOffset = 0;
//TODO: report that we have overrun our serial read buffer
}
}
//if this is a \n, check the whole message
if (byteRead == '\n') {
byte numBytesRead = serialReadBufferOffset - 1; //we don't count the \n
if (numBytesRead == 0) {
//too little to be interesting
}
//single character debugging commands
else if (numBytesRead == 1 && serialReadBuffer[0] == 'f') {
mt_fireShot();
}
else if (numBytesRead == 1 && serialReadBuffer[0] == 'b') {
checkBattery();
}
else if (numBytesRead == 1 && serialReadBuffer[0] == 's') {
shutdown();
}
//commands from the real client
else if (numBytesRead == 1 && serialReadBuffer[0] == 'c') {
clientConnected = true;
serialQueue_s("c\n");
}
else if (numBytesRead == 1 && serialReadBuffer[0] == 'd') {
clientConnected = false;
serialQueue_s("d\n");
}
else if (numBytesRead > 4 && strncmp("Fire", serialReadBuffer, 4) == 0) {
//Fire
byte teamId, playerId, dmg;
sscanf(serialReadBuffer, "Fire(%hhd,%hhd,%hhd)", &teamId, &playerId, &dmg);
mt_fireShot(teamId, playerId, dmg);
}
else if (numBytesRead > 12 && strncmp("BatteryCheck", serialReadBuffer, 12) == 0) {
checkBattery();
}
else if (numBytesRead > 8 && strncmp("Shutdown", serialReadBuffer, 8) == 0) {
shutdown();
}
//temporary crap code for team select
else if (numBytesRead > 3 && strncmp("red", serialReadBuffer, 3) == 0) {
preConnectedTeamId = 1;
}
else if (numBytesRead > 5 && strncmp("green", serialReadBuffer, 5) == 0) {
preConnectedTeamId = 2;
}
else if (numBytesRead > 4 && strncmp("blue", serialReadBuffer, 4) == 0) {
preConnectedTeamId = 3;
}
else if (numBytesRead > 6 && strncmp("yellow", serialReadBuffer, 6) == 0) {
preConnectedTeamId = 4;
}
else if (numBytesRead > 6 && strncmp("purple", serialReadBuffer, 6) == 0) {
preConnectedTeamId = 5;
}
else if (numBytesRead > 4 && strncmp("cyan", serialReadBuffer, 4) == 0) {
preConnectedTeamId = 6;
}
else if (numBytesRead > 5 && strncmp("white", serialReadBuffer, 5) == 0) {
preConnectedTeamId = 7;
}
//reset the buffer
serialReadBufferOffset = 0;
}
}
#define serialWriteBufferSize 64
//the circle buffer
char serialWriteBuffer[serialWriteBufferSize];
//an offset from serialWriteBuffer ptr to the byte we next need to write in the circle buffer
byte writeOffset = 0;
//an offset from serialWriteBuffer ptr to the byte we next need to read from the circle buffer
byte readOffset = 0;
void serialQueue_s(const char *str) {
//copy this byte-by-byte into the circle buffer
int offset = 0;
while (*(str + offset)) {
if (writeOffset + 1 == readOffset || (writeOffset + 1 == serialWriteBufferSize && readOffset == 0)) {
//we have already written as many bytes as we are allowed to write.
//As we are trying to write another byte, we have an issue.
//replace the previous byte with an "X" and abandon anything else in this queue request
if (writeOffset == 0) {
*(serialWriteBuffer + serialWriteBufferSize - 1) = 'X';
}
else {
*(serialWriteBuffer + writeOffset - 1) = 'X';
}
return;
}
*(serialWriteBuffer + writeOffset) = *(str + offset);
writeOffset++;
if (writeOffset == serialWriteBufferSize) {
//we have gone off the end of the buffer
writeOffset = 0;
}
offset++;
}
}
void serialQueue_d(double d) {
char* out = (char*) malloc(32);
snprintf(out, 32, "%.2f", d);
serialQueue_s(out);
free(out);
}
void serialQueue_i(int i) {
char* out = (char*) malloc(11);
snprintf(out, 11, "%d", i);
serialQueue_s(out);
free(out);
}
void writeSerialChar() {
if (writeOffset == readOffset) {
//nothng to be read form the circle buffer (to be written to the serial line)
serialWritten = false;
return;
}
Serial.print(*(serialWriteBuffer + readOffset));
readOffset++;
if (readOffset == serialWriteBufferSize) {
//we have gone off the end of the buffer
readOffset = 0;
}
serialWritten = true;
}