3 Copyright (C) Dean Camera, 2012.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * Main source file for the KeyboardMouse demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
37 #include "KeyboardMouse.h"
39 /** Buffer to hold the previously generated Keyboard HID report, for comparison purposes inside the HID class driver. */
40 static uint8_t PrevKeyboardHIDReportBuffer
[sizeof(USB_KeyboardReport_Data_t
)];
42 /** Buffer to hold the previously generated Mouse HID report, for comparison purposes inside the HID class driver. */
43 static uint8_t PrevMouseHIDReportBuffer
[sizeof(USB_MouseReport_Data_t
)];
45 /** LUFA HID Class driver interface configuration and state information. This structure is
46 * passed to all HID Class driver functions, so that multiple instances of the same class
47 * within a device can be differentiated from one another. This is for the keyboard HID
48 * interface within the device.
50 USB_ClassInfo_HID_Device_t Keyboard_HID_Interface
=
56 .ReportINEndpointNumber
= KEYBOARD_IN_EPNUM
,
57 .ReportINEndpointSize
= HID_EPSIZE
,
58 .ReportINEndpointDoubleBank
= false,
60 .PrevReportINBuffer
= PrevKeyboardHIDReportBuffer
,
61 .PrevReportINBufferSize
= sizeof(PrevKeyboardHIDReportBuffer
),
65 /** LUFA HID Class driver interface configuration and state information. This structure is
66 * passed to all HID Class driver functions, so that multiple instances of the same class
67 * within a device can be differentiated from one another. This is for the mouse HID
68 * interface within the device.
70 USB_ClassInfo_HID_Device_t Mouse_HID_Interface
=
76 .ReportINEndpointNumber
= MOUSE_IN_EPNUM
,
77 .ReportINEndpointSize
= HID_EPSIZE
,
79 .PrevReportINBuffer
= PrevMouseHIDReportBuffer
,
80 .PrevReportINBufferSize
= sizeof(PrevMouseHIDReportBuffer
),
85 /** Main program entry point. This routine contains the overall program flow, including initial
86 * setup of all components and the main program loop.
92 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
97 HID_Device_USBTask(&Keyboard_HID_Interface
);
98 HID_Device_USBTask(&Mouse_HID_Interface
);
103 /** Configures the board hardware and chip peripherals for the demo's functionality. */
106 /* Disable watchdog if enabled by bootloader/fuses */
107 MCUSR
&= ~(1 << WDRF
);
110 /* Disable clock division */
111 clock_prescale_set(clock_div_1
);
113 /* Hardware Initialization */
119 /** Event handler for the library USB Connection event. */
120 void EVENT_USB_Device_Connect(void)
122 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
125 /** Event handler for the library USB Disconnection event. */
126 void EVENT_USB_Device_Disconnect(void)
128 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
131 /** Event handler for the library USB Configuration Changed event. */
132 void EVENT_USB_Device_ConfigurationChanged(void)
134 bool ConfigSuccess
= true;
136 ConfigSuccess
&= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface
);
137 ConfigSuccess
&= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface
);
139 USB_Device_EnableSOFEvents();
141 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY
: LEDMASK_USB_ERROR
);
144 /** Event handler for the library USB Control Request reception event. */
145 void EVENT_USB_Device_ControlRequest(void)
147 HID_Device_ProcessControlRequest(&Keyboard_HID_Interface
);
148 HID_Device_ProcessControlRequest(&Mouse_HID_Interface
);
151 /** Event handler for the USB device Start Of Frame event. */
152 void EVENT_USB_Device_StartOfFrame(void)
154 HID_Device_MillisecondElapsed(&Keyboard_HID_Interface
);
155 HID_Device_MillisecondElapsed(&Mouse_HID_Interface
);
158 /** HID class driver callback function for the creation of HID reports to the host.
160 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
161 * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
162 * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
163 * \param[out] ReportData Pointer to a buffer where the created report should be stored
164 * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
166 * \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent
168 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
,
169 uint8_t* const ReportID
,
170 const uint8_t ReportType
,
172 uint16_t* const ReportSize
)
174 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
175 uint8_t ButtonStatus_LCL
= Buttons_GetStatus();
177 /* Determine which interface must have its report generated */
178 if (HIDInterfaceInfo
== &Keyboard_HID_Interface
)
180 USB_KeyboardReport_Data_t
* KeyboardReport
= (USB_KeyboardReport_Data_t
*)ReportData
;
182 /* If first board button not being held down, no keyboard report */
183 if (!(ButtonStatus_LCL
& BUTTONS_BUTTON1
))
186 KeyboardReport
->Modifier
= HID_KEYBOARD_MODIFIER_LEFTSHIFT
;
188 if (JoyStatus_LCL
& JOY_UP
)
189 KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_A
;
190 else if (JoyStatus_LCL
& JOY_DOWN
)
191 KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_B
;
193 if (JoyStatus_LCL
& JOY_LEFT
)
194 KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_C
;
195 else if (JoyStatus_LCL
& JOY_RIGHT
)
196 KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_D
;
198 if (JoyStatus_LCL
& JOY_PRESS
)
199 KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_E
;
201 *ReportSize
= sizeof(USB_KeyboardReport_Data_t
);
206 USB_MouseReport_Data_t
* MouseReport
= (USB_MouseReport_Data_t
*)ReportData
;
208 /* If first board button being held down, no mouse report */
209 if (ButtonStatus_LCL
& BUTTONS_BUTTON1
)
212 if (JoyStatus_LCL
& JOY_UP
)
214 else if (JoyStatus_LCL
& JOY_DOWN
)
217 if (JoyStatus_LCL
& JOY_LEFT
)
219 else if (JoyStatus_LCL
& JOY_RIGHT
)
222 if (JoyStatus_LCL
& JOY_PRESS
)
223 MouseReport
->Button
|= (1 << 0);
225 *ReportSize
= sizeof(USB_MouseReport_Data_t
);
230 /** HID class driver callback function for the processing of HID reports from the host.
232 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
233 * \param[in] ReportID Report ID of the received report from the host
234 * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
235 * \param[in] ReportData Pointer to a buffer where the received report has been stored
236 * \param[in] ReportSize Size in bytes of the received HID report
238 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
,
239 const uint8_t ReportID
,
240 const uint8_t ReportType
,
241 const void* ReportData
,
242 const uint16_t ReportSize
)
244 if (HIDInterfaceInfo
== &Keyboard_HID_Interface
)
246 uint8_t LEDMask
= LEDS_NO_LEDS
;
247 uint8_t* LEDReport
= (uint8_t*)ReportData
;
249 if (*LEDReport
& HID_KEYBOARD_LED_NUMLOCK
)
250 LEDMask
|= LEDS_LED1
;
252 if (*LEDReport
& HID_KEYBOARD_LED_CAPSLOCK
)
253 LEDMask
|= LEDS_LED3
;
255 if (*LEDReport
& HID_KEYBOARD_LED_SCROLLLOCK
)
256 LEDMask
|= LEDS_LED4
;
258 LEDs_SetAllLEDs(LEDMask
);