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 clock_prescale_set(clock_div_1
);
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 /* Acknowledge status stage */
217 while (!(Endpoint_IsSetupINReady()));
218 Endpoint_ClearSetupIN();
225 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
226 * log to a serial port, or anything else that is suitable for status updates.
228 * \param CurrentStatus Current status of the system, from the KeyboardMouse_StatusCodes_t enum
230 void UpdateStatus(uint8_t CurrentStatus
)
232 uint8_t LEDMask
= LEDS_NO_LEDS
;
234 /* Set the LED mask to the appropriate LED mask based on the given status code */
235 switch (CurrentStatus
)
237 case Status_USBNotReady
:
238 LEDMask
= (LEDS_LED1
);
240 case Status_USBEnumerating
:
241 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
243 case Status_USBReady
:
244 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
248 /* Set the board LEDs to the new LED mask */
249 LEDs_SetAllLEDs(LEDMask
);
252 /** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the
253 * keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status
254 * reports sent to the device via the keyboard OUT reporting endpoint.
258 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
260 /* Check if HWB is not pressed, if so mouse mode enabled */
261 if (!(HWB_GetStatus()))
263 if (JoyStatus_LCL
& JOY_UP
)
264 KeyboardReportData
.KeyCode
[0] = 0x04; // A
265 else if (JoyStatus_LCL
& JOY_DOWN
)
266 KeyboardReportData
.KeyCode
[0] = 0x05; // B
268 if (JoyStatus_LCL
& JOY_LEFT
)
269 KeyboardReportData
.KeyCode
[0] = 0x06; // C
270 else if (JoyStatus_LCL
& JOY_RIGHT
)
271 KeyboardReportData
.KeyCode
[0] = 0x07; // D
273 if (JoyStatus_LCL
& JOY_PRESS
)
274 KeyboardReportData
.KeyCode
[0] = 0x08; // E
277 /* Check if the USB system is connected to a host and report protocol mode is enabled */
280 /* Select the Keyboard Report Endpoint */
281 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM
);
283 /* Check if Keyboard Endpoint Ready for Read/Write */
284 if (Endpoint_ReadWriteAllowed())
286 /* Write Keyboard Report Data */
287 Endpoint_Write_Stream_LE(&KeyboardReportData
, sizeof(KeyboardReportData
));
289 /* Finalize the stream transfer to send the last packet */
290 Endpoint_ClearCurrentBank();
292 /* Clear the report data afterwards */
293 memset(&KeyboardReportData
, 0, sizeof(KeyboardReportData
));
296 /* Select the Keyboard LED Report Endpoint */
297 Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM
);
299 /* Check if Keyboard LED Endpoint Ready for Read/Write */
300 if (Endpoint_ReadWriteAllowed())
302 /* Read in the LED report from the host */
303 uint8_t LEDStatus
= Endpoint_Read_Byte();
304 uint8_t LEDMask
= LEDS_LED2
;
306 if (LEDStatus
& 0x01) // NUM Lock
307 LEDMask
|= LEDS_LED1
;
309 if (LEDStatus
& 0x02) // CAPS Lock
310 LEDMask
|= LEDS_LED3
;
312 if (LEDStatus
& 0x04) // SCROLL Lock
313 LEDMask
|= LEDS_LED4
;
315 /* Set the status LEDs to the current Keyboard LED status */
316 LEDs_SetAllLEDs(LEDMask
);
318 /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
319 Endpoint_ClearCurrentBank();
324 /** Mouse task. This generates the next mouse HID report for the host, and transmits it via the
325 * mouse IN endpoint when the host is ready for more data.
329 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
331 /* Check if HWB is pressed, if so mouse mode enabled */
334 if (JoyStatus_LCL
& JOY_UP
)
335 MouseReportData
.Y
= 1;
336 else if (JoyStatus_LCL
& JOY_DOWN
)
337 MouseReportData
.Y
= -1;
339 if (JoyStatus_LCL
& JOY_RIGHT
)
340 MouseReportData
.X
= 1;
341 else if (JoyStatus_LCL
& JOY_LEFT
)
342 MouseReportData
.X
= -1;
344 if (JoyStatus_LCL
& JOY_PRESS
)
345 MouseReportData
.Button
= (1 << 0);
348 /* Check if the USB system is connected to a host and report protocol mode is enabled */
351 /* Select the Mouse Report Endpoint */
352 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM
);
354 /* Check if Mouse Endpoint Ready for Read/Write */
355 if (Endpoint_ReadWriteAllowed())
357 /* Write Mouse Report Data */
358 Endpoint_Write_Stream_LE(&MouseReportData
, sizeof(MouseReportData
));
360 /* Finalize the stream transfer to send the last packet */
361 Endpoint_ClearCurrentBank();
363 /* Clear the report data afterwards */
364 memset(&MouseReportData
, 0, sizeof(MouseReportData
));