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
);
65 /** Configures the board hardware and chip peripherals for the demo's functionality. */
66 void SetupHardware(void)
68 /* Disable watchdog if enabled by bootloader/fuses */
69 MCUSR
&= ~(1 << WDRF
);
72 /* Disable clock division */
73 clock_prescale_set(clock_div_1
);
75 /* Hardware Initialization */
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.
84 void EVENT_USB_Device_Connect(void)
86 /* Indicate USB enumerating */
87 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
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.
93 void EVENT_USB_Device_Disconnect(void)
95 /* Indicate USB not ready */
96 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
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.
102 void EVENT_USB_Device_ConfigurationChanged(void)
104 bool ConfigSuccess
= true;
106 /* Setup Keyboard HID Report Endpoints */
107 ConfigSuccess
&= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
,
108 HID_EPSIZE
, ENDPOINT_BANK_SINGLE
);
109 ConfigSuccess
&= Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_OUT
,
110 HID_EPSIZE
, ENDPOINT_BANK_SINGLE
);
112 /* Setup Mouse HID Report Endpoint */
113 ConfigSuccess
&= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
,
114 HID_EPSIZE
, ENDPOINT_BANK_SINGLE
);
116 /* Indicate endpoint configuration success or failure */
117 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY
: LEDMASK_USB_ERROR
);
120 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
121 * control requests that are not handled internally by the USB library (including the HID commands, which are
122 * all issued via the control endpoint), so that they can be handled appropriately for the application.
124 void EVENT_USB_Device_UnhandledControlRequest(void)
129 /* Handle HID Class specific requests */
130 switch (USB_ControlRequest
.bRequest
)
133 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
135 Endpoint_ClearSETUP();
137 /* Determine if it is the mouse or the keyboard data that is being requested */
138 if (!(USB_ControlRequest
.wIndex
))
140 ReportData
= (uint8_t*)&KeyboardReportData
;
141 ReportSize
= sizeof(KeyboardReportData
);
145 ReportData
= (uint8_t*)&MouseReportData
;
146 ReportSize
= sizeof(MouseReportData
);
149 /* Write the report data to the control endpoint */
150 Endpoint_Write_Control_Stream_LE(ReportData
, ReportSize
);
152 /* Clear the report data afterwards */
153 memset(ReportData
, 0, ReportSize
);
155 /* Finalize the stream transfer to send the last packet or clear the host abort */
161 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
163 Endpoint_ClearSETUP();
165 /* Wait until the LED report has been sent by the host */
166 while (!(Endpoint_IsOUTReceived()))
168 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
172 /* Read in and process the LED report from the host */
173 Keyboard_ProcessLEDReport(Endpoint_Read_Byte());
175 /* Clear the endpoint data */
178 Endpoint_ClearStatusStage();
185 /** Processes a given Keyboard LED report from the host, and sets the board LEDs to match. Since the Keyboard
186 * LED report can be sent through either the control endpoint (via a HID SetReport request) or the HID OUT
187 * endpoint, the processing code is placed here to avoid duplicating it and potentially having different
188 * behaviour depending on the method used to sent it.
190 void Keyboard_ProcessLEDReport(const uint8_t LEDStatus
)
192 uint8_t LEDMask
= LEDS_LED2
;
194 if (LEDStatus
& KEYBOARD_LED_NUMLOCK
)
195 LEDMask
|= LEDS_LED1
;
197 if (LEDStatus
& KEYBOARD_LED_CAPSLOCK
)
198 LEDMask
|= LEDS_LED3
;
200 if (LEDStatus
& KEYBOARD_LED_SCROLLLOCK
)
201 LEDMask
|= LEDS_LED4
;
203 /* Set the status LEDs to the current Keyboard LED status */
204 LEDs_SetAllLEDs(LEDMask
);
207 /** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the
208 * keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status
209 * reports sent to the device via the keyboard OUT reporting endpoint.
211 void Keyboard_HID_Task(void)
213 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
215 /* Device must be connected and configured for the task to run */
216 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
219 /* Check if board button is not pressed, if so mouse mode enabled */
220 if (!(Buttons_GetStatus() & BUTTONS_BUTTON1
))
222 /* Make sent key uppercase by indicating that the left shift key is pressed */
223 KeyboardReportData
.Modifier
= KEYBOARD_MODIFER_LEFTSHIFT
;
225 if (JoyStatus_LCL
& JOY_UP
)
226 KeyboardReportData
.KeyCode
[0] = 0x04; // A
227 else if (JoyStatus_LCL
& JOY_DOWN
)
228 KeyboardReportData
.KeyCode
[0] = 0x05; // B
230 if (JoyStatus_LCL
& JOY_LEFT
)
231 KeyboardReportData
.KeyCode
[0] = 0x06; // C
232 else if (JoyStatus_LCL
& JOY_RIGHT
)
233 KeyboardReportData
.KeyCode
[0] = 0x07; // D
235 if (JoyStatus_LCL
& JOY_PRESS
)
236 KeyboardReportData
.KeyCode
[0] = 0x08; // E
239 /* Select the Keyboard Report Endpoint */
240 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM
);
242 /* Check if Keyboard Endpoint Ready for Read/Write */
243 if (Endpoint_IsReadWriteAllowed())
245 /* Write Keyboard Report Data */
246 Endpoint_Write_Stream_LE(&KeyboardReportData
, sizeof(KeyboardReportData
));
248 /* Finalize the stream transfer to send the last packet */
251 /* Clear the report data afterwards */
252 memset(&KeyboardReportData
, 0, sizeof(KeyboardReportData
));
255 /* Select the Keyboard LED Report Endpoint */
256 Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM
);
258 /* Check if Keyboard LED Endpoint Ready for Read/Write */
259 if (Endpoint_IsReadWriteAllowed())
261 /* Read in and process the LED report from the host */
262 Keyboard_ProcessLEDReport(Endpoint_Read_Byte());
264 /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
269 /** Mouse task. This generates the next mouse HID report for the host, and transmits it via the
270 * mouse IN endpoint when the host is ready for more data.
272 void Mouse_HID_Task(void)
274 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
276 /* Device must be connected and configured for the task to run */
277 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
280 /* Check if board button is pressed, if so mouse mode enabled */
281 if (Buttons_GetStatus() & BUTTONS_BUTTON1
)
283 if (JoyStatus_LCL
& JOY_UP
)
284 MouseReportData
.Y
= 1;
285 else if (JoyStatus_LCL
& JOY_DOWN
)
286 MouseReportData
.Y
= -1;
288 if (JoyStatus_LCL
& JOY_RIGHT
)
289 MouseReportData
.X
= 1;
290 else if (JoyStatus_LCL
& JOY_LEFT
)
291 MouseReportData
.X
= -1;
293 if (JoyStatus_LCL
& JOY_PRESS
)
294 MouseReportData
.Button
= (1 << 0);
297 /* Select the Mouse Report Endpoint */
298 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM
);
300 /* Check if Mouse Endpoint Ready for Read/Write */
301 if (Endpoint_IsReadWriteAllowed())
303 /* Write Mouse Report Data */
304 Endpoint_Write_Stream_LE(&MouseReportData
, sizeof(MouseReportData
));
306 /* Finalize the stream transfer to send the last packet */
309 /* Clear the report data afterwards */
310 memset(&MouseReportData
, 0, sizeof(MouseReportData
));