3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11 Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
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.
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 /** Global structure to hold the current keyboard interface HID report, for transmission to the host */
41 USB_KeyboardReport_Data_t KeyboardReportData
;
43 /** Global structure to hold the current mouse interface HID report, for transmission to the host */
44 USB_MouseReport_Data_t MouseReportData
;
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.
54 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
64 /** Configures the board hardware and chip peripherals for the demo's functionality. */
65 void SetupHardware(void)
67 /* Disable watchdog if enabled by bootloader/fuses */
68 MCUSR
&= ~(1 << WDRF
);
71 /* Disable clock division */
72 clock_prescale_set(clock_div_1
);
74 /* Hardware Initialization */
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.
83 void EVENT_USB_Device_Connect(void)
85 /* Indicate USB enumerating */
86 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
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.
92 void EVENT_USB_Device_Disconnect(void)
94 /* Indicate USB not ready */
95 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
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.
101 void EVENT_USB_Device_ConfigurationChanged(void)
103 /* Indicate USB connected and ready */
104 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
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
)))
111 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
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
)))
119 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
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
)))
127 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
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.
135 void EVENT_USB_Device_UnhandledControlRequest(void)
140 /* Handle HID Class specific requests */
141 switch (USB_ControlRequest
.bRequest
)
144 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
146 Endpoint_ClearSETUP();
148 /* Determine if it is the mouse or the keyboard data that is being requested */
149 if (!(USB_ControlRequest
.wIndex
))
151 ReportData
= (uint8_t*)&KeyboardReportData
;
152 ReportSize
= sizeof(KeyboardReportData
);
156 ReportData
= (uint8_t*)&MouseReportData
;
157 ReportSize
= sizeof(MouseReportData
);
160 /* Write the report data to the control endpoint */
161 Endpoint_Write_Control_Stream_LE(ReportData
, ReportSize
);
163 /* Clear the report data afterwards */
164 memset(ReportData
, 0, ReportSize
);
166 /* Finalize the stream transfer to send the last packet or clear the host abort */
172 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
174 Endpoint_ClearSETUP();
176 /* Wait until the LED report has been sent by the host */
177 while (!(Endpoint_IsOUTReceived()))
179 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
183 /* Read in and process the LED report from the host */
184 Keyboard_ProcessLEDReport(Endpoint_Read_Byte());
186 /* Clear the endpoint data */
189 Endpoint_ClearStatusStage();
196 /** Processes a given Keyboard LED report from the host, and sets the board LEDs to match. Since the Keyboard
197 * LED report can be sent through either the control endpoint (via a HID SetReport request) or the HID OUT
198 * endpoint, the processing code is placed here to avoid duplicating it and potentially having different
199 * behaviour depending on the method used to sent it.
201 void Keyboard_ProcessLEDReport(const uint8_t LEDStatus
)
203 uint8_t LEDMask
= LEDS_LED2
;
205 if (LEDStatus
& KEYBOARD_LED_NUMLOCK
)
206 LEDMask
|= LEDS_LED1
;
208 if (LEDStatus
& KEYBOARD_LED_CAPSLOCK
)
209 LEDMask
|= LEDS_LED3
;
211 if (LEDStatus
& KEYBOARD_LED_SCROLLLOCK
)
212 LEDMask
|= LEDS_LED4
;
214 /* Set the status LEDs to the current Keyboard LED status */
215 LEDs_SetAllLEDs(LEDMask
);
218 /** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the
219 * keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status
220 * reports sent to the device via the keyboard OUT reporting endpoint.
222 void Keyboard_HID_Task(void)
224 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
226 /* Device must be connected and configured for the task to run */
227 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
230 /* Check if board button is not pressed, if so mouse mode enabled */
231 if (!(Buttons_GetStatus() & BUTTONS_BUTTON1
))
233 /* Make sent key uppercase by indicating that the left shift key is pressed */
234 KeyboardReportData
.Modifier
= KEYBOARD_MODIFER_LEFTSHIFT
;
236 if (JoyStatus_LCL
& JOY_UP
)
237 KeyboardReportData
.KeyCode
[0] = 0x04; // A
238 else if (JoyStatus_LCL
& JOY_DOWN
)
239 KeyboardReportData
.KeyCode
[0] = 0x05; // B
241 if (JoyStatus_LCL
& JOY_LEFT
)
242 KeyboardReportData
.KeyCode
[0] = 0x06; // C
243 else if (JoyStatus_LCL
& JOY_RIGHT
)
244 KeyboardReportData
.KeyCode
[0] = 0x07; // D
246 if (JoyStatus_LCL
& JOY_PRESS
)
247 KeyboardReportData
.KeyCode
[0] = 0x08; // E
250 /* Select the Keyboard Report Endpoint */
251 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM
);
253 /* Check if Keyboard Endpoint Ready for Read/Write */
254 if (Endpoint_IsReadWriteAllowed())
256 /* Write Keyboard Report Data */
257 Endpoint_Write_Stream_LE(&KeyboardReportData
, sizeof(KeyboardReportData
));
259 /* Finalize the stream transfer to send the last packet */
262 /* Clear the report data afterwards */
263 memset(&KeyboardReportData
, 0, sizeof(KeyboardReportData
));
266 /* Select the Keyboard LED Report Endpoint */
267 Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM
);
269 /* Check if Keyboard LED Endpoint Ready for Read/Write */
270 if (Endpoint_IsReadWriteAllowed())
272 /* Read in and process the LED report from the host */
273 Keyboard_ProcessLEDReport(Endpoint_Read_Byte());
275 /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
280 /** Mouse task. This generates the next mouse HID report for the host, and transmits it via the
281 * mouse IN endpoint when the host is ready for more data.
283 void Mouse_HID_Task(void)
285 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
287 /* Device must be connected and configured for the task to run */
288 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
291 /* Check if board button is pressed, if so mouse mode enabled */
292 if (Buttons_GetStatus() & BUTTONS_BUTTON1
)
294 if (JoyStatus_LCL
& JOY_UP
)
295 MouseReportData
.Y
= 1;
296 else if (JoyStatus_LCL
& JOY_DOWN
)
297 MouseReportData
.Y
= -1;
299 if (JoyStatus_LCL
& JOY_RIGHT
)
300 MouseReportData
.X
= 1;
301 else if (JoyStatus_LCL
& JOY_LEFT
)
302 MouseReportData
.X
= -1;
304 if (JoyStatus_LCL
& JOY_PRESS
)
305 MouseReportData
.Button
= (1 << 0);
308 /* Select the Mouse Report Endpoint */
309 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM
);
311 /* Check if Mouse Endpoint Ready for Read/Write */
312 if (Endpoint_IsReadWriteAllowed())
314 /* Write Mouse Report Data */
315 Endpoint_Write_Stream_LE(&MouseReportData
, sizeof(MouseReportData
));
317 /* Finalize the stream transfer to send the last packet */
320 /* Clear the report data afterwards */
321 memset(&MouseReportData
, 0, sizeof(MouseReportData
));