Created
February 24, 2015 00:15
-
-
Save rocketjosh/fcf29eec0cff1639a268 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) { | |
uint16_t buffidx = 0; | |
boolean timeoutvalid = true; | |
if (timeout == 0) timeoutvalid = false; | |
while (true) { | |
if (buffidx > maxbuff) { | |
//Serial.println(F("SPACE")); | |
break; | |
} | |
while(Serial.available()) { | |
char c = Serial.read(); | |
//Serial.print(c, HEX); Serial.print("#"); Serial.println(c); | |
if (c == '\r') continue; | |
if (c == 0xA) { | |
if (buffidx == 0) // the first 0x0A is ignored | |
continue; | |
timeout = 0; // the second 0x0A is the end of the line | |
timeoutvalid = true; | |
break; | |
} | |
buff[buffidx] = c; | |
buffidx++; | |
} | |
if (timeoutvalid && timeout == 0) { | |
//Serial.println(F("TIMEOUT")); | |
break; | |
} | |
delay(1); | |
} | |
buff[buffidx] = 0; // null term | |
return buffidx; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment