-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicUI.h
More file actions
145 lines (124 loc) · 2.84 KB
/
BasicUI.h
File metadata and controls
145 lines (124 loc) · 2.84 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef _QKEY_BASICUI_H_
#define _QKEY_BASICUI_H_
static int readGeneric(const __FlashStringHelper* prompt,
char buffer[PASSLEN],
bool (*isValid)(int),
bool echo) {
Serial.print(prompt);
// Read loop
int i;
char c;
for (i=0; i<PASSLEN; ) {
while (!Serial.available()) {
// Wait for more input
}
c = Serial.read();
if (c == '\r') {
// End of string, mark as done
break;
} else if (isValid(c)) {
// Valid char
buffer[i] = c;
i++;
if (echo) {
Serial.print(c);
}
} else if (c == 0x8 && i > 0) {
// Backspace
i--;
if (echo) {
Serial.print(c);
// Clear to end of line
Serial.print((char)0x1b);
Serial.print("[K");
}
} else {
// Invalid char
Serial.print("Invalid char: ");
Serial.print(c, HEX);
Serial.println();
return -1;
}
}
int len = i;
// Ensure buffer is always null terminated, filled with 0
while (i<PASSLEN) {
buffer[i] = 0;
i++;
}
buffer[PASSLEN-1] = 0;
Serial.println();
return len;
}
static int readString(const __FlashStringHelper* prompt,
char buffer[PASSLEN]) {
return readGeneric(prompt, buffer, isPrintable, true);
}
static int readPassword(const __FlashStringHelper* prompt,
char buffer[PASSLEN]) {
return readGeneric(prompt, buffer, isHuff, false);
}
static int readPosInt(const __FlashStringHelper* prompt) {
char buffer[PASSLEN];
int stat = readGeneric(prompt, buffer, isDigit, true);
if (stat <= 0) {
return -1;
}
int i = 0;
unsigned int value = 0;
while (buffer[i] != 0) {
value = value * 10;
value = value + buffer[i] - '0';
i++;
}
return value;
}
static char readChar(const __FlashStringHelper* prompt, const char * allowed) {
Serial.print(prompt);
String allowed_s(allowed);
char r;
do {
while (!Serial.available()) {
delay(100);
}
r = Serial.read();
} while(allowed_s.indexOf(r) == -1);
Serial.println(r);
return r;
}
static bool confirm() {
bool blink = false;
int count = 0;
int previous = HIGH;
if (digitalRead(buttonPin) == LOW) {
Serial.println("Button appears stuck.");
return false;
}
Serial.println(F("Are you sure?(Button)"));
while (count == 0 || previous == LOW) {
if (digitalRead(buttonPin) == LOW) {
count++;
previous = LOW;
if (count > 10) {
break;
}
} else {
previous = HIGH;
}
if (blink && count == 0) {
digitalWrite(LEDSerial, HIGH);
} else {
digitalWrite(LEDSerial, LOW);
}
blink = !blink;
delay(100);
}
if (count > 10) {
Serial.println(F("Aborted"));
return false;
} else {
Serial.println(F("Ok"));
return true;
}
}
#endif