-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_serial_utils.h
50 lines (38 loc) · 1.01 KB
/
sys_serial_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// ----[SERIAL PORT UTILITIES]-----
// serial port interaction utilities from
// https://github.com/arduino-libraries/ArduinoECCX08/blob/master/examples/Tools/ECCX08JWSPublicKey/ECCX08JWSPublicKey.ino
#pragma once
String readLine() {
String line;
while (1) {
if (Serial.available()) {
char c = Serial.read();
if (c == '\r') {
// ignore
continue;
} else if (c == '\n') {
break;
}
line += c;
}
}
return line;
}
String promptAndReadLine(const char* prompt, const char* defaultValue) {
Serial.print(prompt);
Serial.print(" [");
Serial.print(defaultValue);
Serial.print("]: ");
String s = readLine();
if (s.length() == 0) {
s = defaultValue;
}
Serial.println(s);
return s;
}
bool promptAndReadYesNo(String prompt, bool defaultValue) {
prompt = prompt + (defaultValue ? " (Y/n)" : " (y/N)" );
String yesno = promptAndReadLine(prompt.c_str(), defaultValue ? "Y" : "N");
yesno.toLowerCase();
return (yesno.startsWith("y"));
}