@@ -114,7 +114,6 @@ bool WEAK CDC_Setup(Setup& setup)
114114}
115115
116116
117- int _serialPeek = -1 ;
118117void Serial_::begin (unsigned long /* baud_count */ )
119118{
120119}
@@ -127,51 +126,29 @@ void Serial_::end(void)
127126{
128127}
129128
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-
151129int Serial_::available (void )
152130{
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);
154135}
155136
156137int Serial_::peek (void )
157138{
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;
163142}
164143
165144int Serial_::read (void )
166145{
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 ;
173149 return c;
174- }
150+ }
151+ return USB_Recv (CDC_RX);
175152}
176153
177154void Serial_::flush (void )
@@ -180,6 +157,11 @@ void Serial_::flush(void)
180157}
181158
182159size_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)
183165{
184166 /* only try to send bytes if the high-level CDC connection itself
185167 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)
191173 // open connection isn't broken cleanly (cable is yanked out, host dies
192174 // or locks up, or host virtual serial port hangs)
193175 if (_usbLineInfo.lineState > 0 ) {
194- int r = USB_Send (CDC_TX,&c, 1 );
176+ int r = USB_Send (CDC_TX,buffer,size );
195177 if (r > 0 ) {
196178 return r;
197179 } else {
0 commit comments