2019-12-23 13:13:13 +00:00
|
|
|
namespace slip {
|
|
|
|
|
2019-12-23 18:02:05 +00:00
|
|
|
void send(const char* buffer, int len) {
|
2019-12-23 13:13:13 +00:00
|
|
|
// Serial.write(END); /* frame-out any possible noise on the line */
|
|
|
|
|
2019-12-23 20:45:25 +00:00
|
|
|
for (int i = 0; i < len; ++i) {
|
2019-12-23 13:13:13 +00:00
|
|
|
switch (buffer[i]) {
|
|
|
|
case END:
|
|
|
|
Serial.write(ESC);
|
|
|
|
Serial.write(ESC_END);
|
|
|
|
break;
|
|
|
|
case ESC:
|
|
|
|
Serial.write(ESC);
|
|
|
|
Serial.write(ESC_ESC);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Serial.write(buffer[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.write(END);
|
|
|
|
}
|
|
|
|
|
2019-12-23 18:02:05 +00:00
|
|
|
int recv(char* buffer, int len) {
|
2019-12-23 13:13:13 +00:00
|
|
|
char r;
|
2019-12-23 20:45:25 +00:00
|
|
|
int i = 0;
|
2019-12-23 13:13:13 +00:00
|
|
|
|
|
|
|
while (i < len) {
|
|
|
|
if (Serial.available()) {
|
|
|
|
r = Serial.read();
|
|
|
|
|
|
|
|
switch (r) {
|
|
|
|
case ESC:
|
2019-12-23 20:45:25 +00:00
|
|
|
while (Serial.available() <= 0);
|
2019-12-23 13:13:13 +00:00
|
|
|
r = Serial.read();
|
|
|
|
if (r == ESC_END) {
|
|
|
|
buffer[i++] = END;
|
|
|
|
} else if (r == ESC_ESC) {
|
|
|
|
buffer[i++] = ESC;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case END:
|
|
|
|
return i;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buffer[i++] = r;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace slip */
|