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) 
  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 Joystick demo. This file contains the main tasks of the demo and 
  34  *  is responsible for the initial application hardware configuration. 
  39 /** Main program entry point. This routine configures the hardware required by the application, then 
  40  *  enters a loop to run the application tasks in sequence. 
  46         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  56 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  57 void SetupHardware(void) 
  59         /* Disable watchdog if enabled by bootloader/fuses */ 
  60         MCUSR 
&= ~(1 << WDRF
); 
  63         /* Disable clock division */ 
  64         clock_prescale_set(clock_div_1
); 
  66         /* Hardware Initialization */ 
  73 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and 
  74  *  starts the library USB task to begin the enumeration and USB management process. 
  76 void EVENT_USB_Device_Connect(void) 
  78         /* Indicate USB enumerating */ 
  79         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
  82 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via 
  83  *  the status LEDs and stops the USB management and joystick reporting tasks. 
  85 void EVENT_USB_Device_Disconnect(void) 
  87         /* Indicate USB not ready */ 
  88         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  91 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration 
  92  *  of the USB device after enumeration - the device endpoints are configured and the joystick reporting task started. 
  94 void EVENT_USB_Device_ConfigurationChanged(void) 
  96         /* Indicate USB connected and ready */ 
  97         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
  99         /* Setup Joystick Report Endpoint */ 
 100         if (!(Endpoint_ConfigureEndpoint(JOYSTICK_EPNUM
, EP_TYPE_INTERRUPT
, 
 101                                              ENDPOINT_DIR_IN
, JOYSTICK_EPSIZE
, 
 102                                          ENDPOINT_BANK_SINGLE
))) 
 104                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 108 /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific 
 109  *  control requests that are not handled internally by the USB library (including the HID commands, which are 
 110  *  all issued via the control endpoint), so that they can be handled appropriately for the application. 
 112 void EVENT_USB_Device_UnhandledControlRequest(void) 
 114         /* Handle HID Class specific requests */ 
 115         switch (USB_ControlRequest
.bRequest
) 
 118                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 120                                 USB_JoystickReport_Data_t JoystickReportData
; 
 122                                 Endpoint_ClearSETUP(); 
 124                                 /* Create the next HID report to send to the host */                             
 125                                 GetNextReport(&JoystickReportData
); 
 127                                 /* Write the report data to the control endpoint */ 
 128                                 Endpoint_Write_Control_Stream_LE(&JoystickReportData
, sizeof(JoystickReportData
)); 
 130                                 /* Finalize the stream transfer to send the last packet or clear the host abort */ 
 138 /** Fills the given HID report data structure with the next HID report to send to the host. 
 140  *  \param[out] ReportData  Pointer to a HID report data structure to be filled 
 142  *  \return Boolean true if the new report differs from the last report, false otherwise 
 144 bool GetNextReport(USB_JoystickReport_Data_t
* const ReportData
) 
 146         static uint8_t PrevJoyStatus    
= 0; 
 147         static uint8_t PrevButtonStatus 
= 0; 
 148         uint8_t        JoyStatus_LCL    
= Joystick_GetStatus(); 
 149         uint8_t        ButtonStatus_LCL 
= Buttons_GetStatus(); 
 150         bool           InputChanged     
= false; 
 152         /* Clear the report contents */ 
 153         memset(ReportData
, 0, sizeof(USB_JoystickReport_Data_t
)); 
 155         if (JoyStatus_LCL 
& JOY_UP
) 
 156           ReportData
->Y 
= -100; 
 157         else if (JoyStatus_LCL 
& JOY_DOWN
) 
 160         if (JoyStatus_LCL 
& JOY_LEFT
) 
 161           ReportData
->X 
= -100; 
 162         else if (JoyStatus_LCL 
& JOY_RIGHT
) 
 165         if (JoyStatus_LCL 
& JOY_PRESS
) 
 166           ReportData
->Button  
= (1 << 1); 
 168         if (ButtonStatus_LCL 
& BUTTONS_BUTTON1
) 
 169           ReportData
->Button 
|= (1 << 0); 
 171         /* Check if the new report is different to the previous report */ 
 172         InputChanged 
= (uint8_t)(PrevJoyStatus 
^ JoyStatus_LCL
) | (uint8_t)(PrevButtonStatus 
^ ButtonStatus_LCL
); 
 174         /* Save the current joystick status for later comparison */ 
 175         PrevJoyStatus    
= JoyStatus_LCL
; 
 176         PrevButtonStatus 
= ButtonStatus_LCL
; 
 178         /* Return whether the new report is different to the previous report or not */ 
 182 /** Function to manage HID report generation and transmission to the host. */ 
 185         /* Device must be connected and configured for the task to run */ 
 186         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 189         /* Select the Joystick Report Endpoint */ 
 190         Endpoint_SelectEndpoint(JOYSTICK_EPNUM
); 
 192         /* Check to see if the host is ready for another packet */ 
 193         if (Endpoint_IsINReady()) 
 195                 USB_JoystickReport_Data_t JoystickReportData
; 
 197                 /* Create the next HID report to send to the host */ 
 198                 GetNextReport(&JoystickReportData
); 
 200                 /* Write Joystick Report Data */ 
 201                 Endpoint_Write_Stream_LE(&JoystickReportData
, sizeof(JoystickReportData
)); 
 203                 /* Finalize the stream transfer to send the last packet */ 
 206                 /* Clear the report data afterwards */ 
 207                 memset(&JoystickReportData
, 0, sizeof(JoystickReportData
));