Skip to content

Commit 00bd3fd

Browse files
committed
Simpllified ConsoleRead, added available(), added explanation.
1 parent 45f7d1e commit 00bd3fd

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

hardware/arduino/avr/libraries/Bridge/examples/ConsoleRead/ConsoleRead.ino

+29-17
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,53 @@
33
read data coming from bridge using the Console.read() function
44
and store it in a string.
55
6+
To see the Console, pick your Yun's name and IP address in the Port menu
7+
then open the Port Monitor. You can also see it by opening a terminal window
8+
and typing
9+
ssh root@ yourYunsName.local 'telnet localhost 6571'
10+
then pressing enter. When prompted for the password, enter it.
11+
612
created 13 Jun 2013
713
by Angelo Scialabba
14+
modified 16 June 2013
15+
by Tom Igoe
816
917
This example code is in the public domain.
1018
*/
1119

1220
#include <Console.h>
1321

1422
String name;
15-
int current_char;
1623

1724
void setup() {
1825
//Initialize Console and wait for port to open:
1926
Bridge.begin();
2027
Console.begin();
21-
22-
while (!Console) {
28+
29+
while (!Console){
2330
; // wait for Console port to connect.
2431
}
25-
Console.println("Hi, who are you?");
32+
Console.println("Hi, what's your name?");
2633
}
2734

2835
void loop() {
29-
current_char = Console.read(); //read the next char received
30-
//look for the newline character, this is the last character in the string
31-
if (current_char == '\n') {
32-
//print text with the name received
33-
Console.print("Hi ");
34-
Console.print(name);
35-
Console.println("! Nice to meet you!");
36-
Console.println();
37-
//Ask again for name and clear the old name
38-
Console.println("Hi, who are you?");
39-
name = "";
40-
} else if (current_char != -1) { //if the buffer is empty Cosole.read returns -1
41-
name = name + (char)current_char; //current_char is int, treat him as char and add it to the name string
36+
if (Console.available() > 0) {
37+
char thisChar = Console.read(); //read the next char received
38+
//look for the newline character, this is the last character in the string
39+
if (thisChar == '\n') {
40+
//print text with the name received
41+
Console.print("Hi ");
42+
Console.print(name);
43+
Console.println("! Nice to meet you!");
44+
Console.println();
45+
//Ask again for name and clear the old name
46+
Console.println("Hi, what's your name?");
47+
name = "";
48+
}
49+
else { //if the buffer is empty Cosole.read returns -1
50+
name += thisChar; //thisChar is int, treat him as char and add it to the name string
51+
}
4252
}
4353
}
54+
55+

0 commit comments

Comments
 (0)