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 
  32 #include "KeyboardMouse.h" 
  34 USB_ClassInfo_HID_t Keyboard_HID_Interface 
= 
  38                 .ReportINEndpointNumber  
= KEYBOARD_IN_EPNUM
, 
  39                 .ReportINEndpointSize    
= HID_EPSIZE
, 
  41                 .ReportOUTEndpointNumber 
= KEYBOARD_OUT_EPNUM
, 
  42                 .ReportOUTEndpointSize   
= HID_EPSIZE
, 
  44                 .ReportBufferSize        
= sizeof(USB_KeyboardReport_Data_t
), 
  49 USB_ClassInfo_HID_t Mouse_HID_Interface 
= 
  53                 .ReportINEndpointNumber  
= MOUSE_IN_EPNUM
, 
  54                 .ReportINEndpointSize    
= HID_EPSIZE
, 
  56                 .ReportBufferSize        
= sizeof(USB_MouseReport_Data_t
), 
  58                 .ReportOUTEndpointNumber 
= 0, 
  59                 .ReportOUTEndpointSize   
= 0, 
  66         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  70                 USB_HID_USBTask(&Keyboard_HID_Interface
); 
  71                 USB_HID_USBTask(&Mouse_HID_Interface
); 
  78         /* Disable watchdog if enabled by bootloader/fuses */ 
  79         MCUSR 
&= ~(1 << WDRF
); 
  82         /* Disable clock division */ 
  83         clock_prescale_set(clock_div_1
); 
  85         /* Hardware Initialization */ 
  91 void EVENT_USB_Connect(void) 
  93         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
  96 void EVENT_USB_Disconnect(void) 
  98         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 101 void EVENT_USB_ConfigurationChanged(void) 
 103         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 105         if (!(USB_HID_ConfigureEndpoints(&Keyboard_HID_Interface
))) 
 106           LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 108         if (!(USB_HID_ConfigureEndpoints(&Mouse_HID_Interface
))) 
 109           LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 112 void EVENT_USB_UnhandledControlPacket(void) 
 114         USB_HID_ProcessControlPacket(&Keyboard_HID_Interface
); 
 115         USB_HID_ProcessControlPacket(&Mouse_HID_Interface
); 
 118 void EVENT_USB_StartOfFrame(void) 
 120         USB_HID_RegisterStartOfFrame(&Keyboard_HID_Interface
); 
 121         USB_HID_RegisterStartOfFrame(&Mouse_HID_Interface
); 
 124 uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t
* HIDInterfaceInfo
, void* ReportData
) 
 126         uint8_t JoyStatus_LCL    
= Joystick_GetStatus(); 
 127         uint8_t ButtonStatus_LCL 
= Buttons_GetStatus(); 
 129         if (HIDInterfaceInfo 
== &Keyboard_HID_Interface
) 
 131                 USB_KeyboardReport_Data_t
* KeyboardReport 
= (USB_KeyboardReport_Data_t
*)ReportData
; 
 133                 /* If first board button not being held down, no keyboard report */ 
 134                 if (!(ButtonStatus_LCL 
& BUTTONS_BUTTON1
)) 
 137                 if (JoyStatus_LCL 
& JOY_UP
) 
 138                   KeyboardReport
->KeyCode
[0] = 0x04; // A 
 139                 else if (JoyStatus_LCL 
& JOY_DOWN
) 
 140                   KeyboardReport
->KeyCode
[0] = 0x05; // B 
 142                 if (JoyStatus_LCL 
& JOY_LEFT
) 
 143                   KeyboardReport
->KeyCode
[0] = 0x06; // C 
 144                 else if (JoyStatus_LCL 
& JOY_RIGHT
) 
 145                   KeyboardReport
->KeyCode
[0] = 0x07; // D 
 147                 if (JoyStatus_LCL 
& JOY_PRESS
) 
 148                   KeyboardReport
->KeyCode
[0] = 0x08; // E 
 150                 return sizeof(USB_KeyboardReport_Data_t
); 
 154                 USB_MouseReport_Data_t
* MouseReport 
= (USB_MouseReport_Data_t
*)ReportData
; 
 156                 /* If first board button being held down, no mouse report */ 
 157                 if (ButtonStatus_LCL 
& BUTTONS_BUTTON1
) 
 160                 if (JoyStatus_LCL 
& JOY_UP
) 
 162                 else if (JoyStatus_LCL 
& JOY_DOWN
) 
 165                 if (JoyStatus_LCL 
& JOY_RIGHT
) 
 167                 else if (JoyStatus_LCL 
& JOY_LEFT
) 
 170                 if (JoyStatus_LCL 
& JOY_PRESS
) 
 171                   MouseReport
->Button  
= (1 << 0); 
 173                 return sizeof(USB_MouseReport_Data_t
);           
 177 void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_t
* HIDInterfaceInfo
, void* ReportData
, uint16_t ReportSize
) 
 179         if (HIDInterfaceInfo 
== &Keyboard_HID_Interface
) 
 181                 uint8_t  LEDMask   
= LEDS_NO_LEDS
; 
 182                 uint8_t* LEDReport 
= (uint8_t*)ReportData
; 
 184                 if (*LEDReport 
& 0x01) // NUM Lock 
 185                   LEDMask 
|= LEDS_LED1
; 
 187                 if (*LEDReport 
& 0x02) // CAPS Lock 
 188                   LEDMask 
|= LEDS_LED3
; 
 190                 if (*LEDReport 
& 0x04) // SCROLL Lock 
 191                   LEDMask 
|= LEDS_LED4
; 
 193                 LEDs_SetAllLEDs(LEDMask
);