Fri, 17 Nov 2017 10:13:31 +0100
proper configuration, homing and planner optimization
2 | 1 | |
2 | ||
3 | #ifndef __USBAPI__ | |
4 | #define __USBAPI__ | |
5 | ||
6 | #if defined(USBCON) | |
7 | ||
8 | //================================================================================ | |
9 | //================================================================================ | |
10 | // USB | |
11 | ||
12 | class USBDevice_ | |
13 | { | |
14 | public: | |
15 | USBDevice_(); | |
16 | bool configured(); | |
17 | ||
18 | void attach(); | |
19 | void detach(); // Serial port goes down too... | |
20 | void poll(); | |
21 | }; | |
22 | extern USBDevice_ USBDevice; | |
23 | ||
24 | //================================================================================ | |
25 | //================================================================================ | |
26 | // Serial over CDC (Serial1 is the physical port) | |
27 | ||
28 | class Serial_ : public Stream | |
29 | { | |
30 | private: | |
31 | ring_buffer *_cdc_rx_buffer; | |
32 | public: | |
33 | void begin(uint16_t baud_count); | |
34 | void end(void); | |
35 | ||
36 | virtual int available(void); | |
37 | virtual void accept(void); | |
38 | virtual int peek(void); | |
39 | virtual int read(void); | |
40 | virtual void flush(void); | |
41 | virtual size_t write(uint8_t); | |
42 | operator bool(); | |
43 | }; | |
44 | extern Serial_ Serial; | |
45 | ||
46 | //================================================================================ | |
47 | //================================================================================ | |
48 | // Mouse | |
49 | ||
50 | #define MOUSE_LEFT 1 | |
51 | #define MOUSE_RIGHT 2 | |
52 | #define MOUSE_MIDDLE 4 | |
53 | #define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE) | |
54 | ||
55 | class Mouse_ | |
56 | { | |
57 | private: | |
58 | uint8_t _buttons; | |
59 | void buttons(uint8_t b); | |
60 | public: | |
61 | Mouse_(void); | |
62 | void begin(void); | |
63 | void end(void); | |
64 | void click(uint8_t b = MOUSE_LEFT); | |
65 | void move(signed char x, signed char y, signed char wheel = 0); | |
66 | void press(uint8_t b = MOUSE_LEFT); // press LEFT by default | |
67 | void release(uint8_t b = MOUSE_LEFT); // release LEFT by default | |
68 | bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default | |
69 | }; | |
70 | extern Mouse_ Mouse; | |
71 | ||
72 | //================================================================================ | |
73 | //================================================================================ | |
74 | // Keyboard | |
75 | ||
76 | #define KEY_LEFT_CTRL 0x80 | |
77 | #define KEY_LEFT_SHIFT 0x81 | |
78 | #define KEY_LEFT_ALT 0x82 | |
79 | #define KEY_LEFT_GUI 0x83 | |
80 | #define KEY_RIGHT_CTRL 0x84 | |
81 | #define KEY_RIGHT_SHIFT 0x85 | |
82 | #define KEY_RIGHT_ALT 0x86 | |
83 | #define KEY_RIGHT_GUI 0x87 | |
84 | ||
85 | #define KEY_UP_ARROW 0xDA | |
86 | #define KEY_DOWN_ARROW 0xD9 | |
87 | #define KEY_LEFT_ARROW 0xD8 | |
88 | #define KEY_RIGHT_ARROW 0xD7 | |
89 | #define KEY_BACKSPACE 0xB2 | |
90 | #define KEY_TAB 0xB3 | |
91 | #define KEY_RETURN 0xB0 | |
92 | #define KEY_ESC 0xB1 | |
93 | #define KEY_INSERT 0xD1 | |
94 | #define KEY_DELETE 0xD4 | |
95 | #define KEY_PAGE_UP 0xD3 | |
96 | #define KEY_PAGE_DOWN 0xD6 | |
97 | #define KEY_HOME 0xD2 | |
98 | #define KEY_END 0xD5 | |
99 | #define KEY_CAPS_LOCK 0xC1 | |
100 | #define KEY_F1 0xC2 | |
101 | #define KEY_F2 0xC3 | |
102 | #define KEY_F3 0xC4 | |
103 | #define KEY_F4 0xC5 | |
104 | #define KEY_F5 0xC6 | |
105 | #define KEY_F6 0xC7 | |
106 | #define KEY_F7 0xC8 | |
107 | #define KEY_F8 0xC9 | |
108 | #define KEY_F9 0xCA | |
109 | #define KEY_F10 0xCB | |
110 | #define KEY_F11 0xCC | |
111 | #define KEY_F12 0xCD | |
112 | ||
113 | // Low level key report: up to 6 keys and shift, ctrl etc at once | |
114 | typedef struct | |
115 | { | |
116 | uint8_t modifiers; | |
117 | uint8_t reserved; | |
118 | uint8_t keys[6]; | |
119 | } KeyReport; | |
120 | ||
121 | class Keyboard_ : public Print | |
122 | { | |
123 | private: | |
124 | KeyReport _keyReport; | |
125 | void sendReport(KeyReport* keys); | |
126 | public: | |
127 | Keyboard_(void); | |
128 | void begin(void); | |
129 | void end(void); | |
130 | virtual size_t write(uint8_t k); | |
131 | virtual size_t press(uint8_t k); | |
132 | virtual size_t release(uint8_t k); | |
133 | virtual void releaseAll(void); | |
134 | }; | |
135 | extern Keyboard_ Keyboard; | |
136 | ||
137 | //================================================================================ | |
138 | //================================================================================ | |
139 | // Low level API | |
140 | ||
141 | typedef struct | |
142 | { | |
143 | uint8_t bmRequestType; | |
144 | uint8_t bRequest; | |
145 | uint8_t wValueL; | |
146 | uint8_t wValueH; | |
147 | uint16_t wIndex; | |
148 | uint16_t wLength; | |
149 | } Setup; | |
150 | ||
151 | //================================================================================ | |
152 | //================================================================================ | |
153 | // HID 'Driver' | |
154 | ||
155 | int HID_GetInterface(uint8_t* interfaceNum); | |
156 | int HID_GetDescriptor(int i); | |
157 | bool HID_Setup(Setup& setup); | |
158 | void HID_SendReport(uint8_t id, const void* data, int len); | |
159 | ||
160 | //================================================================================ | |
161 | //================================================================================ | |
162 | // MSC 'Driver' | |
163 | ||
164 | int MSC_GetInterface(uint8_t* interfaceNum); | |
165 | int MSC_GetDescriptor(int i); | |
166 | bool MSC_Setup(Setup& setup); | |
167 | bool MSC_Data(uint8_t rx,uint8_t tx); | |
168 | ||
169 | //================================================================================ | |
170 | //================================================================================ | |
171 | // CSC 'Driver' | |
172 | ||
173 | int CDC_GetInterface(uint8_t* interfaceNum); | |
174 | int CDC_GetDescriptor(int i); | |
175 | bool CDC_Setup(Setup& setup); | |
176 | ||
177 | //================================================================================ | |
178 | //================================================================================ | |
179 | ||
180 | #define TRANSFER_PGM 0x80 | |
181 | #define TRANSFER_RELEASE 0x40 | |
182 | #define TRANSFER_ZERO 0x20 | |
183 | ||
184 | int USB_SendControl(uint8_t flags, const void* d, int len); | |
185 | int USB_RecvControl(void* d, int len); | |
186 | ||
187 | uint8_t USB_Available(uint8_t ep); | |
188 | int USB_Send(uint8_t ep, const void* data, int len); // blocking | |
189 | int USB_Recv(uint8_t ep, void* data, int len); // non-blocking | |
190 | int USB_Recv(uint8_t ep); // non-blocking | |
191 | void USB_Flush(uint8_t ep); | |
192 | ||
193 | #endif | |
194 | ||
195 | #endif /* if defined(USBCON) */ |