bf4669a2dbc2f8c2ce9506be811da1f0b9a30f53
[clinton/Smoothieware.git] / src / libs / USBDevice / USBSerial / USBSerial.h
1 /* Copyright (c) 2010-2011 mbed.org, MIT License
2 *
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:
8 *
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
11 *
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.
17 */
18
19 #ifndef USBSERIAL_H
20 #define USBSERIAL_H
21
22 #include "USBCDC.h"
23 // #include "Stream.h"
24 #include "CircBuffer.h"
25
26 #include "Module.h"
27 #include "StreamOutput.h"
28
29 class USBSerial_Receiver {
30 protected:
31 virtual bool SerialEvent_RX(void) = 0;
32 };
33
34 class USBSerial: public USBCDC, public USBSerial_Receiver, public Module, public StreamOutput {
35 public:
36 USBSerial(USB *);
37
38 int _putc(int c);
39 int _getc();
40 int puts(const char *);
41
42 uint8_t available();
43 bool ready();
44
45 uint16_t writeBlock(const uint8_t * buf, uint16_t size);
46
47 CircBuffer<uint8_t> rxbuf;
48 CircBuffer<uint8_t> txbuf;
49
50 void on_module_loaded(void);
51 void on_main_loop(void *);
52 void on_idle(void *);
53
54 protected:
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);
58
59 virtual bool SerialEvent_RX(void){return false;};
60
61 virtual void on_attach(void);
62 virtual void on_detach(void);
63
64 void ensure_tx_space(int);
65
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;
69
70
71 volatile struct {
72 volatile bool attach:1;
73 bool attached:1;
74 bool halt_flag:1;
75 bool query_flag: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
82 bool flush_to_nl:1;
83 };
84
85 private:
86 USB *usb;
87 // mbed::FunctionPointer rx;
88 };
89
90 #endif