sanguino/cores/arduino/CDC.cpp

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
permissions
-rw-r--r--

added missing sanguino files

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 /* Copyright (c) 2011, Peter Barrett
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4 **
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 ** Permission to use, copy, modify, and/or distribute this software for
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6 ** any purpose with or without fee is hereby granted, provided that the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7 ** above copyright notice and this permission notice appear in all copies.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8 **
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9 ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11 ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12 ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14 ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16 ** SOFTWARE.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 #include "Platform.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20 #include "USBAPI.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21 #include <avr/wdt.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23 #if defined(USBCON)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24 #ifdef CDC_ENABLED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26 #if (RAMEND < 1000)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27 #define SERIAL_BUFFER_SIZE 16
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29 #define SERIAL_BUFFER_SIZE 64
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 struct ring_buffer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 unsigned char buffer[SERIAL_BUFFER_SIZE];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35 volatile int head;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36 volatile int tail;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39 ring_buffer cdc_rx_buffer = { { 0 }, 0, 0};
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41 typedef struct
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43 u32 dwDTERate;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44 u8 bCharFormat;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 u8 bParityType;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46 u8 bDataBits;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 u8 lineState;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 } LineInfo;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52 #define WEAK __attribute__ ((weak))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54 extern const CDCDescriptor _cdcInterface PROGMEM;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 const CDCDescriptor _cdcInterface =
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1),
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 // CDC communication interface
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61 D_CDCCS(CDC_HEADER,0x10,0x01), // Header (1.10 bcd)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62 D_CDCCS(CDC_CALL_MANAGEMENT,1,1), // Device handles call management (not)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,6), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64 D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40),
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67 // CDC data interface
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68 D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0),
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69 D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0),
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70 D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73 int WEAK CDC_GetInterface(u8* interfaceNum)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75 interfaceNum[0] += 2; // uses 2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76 return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79 bool WEAK CDC_Setup(Setup& setup)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81 u8 r = setup.bRequest;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82 u8 requestType = setup.bmRequestType;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84 if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86 if (CDC_GET_LINE_CODING == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88 USB_SendControl(0,(void*)&_usbLineInfo,7);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89 return true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93 if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95 if (CDC_SET_LINE_CODING == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
97 USB_RecvControl((void*)&_usbLineInfo,7);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
98 return true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
99 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
100
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
101 if (CDC_SET_CONTROL_LINE_STATE == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
102 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
103 _usbLineInfo.lineState = setup.wValueL;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
104
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
105 // auto-reset into the bootloader is triggered when the port, already
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
106 // open at 1200 bps, is closed. this is the signal to start the watchdog
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
107 // with a relatively long period so it can finish housekeeping tasks
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
108 // like servicing endpoints before the sketch ends
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
109 if (1200 == _usbLineInfo.dwDTERate) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
110 // We check DTR state to determine if host port is open (bit 0 of lineState).
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
111 if ((_usbLineInfo.lineState & 0x01) == 0) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
112 *(uint16_t *)0x0800 = 0x7777;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
113 wdt_enable(WDTO_120MS);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
114 } else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
115 // Most OSs do some intermediate steps when configuring ports and DTR can
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
116 // twiggle more than once before stabilizing.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
117 // To avoid spurious resets we set the watchdog to 250ms and eventually
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
118 // cancel if DTR goes back high.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
119
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
120 wdt_disable();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
121 wdt_reset();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
122 *(uint16_t *)0x0800 = 0x0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
123 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
124 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
125 return true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
126 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
127 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
128 return false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
129 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
130
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
131
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
132 int _serialPeek = -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
133 void Serial_::begin(uint16_t baud_count)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
134 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
135 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
136
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
137 void Serial_::end(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
138 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
139 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
140
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
141 void Serial_::accept(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
142 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
143 ring_buffer *buffer = &cdc_rx_buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
144 int c = USB_Recv(CDC_RX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
145 int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
146
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
147 // if we should be storing the received character into the location
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
148 // just before the tail (meaning that the head would advance to the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
149 // current location of the tail), we're about to overflow the buffer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
150 // and so we don't write the character or advance the head.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
151 if (i != buffer->tail) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
152 buffer->buffer[buffer->head] = c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
153 buffer->head = i;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
154 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
155 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
156
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
157 int Serial_::available(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
158 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
159 ring_buffer *buffer = &cdc_rx_buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
160 return (unsigned int)(SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % SERIAL_BUFFER_SIZE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
161 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
162
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
163 int Serial_::peek(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
164 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
165 ring_buffer *buffer = &cdc_rx_buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
166 if (buffer->head == buffer->tail) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
167 return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
168 } else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
169 return buffer->buffer[buffer->tail];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
170 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
171 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
172
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
173 int Serial_::read(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
174 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
175 ring_buffer *buffer = &cdc_rx_buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
176 // if the head isn't ahead of the tail, we don't have any characters
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
177 if (buffer->head == buffer->tail) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
178 return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
179 } else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
180 unsigned char c = buffer->buffer[buffer->tail];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
181 buffer->tail = (unsigned int)(buffer->tail + 1) % SERIAL_BUFFER_SIZE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
182 return c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
183 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
184 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
185
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
186 void Serial_::flush(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
187 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
188 USB_Flush(CDC_TX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
189 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
190
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
191 size_t Serial_::write(uint8_t c)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
192 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
193 /* only try to send bytes if the high-level CDC connection itself
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
194 is open (not just the pipe) - the OS should set lineState when the port
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
195 is opened and clear lineState when the port is closed.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
196 bytes sent before the user opens the connection or after
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
197 the connection is closed are lost - just like with a UART. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
198
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
199 // TODO - ZE - check behavior on different OSes and test what happens if an
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
200 // open connection isn't broken cleanly (cable is yanked out, host dies
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
201 // or locks up, or host virtual serial port hangs)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
202 if (_usbLineInfo.lineState > 0) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
203 int r = USB_Send(CDC_TX,&c,1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
204 if (r > 0) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
205 return r;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
206 } else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
207 setWriteError();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
208 return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
209 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
210 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
211 setWriteError();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
212 return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
213 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
214
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
215 // This operator is a convenient way for a sketch to check whether the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
216 // port has actually been configured and opened by the host (as opposed
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
217 // to just being connected to the host). It can be used, for example, in
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
218 // setup() before printing to ensure that an application on the host is
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
219 // actually ready to receive and display the data.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
220 // We add a short delay before returning to fix a bug observed by Federico
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
221 // where the port is configured (lineState != 0) but not quite opened.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
222 Serial_::operator bool() {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
223 bool result = false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
224 if (_usbLineInfo.lineState > 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
225 result = true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
226 delay(10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
227 return result;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
228 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
229
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
230 Serial_ Serial;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
231
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
232 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
233 #endif /* if defined(USBCON) */

mercurial