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 
= 
  57                                                 .Address              
= KEYBOARD_IN_EPADDR
, 
  61                                 .PrevReportINBuffer           
= PrevKeyboardHIDReportBuffer
, 
  62                                 .PrevReportINBufferSize       
= sizeof(PrevKeyboardHIDReportBuffer
), 
  66 /** LUFA HID Class driver interface configuration and state information. This structure is 
  67  *  passed to all HID Class driver functions, so that multiple instances of the same class 
  68  *  within a device can be differentiated from one another. This is for the mouse HID 
  69  *  interface within the device. 
  71 USB_ClassInfo_HID_Device_t Mouse_HID_Interface 
= 
  78                                                 .Address              
= MOUSE_IN_EPADDR
, 
  82                                 .PrevReportINBuffer           
= PrevMouseHIDReportBuffer
, 
  83                                 .PrevReportINBufferSize       
= sizeof(PrevMouseHIDReportBuffer
), 
  88 /** Main program entry point. This routine contains the overall program flow, including initial 
  89  *  setup of all components and the main program loop. 
  95         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 100                 HID_Device_USBTask(&Keyboard_HID_Interface
); 
 101                 HID_Device_USBTask(&Mouse_HID_Interface
); 
 106 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
 109         /* Disable watchdog if enabled by bootloader/fuses */ 
 110         MCUSR 
&= ~(1 << WDRF
); 
 113         /* Disable clock division */ 
 114         clock_prescale_set(clock_div_1
); 
 116         /* Hardware Initialization */ 
 122 /** Event handler for the library USB Connection event. */ 
 123 void EVENT_USB_Device_Connect(void) 
 125     LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 128 /** Event handler for the library USB Disconnection event. */ 
 129 void EVENT_USB_Device_Disconnect(void) 
 131     LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 134 /** Event handler for the library USB Configuration Changed event. */ 
 135 void EVENT_USB_Device_ConfigurationChanged(void) 
 137         bool ConfigSuccess 
= true; 
 139         ConfigSuccess 
&= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface
); 
 140         ConfigSuccess 
&= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface
); 
 142         USB_Device_EnableSOFEvents(); 
 144         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 147 /** Event handler for the library USB Control Request reception event. */ 
 148 void EVENT_USB_Device_ControlRequest(void) 
 150         HID_Device_ProcessControlRequest(&Keyboard_HID_Interface
); 
 151         HID_Device_ProcessControlRequest(&Mouse_HID_Interface
); 
 154 /** Event handler for the USB device Start Of Frame event. */ 
 155 void EVENT_USB_Device_StartOfFrame(void) 
 157         HID_Device_MillisecondElapsed(&Keyboard_HID_Interface
); 
 158         HID_Device_MillisecondElapsed(&Mouse_HID_Interface
); 
 161 /** HID class driver callback function for the creation of HID reports to the host. 
 163  *  \param[in]     HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced 
 164  *  \param[in,out] ReportID    Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID 
 165  *  \param[in]     ReportType  Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature 
 166  *  \param[out]    ReportData  Pointer to a buffer where the created report should be stored 
 167  *  \param[out]    ReportSize  Number of bytes written in the report (or zero if no report is to be sent) 
 169  *  \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent 
 171 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
, 
 172                                          uint8_t* const ReportID
, 
 173                                          const uint8_t ReportType
, 
 175                                          uint16_t* const ReportSize
) 
 177         uint8_t JoyStatus_LCL    
= Joystick_GetStatus(); 
 178         uint8_t ButtonStatus_LCL 
= Buttons_GetStatus(); 
 180         /* Determine which interface must have its report generated */ 
 181         if (HIDInterfaceInfo 
== &Keyboard_HID_Interface
) 
 183                 USB_KeyboardReport_Data_t
* KeyboardReport 
= (USB_KeyboardReport_Data_t
*)ReportData
; 
 185                 /* If first board button not being held down, no keyboard report */ 
 186                 if (!(ButtonStatus_LCL 
& BUTTONS_BUTTON1
)) 
 189                 KeyboardReport
->Modifier 
= HID_KEYBOARD_MODIFIER_LEFTSHIFT
; 
 191                 if (JoyStatus_LCL 
& JOY_UP
) 
 192                   KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_A
; 
 193                 else if (JoyStatus_LCL 
& JOY_DOWN
) 
 194                   KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_B
; 
 196                 if (JoyStatus_LCL 
& JOY_LEFT
) 
 197                   KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_C
; 
 198                 else if (JoyStatus_LCL 
& JOY_RIGHT
) 
 199                   KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_D
; 
 201                 if (JoyStatus_LCL 
& JOY_PRESS
) 
 202                   KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_E
; 
 204                 *ReportSize 
= sizeof(USB_KeyboardReport_Data_t
); 
 209                 USB_MouseReport_Data_t
* MouseReport 
= (USB_MouseReport_Data_t
*)ReportData
; 
 211                 /* If first board button being held down, no mouse report */ 
 212                 if (ButtonStatus_LCL 
& BUTTONS_BUTTON1
) 
 215                 if (JoyStatus_LCL 
& JOY_UP
) 
 217                 else if (JoyStatus_LCL 
& JOY_DOWN
) 
 220                 if (JoyStatus_LCL 
& JOY_LEFT
) 
 222                 else if (JoyStatus_LCL 
& JOY_RIGHT
) 
 225                 if (JoyStatus_LCL 
& JOY_PRESS
) 
 226                   MouseReport
->Button 
|= (1 << 0); 
 228                 *ReportSize 
= sizeof(USB_MouseReport_Data_t
); 
 233 /** HID class driver callback function for the processing of HID reports from the host. 
 235  *  \param[in] HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced 
 236  *  \param[in] ReportID    Report ID of the received report from the host 
 237  *  \param[in] ReportType  The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature 
 238  *  \param[in] ReportData  Pointer to a buffer where the received report has been stored 
 239  *  \param[in] ReportSize  Size in bytes of the received HID report 
 241 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
, 
 242                                           const uint8_t ReportID
, 
 243                                           const uint8_t ReportType
, 
 244                                           const void* ReportData
, 
 245                                           const uint16_t ReportSize
) 
 247         if (HIDInterfaceInfo 
== &Keyboard_HID_Interface
) 
 249                 uint8_t  LEDMask   
= LEDS_NO_LEDS
; 
 250                 uint8_t* LEDReport 
= (uint8_t*)ReportData
; 
 252                 if (*LEDReport 
& HID_KEYBOARD_LED_NUMLOCK
) 
 253                   LEDMask 
|= LEDS_LED1
; 
 255                 if (*LEDReport 
& HID_KEYBOARD_LED_CAPSLOCK
) 
 256                   LEDMask 
|= LEDS_LED3
; 
 258                 if (*LEDReport 
& HID_KEYBOARD_LED_SCROLLLOCK
) 
 259                   LEDMask 
|= LEDS_LED4
; 
 261                 LEDs_SetAllLEDs(LEDMask
);