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 * starts the scheduler to run the application tasks.
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();
141 if (BUTTONS_BUTTON1
&& Buttons_GetStatus())
142 Send_Command(CMD_FIRE
);
143 else if (JoyStatus_LCL
& JOY_UP
)
144 Send_Command(CMD_UP
);
145 else if (JoyStatus_LCL
& JOY_DOWN
)
146 Send_Command(CMD_DOWN
);
147 else if (JoyStatus_LCL
& JOY_LEFT
)
148 Send_Command(CMD_LEFT
);
149 else if (JoyStatus_LCL
& JOY_RIGHT
)
150 Send_Command(CMD_RIGHT
);
151 else if (CmdState
!= CMD_STOP
)
152 Send_Command(CMD_STOP
);
155 /** Lower level send routine, copies report into a larger buffer and sends.
157 * \param Report Report data to send.
158 * \param ReportSize Report length in bytes.
160 void Send_Command_Report(uint8_t *Report
, uint16_t ReportSize
)
162 memcpy(CmdBuffer
, Report
, 8);
163 WriteNextReport(CmdBuffer
, ReportSize
);
166 /** Send one of the CMD_* command constants listed above.
168 * \param Command One of the command constants.
170 void Send_Command(uint8_t* Command
)
172 if ((CmdState
== CMD_STOP
&& Command
!= CMD_STOP
) ||
173 (CmdState
!= CMD_STOP
&& Command
== CMD_STOP
))
175 LEDs_ChangeLEDs(LEDS_LED4
, ~LEDs_GetLEDs() & LEDS_LED4
);
177 Send_Command_Report(CMD_INITA
, 8);
178 Send_Command_Report(CMD_INITB
, 8);
179 Send_Command_Report(Command
, LAUNCHER_CMD_BUFFER_SIZE
);
185 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
186 * starts the library USB task to begin the enumeration and USB management process.
188 void EVENT_USB_DeviceAttached(void)
190 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
193 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
194 * stops the library USB task management process.
196 void EVENT_USB_DeviceUnattached(void)
198 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
201 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
202 * enumerated by the host and is now ready to be used by the application.
204 void EVENT_USB_DeviceEnumerationComplete(void)
206 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
209 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
210 void EVENT_USB_HostError(const uint8_t ErrorCode
)
214 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
218 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
219 * enumerating an attached USB device.
221 void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode
, const uint8_t SubErrorCode
)
223 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
226 /** Reads in and discards the next report from the attached device. */
227 void DiscardNextReport(void)
229 /* Select and unfreeze HID data IN pipe */
230 Pipe_SelectPipe(HID_DATA_IN_PIPE
);
233 /* Check to see if a packet has been received */
234 if (!(Pipe_IsINReceived()))
236 /* Refreeze HID data IN pipe */
242 /* Clear the IN endpoint, ready for next data packet */
245 /* Refreeze HID data IN pipe */
249 /** Writes a report to the attached device.
251 * \param ReportOUTData Buffer containing the report to send to the device
252 * \param ReportLength Length of the report to send
254 void WriteNextReport(uint8_t* ReportOUTData
, uint16_t ReportLength
)
256 /* Select and unfreeze HID data OUT pipe */
257 Pipe_SelectPipe(HID_DATA_OUT_PIPE
);
259 /* Not all HID devices have an OUT endpoint (some require OUT reports to be sent over the
260 * control endpoint instead) - check to see if the OUT endpoint has been initialized */
261 if (Pipe_IsConfigured())
265 /* Ensure pipe is ready to be written to before continuing */
266 if (!(Pipe_IsOUTReady()))
268 /* Refreeze the data OUT pipe */
274 /* Write out HID report data */
275 Pipe_Write_Stream_LE(ReportOUTData
, ReportLength
);
277 /* Clear the OUT endpoint, send last data packet */
280 /* Refreeze the data OUT pipe */
285 /* Class specific request to send a HID report to the device */
286 USB_ControlRequest
= (USB_Request_Header_t
)
288 .bmRequestType
= 0x21,
292 .wLength
= ReportLength
,
295 /* Select the control pipe for the request transfer */
296 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
298 /* Send the request to the device */
299 USB_Host_SendControlRequest(ReportOUTData
);
303 /** Task to set the configuration of the attached device after it has been enumerated, and to read and process
304 * HID reports from the device and to send reports if desired.
306 void HID_Host_Task(void)
310 /* Switch to determine what user-application handled host state the host state machine is in */
311 switch (USB_HostState
)
313 case HOST_STATE_Addressed
:
314 /* Get and process the configuration descriptor data */
315 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
317 /* Indicate error status */
318 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
320 /* Wait until USB device disconnected */
321 while (USB_IsConnected
);
325 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
326 if ((ErrorCode
= USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful
)
328 /* Indicate error status */
329 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
331 /* Wait until USB device disconnected */
332 while (USB_IsConnected
);
336 USB_HostState
= HOST_STATE_Configured
;
338 case HOST_STATE_Configured
:
339 USB_HostState
= HOST_STATE_Ready
;
341 case HOST_STATE_Ready
: