@@ -114,7 +114,6 @@ bool WEAK CDC_Setup(Setup& setup)
114
114
}
115
115
116
116
117
- int _serialPeek = -1 ;
118
117
void Serial_::begin (unsigned long /* baud_count */ )
119
118
{
120
119
}
@@ -127,51 +126,29 @@ void Serial_::end(void)
127
126
{
128
127
}
129
128
130
- void Serial_::accept (void )
131
- {
132
- int i = (unsigned int )(_rx_buffer_head+1 ) % SERIAL_BUFFER_SIZE;
133
-
134
- // if we should be storing the received character into the location
135
- // just before the tail (meaning that the head would advance to the
136
- // current location of the tail), we're about to overflow the buffer
137
- // and so we don't write the character or advance the head.
138
-
139
- // while we have room to store a byte
140
- while (i != _rx_buffer_tail) {
141
- int c = USB_Recv (CDC_RX);
142
- if (c == -1 )
143
- break ; // no more data
144
- _rx_buffer[_rx_buffer_head] = c;
145
- _rx_buffer_head = i;
146
-
147
- i = (unsigned int )(_rx_buffer_head+1 ) % SERIAL_BUFFER_SIZE;
148
- }
149
- }
150
-
151
129
int Serial_::available (void )
152
130
{
153
- return (unsigned int )(SERIAL_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail) % SERIAL_BUFFER_SIZE;
131
+ if (peek_buffer >= 0 ) {
132
+ return 1 ;
133
+ }
134
+ return USB_Available (CDC_RX);
154
135
}
155
136
156
137
int Serial_::peek (void )
157
138
{
158
- if (_rx_buffer_head == _rx_buffer_tail) {
159
- return -1 ;
160
- } else {
161
- return _rx_buffer[_rx_buffer_tail];
162
- }
139
+ if (peek_buffer < 0 )
140
+ peek_buffer = USB_Recv (CDC_RX);
141
+ return peek_buffer;
163
142
}
164
143
165
144
int Serial_::read (void )
166
145
{
167
- // if the head isn't ahead of the tail, we don't have any characters
168
- if (_rx_buffer_head == _rx_buffer_tail) {
169
- return -1 ;
170
- } else {
171
- unsigned char c = _rx_buffer[_rx_buffer_tail];
172
- _rx_buffer_tail = (unsigned int )(_rx_buffer_tail + 1 ) % SERIAL_BUFFER_SIZE;
146
+ if (peek_buffer >= 0 ) {
147
+ int c = peek_buffer;
148
+ peek_buffer = -1 ;
173
149
return c;
174
- }
150
+ }
151
+ return USB_Recv (CDC_RX);
175
152
}
176
153
177
154
void Serial_::flush (void )
@@ -180,6 +157,11 @@ void Serial_::flush(void)
180
157
}
181
158
182
159
size_t Serial_::write (uint8_t c)
160
+ {
161
+ return write (&c, 1 );
162
+ }
163
+
164
+ size_t Serial_::write (const uint8_t *buffer, size_t size)
183
165
{
184
166
/* only try to send bytes if the high-level CDC connection itself
185
167
is open (not just the pipe) - the OS should set lineState when the port
@@ -191,7 +173,7 @@ size_t Serial_::write(uint8_t c)
191
173
// open connection isn't broken cleanly (cable is yanked out, host dies
192
174
// or locks up, or host virtual serial port hangs)
193
175
if (_usbLineInfo.lineState > 0 ) {
194
- int r = USB_Send (CDC_TX,&c, 1 );
176
+ int r = USB_Send (CDC_TX,buffer,size );
195
177
if (r > 0 ) {
196
178
return r;
197
179
} else {
0 commit comments