/*
bool waitWrite(const char *str, String &output, unsigned long timeout) {
int n = strlen(str);
const char *pC = str;
unsigned long done = ( millis() + timeout );
while (n && (millis() < done)) {
if (Serial.available()) {
char c = (char)Serial.read();
output += c;
if (c == *pC)
pC++, n--;
}
}
return (n == 0);
}
//--------------------------------------------
*/
#define BUF_SIZE 256
char buf[BUF_SIZE] = {'\0'};
void checkLine(String &line) {
Serial1.print("line: "); Serial1.println(line);
line = "";
}
bool waitFor(String match, unsigned long period = 5000, bool flush = false, char lineSep = '\n') {
unsigned long timeout = ( millis() + period );
String line = "";
bool found = false;
int n = 0;
Serial1.println(match.length());
memset(buf, '\0', BUF_SIZE);
while (!found && (millis() < timeout)) {
if (Serial1.available()) {
char c = Serial1.read();
if (c == lineSep) {
checkLine(line);
}
else { line += c;
if (c == match[n]) n++;
if (n == match.length())
found = true;
}
}
delay(1);
}
if (flush)
Serial1.readStringUntil(lineSep);
return true;
}
void setup() {
Serial1.begin(115200);
Serial1.println("");
}
void loop() {
while (!Serial1.available()) delay(1);
waitFor("OK",10000UL,true);
Serial1.println("DONE");
Serial1.flush();
}