3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11 Copyright 2009 Denver Gingerich (denver [at] ossguy [dot] com)
13 Permission to use, copy, modify, and distribute this software
14 and its documentation for any purpose and without fee is hereby
15 granted, provided that the above copyright notice appear in all
16 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.
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
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.
38 #include "KeyboardMouse.h"
40 /* Project Tags, for reading out using the ButtLoad project */
41 BUTTLOADTAG(ProjName
, "LUFA MouseKBD App");
42 BUTTLOADTAG(BuildTime
, __TIME__
);
43 BUTTLOADTAG(BuildDate
, __DATE__
);
44 BUTTLOADTAG(LUFAVersion
, "LUFA V" LUFA_VERSION_STRING
);
46 /* Scheduler Task List */
49 { Task
: USB_USBTask
, TaskStatus
: TASK_STOP
},
50 { Task
: USB_Mouse
, TaskStatus
: TASK_RUN
},
51 { Task
: USB_Keyboard
, TaskStatus
: TASK_RUN
},
54 /* Global Variables */
55 /** Global structure to hold the current keyboard interface HID report, for transmission to the host */
56 USB_KeyboardReport_Data_t KeyboardReportData
;
58 /** Global structure to hold the current mouse interface HID report, for transmission to the host */
59 USB_MouseReport_Data_t MouseReportData
;
61 /** Main program entry point. This routine configures the hardware required by the application, then
62 * starts the scheduler to run the USB management task.
66 /* Disable watchdog if enabled by bootloader/fuses */
67 MCUSR
&= ~(1 << WDRF
);
70 /* Disable Clock Division */
71 SetSystemClockPrescaler(0);
73 /* Hardware Initialization */
77 /* Indicate USB not ready */
78 UpdateStatus(Status_USBNotReady
);
80 /* Initialize Scheduler so that it can be used */
83 /* Initialize USB Subsystem */
86 /* Scheduling - routine never returns, so put this last in the main function */
90 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
91 * starts the library USB task to begin the enumeration and USB management process.
93 EVENT_HANDLER(USB_Connect
)
95 /* Start USB management task */
96 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
98 /* Indicate USB enumerating */
99 UpdateStatus(Status_USBEnumerating
);
102 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
103 * the status LEDs and stops the USB management task.
105 EVENT_HANDLER(USB_Disconnect
)
107 /* Stop running HID reporting and USB management tasks */
108 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
110 /* Indicate USB not ready */
111 UpdateStatus(Status_USBNotReady
);
114 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
115 * of the USB device after enumeration, and configures the keyboard and mouse device endpoints.
117 EVENT_HANDLER(USB_ConfigurationChanged
)
119 /* Setup Keyboard Report Endpoint */
120 Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM
, EP_TYPE_INTERRUPT
,
121 ENDPOINT_DIR_IN
, HID_EPSIZE
,
122 ENDPOINT_BANK_SINGLE
);
124 /* Setup Keyboard LED Report Endpoint */
125 Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPNUM
, EP_TYPE_INTERRUPT
,
126 ENDPOINT_DIR_OUT
, HID_EPSIZE
,
127 ENDPOINT_BANK_SINGLE
);
129 /* Setup Mouse Report Endpoint */
130 Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM
, EP_TYPE_INTERRUPT
,
131 ENDPOINT_DIR_IN
, HID_EPSIZE
,
132 ENDPOINT_BANK_SINGLE
);
134 /* Indicate USB connected and ready */
135 UpdateStatus(Status_USBReady
);
138 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
139 * control requests that are not handled internally by the USB library (including the HID commands, which are
140 * all issued via the control endpoint), so that they can be handled appropriately for the application.
142 EVENT_HANDLER(USB_UnhandledControlPacket
)
147 /* Handle HID Class specific requests */
151 if (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
153 Endpoint_Ignore_Word();
155 uint16_t wIndex
= Endpoint_Read_Word_LE();
157 /* Determine if it is the mouse or the keyboard data that is being requested */
160 ReportData
= (uint8_t*)&KeyboardReportData
;
161 ReportSize
= sizeof(KeyboardReportData
);
165 ReportData
= (uint8_t*)&MouseReportData
;
166 ReportSize
= sizeof(MouseReportData
);
169 /* Read in the number of bytes in the report to send to the host */
170 uint16_t wLength
= Endpoint_Read_Word_LE();
172 /* If trying to send more bytes than exist to the host, clamp the value at the report size */
173 if (wLength
> ReportSize
)
174 wLength
= ReportSize
;
176 Endpoint_ClearSetupReceived();
178 /* Write the report data to the control endpoint */
179 Endpoint_Write_Control_Stream_LE(ReportData
, wLength
);
181 /* Clear the report data afterwards */
182 memset(ReportData
, 0, ReportSize
);
184 /* Finalize the stream transfer to send the last packet or clear the host abort */
185 Endpoint_ClearSetupOUT();
190 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
192 Endpoint_ClearSetupReceived();
194 /* Wait until the LED report has been sent by the host */
195 while (!(Endpoint_IsSetupOUTReceived()));
197 /* Read in the LED report from the host */
198 uint8_t LEDStatus
= Endpoint_Read_Byte();
199 uint8_t LEDMask
= LEDS_LED2
;
201 if (LEDStatus
& 0x01) // NUM Lock
202 LEDMask
|= LEDS_LED1
;
204 if (LEDStatus
& 0x02) // CAPS Lock
205 LEDMask
|= LEDS_LED3
;
207 if (LEDStatus
& 0x04) // SCROLL Lock
208 LEDMask
|= LEDS_LED4
;
210 /* Set the status LEDs to the current HID LED status */
211 LEDs_SetAllLEDs(LEDMask
);
213 /* Clear the endpoint data */
214 Endpoint_ClearSetupOUT();
216 /* Wait until the host is ready to receive the request confirmation */
217 while (!(Endpoint_IsSetupINReady()));
219 /* Handshake the request by sending an empty IN packet */
220 Endpoint_ClearSetupIN();
227 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
228 * log to a serial port, or anything else that is suitable for status updates.
230 * \param CurrentStatus Current status of the system, from the KeyboardMouse_StatusCodes_t enum
232 void UpdateStatus(uint8_t CurrentStatus
)
234 uint8_t LEDMask
= LEDS_NO_LEDS
;
236 /* Set the LED mask to the appropriate LED mask based on the given status code */
237 switch (CurrentStatus
)
239 case Status_USBNotReady
:
240 LEDMask
= (LEDS_LED1
);
242 case Status_USBEnumerating
:
243 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
245 case Status_USBReady
:
246 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
250 /* Set the board LEDs to the new LED mask */
251 LEDs_SetAllLEDs(LEDMask
);
254 /** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the
255 * keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status
256 * reports sent to the device via the keyboard OUT reporting endpoint.
260 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
262 /* Check if HWB is not pressed, if so mouse mode enabled */
263 if (!(HWB_GetStatus()))
265 if (JoyStatus_LCL
& JOY_UP
)
266 KeyboardReportData
.KeyCode
[0] = 0x04; // A
267 else if (JoyStatus_LCL
& JOY_DOWN
)
268 KeyboardReportData
.KeyCode
[0] = 0x05; // B
270 if (JoyStatus_LCL
& JOY_LEFT
)
271 KeyboardReportData
.KeyCode
[0] = 0x06; // C
272 else if (JoyStatus_LCL
& JOY_RIGHT
)
273 KeyboardReportData
.KeyCode
[0] = 0x07; // D
275 if (JoyStatus_LCL
& JOY_PRESS
)
276 KeyboardReportData
.KeyCode
[0] = 0x08; // E
279 /* Check if the USB system is connected to a host and report protocol mode is enabled */
282 /* Select the Keyboard Report Endpoint */
283 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM
);
285 /* Check if Keyboard Endpoint Ready for Read/Write */
286 if (Endpoint_ReadWriteAllowed())
288 /* Write Keyboard Report Data */
289 Endpoint_Write_Stream_LE(&KeyboardReportData
, sizeof(KeyboardReportData
));
291 /* Finalize the stream transfer to send the last packet */
292 Endpoint_ClearCurrentBank();
294 /* Clear the report data afterwards */
295 memset(&KeyboardReportData
, 0, sizeof(KeyboardReportData
));
298 /* Select the Keyboard LED Report Endpoint */
299 Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM
);
301 /* Check if Keyboard LED Endpoint Ready for Read/Write */
302 if (Endpoint_ReadWriteAllowed())
304 /* Read in the LED report from the host */
305 uint8_t LEDStatus
= Endpoint_Read_Byte();
306 uint8_t LEDMask
= LEDS_LED2
;
308 if (LEDStatus
& 0x01) // NUM Lock
309 LEDMask
|= LEDS_LED1
;
311 if (LEDStatus
& 0x02) // CAPS Lock
312 LEDMask
|= LEDS_LED3
;
314 if (LEDStatus
& 0x04) // SCROLL Lock
315 LEDMask
|= LEDS_LED4
;
317 /* Set the status LEDs to the current Keyboard LED status */
318 LEDs_SetAllLEDs(LEDMask
);
320 /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
321 Endpoint_ClearCurrentBank();
326 /** Mouse task. This generates the next mouse HID report for the host, and transmits it via the
327 * mouse IN endpoint when the host is ready for more data.
331 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
333 /* Check if HWB is pressed, if so mouse mode enabled */
336 if (JoyStatus_LCL
& JOY_UP
)
337 MouseReportData
.Y
= 1;
338 else if (JoyStatus_LCL
& JOY_DOWN
)
339 MouseReportData
.Y
= -1;
341 if (JoyStatus_LCL
& JOY_RIGHT
)
342 MouseReportData
.X
= 1;
343 else if (JoyStatus_LCL
& JOY_LEFT
)
344 MouseReportData
.X
= -1;
346 if (JoyStatus_LCL
& JOY_PRESS
)
347 MouseReportData
.Button
= (1 << 0);
350 /* Check if the USB system is connected to a host and report protocol mode is enabled */
353 /* Select the Mouse Report Endpoint */
354 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM
);
356 /* Check if Mouse Endpoint Ready for Read/Write */
357 if (Endpoint_ReadWriteAllowed())
359 /* Write Mouse Report Data */
360 Endpoint_Write_Stream_LE(&MouseReportData
, sizeof(MouseReportData
));
362 /* Finalize the stream transfer to send the last packet */
363 Endpoint_ClearCurrentBank();
365 /* Clear the report data afterwards */
366 memset(&MouseReportData
, 0, sizeof(MouseReportData
));