|
1 | 1 | /* |
2 | | - Console.read() example: |
3 | | - read data coming from bridge using the Console.read() function |
| 2 | + Console Read example |
| 3 | +
|
| 4 | + Read data coming from bridge using the Console.read() function |
4 | 5 | and store it in a string. |
5 | 6 | |
6 | | - To see the Console, pick your Yun's name and IP address in the Port menu |
| 7 | + To see the Console, pick your Yún's name and IP address in the Port menu |
7 | 8 | then open the Port Monitor. You can also see it by opening a terminal window |
8 | | - and typing |
| 9 | + and typing: |
9 | 10 | ssh root@ yourYunsName.local 'telnet localhost 6571' |
10 | 11 | then pressing enter. When prompted for the password, enter it. |
11 | 12 | |
|
22 | 23 | String name; |
23 | 24 |
|
24 | 25 | void setup() { |
25 | | - //Initialize Console and wait for port to open: |
| 26 | + // Initialize Console and wait for port to open: |
26 | 27 | Bridge.begin(); |
27 | 28 | Console.begin(); |
28 | 29 |
|
29 | | - while (!Console){ |
30 | | - ; // wait for Console port to connect. |
31 | | - } |
| 30 | + // Wait for Console port to connect |
| 31 | + while (!Console); |
| 32 | + |
32 | 33 | Console.println("Hi, what's your name?"); |
33 | 34 | } |
34 | 35 |
|
35 | 36 | void loop() { |
36 | 37 | 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') { |
| 38 | + char c = Console.read(); // read the next char received |
| 39 | + // look for the newline character, this is the last character in the string |
| 40 | + if (c == '\n') { |
40 | 41 | //print text with the name received |
41 | 42 | Console.print("Hi "); |
42 | 43 | Console.print(name); |
43 | 44 | Console.println("! Nice to meet you!"); |
44 | 45 | Console.println(); |
45 | | - //Ask again for name and clear the old name |
| 46 | + // Ask again for name and clear the old name |
46 | 47 | Console.println("Hi, what's your name?"); |
47 | | - name = ""; |
| 48 | + name = ""; // clear the name string |
48 | 49 | } |
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 |
| 50 | + else { // if the buffer is empty Cosole.read() returns -1 |
| 51 | + name += c; // append the read char from Console to the name string |
51 | 52 | } |
52 | 53 | } |
53 | 54 | } |
|
0 commit comments