Last active
May 4, 2019 07:12
-
-
Save paulhayes/509cf208afe6f11cda5a6393bfd805ed to your computer and use it in GitHub Desktop.
Serial line reading for arduino
This file contains hidden or 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
#include <Arduino.h> | |
bool lineMatches(char *buffer,const char *comp, int len){ | |
return len>0 && strcmp(buffer,comp)==0; | |
} | |
bool lineStarts(char *buffer,const char *comp, int len){ | |
if(len==0){ | |
return false; | |
} | |
int compLen = sizeof(comp); | |
return strncmp(buffer,comp,compLen)==0; | |
} | |
bool lineCharIntValue(char *buffer,const char comp, int& value){ | |
if(buffer[0]==comp){ | |
char* p = buffer; | |
p++; | |
value = atoi(p); | |
return true; | |
} | |
return false; | |
} | |
int readline(char *buffer, int len) { | |
static int pos = 0; | |
int rpos; | |
while (Serial.available()) { | |
int readch = Serial.read(); | |
switch (readch) { | |
case '\r': // Ignore CR | |
break; | |
case '\n': // Return on new-line | |
rpos = pos; | |
pos = 0; // Reset position index ready for next time | |
return rpos; | |
default: | |
if (pos < len-1) { | |
buffer[pos++] = readch; | |
buffer[pos] = 0; | |
} | |
} | |
} | |
return 0; | |
} |
This file contains hidden or 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
#ifndef readline_h | |
#define readlin_h | |
bool lineMatches(char *buffer,const char *comp, int len); | |
bool lineStarts(char *buffer,const char *comp, int len); | |
bool lineCharIntValue(char *buffer,const char comp, int& value); | |
int readline(char *buffer, int len); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment