d31f94db011fdac6b4867ef7ccf7a5fc99d338fe
[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
56 for (;;)
57 {
58 Keyboard_HID_Task();
59 Mouse_HID_Task();
60 USB_USBTask();
61 }
62 }
63
64 /** Configures the board hardware and chip peripherals for the demo's functionality. */
65 void SetupHardware(void)
66 {
67 /* Disable watchdog if enabled by bootloader/fuses */
68 MCUSR &= ~(1 << WDRF);
69 wdt_disable();
70
71 /* Disable clock division */
72 clock_prescale_set(clock_div_1);
73
74 /* Hardware Initialization */
75 Joystick_Init();
76 LEDs_Init();
77 USB_Init();
78 }
79
80 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
81 * starts the library USB task to begin the enumeration and USB management process.
82 */
83 void EVENT_USB_Device_Connect(void)
84 {
85 /* Indicate USB enumerating */
86 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
87 }
88
89 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
90 * the status LEDs and stops the USB management task.
91 */
92 void EVENT_USB_Device_Disconnect(void)
93 {
94 /* Indicate USB not ready */
95 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
96 }
97
98 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
99 * of the USB device after enumeration, and configures the keyboard and mouse device endpoints.
100 */
101 void EVENT_USB_Device_ConfigurationChanged(void)
102 {
103 /* Indicate USB connected and ready */
104 LEDs_SetAllLEDs(LEDMASK_USB_READY);
105
106 /* Setup Keyboard Report Endpoint */
107 if (!(Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT,
108 ENDPOINT_DIR_IN, HID_EPSIZE,
109 ENDPOINT_BANK_SINGLE)))
110 {
111 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
112 }
113
114 /* Setup Keyboard LED Report Endpoint */
115 if (!(Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPNUM, EP_TYPE_INTERRUPT,
116 ENDPOINT_DIR_OUT, HID_EPSIZE,
117 ENDPOINT_BANK_SINGLE)))
118 {
119 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
120 }
121
122 /* Setup Mouse Report Endpoint */
123 if (!(Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT,
124 ENDPOINT_DIR_IN, HID_EPSIZE,
125 ENDPOINT_BANK_SINGLE)))
126 {
127 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
128 }
129 }
130
131 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
132 * control requests that are not handled internally by the USB library (including the HID commands, which are
133 * all issued via the control endpoint), so that they can be handled appropriately for the application.
134 */
135 void EVENT_USB_Device_UnhandledControlRequest(void)
136 {
137 uint8_t* ReportData;
138 uint8_t ReportSize;
139
140 /* Handle HID Class specific requests */
141 switch (USB_ControlRequest.bRequest)
142 {
143 case REQ_GetReport:
144 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
145 {
146 Endpoint_ClearSETUP();
147
148 /* Determine if it is the mouse or the keyboard data that is being requested */
149 if (!(USB_ControlRequest.wIndex))
150 {
151 ReportData = (uint8_t*)&KeyboardReportData;
152 ReportSize = sizeof(KeyboardReportData);
153 }
154 else
155 {
156 ReportData = (uint8_t*)&MouseReportData;
157 ReportSize = sizeof(MouseReportData);
158 }
159
160 /* Write the report data to the control endpoint */
161 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
162
163 /* Clear the report data afterwards */
164 memset(ReportData, 0, ReportSize);
165
166 /* Finalize the stream transfer to send the last packet or clear the host abort */
167 Endpoint_ClearOUT();
168 }
169
170 break;
171 case REQ_SetReport:
172 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
173 {
174 Endpoint_ClearSETUP();
175
176 /* Wait until the LED report has been sent by the host */
177 while (!(Endpoint_IsOUTReceived()))
178 {
179 if (USB_DeviceState == DEVICE_STATE_Unattached)
180 return;
181 }
182
183 /* Read in the LED report from the host */
184 uint8_t LEDStatus = Endpoint_Read_Byte();
185 uint8_t LEDMask = LEDS_LED2;
186
187 if (LEDStatus & KEYBOARD_LED_NUMLOCK)
188 LEDMask |= LEDS_LED1;
189
190 if (LEDStatus & KEYBOARD_LED_CAPSLOCK)
191 LEDMask |= LEDS_LED3;
192
193 if (LEDStatus & KEYBOARD_LED_SCROLLLOCK)
194 LEDMask |= LEDS_LED4;
195
196 /* Set the status LEDs to the current HID LED status */
197 LEDs_SetAllLEDs(LEDMask);
198
199 /* Clear the endpoint data */
200 Endpoint_ClearOUT();
201
202 Endpoint_ClearStatusStage();
203 }
204
205 break;
206 }
207 }
208
209 /** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the
210 * keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status
211 * reports sent to the device via the keyboard OUT reporting endpoint.
212 */
213 void Keyboard_HID_Task(void)
214 {
215 uint8_t JoyStatus_LCL = Joystick_GetStatus();
216
217 /* Device must be connected and configured for the task to run */
218 if (USB_DeviceState != DEVICE_STATE_Configured)
219 return;
220
221 /* Check if board button is not pressed, if so mouse mode enabled */
222 if (!(Buttons_GetStatus() & BUTTONS_BUTTON1))
223 {
224 /* Make sent key uppercase by indicating that the left shift key is pressed */
225 KeyboardReportData.Modifier = KEYBOARD_MODIFER_LEFTSHIFT;
226
227 if (JoyStatus_LCL & JOY_UP)
228 KeyboardReportData.KeyCode[0] = 0x04; // A
229 else if (JoyStatus_LCL & JOY_DOWN)
230 KeyboardReportData.KeyCode[0] = 0x05; // B
231
232 if (JoyStatus_LCL & JOY_LEFT)
233 KeyboardReportData.KeyCode[0] = 0x06; // C
234 else if (JoyStatus_LCL & JOY_RIGHT)
235 KeyboardReportData.KeyCode[0] = 0x07; // D
236
237 if (JoyStatus_LCL & JOY_PRESS)
238 KeyboardReportData.KeyCode[0] = 0x08; // E
239 }
240
241 /* Select the Keyboard Report Endpoint */
242 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
243
244 /* Check if Keyboard Endpoint Ready for Read/Write */
245 if (Endpoint_IsReadWriteAllowed())
246 {
247 /* Write Keyboard Report Data */
248 Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));
249
250 /* Finalize the stream transfer to send the last packet */
251 Endpoint_ClearIN();
252
253 /* Clear the report data afterwards */
254 memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));
255 }
256
257 /* Select the Keyboard LED Report Endpoint */
258 Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM);
259
260 /* Check if Keyboard LED Endpoint Ready for Read/Write */
261 if (Endpoint_IsReadWriteAllowed())
262 {
263 /* Read in the LED report from the host */
264 uint8_t LEDStatus = Endpoint_Read_Byte();
265 uint8_t LEDMask = LEDS_LED2;
266
267 if (LEDStatus & KEYBOARD_LED_NUMLOCK)
268 LEDMask |= LEDS_LED1;
269
270 if (LEDStatus & KEYBOARD_LED_CAPSLOCK)
271 LEDMask |= LEDS_LED3;
272
273 if (LEDStatus & KEYBOARD_LED_SCROLLLOCK)
274 LEDMask |= LEDS_LED4;
275
276 /* Set the status LEDs to the current Keyboard LED status */
277 LEDs_SetAllLEDs(LEDMask);
278
279 /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
280 Endpoint_ClearOUT();
281 }
282 }
283
284 /** Mouse task. This generates the next mouse HID report for the host, and transmits it via the
285 * mouse IN endpoint when the host is ready for more data.
286 */
287 void Mouse_HID_Task(void)
288 {
289 uint8_t JoyStatus_LCL = Joystick_GetStatus();
290
291 /* Device must be connected and configured for the task to run */
292 if (USB_DeviceState != DEVICE_STATE_Configured)
293 return;
294
295 /* Check if board button is pressed, if so mouse mode enabled */
296 if (Buttons_GetStatus() & BUTTONS_BUTTON1)
297 {
298 if (JoyStatus_LCL & JOY_UP)
299 MouseReportData.Y = 1;
300 else if (JoyStatus_LCL & JOY_DOWN)
301 MouseReportData.Y = -1;
302
303 if (JoyStatus_LCL & JOY_RIGHT)
304 MouseReportData.X = 1;
305 else if (JoyStatus_LCL & JOY_LEFT)
306 MouseReportData.X = -1;
307
308 if (JoyStatus_LCL & JOY_PRESS)
309 MouseReportData.Button = (1 << 0);
310 }
311
312 /* Select the Mouse Report Endpoint */
313 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
314
315 /* Check if Mouse Endpoint Ready for Read/Write */
316 if (Endpoint_IsReadWriteAllowed())
317 {
318 /* Write Mouse Report Data */
319 Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));
320
321 /* Finalize the stream transfer to send the last packet */
322 Endpoint_ClearIN();
323
324 /* Clear the report data afterwards */
325 memset(&MouseReportData, 0, sizeof(MouseReportData));
326 }
327 }