1
1
/*
2
- This example demonstrates the use of multiple USB CDC/ACM "Virtual Serial" ports,
3
- using Ha Thach's tinyUSB library, and the port of that library to the Arduino environment
2
+ This example demonstrates the use of multiple USB CDC/ACM "Virtual
3
+ Serial" ports, using Ha Thach's tinyUSB library, and the port of
4
+ that library to the Arduino environment
5
+
4
6
https://github.com/hathach/tinyusb
5
7
https://github.com/adafruit/Adafruit_TinyUSB_Arduino
6
8
7
9
Written by Bill Westfield (aka WestfW), June 2021.
8
- This code is released to the public domain (note that this is a different
9
- license than the rest of the TinyUSB library and examples.)
10
+ Copyright 2021 by Bill Westfield
11
+ MIT license, check LICENSE for more information
10
12
11
- The example creates three virtual serial ports, runs a non-blocking parser on
12
- each one, and allows them to send messages to each other. This is pretty useless,
13
- and is relatively complex for an example, but it exercises a bunch of the CDC features.
13
+ The example creates three virtual serial ports. Text entered on
14
+ any of the ports will be echoed to the other two ports.
14
15
15
- The max number of CDC ports (CFG_TUD_CDC) has to be changed to at least 3, changed in
16
- the core tusb_config.h file.
16
+ The max number of CDC ports (CFG_TUD_CDC) has to be changed to at
17
+ least 3, changed in the core tusb_config.h file.
17
18
*/
18
19
19
20
#include < Adafruit_TinyUSB.h>
20
- #include " simpleParser.h"
21
21
22
22
#define LED LED_BUILTIN
23
23
@@ -47,74 +47,31 @@ void setup() {
47
47
}
48
48
delay (1000 );
49
49
}
50
- Serial.print (" You are TTY0 \n\r\n 0> " );
51
- USBSer2.print (" You are TTY1 \n\r\n 1> " );
52
- USBSer3.print (" You are TTY2 \n\r\n 2> " );
50
+ Serial.print (" You are port 0 \n\r\n 0> " );
51
+ USBSer2.print (" You are port 1 \n\r\n 1> " );
52
+ USBSer3.print (" You are port 2 \n\r\n 2> " );
53
53
}
54
54
55
-
56
- // We need a parser for each Virtual Serial port
57
- simpleParser<80 > line0 (Serial);
58
- simpleParser<80 > line1 (USBSer2);
59
- simpleParser<80 > line2 (USBSer3);
60
-
61
55
int LEDstate = 0 ;
62
56
63
- // Given an input port and an output port and a message, send it off.
64
-
65
- void sendmsg (Adafruit_USBD_CDC &out, Adafruit_USBD_CDC &in, char *msg) {
66
- out.print (" \r\n TTY" );
67
- out.print (in.getInstance ());
68
- out.print (" : " );
69
- out.println (msg);
70
- out.flush ();
71
- }
72
-
73
- // we've received a line on some port. Check if it's a valid comand,
74
- // parse the arguments, and take appropriate action
75
- void parseMsg (Adafruit_USBD_CDC &in, parserCore &parser) {
76
- int target;
77
- enum { CMD_SEND, CMD_HELP, CMD_HELP2 };
78
- int cmd = parser.keyword (" send help ? " );
79
- switch (cmd) {
80
- case CMD_SEND:
81
- target = parser.number ();
82
- if (target < 0 || target >= CFG_TUD_CDC || ports[target] == NULL ) {
83
- in.println (" Bad target line" );
84
- return ;
85
- }
86
- sendmsg (*ports[target], in, parser.restOfLine ());
87
- break ;
88
-
89
- case CMD_HELP:
90
- case CMD_HELP2:
91
- in.println (" Available commands:\r\n SEND N msg" );
92
- break ;
93
- default :
94
- in.println (" invalid command" );
95
- break ;
96
- }
97
- }
98
-
99
57
void loop () {
100
- if (line0. getLine ()) {
101
- parseMsg ( Serial, line0 );
102
- line0. reset ();
103
- Serial. print ( " 0> " );
104
-
58
+ int ch;
59
+ ch = Serial. read ( );
60
+ if (ch > 0 ) {
61
+ USBSer2. write (ch );
62
+ USBSer3. write (ch);
105
63
}
106
- if (line1. getLine ()) {
107
- parseMsg (USBSer2, line1);
108
- line1. reset ( );
109
- USBSer2. print ( " 1> " );
64
+ ch = USBSer2. read ();
65
+ if (ch > 0 ) {
66
+ Serial. write (ch );
67
+ USBSer3. write (ch );
110
68
}
111
- if (line2. getLine ()) {
112
- parseMsg (USBSer3, line2);
113
- line2. reset ( );
114
- USBSer3. print ( " 2> " );
69
+ ch = USBSer3. read ();
70
+ if (ch > 0 ) {
71
+ Serial. write (ch );
72
+ USBSer2. write (ch );
115
73
}
116
74
if (delay_without_delaying (500 )) {
117
- // blink LED to show we're still alive
118
75
LEDstate = !LEDstate;
119
76
digitalWrite (LED, LEDstate);
120
77
}
@@ -132,3 +89,5 @@ boolean delay_without_delaying(unsigned long time) {
132
89
}
133
90
return false ;
134
91
}
92
+
93
+ // Helper: non-blocking line-at-a-time read.
0 commit comments