Fixed RNDISEthernet demos crashing when calculating checksums for Ethernet/TCP packet...
[pub/USBasp.git] / Demos / Device / LowLevel / KeyboardMouse / KeyboardMouse.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11 Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
12
13 Permission to use, copy, modify, distribute, and sell this
14 software and its documentation for any purpose is hereby granted
15 without fee, provided that the above copyright notice appear in
16 all copies and that both that the copyright notice and this
17 permission notice and warranty disclaimer appear in supporting
18 documentation, and that the name of the author not be used in
19 advertising or publicity pertaining to distribution of the
20 software without specific, written prior permission.
21
22 The author disclaim all warranties with regard to this
23 software, including all implied warranties of merchantability
24 and fitness. In no event shall the author be liable for any
25 special, indirect or consequential damages or any damages
26 whatsoever resulting from loss of use, data or profits, whether
27 in an action of contract, negligence or other tortious action,
28 arising out of or in connection with the use or performance of
29 this software.
30 */
31
32 /** \file
33 *
34 * Main source file for the KeyboardMouse demo. This file contains the main tasks of the demo and
35 * is responsible for the initial application hardware configuration.
36 */
37
38 #include "KeyboardMouse.h"
39
40 /** Global structure to hold the current keyboard interface HID report, for transmission to the host */
41 USB_KeyboardReport_Data_t KeyboardReportData;
42
43 /** Global structure to hold the current mouse interface HID report, for transmission to the host */
44 USB_MouseReport_Data_t MouseReportData;
45
46
47 /** Main program entry point. This routine configures the hardware required by the application, then
48 * enters a loop to run the application tasks in sequence.
49 */
50 int main(void)
51 {
52 SetupHardware();
53
54 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
55 sei();
56
57 for (;;)
58 {
59 Keyboard_HID_Task();
60 Mouse_HID_Task();
61 USB_USBTask();
62 }
63 }
64
65 /** Configures the board hardware and chip peripherals for the demo's functionality. */
66 void SetupHardware(void)
67 {
68 /* Disable watchdog if enabled by bootloader/fuses */
69 MCUSR &= ~(1 << WDRF);
70 wdt_disable();
71
72 /* Disable clock division */
73 clock_prescale_set(clock_div_1);
74
75 /* Hardware Initialization */
76 Joystick_Init();
77 LEDs_Init();
78 USB_Init();
79 }
80
81 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
82 * starts the library USB task to begin the enumeration and USB management process.
83 */
84 void EVENT_USB_Device_Connect(void)
85 {
86 /* Indicate USB enumerating */
87 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
88 }
89
90 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
91 * the status LEDs and stops the USB management task.
92 */
93 void EVENT_USB_Device_Disconnect(void)
94 {
95 /* Indicate USB not ready */
96 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
97 }
98
99 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
100 * of the USB device after enumeration, and configures the keyboard and mouse device endpoints.
101 */
102 void EVENT_USB_Device_ConfigurationChanged(void)
103 {
104 /* Indicate USB connected and ready */
105 LEDs_SetAllLEDs(LEDMASK_USB_READY);
106
107 /* Setup Keyboard Report Endpoint */
108 if (!(Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT,
109 ENDPOINT_DIR_IN, HID_EPSIZE,
110 ENDPOINT_BANK_SINGLE)))
111 {
112 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
113 }
114
115 /* Setup Keyboard LED Report Endpoint */
116 if (!(Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPNUM, EP_TYPE_INTERRUPT,
117 ENDPOINT_DIR_OUT, HID_EPSIZE,
118 ENDPOINT_BANK_SINGLE)))
119 {
120 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
121 }
122
123 /* Setup Mouse Report Endpoint */
124 if (!(Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT,
125 ENDPOINT_DIR_IN, HID_EPSIZE,
126 ENDPOINT_BANK_SINGLE)))
127 {
128 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
129 }
130 }
131
132 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
133 * control requests that are not handled internally by the USB library (including the HID commands, which are
134 * all issued via the control endpoint), so that they can be handled appropriately for the application.
135 */
136 void EVENT_USB_Device_UnhandledControlRequest(void)
137 {
138 uint8_t* ReportData;
139 uint8_t ReportSize;
140
141 /* Handle HID Class specific requests */
142 switch (USB_ControlRequest.bRequest)
143 {
144 case REQ_GetReport:
145 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
146 {
147 Endpoint_ClearSETUP();
148
149 /* Determine if it is the mouse or the keyboard data that is being requested */
150 if (!(USB_ControlRequest.wIndex))
151 {
152 ReportData = (uint8_t*)&KeyboardReportData;
153 ReportSize = sizeof(KeyboardReportData);
154 }
155 else
156 {
157 ReportData = (uint8_t*)&MouseReportData;
158 ReportSize = sizeof(MouseReportData);
159 }
160
161 /* Write the report data to the control endpoint */
162 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
163
164 /* Clear the report data afterwards */
165 memset(ReportData, 0, ReportSize);
166
167 /* Finalize the stream transfer to send the last packet or clear the host abort */
168 Endpoint_ClearOUT();
169 }
170
171 break;
172 case REQ_SetReport:
173 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
174 {
175 Endpoint_ClearSETUP();
176
177 /* Wait until the LED report has been sent by the host */
178 while (!(Endpoint_IsOUTReceived()))
179 {
180 if (USB_DeviceState == DEVICE_STATE_Unattached)
181 return;
182 }
183
184 /* Read in and process the LED report from the host */
185 Keyboard_ProcessLEDReport(Endpoint_Read_Byte());
186
187 /* Clear the endpoint data */
188 Endpoint_ClearOUT();
189
190 Endpoint_ClearStatusStage();
191 }
192
193 break;
194 }
195 }
196
197 /** Processes a given Keyboard LED report from the host, and sets the board LEDs to match. Since the Keyboard
198 * LED report can be sent through either the control endpoint (via a HID SetReport request) or the HID OUT
199 * endpoint, the processing code is placed here to avoid duplicating it and potentially having different
200 * behaviour depending on the method used to sent it.
201 */
202 void Keyboard_ProcessLEDReport(const uint8_t LEDStatus)
203 {
204 uint8_t LEDMask = LEDS_LED2;
205
206 if (LEDStatus & KEYBOARD_LED_NUMLOCK)
207 LEDMask |= LEDS_LED1;
208
209 if (LEDStatus & KEYBOARD_LED_CAPSLOCK)
210 LEDMask |= LEDS_LED3;
211
212 if (LEDStatus & KEYBOARD_LED_SCROLLLOCK)
213 LEDMask |= LEDS_LED4;
214
215 /* Set the status LEDs to the current Keyboard LED status */
216 LEDs_SetAllLEDs(LEDMask);
217 }
218
219 /** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the
220 * keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status
221 * reports sent to the device via the keyboard OUT reporting endpoint.
222 */
223 void Keyboard_HID_Task(void)
224 {
225 uint8_t JoyStatus_LCL = Joystick_GetStatus();
226
227 /* Device must be connected and configured for the task to run */
228 if (USB_DeviceState != DEVICE_STATE_Configured)
229 return;
230
231 /* Check if board button is not pressed, if so mouse mode enabled */
232 if (!(Buttons_GetStatus() & BUTTONS_BUTTON1))
233 {
234 /* Make sent key uppercase by indicating that the left shift key is pressed */
235 KeyboardReportData.Modifier = KEYBOARD_MODIFER_LEFTSHIFT;
236
237 if (JoyStatus_LCL & JOY_UP)
238 KeyboardReportData.KeyCode[0] = 0x04; // A
239 else if (JoyStatus_LCL & JOY_DOWN)
240 KeyboardReportData.KeyCode[0] = 0x05; // B
241
242 if (JoyStatus_LCL & JOY_LEFT)
243 KeyboardReportData.KeyCode[0] = 0x06; // C
244 else if (JoyStatus_LCL & JOY_RIGHT)
245 KeyboardReportData.KeyCode[0] = 0x07; // D
246
247 if (JoyStatus_LCL & JOY_PRESS)
248 KeyboardReportData.KeyCode[0] = 0x08; // E
249 }
250
251 /* Select the Keyboard Report Endpoint */
252 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
253
254 /* Check if Keyboard Endpoint Ready for Read/Write */
255 if (Endpoint_IsReadWriteAllowed())
256 {
257 /* Write Keyboard Report Data */
258 Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));
259
260 /* Finalize the stream transfer to send the last packet */
261 Endpoint_ClearIN();
262
263 /* Clear the report data afterwards */
264 memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));
265 }
266
267 /* Select the Keyboard LED Report Endpoint */
268 Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM);
269
270 /* Check if Keyboard LED Endpoint Ready for Read/Write */
271 if (Endpoint_IsReadWriteAllowed())
272 {
273 /* Read in and process the LED report from the host */
274 Keyboard_ProcessLEDReport(Endpoint_Read_Byte());
275
276 /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
277 Endpoint_ClearOUT();
278 }
279 }
280
281 /** Mouse task. This generates the next mouse HID report for the host, and transmits it via the
282 * mouse IN endpoint when the host is ready for more data.
283 */
284 void Mouse_HID_Task(void)
285 {
286 uint8_t JoyStatus_LCL = Joystick_GetStatus();
287
288 /* Device must be connected and configured for the task to run */
289 if (USB_DeviceState != DEVICE_STATE_Configured)
290 return;
291
292 /* Check if board button is pressed, if so mouse mode enabled */
293 if (Buttons_GetStatus() & BUTTONS_BUTTON1)
294 {
295 if (JoyStatus_LCL & JOY_UP)
296 MouseReportData.Y = 1;
297 else if (JoyStatus_LCL & JOY_DOWN)
298 MouseReportData.Y = -1;
299
300 if (JoyStatus_LCL & JOY_RIGHT)
301 MouseReportData.X = 1;
302 else if (JoyStatus_LCL & JOY_LEFT)
303 MouseReportData.X = -1;
304
305 if (JoyStatus_LCL & JOY_PRESS)
306 MouseReportData.Button = (1 << 0);
307 }
308
309 /* Select the Mouse Report Endpoint */
310 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
311
312 /* Check if Mouse Endpoint Ready for Read/Write */
313 if (Endpoint_IsReadWriteAllowed())
314 {
315 /* Write Mouse Report Data */
316 Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));
317
318 /* Finalize the stream transfer to send the last packet */
319 Endpoint_ClearIN();
320
321 /* Clear the report data afterwards */
322 memset(&MouseReportData, 0, sizeof(MouseReportData));
323 }
324 }