3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  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 VirtualSerial demo. This file contains the main tasks of 
  34  *  the demo and is responsible for the initial application hardware configuration. 
  37 #include "VirtualSerial.h" 
  39 /** LUFA CDC Class driver interface configuration and state information. This structure is 
  40  *  passed to all CDC Class driver functions, so that multiple instances of the same class 
  41  *  within a device can be differentiated from one another. 
  43 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface 
= 
  47                                 .ControlInterfaceNumber         
= 0, 
  49                                 .DataINEndpointNumber           
= CDC_TX_EPNUM
, 
  50                                 .DataINEndpointSize             
= CDC_TXRX_EPSIZE
, 
  51                                 .DataINEndpointDoubleBank       
= false, 
  53                                 .DataOUTEndpointNumber          
= CDC_RX_EPNUM
, 
  54                                 .DataOUTEndpointSize            
= CDC_TXRX_EPSIZE
, 
  55                                 .DataOUTEndpointDoubleBank      
= false, 
  57                                 .NotificationEndpointNumber     
= CDC_NOTIFICATION_EPNUM
, 
  58                                 .NotificationEndpointSize       
= CDC_NOTIFICATION_EPSIZE
, 
  59                                 .NotificationEndpointDoubleBank 
= false, 
  63 /** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be 
  64  *  used like any regular character stream in the C APIs 
  66 static FILE USBSerialStream
; 
  69 /** Main program entry point. This routine contains the overall program flow, including initial 
  70  *  setup of all components and the main program loop. 
  76         /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */ 
  77         CDC_Device_CreateStream(&VirtualSerial_CDC_Interface
, &USBSerialStream
); 
  79         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  84                 CheckJoystickMovement(); 
  86                 /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */ 
  87                 CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface
); 
  89                 CDC_Device_USBTask(&VirtualSerial_CDC_Interface
); 
  94 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  95 void SetupHardware(void) 
  97         /* Disable watchdog if enabled by bootloader/fuses */ 
  98         MCUSR 
&= ~(1 << WDRF
); 
 101         /* Disable clock division */ 
 102         clock_prescale_set(clock_div_1
); 
 104         /* Hardware Initialization */ 
 110 /** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */ 
 111 void CheckJoystickMovement(void) 
 113         uint8_t     JoyStatus_LCL 
= Joystick_GetStatus(); 
 114         char*       ReportString  
= NULL
; 
 115         static bool ActionSent    
= false; 
 117         if (JoyStatus_LCL 
& JOY_UP
) 
 118           ReportString 
= "Joystick Up\r\n"; 
 119         else if (JoyStatus_LCL 
& JOY_DOWN
) 
 120           ReportString 
= "Joystick Down\r\n"; 
 121         else if (JoyStatus_LCL 
& JOY_LEFT
) 
 122           ReportString 
= "Joystick Left\r\n"; 
 123         else if (JoyStatus_LCL 
& JOY_RIGHT
) 
 124           ReportString 
= "Joystick Right\r\n"; 
 125         else if (JoyStatus_LCL 
& JOY_PRESS
) 
 126           ReportString 
= "Joystick Pressed\r\n"; 
 130         if ((ReportString 
!= NULL
) && (ActionSent 
== false)) 
 134                 /* Write the string to the virtual COM port via the created character stream */ 
 135                 fputs(ReportString
, &USBSerialStream
); 
 137                 /* Alternatively, without the stream: */ 
 138                 // CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString); 
 142 /** Event handler for the library USB Connection event. */ 
 143 void EVENT_USB_Device_Connect(void) 
 145         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 148 /** Event handler for the library USB Disconnection event. */ 
 149 void EVENT_USB_Device_Disconnect(void) 
 151         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 154 /** Event handler for the library USB Configuration Changed event. */ 
 155 void EVENT_USB_Device_ConfigurationChanged(void) 
 157         bool ConfigSuccess 
= true; 
 159         ConfigSuccess 
&= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface
); 
 161         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 164 /** Event handler for the library USB Control Request reception event. */ 
 165 void EVENT_USB_Device_ControlRequest(void) 
 167         CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface
);