-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd_tail.cpp
107 lines (97 loc) · 2.24 KB
/
cmd_tail.cpp
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
#include "arduino_cli.h"
/**
* Keep reading input from digital/analog pin
*/
void cmd_tail(String *cmd, String *args) {
// Make sure cmd parameter isn't null
if(cmd == NULL) {
Serial.println("ERROR: 1st param of cmd_tail must not NULL");
return;
}
// Show usage if there is no argument
if(args == NULL) {
help(cmd);
return;
}
// Prepare variables
String pinStr = "";
String intervalStr = "";
// Tokenize arguments
byte argIndex = 0;
for(byte i = 0; i < args->length(); i++) {
char each = args->charAt(i);
if(each == ' ') {
if(argIndex >= 1) {
Serial.println("Excessive arguments");
Serial.println("");
help(cmd);
return;
} else {
argIndex++;
}
} else {
switch(argIndex) {
case 0:
pinStr += each;
break;
case 1:
intervalStr += each;
break;
}
}
}
// Abort if not enough argument
if(args->length() <= 0) {
help(cmd);
return;
}
// Parse pin string
byte pin = parsePin(&pinStr);
if(pin == PIN_INVALID) {
// CONDITION: Invalid Pin ID
Serial.println("Pin ID \"" + pinStr + "\" is not valid");
Serial.println("");
help(cmd);
return;
}
// Parse interval
unsigned int interval = intervalStr.toInt();
if(interval == 0) {
if(intervalStr.length() > 0) {
// CONDITION: interval_ms is invalid
Serial.println("interval_ms must number, not \"" + intervalStr + "\"");
Serial.println("");
help(cmd);
return;
} else {
// CONDITION: interval_ms is not specified, fallback to default
interval = 500;
}
}
// Set pin role
pinMode(pin, INPUT);
// Read and print the data
if(pinStr.charAt(0) == 'A') {
// CONDITION: Analog pin
while(true) {
Serial.println(analogRead(pin));
delay(interval);
if(Serial.available() > 0) break;
}
} else {
// CONDITION: Digital pin
while(true) {
Serial.println(digitalRead(pin));
delay(interval);
if(Serial.available() > 0) break;
}
}
Serial.println("");
Serial.println("[Stopped]");
Serial.println("");
// Serial buffer cleanup
int n = Serial.available();
for(int i = 0; i < n; i++) {
Serial.read();
}
}