3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 USB Missile Launcher Demo
11 Copyright (C) Dave Fletcher, 2009.
12 fletch at fletchtronics dot net
14 Based on research by Scott Weston at
15 http://code.google.com/p/pymissile
19 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
20 Copyright 2009 Dave Fletcher (fletch [at] fletchtronics [dot] net)
22 Permission to use, copy, modify, and distribute this software
23 and its documentation for any purpose and without fee is hereby
24 granted, provided that the above copyright notice appear in all
25 copies and that both that the copyright notice and this
26 permission notice and warranty disclaimer appear in supporting
27 documentation, and that the name of the author not be used in
28 advertising or publicity pertaining to distribution of the
29 software without specific, written prior permission.
31 The author disclaim all warranties with regard to this
32 software, including all implied warranties of merchantability
33 and fitness. In no event shall the author be liable for any
34 special, indirect or consequential damages or any damages
35 whatsoever resulting from loss of use, data or profits, whether
36 in an action of contract, negligence or other tortious action,
37 arising out of or in connection with the use or performance of
42 * Missile Launcher host. This is a host driver for the popular USB-controller table top toy missile launchers,
43 * which can typically aim and fire small foam "missiles" from a spring-loaded turret. This project controls the
44 * launcher via a joystick and button to aim and fire missiles at targets without a PC.
49 * Main source file for the MissileLauncher application. This file contains the main tasks of
50 * the application and is responsible for the initial application hardware configuration as well
51 * as the sending of commands to the attached launcher toy.
54 #include "MissileLauncher.h"
56 /** Launcher first init command report data sequence */
57 uint8_t CMD_INITA
[8] = { 85, 83, 66, 67, 0, 0, 4, 0 };
59 /** Launcher second init command report data sequence */
60 uint8_t CMD_INITB
[8] = { 85, 83, 66, 67, 0, 64, 2, 0 };
62 /** Launcher command report data sequence to stop all movement */
63 uint8_t CMD_STOP
[8] = { 0, 0, 0, 0, 0, 0, 8, 8 };
65 /** Launcher command report data sequence to move left */
66 uint8_t CMD_LEFT
[8] = { 0, 1, 0, 0, 0, 0, 8, 8 };
68 /** Launcher command report data sequence to move right */
69 uint8_t CMD_RIGHT
[8] = { 0, 0, 1, 0, 0, 0, 8, 8 };
71 /** Launcher command report data sequence to move up */
72 uint8_t CMD_UP
[8] = { 0, 0, 0, 1, 0, 0, 8, 8 };
74 /** Launcher command report data sequence to move down */
75 uint8_t CMD_DOWN
[8] = { 0, 0, 0, 0, 1, 0, 8, 8 };
77 /** Launcher command report data sequence to move left and up */
78 uint8_t CMD_LEFTUP
[8] = { 0, 1, 0, 1, 0, 0, 8, 8 };
80 /** Launcher command report data sequence to move right and up */
81 uint8_t CMD_RIGHTUP
[8] = { 0, 0, 1, 1, 0, 0, 8, 8 };
83 /** Launcher command report data sequence to move left and down */
84 uint8_t CMD_LEFTDOWN
[8] = { 0, 1, 0, 0, 1, 0, 8, 8 };
86 /** Launcher command report data sequence to move right and down */
87 uint8_t CMD_RIGHTDOWN
[8] = { 0, 0, 1, 0, 1, 0, 8, 8 };
89 /** Launcher command report data sequence to fire a missile */
90 uint8_t CMD_FIRE
[8] = { 0, 0, 0, 0, 0, 1, 8, 8 };
92 /** Last command sent to the launcher, to determine what new command (if any) must be sent */
95 /** Buffer to hold a command to send to the launcher */
96 uint8_t CmdBuffer
[LAUNCHER_CMD_BUFFER_SIZE
];
99 /** Main program entry point. This routine configures the hardware required by the application, then
100 * enters a loop to run the application tasks in sequence.
106 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
112 Read_Joystick_Status();
119 /** Configures the board hardware and chip peripherals for the demo's functionality. */
120 void SetupHardware(void)
122 /* Disable watchdog if enabled by bootloader/fuses */
123 MCUSR
&= ~(1 << WDRF
);
126 /* Disable clock division */
127 clock_prescale_set(clock_div_1
);
129 /* Hardware Initialization */
136 /** Reads the joystick and button status, sending commands to the launcher as needed. */
137 void Read_Joystick_Status(void)
139 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
140 uint8_t Buttons_LCL
= Buttons_GetStatus();
142 if (Buttons_LCL
& BUTTONS_BUTTON1
)
143 Send_Command(CMD_FIRE
);
144 else if (JoyStatus_LCL
& JOY_UP
)
145 Send_Command(CMD_UP
);
146 else if (JoyStatus_LCL
& JOY_DOWN
)
147 Send_Command(CMD_DOWN
);
148 else if (JoyStatus_LCL
& JOY_LEFT
)
149 Send_Command(CMD_LEFT
);
150 else if (JoyStatus_LCL
& JOY_RIGHT
)
151 Send_Command(CMD_RIGHT
);
152 else if (CmdState
!= CMD_STOP
)
153 Send_Command(CMD_STOP
);
156 /** Lower level send routine, copies report into a larger buffer and sends.
158 * \param[in] Report Report data to send.
159 * \param[in] ReportSize Report length in bytes.
161 void Send_Command_Report(uint8_t *Report
, uint16_t ReportSize
)
163 memcpy(CmdBuffer
, Report
, 8);
164 WriteNextReport(CmdBuffer
, ReportSize
);
167 /** Sends one of the CMD_* command constants to the attached device.
169 * \param[in] Command One of the command constants.
171 void Send_Command(uint8_t* Command
)
173 if ((CmdState
== CMD_STOP
&& Command
!= CMD_STOP
) ||
174 (CmdState
!= CMD_STOP
&& Command
== CMD_STOP
))
176 LEDs_ToggleLEDs(LEDS_LED4
);
178 Send_Command_Report(CMD_INITA
, 8);
179 Send_Command_Report(CMD_INITB
, 8);
180 Send_Command_Report(Command
, LAUNCHER_CMD_BUFFER_SIZE
);
186 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
187 * starts the library USB task to begin the enumeration and USB management process.
189 void EVENT_USB_Host_DeviceAttached(void)
191 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
194 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
195 * stops the library USB task management process.
197 void EVENT_USB_Host_DeviceUnattached(void)
199 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
202 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
203 * enumerated by the host and is now ready to be used by the application.
205 void EVENT_USB_Host_DeviceEnumerationComplete(void)
207 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
210 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
211 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
)
215 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
219 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
220 * enumerating an attached USB device.
222 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
, const uint8_t SubErrorCode
)
224 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
227 /** Reads in and discards the next report from the attached device. */
228 void DiscardNextReport(void)
230 /* Select and unfreeze HID data IN pipe */
231 Pipe_SelectPipe(HID_DATA_IN_PIPE
);
234 /* Check to see if a packet has been received */
235 if (!(Pipe_IsINReceived()))
237 /* Refreeze HID data IN pipe */
243 /* Clear the IN endpoint, ready for next data packet */
246 /* Refreeze HID data IN pipe */
250 /** Writes a report to the attached device.
252 * \param[in] ReportOUTData Buffer containing the report to send to the device
253 * \param[in] ReportLength Length of the report to send
255 void WriteNextReport(uint8_t* ReportOUTData
, uint16_t ReportLength
)
257 /* Select and unfreeze HID data OUT pipe */
258 Pipe_SelectPipe(HID_DATA_OUT_PIPE
);
260 /* Not all HID devices have an OUT endpoint (some require OUT reports to be sent over the
261 * control endpoint instead) - check to see if the OUT endpoint has been initialized */
262 if (Pipe_IsConfigured())
266 /* Ensure pipe is ready to be written to before continuing */
267 if (!(Pipe_IsOUTReady()))
269 /* Refreeze the data OUT pipe */
275 /* Write out HID report data */
276 Pipe_Write_Stream_LE(ReportOUTData
, ReportLength
);
278 /* Clear the OUT endpoint, send last data packet */
281 /* Refreeze the data OUT pipe */
286 /* Class specific request to send a HID report to the device */
287 USB_ControlRequest
= (USB_Request_Header_t
)
289 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
293 .wLength
= ReportLength
,
296 /* Select the control pipe for the request transfer */
297 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
299 /* Send the request to the device */
300 USB_Host_SendControlRequest(ReportOUTData
);
304 /** Task to set the configuration of the attached device after it has been enumerated, and to read and process
305 * HID reports from the device and to send reports if desired.
307 void HID_Host_Task(void)
311 /* Switch to determine what user-application handled host state the host state machine is in */
312 switch (USB_HostState
)
314 case HOST_STATE_Addressed
:
315 /* Get and process the configuration descriptor data */
316 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
318 /* Indicate error status */
319 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
321 /* Wait until USB device disconnected */
322 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
326 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
327 if ((ErrorCode
= USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful
)
329 /* Indicate error status */
330 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
332 /* Wait until USB device disconnected */
333 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
337 USB_HostState
= HOST_STATE_Configured
;
339 case HOST_STATE_Configured
: