coppino/slip.ino

59 lines
1.1 KiB
Arduino
Raw Normal View History

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 */
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;
int i = 0;
2019-12-23 13:13:13 +00:00
while (i < len) {
if (Serial.available()) {
r = Serial.read();
switch (r) {
case ESC:
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 */