Tag for the LUFA-120730-BETA release.
[pub/USBasp.git] / Demos / Device / LowLevel / KeyboardMouse / KeyboardMouse.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 static USB_KeyboardReport_Data_t KeyboardReportData;
42
43 /** Global structure to hold the current mouse interface HID report, for transmission to the host */
44 static 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 bool ConfigSuccess = true;
105
106 /* Setup Keyboard HID Report Endpoints */
107 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPADDR, EP_TYPE_INTERRUPT, HID_EPSIZE, 1);
108 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPADDR, EP_TYPE_INTERRUPT, HID_EPSIZE, 1);
109
110 /* Setup Mouse HID Report Endpoint */
111 ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPADDR, EP_TYPE_INTERRUPT, HID_EPSIZE, 1);
112
113 /* Indicate endpoint configuration success or failure */
114 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
115 }
116
117 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
118 * the device from the USB host before passing along unhandled control requests to the library for processing
119 * internally.
120 */
121 void EVENT_USB_Device_ControlRequest(void)
122 {
123 uint8_t* ReportData;
124 uint8_t ReportSize;
125
126 /* Handle HID Class specific requests */
127 switch (USB_ControlRequest.bRequest)
128 {
129 case HID_REQ_GetReport:
130 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
131 {
132 Endpoint_ClearSETUP();
133
134 /* Determine if it is the mouse or the keyboard data that is being requested */
135 if (!(USB_ControlRequest.wIndex))
136 {
137 ReportData = (uint8_t*)&KeyboardReportData;
138 ReportSize = sizeof(KeyboardReportData);
139 }
140 else
141 {
142 ReportData = (uint8_t*)&MouseReportData;
143 ReportSize = sizeof(MouseReportData);
144 }
145
146 /* Write the report data to the control endpoint */
147 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
148 Endpoint_ClearOUT();
149
150 /* Clear the report data afterwards */
151 memset(ReportData, 0, ReportSize);
152 }
153
154 break;
155 case HID_REQ_SetReport:
156 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
157 {
158 Endpoint_ClearSETUP();
159
160 /* Wait until the LED report has been sent by the host */
161 while (!(Endpoint_IsOUTReceived()))
162 {
163 if (USB_DeviceState == DEVICE_STATE_Unattached)
164 return;
165 }
166
167 /* Read in the LED report from the host */
168 uint8_t LEDStatus = Endpoint_Read_8();
169
170 Endpoint_ClearOUT();
171 Endpoint_ClearStatusStage();
172
173 /* Process the incoming LED report */
174 Keyboard_ProcessLEDReport(LEDStatus);
175 }
176
177 break;
178 }
179 }
180
181 /** Processes a given Keyboard LED report from the host, and sets the board LEDs to match. Since the Keyboard
182 * LED report can be sent through either the control endpoint (via a HID SetReport request) or the HID OUT
183 * endpoint, the processing code is placed here to avoid duplicating it and potentially having different
184 * behavior depending on the method used to sent it.
185 */
186 void Keyboard_ProcessLEDReport(const uint8_t LEDStatus)
187 {
188 uint8_t LEDMask = LEDS_LED2;
189
190 if (LEDStatus & HID_KEYBOARD_LED_NUMLOCK)
191 LEDMask |= LEDS_LED1;
192
193 if (LEDStatus & HID_KEYBOARD_LED_CAPSLOCK)
194 LEDMask |= LEDS_LED3;
195
196 if (LEDStatus & HID_KEYBOARD_LED_SCROLLLOCK)
197 LEDMask |= LEDS_LED4;
198
199 /* Set the status LEDs to the current Keyboard LED status */
200 LEDs_SetAllLEDs(LEDMask);
201 }
202
203 /** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the
204 * keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status
205 * reports sent to the device via the keyboard OUT reporting endpoint.
206 */
207 void Keyboard_HID_Task(void)
208 {
209 uint8_t JoyStatus_LCL = Joystick_GetStatus();
210
211 /* Device must be connected and configured for the task to run */
212 if (USB_DeviceState != DEVICE_STATE_Configured)
213 return;
214
215 /* Check if board button is not pressed, if so mouse mode enabled */
216 if (!(Buttons_GetStatus() & BUTTONS_BUTTON1))
217 {
218 /* Make sent key uppercase by indicating that the left shift key is pressed */
219 KeyboardReportData.Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT;
220
221 if (JoyStatus_LCL & JOY_UP)
222 KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_A;
223 else if (JoyStatus_LCL & JOY_DOWN)
224 KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_B;
225
226 if (JoyStatus_LCL & JOY_LEFT)
227 KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_C;
228 else if (JoyStatus_LCL & JOY_RIGHT)
229 KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_D;
230
231 if (JoyStatus_LCL & JOY_PRESS)
232 KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_E;
233 }
234
235 /* Select the Keyboard Report Endpoint */
236 Endpoint_SelectEndpoint(KEYBOARD_IN_EPADDR);
237
238 /* Check if Keyboard Endpoint Ready for Read/Write */
239 if (Endpoint_IsReadWriteAllowed())
240 {
241 /* Write Keyboard Report Data */
242 Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData), NULL);
243
244 /* Finalize the stream transfer to send the last packet */
245 Endpoint_ClearIN();
246
247 /* Clear the report data afterwards */
248 memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));
249 }
250
251 /* Select the Keyboard LED Report Endpoint */
252 Endpoint_SelectEndpoint(KEYBOARD_OUT_EPADDR);
253
254 /* Check if Keyboard LED Endpoint Ready for Read/Write */
255 if (Endpoint_IsReadWriteAllowed())
256 {
257 /* Read in and process the LED report from the host */
258 Keyboard_ProcessLEDReport(Endpoint_Read_8());
259
260 /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
261 Endpoint_ClearOUT();
262 }
263 }
264
265 /** Mouse task. This generates the next mouse HID report for the host, and transmits it via the
266 * mouse IN endpoint when the host is ready for more data.
267 */
268 void Mouse_HID_Task(void)
269 {
270 uint8_t JoyStatus_LCL = Joystick_GetStatus();
271
272 /* Device must be connected and configured for the task to run */
273 if (USB_DeviceState != DEVICE_STATE_Configured)
274 return;
275
276 /* Check if board button is pressed, if so mouse mode enabled */
277 if (Buttons_GetStatus() & BUTTONS_BUTTON1)
278 {
279 if (JoyStatus_LCL & JOY_UP)
280 MouseReportData.Y = 1;
281 else if (JoyStatus_LCL & JOY_DOWN)
282 MouseReportData.Y = -1;
283
284 if (JoyStatus_LCL & JOY_RIGHT)
285 MouseReportData.X = 1;
286 else if (JoyStatus_LCL & JOY_LEFT)
287 MouseReportData.X = -1;
288
289 if (JoyStatus_LCL & JOY_PRESS)
290 MouseReportData.Button |= (1 << 0);
291 }
292
293 /* Select the Mouse Report Endpoint */
294 Endpoint_SelectEndpoint(MOUSE_IN_EPADDR);
295
296 /* Check if Mouse Endpoint Ready for Read/Write */
297 if (Endpoint_IsReadWriteAllowed())
298 {
299 /* Write Mouse Report Data */
300 Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData), NULL);
301
302 /* Finalize the stream transfer to send the last packet */
303 Endpoint_ClearIN();
304
305 /* Clear the report data afterwards */
306 memset(&MouseReportData, 0, sizeof(MouseReportData));
307 }
308 }
309