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) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   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  *  starts the scheduler to run the application tasks. 
  46         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  55 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  56 void SetupHardware(void) 
  58         /* Disable watchdog if enabled by bootloader/fuses */ 
  59         MCUSR 
&= ~(1 << WDRF
); 
  62         /* Disable clock division */ 
  63         clock_prescale_set(clock_div_1
); 
  65         /* Hardware Initialization */ 
  72 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and 
  73  *  starts the library USB task to begin the enumeration and USB management process. 
  75 void EVENT_USB_Connect(void) 
  77         /* Indicate USB enumerating */ 
  78         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
  81 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via 
  82  *  the status LEDs and stops the USB management and joystick reporting tasks. 
  84 void EVENT_USB_Disconnect(void) 
  86         /* Indicate USB not ready */ 
  87         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  90 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration 
  91  *  of the USB device after enumeration - the device endpoints are configured and the joystick reporting task started. 
  93 void EVENT_USB_ConfigurationChanged(void) 
  95         /* Indicate USB connected and ready */ 
  96         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
  98         /* Setup Joystick Report Endpoint */ 
  99         if (!(Endpoint_ConfigureEndpoint(JOYSTICK_EPNUM
, EP_TYPE_INTERRUPT
, 
 100                                              ENDPOINT_DIR_IN
, JOYSTICK_EPSIZE
, 
 101                                          ENDPOINT_BANK_SINGLE
))) 
 103                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 107 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific 
 108  *  control requests that are not handled internally by the USB library (including the HID commands, which are 
 109  *  all issued via the control endpoint), so that they can be handled appropriately for the application. 
 111 void EVENT_USB_UnhandledControlPacket(void) 
 113         /* Handle HID Class specific requests */ 
 114         switch (USB_ControlRequest
.bRequest
) 
 117                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 119                                 USB_JoystickReport_Data_t JoystickReportData
; 
 121                                 Endpoint_ClearSETUP(); 
 123                                 /* Create the next HID report to send to the host */                             
 124                                 GetNextReport(&JoystickReportData
); 
 126                                 /* Write the report data to the control endpoint */ 
 127                                 Endpoint_Write_Control_Stream_LE(&JoystickReportData
, sizeof(JoystickReportData
)); 
 129                                 /* Finalize the stream transfer to send the last packet or clear the host abort */ 
 137 /** Fills the given HID report data structure with the next HID report to send to the host. 
 139  *  \param[out] ReportData  Pointer to a HID report data structure to be filled 
 141  *  \return Boolean true if the new report differs from the last report, false otherwise 
 143 bool GetNextReport(USB_JoystickReport_Data_t
* ReportData
) 
 145         static uint8_t PrevJoyStatus    
= 0; 
 146         static uint8_t PrevButtonStatus 
= 0; 
 147         uint8_t        JoyStatus_LCL    
= Joystick_GetStatus(); 
 148         uint8_t        ButtonStatus_LCL 
= Buttons_GetStatus(); 
 149         bool           InputChanged     
= false; 
 151         /* Clear the report contents */ 
 152         memset(ReportData
, 0, sizeof(USB_JoystickReport_Data_t
)); 
 154         if (JoyStatus_LCL 
& JOY_UP
) 
 155           ReportData
->Y 
= -100; 
 156         else if (JoyStatus_LCL 
& JOY_DOWN
) 
 159         if (JoyStatus_LCL 
& JOY_RIGHT
) 
 161         else if (JoyStatus_LCL 
& JOY_LEFT
) 
 162           ReportData
->X 
= -100; 
 164         if (JoyStatus_LCL 
& JOY_PRESS
) 
 165           ReportData
->Button  
= (1 << 1); 
 167         if (ButtonStatus_LCL 
& BUTTONS_BUTTON1
) 
 168           ReportData
->Button 
|= (1 << 0); 
 170         /* Check if the new report is different to the previous report */ 
 171         InputChanged 
= (uint8_t)(PrevJoyStatus 
^ JoyStatus_LCL
) | (uint8_t)(PrevButtonStatus 
^ ButtonStatus_LCL
); 
 173         /* Save the current joystick status for later comparison */ 
 174         PrevJoyStatus    
= JoyStatus_LCL
; 
 175         PrevButtonStatus 
= ButtonStatus_LCL
; 
 177         /* Return whether the new report is different to the previous report or not */ 
 181 /** Function to manage HID report generation and transmission to the host. */ 
 184         /* Device must be connected and configured for the task to run */ 
 185         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 188         /* Select the Joystick Report Endpoint */ 
 189         Endpoint_SelectEndpoint(JOYSTICK_EPNUM
); 
 191         /* Check to see if the host is ready for another packet */ 
 192         if (Endpoint_IsINReady()) 
 194                 USB_JoystickReport_Data_t JoystickReportData
; 
 196                 /* Create the next HID report to send to the host */ 
 197                 GetNextReport(&JoystickReportData
); 
 199                 /* Write Joystick Report Data */ 
 200                 Endpoint_Write_Stream_LE(&JoystickReportData
, sizeof(JoystickReportData
)); 
 202                 /* Finalize the stream transfer to send the last packet */ 
 205                 /* Clear the report data afterwards */ 
 206                 memset(&JoystickReportData
, 0, sizeof(JoystickReportData
));