2             USB Missile Launcher Demo 
   3         Copyright (C) Dave Fletcher, 2010. 
   4          fletch at fletchtronics dot net 
   6        Based on research by Scott Weston at 
   7         http://code.google.com/p/pymissile 
  11   Copyright 2020  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Copyright 2010  Dave Fletcher (fletch [at] fletchtronics [dot] net) 
  14   Permission to use, copy, modify, distribute, and sell this 
  15   software and its documentation for any purpose is hereby granted 
  16   without fee, provided that the above copyright notice appear in 
  17   all copies and that both that the copyright notice and this 
  18   permission notice and warranty disclaimer appear in supporting 
  19   documentation, and that the name of the author not be used in 
  20   advertising or publicity pertaining to distribution of the 
  21   software without specific, written prior permission. 
  23   The author disclaims all warranties with regard to this 
  24   software, including all implied warranties of merchantability 
  25   and fitness.  In no event shall the author be liable for any 
  26   special, indirect or consequential damages or any damages 
  27   whatsoever resulting from loss of use, data or profits, whether 
  28   in an action of contract, negligence or other tortious action, 
  29   arising out of or in connection with the use or performance of 
  34  *  Missile Launcher host. This is a host driver for the popular USB-controller table top toy missile launchers, 
  35  *  which can typically aim and fire small foam "missiles" from a spring-loaded turret. This project controls the 
  36  *  launcher via a joystick and button to aim and fire missiles at targets without a PC. 
  41  *  Main source file for the MissileLauncher application. This file contains the main tasks of 
  42  *  the application and is responsible for the initial application hardware configuration as well 
  43  *  as the sending of commands to the attached launcher toy. 
  46 #include "MissileLauncher.h" 
  48 /** Launcher first init command report data sequence */ 
  49 static const uint8_t CMD_INITA
[8]     = {  85, 83, 66, 67,  0,  0,  4,  0  }; 
  51 /** Launcher second init command report data sequence */ 
  52 static const uint8_t CMD_INITB
[8]     = {  85, 83, 66, 67,  0, 64,  2,  0  }; 
  54 /** Launcher command report data sequence to stop all movement */ 
  55 static const uint8_t CMD_STOP
[8]      = {   0,  0,  0,  0,  0,  0,  8,  8  }; 
  57 /** Launcher command report data sequence to move left */ 
  58 static const uint8_t CMD_LEFT
[8]      = {   0,  1,  0,  0,  0,  0,  8,  8  }; 
  60 /** Launcher command report data sequence to move right */ 
  61 static const uint8_t CMD_RIGHT
[8]     = {   0,  0,  1,  0,  0,  0,  8,  8  }; 
  63 /** Launcher command report data sequence to move up */ 
  64 static const uint8_t CMD_UP
[8]        = {   0,  0,  0,  1,  0,  0,  8,  8  }; 
  66 /** Launcher command report data sequence to move down */ 
  67 static const uint8_t CMD_DOWN
[8]      = {   0,  0,  0,  0,  1,  0,  8,  8  }; 
  69 /** Launcher command report data sequence to move left and up */ 
  70 static const uint8_t CMD_LEFTUP
[8]    = {   0,  1,  0,  1,  0,  0,  8,  8  }; 
  72 /** Launcher command report data sequence to move right and up */ 
  73 static const uint8_t CMD_RIGHTUP
[8]   = {   0,  0,  1,  1,  0,  0,  8,  8  }; 
  75 /** Launcher command report data sequence to move left and down */ 
  76 static const uint8_t CMD_LEFTDOWN
[8]  = {   0,  1,  0,  0,  1,  0,  8,  8  }; 
  78 /** Launcher command report data sequence to move right and down */ 
  79 static const uint8_t CMD_RIGHTDOWN
[8] = {   0,  0,  1,  0,  1,  0,  8,  8  }; 
  81 /** Launcher command report data sequence to fire a missile */ 
  82 static const uint8_t CMD_FIRE
[8]      = {   0,  0,  0,  0,  0,  1,  8,  8  }; 
  84 /** Last command sent to the launcher, to determine what new command (if any) must be sent */ 
  85 static const uint8_t* CmdState
; 
  87 /** Buffer to hold a command to send to the launcher */ 
  88 static uint8_t CmdBuffer
[LAUNCHER_CMD_BUFFER_SIZE
]; 
  91 /** Main program entry point. This routine configures the hardware required by the application, then 
  92  *  enters a loop to run the application tasks in sequence. 
 100         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 101         GlobalInterruptEnable(); 
 105                 Read_Joystick_Status(); 
 112 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
 113 void SetupHardware(void) 
 115 #if (ARCH == ARCH_AVR8) 
 116         /* Disable watchdog if enabled by bootloader/fuses */ 
 117         MCUSR 
&= ~(1 << WDRF
); 
 120         /* Disable clock division */ 
 121         clock_prescale_set(clock_div_1
); 
 124         /* Hardware Initialization */ 
 131 /** Reads the joystick and button status, sending commands to the launcher as needed. */ 
 132 void Read_Joystick_Status(void) 
 134         uint8_t JoyStatus_LCL 
= Joystick_GetStatus(); 
 135         uint8_t Buttons_LCL   
= Buttons_GetStatus(); 
 137         if (Buttons_LCL 
& BUTTONS_BUTTON1
) 
 138           Send_Command(CMD_FIRE
); 
 139         else if (JoyStatus_LCL 
& JOY_UP
) 
 140           Send_Command(CMD_UP
); 
 141         else if (JoyStatus_LCL 
& JOY_DOWN
) 
 142           Send_Command(CMD_DOWN
); 
 143         else if (JoyStatus_LCL 
& JOY_LEFT
) 
 144           Send_Command(CMD_LEFT
); 
 145         else if (JoyStatus_LCL 
& JOY_RIGHT
) 
 146           Send_Command(CMD_RIGHT
); 
 147         else if (CmdState 
!= CMD_STOP
) 
 148           Send_Command(CMD_STOP
); 
 151 /** Lower level send routine, copies report into a larger buffer and sends. 
 153  *  \param[in] Report  Report data to send. 
 154  *  \param[in] ReportSize  Report length in bytes. 
 156 void Send_Command_Report(const uint8_t* const Report
, 
 157                          const uint16_t ReportSize
) 
 159         memcpy(CmdBuffer
, Report
, 8); 
 160         WriteNextReport(CmdBuffer
, ReportSize
); 
 163 /** Sends one of the \c CMD_* command constants to the attached device. 
 165  *  \param[in] Command  One of the command constants. 
 167 void Send_Command(const uint8_t* const Command
) 
 169         if ((CmdState 
== CMD_STOP 
&& Command 
!= CMD_STOP
) || 
 170                 (CmdState 
!= CMD_STOP 
&& Command 
== CMD_STOP
)) 
 172                 LEDs_ToggleLEDs(LEDS_LED4
); 
 174                 Send_Command_Report(CMD_INITA
, 8); 
 175                 Send_Command_Report(CMD_INITB
, 8); 
 176                 Send_Command_Report(Command
, LAUNCHER_CMD_BUFFER_SIZE
); 
 182 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and 
 183  *  starts the library USB task to begin the enumeration and USB management process. 
 185 void EVENT_USB_Host_DeviceAttached(void) 
 187         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 190 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and 
 191  *  stops the library USB task management process. 
 193 void EVENT_USB_Host_DeviceUnattached(void) 
 195         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 198 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully 
 199  *  enumerated by the host and is now ready to be used by the application. 
 201 void EVENT_USB_Host_DeviceEnumerationComplete(void) 
 203         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 205         /* Get and process the configuration descriptor data */ 
 206         if (ProcessConfigurationDescriptor() != SuccessfulConfigRead
) 
 208                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 212         /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */ 
 213         if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful
) 
 215                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 219         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 222 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ 
 223 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
) 
 227         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 231 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while 
 232  *  enumerating an attached USB device. 
 234 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
, 
 235                                             const uint8_t SubErrorCode
) 
 237         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 240 /** Reads in and discards the next report from the attached device. */ 
 241 void DiscardNextReport(void) 
 243         if (USB_HostState 
!= HOST_STATE_Configured
) 
 246         /* Select and unfreeze HID data IN pipe */ 
 247         Pipe_SelectPipe(HID_DATA_IN_PIPE
); 
 250         /* Check to see if a packet has been received */ 
 251         if (!(Pipe_IsINReceived())) 
 253                 /* Refreeze HID data IN pipe */ 
 259         /* Clear the IN endpoint, ready for next data packet */ 
 262         /* Refreeze HID data IN pipe */ 
 266 /** Writes a report to the attached device. 
 268  *  \param[in] ReportOUTData  Buffer containing the report to send to the device 
 269  *  \param[in] ReportLength  Length of the report to send 
 271 void WriteNextReport(uint8_t* const ReportOUTData
, 
 272                      const uint16_t ReportLength
) 
 274         if (USB_HostState 
!= HOST_STATE_Configured
) 
 277         /* Select and unfreeze HID data OUT pipe */ 
 278         Pipe_SelectPipe(HID_DATA_OUT_PIPE
); 
 280         /* Not all HID devices have an OUT endpoint (some require OUT reports to be sent over the 
 281          * control endpoint instead) - check to see if the OUT endpoint has been initialized */ 
 282         if (Pipe_IsConfigured()) 
 286                 /* Ensure pipe is ready to be written to before continuing */ 
 287                 if (!(Pipe_IsOUTReady())) 
 289                         /* Refreeze the data OUT pipe */ 
 295                 /* Write out HID report data */ 
 296                 Pipe_Write_Stream_LE(ReportOUTData
, ReportLength
, NULL
); 
 298                 /* Clear the OUT endpoint, send last data packet */ 
 301                 /* Refreeze the data OUT pipe */ 
 306                 /* Class specific request to send a HID report to the device */ 
 307                 USB_ControlRequest 
= (USB_Request_Header_t
) 
 309                                 .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 310                                 .bRequest      
= HID_REQ_SetReport
, 
 313                                 .wLength       
= ReportLength
, 
 316                 /* Select the control pipe for the request transfer */ 
 317                 Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 319                 /* Send the request to the device */ 
 320                 USB_Host_SendControlRequest(ReportOUTData
);