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 GenericHID demo. This file contains the main tasks of the demo and 
  34  *  is responsible for the initial application hardware configuration. 
  37 #include "GenericHID.h" 
  40 /** Main program entry point. This routine configures the hardware required by the application, then 
  41  *  enters a loop to run the application tasks in sequence. 
  47         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  57 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  58 void SetupHardware(void) 
  60         /* Disable watchdog if enabled by bootloader/fuses */ 
  61         MCUSR 
&= ~(1 << WDRF
); 
  64         /* Disable clock division */ 
  65         clock_prescale_set(clock_div_1
); 
  67         /* 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_Device_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 task. 
  84 void EVENT_USB_Device_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 sets the current configuration 
  91  *  of the USB device after enumeration, and configures the generic HID device endpoints. 
  93 void EVENT_USB_Device_ConfigurationChanged(void) 
  95         bool ConfigSuccess 
= true; 
  97         /* Setup HID Report Endpoints */ 
  98         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(GENERIC_IN_EPADDR
, EP_TYPE_INTERRUPT
, GENERIC_EPSIZE
, 1); 
  99         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(GENERIC_OUT_EPADDR
, EP_TYPE_INTERRUPT
, GENERIC_EPSIZE
, 1); 
 101         /* Indicate endpoint configuration success or failure */ 
 102         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 105 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to 
 106  *  the device from the USB host before passing along unhandled control requests to the library for processing 
 109 void EVENT_USB_Device_ControlRequest(void) 
 111         /* Handle HID Class specific requests */ 
 112         switch (USB_ControlRequest
.bRequest
) 
 114                 case HID_REQ_GetReport
: 
 115                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 117                                 uint8_t GenericData
[GENERIC_REPORT_SIZE
]; 
 118                                 CreateGenericHIDReport(GenericData
); 
 120                                 Endpoint_ClearSETUP(); 
 122                                 /* Write the report data to the control endpoint */ 
 123                                 Endpoint_Write_Control_Stream_LE(&GenericData
, sizeof(GenericData
)); 
 128                 case HID_REQ_SetReport
: 
 129                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 131                                 uint8_t GenericData
[GENERIC_REPORT_SIZE
]; 
 133                                 Endpoint_ClearSETUP(); 
 135                                 /* Read the report data from the control endpoint */ 
 136                                 Endpoint_Read_Control_Stream_LE(&GenericData
, sizeof(GenericData
)); 
 139                                 ProcessGenericHIDReport(GenericData
); 
 146 /** Function to process the last received report from the host. 
 148  *  \param[in] DataArray  Pointer to a buffer where the last received report has been stored 
 150 void ProcessGenericHIDReport(uint8_t* DataArray
) 
 153                 This is where you need to process reports sent from the host to the device. This 
 154                 function is called each time the host has sent a new report. DataArray is an array 
 155                 holding the report sent from the host. 
 158         uint8_t NewLEDMask 
= LEDS_NO_LEDS
; 
 161           NewLEDMask 
|= LEDS_LED1
; 
 164           NewLEDMask 
|= LEDS_LED1
; 
 167           NewLEDMask 
|= LEDS_LED1
; 
 170           NewLEDMask 
|= LEDS_LED1
; 
 172         LEDs_SetAllLEDs(NewLEDMask
); 
 175 /** Function to create the next report to send back to the host at the next reporting interval. 
 177  *  \param[out] DataArray  Pointer to a buffer where the next report data should be stored 
 179 void CreateGenericHIDReport(uint8_t* DataArray
) 
 182                 This is where you need to create reports to be sent to the host from the device. This 
 183                 function is called each time the host is ready to accept a new report. DataArray is 
 184                 an array to hold the report to the host. 
 187         uint8_t CurrLEDMask 
= LEDs_GetLEDs(); 
 189         DataArray
[0] = ((CurrLEDMask 
& LEDS_LED1
) ? 
1 : 0); 
 190         DataArray
[1] = ((CurrLEDMask 
& LEDS_LED2
) ? 
1 : 0); 
 191         DataArray
[2] = ((CurrLEDMask 
& LEDS_LED3
) ? 
1 : 0); 
 192         DataArray
[3] = ((CurrLEDMask 
& LEDS_LED4
) ? 
1 : 0); 
 197         /* Device must be connected and configured for the task to run */ 
 198         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 201         Endpoint_SelectEndpoint(GENERIC_OUT_EPADDR
); 
 203         /* Check to see if a packet has been sent from the host */ 
 204         if (Endpoint_IsOUTReceived()) 
 206                 /* Check to see if the packet contains data */ 
 207                 if (Endpoint_IsReadWriteAllowed()) 
 209                         /* Create a temporary buffer to hold the read in report from the host */ 
 210                         uint8_t GenericData
[GENERIC_REPORT_SIZE
]; 
 212                         /* Read Generic Report Data */ 
 213                         Endpoint_Read_Stream_LE(&GenericData
, sizeof(GenericData
), NULL
); 
 215                         /* Process Generic Report Data */ 
 216                         ProcessGenericHIDReport(GenericData
); 
 219                 /* Finalize the stream transfer to send the last packet */ 
 223         Endpoint_SelectEndpoint(GENERIC_IN_EPADDR
); 
 225         /* Check to see if the host is ready to accept another packet */ 
 226         if (Endpoint_IsINReady()) 
 228                 /* Create a temporary buffer to hold the report to send to the host */ 
 229                 uint8_t GenericData
[GENERIC_REPORT_SIZE
]; 
 231                 /* Create Generic Report Data */ 
 232                 CreateGenericHIDReport(GenericData
); 
 234                 /* Write Generic Report Data */ 
 235                 Endpoint_Write_Stream_LE(&GenericData
, sizeof(GenericData
), NULL
); 
 237                 /* Finalize the stream transfer to send the last packet */