3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 USB Missle Launcher Demo
11 Copyright (C) Dave Fletcher, 2009.
12 fletch at fletchtronics dot net
16 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
17 Copyright 2009 Dave Fletcher (fletch [at] fletchtronics [dot] net)
19 Permission to use, copy, modify, and distribute this software
20 and its documentation for any purpose and without fee is hereby
21 granted, provided that the above copyright notice appear in all
22 copies and that both that the copyright notice and this
23 permission notice and warranty disclaimer appear in supporting
24 documentation, and that the name of the author not be used in
25 advertising or publicity pertaining to distribution of the
26 software without specific, written prior permission.
28 The author disclaim all warranties with regard to this
29 software, including all implied warranties of merchantability
30 and fitness. In no event shall the author be liable for any
31 special, indirect or consequential damages or any damages
32 whatsoever resulting from loss of use, data or profits, whether
33 in an action of contract, negligence or other tortious action,
34 arising out of or in connection with the use or performance of
39 * Missle Launcher host. This is a host driver for the popular USB-controller table top toy missle launchers,
40 * which can typically aim and fire small foam "missles" from a spring-loaded turret. This project controls the
41 * launcher via a joystick and button to aim and fire missles at targets without a PC.
46 * Main source file for the MissileLauncher application. This file contains the main tasks of
47 * the application and is responsible for the initial application hardware configuration as well
48 * as the sending of commands to the attached launcher toy.
51 #include "MissileLauncher.h"
53 /** Command constants */
54 uint8_t CMD_INITA
[8] = { 85, 83, 66, 67, 0, 0, 4, 0 };
55 uint8_t CMD_INITB
[8] = { 85, 83, 66, 67, 0, 64, 2, 0 };
56 uint8_t CMD_STOP
[8] = { 0, 0, 0, 0, 0, 0, 8, 8 };
57 uint8_t CMD_LEFT
[8] = { 0, 1, 0, 0, 0, 0, 8, 8 };
58 uint8_t CMD_RIGHT
[8] = { 0, 0, 1, 0, 0, 0, 8, 8 };
59 uint8_t CMD_UP
[8] = { 0, 0, 0, 1, 0, 0, 8, 8 };
60 uint8_t CMD_DOWN
[8] = { 0, 0, 0, 0, 1, 0, 8, 8 };
61 uint8_t CMD_LEFTUP
[8] = { 0, 1, 0, 1, 0, 0, 8, 8 };
62 uint8_t CMD_RIGHTUP
[8] = { 0, 0, 1, 1, 0, 0, 8, 8 };
63 uint8_t CMD_LEFTDOWN
[8] = { 0, 1, 0, 0, 1, 0, 8, 8 };
64 uint8_t CMD_RIGHTDOWN
[8] = { 0, 0, 1, 0, 1, 0, 8, 8 };
65 uint8_t CMD_FIRE
[8] = { 0, 0, 0, 0, 0, 1, 8, 8 };
68 uint8_t CmdBuffer
[LAUNCHER_CMD_BUFFER_SIZE
];
70 /** Main program entry point. This routine configures the hardware required by the application, then
71 * starts the scheduler to run the application tasks.
77 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
83 Read_Joystick_Status();
90 /** Configures the board hardware and chip peripherals for the demo's functionality. */
91 void SetupHardware(void)
93 /* Disable watchdog if enabled by bootloader/fuses */
94 MCUSR
&= ~(1 << WDRF
);
97 /* Disable clock division */
98 clock_prescale_set(clock_div_1
);
100 /* Hardware Initialization */
107 /** Reads the joystick and button status, sending commands to the launcher as needed. */
108 void Read_Joystick_Status(void)
110 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
112 if (BUTTONS_BUTTON1
&& Buttons_GetStatus())
113 Send_Command(CMD_FIRE
);
114 else if (JoyStatus_LCL
& JOY_UP
)
115 Send_Command(CMD_UP
);
116 else if (JoyStatus_LCL
& JOY_DOWN
)
117 Send_Command(CMD_DOWN
);
118 else if (JoyStatus_LCL
& JOY_LEFT
)
119 Send_Command(CMD_LEFT
);
120 else if (JoyStatus_LCL
& JOY_RIGHT
)
121 Send_Command(CMD_RIGHT
);
122 else if (CmdState
!= CMD_STOP
)
123 Send_Command(CMD_STOP
);
126 /** Lower level send routine, copies report into a larger buffer and sends.
128 * \param Report Report data to send.
129 * \param ReportSize Report length in bytes.
131 void Send_Command_Report(uint8_t *Report
, uint16_t ReportSize
)
133 memcpy(CmdBuffer
, Report
, 8);
134 WriteNextReport(CmdBuffer
, ReportSize
);
137 /** Send one of the CMD_* command constants listed above.
139 * \param Command One of the command constants.
141 void Send_Command(uint8_t* Command
)
143 if ((CmdState
== CMD_STOP
&& Command
!= CMD_STOP
) ||
144 (CmdState
!= CMD_STOP
&& Command
== CMD_STOP
))
146 LEDs_ChangeLEDs(LEDS_LED4
, ~LEDs_GetLEDs() & LEDS_LED4
);
148 Send_Command_Report(CMD_INITA
, 8);
149 Send_Command_Report(CMD_INITB
, 8);
150 Send_Command_Report(Command
, LAUNCHER_CMD_BUFFER_SIZE
);
156 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
157 * starts the library USB task to begin the enumeration and USB management process.
159 void EVENT_USB_DeviceAttached(void)
161 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
164 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
165 * stops the library USB task management process.
167 void EVENT_USB_DeviceUnattached(void)
169 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
172 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
173 * enumerated by the host and is now ready to be used by the application.
175 void EVENT_USB_DeviceEnumerationComplete(void)
177 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
180 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
181 void EVENT_USB_HostError(const uint8_t ErrorCode
)
185 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
189 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
190 * enumerating an attached USB device.
192 void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode
, const uint8_t SubErrorCode
)
194 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
197 /** Reads in and discards the next report from the attached device. */
198 void DiscardNextReport(void)
200 /* Select and unfreeze HID data IN pipe */
201 Pipe_SelectPipe(HID_DATA_IN_PIPE
);
204 /* Check to see if a packet has been received */
205 if (!(Pipe_IsINReceived()))
207 /* Refreeze HID data IN pipe */
213 /* Clear the IN endpoint, ready for next data packet */
216 /* Refreeze HID data IN pipe */
220 /** Writes a report to the attached device.
222 * \param ReportOUTData Buffer containing the report to send to the device
223 * \param ReportLength Length of the report to send
225 void WriteNextReport(uint8_t* ReportOUTData
, uint16_t ReportLength
)
227 /* Select and unfreeze HID data OUT pipe */
228 Pipe_SelectPipe(HID_DATA_OUT_PIPE
);
230 /* Not all HID devices have an OUT endpoint (some require OUT reports to be sent over the
231 * control endpoint instead) - check to see if the OUT endpoint has been initialized */
232 if (Pipe_IsConfigured())
236 /* Ensure pipe is ready to be written to before continuing */
237 if (!(Pipe_IsOUTReady()))
239 /* Refreeze the data OUT pipe */
245 /* Write out HID report data */
246 Pipe_Write_Stream_LE(ReportOUTData
, ReportLength
);
248 /* Clear the OUT endpoint, send last data packet */
251 /* Refreeze the data OUT pipe */
256 /* Class specific request to send a HID report to the device */
257 USB_ControlRequest
= (USB_Request_Header_t
)
259 .bmRequestType
= 0x21,
263 .wLength
= ReportLength
,
266 /* Select the control pipe for the request transfer */
267 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
269 /* Send the request to the device */
270 USB_Host_SendControlRequest(ReportOUTData
);
274 /** Task to set the configuration of the attached device after it has been enumerated, and to read and process
275 * HID reports from the device and to send reports if desired.
277 void HID_Host_Task(void)
281 /* Switch to determine what user-application handled host state the host state machine is in */
282 switch (USB_HostState
)
284 case HOST_STATE_Addressed
:
285 /* Get and process the configuration descriptor data */
286 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
288 /* Indicate error status */
289 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
291 /* Wait until USB device disconnected */
292 while (USB_IsConnected
);
296 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
297 if ((ErrorCode
= USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful
)
299 /* Indicate error status */
300 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
302 /* Wait until USB device disconnected */
303 while (USB_IsConnected
);
307 USB_HostState
= HOST_STATE_Configured
;
309 case HOST_STATE_Configured
:
310 USB_HostState
= HOST_STATE_Ready
;
312 case HOST_STATE_Ready
: