bf4669a2dbc2f8c2ce9506be811da1f0b9a30f53
1 /* Copyright (c) 2010-2011 mbed.org, MIT License
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 // #include "Stream.h"
24 #include "CircBuffer.h"
27 #include "StreamOutput.h"
29 class USBSerial_Receiver
{
31 virtual bool SerialEvent_RX(void) = 0;
34 class USBSerial
: public USBCDC
, public USBSerial_Receiver
, public Module
, public StreamOutput
{
40 int puts(const char *);
45 uint16_t writeBlock(const uint8_t * buf
, uint16_t size
);
47 CircBuffer
<uint8_t> rxbuf
;
48 CircBuffer
<uint8_t> txbuf
;
50 void on_module_loaded(void);
51 void on_main_loop(void *);
55 // virtual bool EpCallback(uint8_t, uint8_t);
56 virtual bool USBEvent_EPIn(uint8_t, uint8_t);
57 virtual bool USBEvent_EPOut(uint8_t, uint8_t);
59 virtual bool SerialEvent_RX(void){return false;};
61 virtual void on_attach(void);
62 virtual void on_detach(void);
64 void ensure_tx_space(int);
66 // keep track of number of newlines in the buffer
67 // this makes it trivial to detect if there's a new line available
68 volatile int nl_in_rx
;
72 volatile bool attach
:1;
76 bool last_char_was_dollar
:1;
77 // if we receive a line that's longer than the buffer, to avoid a deadlock
78 // we must flush the buffer.
79 // then to avoid delivering the tail of a line to Smoothie we must keep
80 // flushing until we find a newline.
81 // this flag asserts when we are doing this
87 // mbed::FunctionPointer rx;