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 /* Scheduler Task List */
43 { Task
: USB_USBTask
, TaskStatus
: TASK_STOP
},
44 { Task
: USB_Mouse
, TaskStatus
: TASK_RUN
},
45 { Task
: USB_Keyboard
, TaskStatus
: TASK_RUN
},
48 /* Global Variables */
49 /** Global structure to hold the current keyboard interface HID report, for transmission to the host */
50 USB_KeyboardReport_Data_t KeyboardReportData
;
52 /** Global structure to hold the current mouse interface HID report, for transmission to the host */
53 USB_MouseReport_Data_t MouseReportData
;
55 /** Main program entry point. This routine configures the hardware required by the application, then
56 * starts the scheduler to run the USB management task.
60 /* Disable watchdog if enabled by bootloader/fuses */
61 MCUSR
&= ~(1 << WDRF
);
64 /* Disable clock division */
65 clock_prescale_set(clock_div_1
);
67 /* Hardware Initialization */
71 /* Indicate USB not ready */
72 UpdateStatus(Status_USBNotReady
);
74 /* Initialize Scheduler so that it can be used */
77 /* Initialize USB Subsystem */
80 /* Scheduling - routine never returns, so put this last in the main function */
84 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
85 * starts the library USB task to begin the enumeration and USB management process.
87 EVENT_HANDLER(USB_Connect
)
89 /* Start USB management task */
90 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
92 /* Indicate USB enumerating */
93 UpdateStatus(Status_USBEnumerating
);
96 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
97 * the status LEDs and stops the USB management task.
99 EVENT_HANDLER(USB_Disconnect
)
101 /* Stop running HID reporting and USB management tasks */
102 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
104 /* Indicate USB not ready */
105 UpdateStatus(Status_USBNotReady
);
108 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
109 * of the USB device after enumeration, and configures the keyboard and mouse device endpoints.
111 EVENT_HANDLER(USB_ConfigurationChanged
)
113 /* Setup Keyboard Report Endpoint */
114 Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM
, EP_TYPE_INTERRUPT
,
115 ENDPOINT_DIR_IN
, HID_EPSIZE
,
116 ENDPOINT_BANK_SINGLE
);
118 /* Setup Keyboard LED Report Endpoint */
119 Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPNUM
, EP_TYPE_INTERRUPT
,
120 ENDPOINT_DIR_OUT
, HID_EPSIZE
,
121 ENDPOINT_BANK_SINGLE
);
123 /* Setup Mouse Report Endpoint */
124 Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM
, EP_TYPE_INTERRUPT
,
125 ENDPOINT_DIR_IN
, HID_EPSIZE
,
126 ENDPOINT_BANK_SINGLE
);
128 /* Indicate USB connected and ready */
129 UpdateStatus(Status_USBReady
);
132 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
133 * control requests that are not handled internally by the USB library (including the HID commands, which are
134 * all issued via the control endpoint), so that they can be handled appropriately for the application.
136 EVENT_HANDLER(USB_UnhandledControlPacket
)
141 /* Handle HID Class specific requests */
145 if (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
147 Endpoint_Discard_Word();
149 uint16_t wIndex
= Endpoint_Read_Word_LE();
151 /* Determine if it is the mouse or the keyboard data that is being requested */
154 ReportData
= (uint8_t*)&KeyboardReportData
;
155 ReportSize
= sizeof(KeyboardReportData
);
159 ReportData
= (uint8_t*)&MouseReportData
;
160 ReportSize
= sizeof(MouseReportData
);
163 /* Read in the number of bytes in the report to send to the host */
164 uint16_t wLength
= Endpoint_Read_Word_LE();
166 /* If trying to send more bytes than exist to the host, clamp the value at the report size */
167 if (wLength
> ReportSize
)
168 wLength
= ReportSize
;
170 Endpoint_ClearControlSETUP();
172 /* Write the report data to the control endpoint */
173 Endpoint_Write_Control_Stream_LE(ReportData
, wLength
);
175 /* Clear the report data afterwards */
176 memset(ReportData
, 0, ReportSize
);
178 /* Finalize the stream transfer to send the last packet or clear the host abort */
179 Endpoint_ClearControlOUT();
184 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
186 Endpoint_ClearControlSETUP();
188 /* Wait until the LED report has been sent by the host */
189 while (!(Endpoint_IsOUTReceived()));
191 /* Read in the LED report from the host */
192 uint8_t LEDStatus
= Endpoint_Read_Byte();
193 uint8_t LEDMask
= LEDS_LED2
;
195 if (LEDStatus
& 0x01) // NUM Lock
196 LEDMask
|= LEDS_LED1
;
198 if (LEDStatus
& 0x02) // CAPS Lock
199 LEDMask
|= LEDS_LED3
;
201 if (LEDStatus
& 0x04) // SCROLL Lock
202 LEDMask
|= LEDS_LED4
;
204 /* Set the status LEDs to the current HID LED status */
205 LEDs_SetAllLEDs(LEDMask
);
207 /* Clear the endpoint data */
208 Endpoint_ClearControlOUT();
210 /* Acknowledge status stage */
211 while (!(Endpoint_IsINReady()));
212 Endpoint_ClearControlIN();
219 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
220 * log to a serial port, or anything else that is suitable for status updates.
222 * \param CurrentStatus Current status of the system, from the KeyboardMouse_StatusCodes_t enum
224 void UpdateStatus(uint8_t CurrentStatus
)
226 uint8_t LEDMask
= LEDS_NO_LEDS
;
228 /* Set the LED mask to the appropriate LED mask based on the given status code */
229 switch (CurrentStatus
)
231 case Status_USBNotReady
:
232 LEDMask
= (LEDS_LED1
);
234 case Status_USBEnumerating
:
235 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
237 case Status_USBReady
:
238 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
242 /* Set the board LEDs to the new LED mask */
243 LEDs_SetAllLEDs(LEDMask
);
246 /** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the
247 * keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status
248 * reports sent to the device via the keyboard OUT reporting endpoint.
252 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
254 /* Check if HWB is not pressed, if so mouse mode enabled */
255 if (!(HWB_GetStatus()))
257 if (JoyStatus_LCL
& JOY_UP
)
258 KeyboardReportData
.KeyCode
[0] = 0x04; // A
259 else if (JoyStatus_LCL
& JOY_DOWN
)
260 KeyboardReportData
.KeyCode
[0] = 0x05; // B
262 if (JoyStatus_LCL
& JOY_LEFT
)
263 KeyboardReportData
.KeyCode
[0] = 0x06; // C
264 else if (JoyStatus_LCL
& JOY_RIGHT
)
265 KeyboardReportData
.KeyCode
[0] = 0x07; // D
267 if (JoyStatus_LCL
& JOY_PRESS
)
268 KeyboardReportData
.KeyCode
[0] = 0x08; // E
271 /* Check if the USB system is connected to a host and report protocol mode is enabled */
274 /* Select the Keyboard Report Endpoint */
275 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM
);
277 /* Check if Keyboard Endpoint Ready for Read/Write */
278 if (Endpoint_IsReadWriteAllowed())
280 /* Write Keyboard Report Data */
281 Endpoint_Write_Stream_LE(&KeyboardReportData
, sizeof(KeyboardReportData
));
283 /* Finalize the stream transfer to send the last packet */
286 /* Clear the report data afterwards */
287 memset(&KeyboardReportData
, 0, sizeof(KeyboardReportData
));
290 /* Select the Keyboard LED Report Endpoint */
291 Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM
);
293 /* Check if Keyboard LED Endpoint Ready for Read/Write */
294 if (Endpoint_IsReadWriteAllowed())
296 /* Read in the LED report from the host */
297 uint8_t LEDStatus
= Endpoint_Read_Byte();
298 uint8_t LEDMask
= LEDS_LED2
;
300 if (LEDStatus
& 0x01) // NUM Lock
301 LEDMask
|= LEDS_LED1
;
303 if (LEDStatus
& 0x02) // CAPS Lock
304 LEDMask
|= LEDS_LED3
;
306 if (LEDStatus
& 0x04) // SCROLL Lock
307 LEDMask
|= LEDS_LED4
;
309 /* Set the status LEDs to the current Keyboard LED status */
310 LEDs_SetAllLEDs(LEDMask
);
312 /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
318 /** Mouse task. This generates the next mouse HID report for the host, and transmits it via the
319 * mouse IN endpoint when the host is ready for more data.
323 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
325 /* Check if HWB is pressed, if so mouse mode enabled */
328 if (JoyStatus_LCL
& JOY_UP
)
329 MouseReportData
.Y
= 1;
330 else if (JoyStatus_LCL
& JOY_DOWN
)
331 MouseReportData
.Y
= -1;
333 if (JoyStatus_LCL
& JOY_RIGHT
)
334 MouseReportData
.X
= 1;
335 else if (JoyStatus_LCL
& JOY_LEFT
)
336 MouseReportData
.X
= -1;
338 if (JoyStatus_LCL
& JOY_PRESS
)
339 MouseReportData
.Button
= (1 << 0);
342 /* Check if the USB system is connected to a host and report protocol mode is enabled */
345 /* Select the Mouse Report Endpoint */
346 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM
);
348 /* Check if Mouse Endpoint Ready for Read/Write */
349 if (Endpoint_IsReadWriteAllowed())
351 /* Write Mouse Report Data */
352 Endpoint_Write_Stream_LE(&MouseReportData
, sizeof(MouseReportData
));
354 /* Finalize the stream transfer to send the last packet */
357 /* Clear the report data afterwards */
358 memset(&MouseReportData
, 0, sizeof(MouseReportData
));