Extend USB_GetDeviceConfigDescriptor() routine to require the configuration number...
[pub/USBasp.git] / Projects / MissileLauncher / MissileLauncher.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 USB Missile Launcher Demo
11 Copyright (C) Dave Fletcher, 2009.
12 fletch at fletchtronics dot net
13
14 Based on research by Scott Weston at
15 http://code.google.com/p/pymissile
16 */
17
18 /*
19 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
20 Copyright 2009 Dave Fletcher (fletch [at] fletchtronics [dot] net)
21
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.
30
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
38 this software.
39 */
40
41 /*
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.
45 */
46
47 /** \file
48 *
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.
52 */
53
54 #include "MissileLauncher.h"
55
56 /** Launcher first init command report data sequence */
57 uint8_t CMD_INITA[8] = { 85, 83, 66, 67, 0, 0, 4, 0 };
58
59 /** Launcher second init command report data sequence */
60 uint8_t CMD_INITB[8] = { 85, 83, 66, 67, 0, 64, 2, 0 };
61
62 /** Launcher command report data sequence to stop all movement */
63 uint8_t CMD_STOP[8] = { 0, 0, 0, 0, 0, 0, 8, 8 };
64
65 /** Launcher command report data sequence to move left */
66 uint8_t CMD_LEFT[8] = { 0, 1, 0, 0, 0, 0, 8, 8 };
67
68 /** Launcher command report data sequence to move right */
69 uint8_t CMD_RIGHT[8] = { 0, 0, 1, 0, 0, 0, 8, 8 };
70
71 /** Launcher command report data sequence to move up */
72 uint8_t CMD_UP[8] = { 0, 0, 0, 1, 0, 0, 8, 8 };
73
74 /** Launcher command report data sequence to move down */
75 uint8_t CMD_DOWN[8] = { 0, 0, 0, 0, 1, 0, 8, 8 };
76
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 };
79
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 };
82
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 };
85
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 };
88
89 /** Launcher command report data sequence to fire a missile */
90 uint8_t CMD_FIRE[8] = { 0, 0, 0, 0, 0, 1, 8, 8 };
91
92 /** Last command sent to the launcher, to determine what new command (if any) must be sent */
93 uint8_t* CmdState;
94
95 /** Buffer to hold a command to send to the launcher */
96 uint8_t CmdBuffer[LAUNCHER_CMD_BUFFER_SIZE];
97
98
99 /** Main program entry point. This routine configures the hardware required by the application, then
100 * starts the scheduler to run the application tasks.
101 */
102 int main(void)
103 {
104 SetupHardware();
105
106 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
107
108 CmdState = CMD_STOP;
109
110 for (;;)
111 {
112 Read_Joystick_Status();
113
114 HID_Host_Task();
115 USB_USBTask();
116 }
117 }
118
119 /** Configures the board hardware and chip peripherals for the demo's functionality. */
120 void SetupHardware(void)
121 {
122 /* Disable watchdog if enabled by bootloader/fuses */
123 MCUSR &= ~(1 << WDRF);
124 wdt_disable();
125
126 /* Disable clock division */
127 clock_prescale_set(clock_div_1);
128
129 /* Hardware Initialization */
130 LEDs_Init();
131 USB_Init();
132 Joystick_Init();
133 Buttons_Init();
134 }
135
136 /** Reads the joystick and button status, sending commands to the launcher as needed. */
137 void Read_Joystick_Status(void)
138 {
139 uint8_t JoyStatus_LCL = Joystick_GetStatus();
140
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);
153 }
154
155 /** Lower level send routine, copies report into a larger buffer and sends.
156 *
157 * \param Report Report data to send.
158 * \param ReportSize Report length in bytes.
159 */
160 void Send_Command_Report(uint8_t *Report, uint16_t ReportSize)
161 {
162 memcpy(CmdBuffer, Report, 8);
163 WriteNextReport(CmdBuffer, ReportSize);
164 }
165
166 /** Send one of the CMD_* command constants listed above.
167 *
168 * \param Command One of the command constants.
169 */
170 void Send_Command(uint8_t* Command)
171 {
172 if ((CmdState == CMD_STOP && Command != CMD_STOP) ||
173 (CmdState != CMD_STOP && Command == CMD_STOP))
174 {
175 LEDs_ChangeLEDs(LEDS_LED4, ~LEDs_GetLEDs() & LEDS_LED4);
176
177 Send_Command_Report(CMD_INITA, 8);
178 Send_Command_Report(CMD_INITB, 8);
179 Send_Command_Report(Command, LAUNCHER_CMD_BUFFER_SIZE);
180 }
181
182 CmdState = Command;
183 }
184
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.
187 */
188 void EVENT_USB_DeviceAttached(void)
189 {
190 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
191 }
192
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.
195 */
196 void EVENT_USB_DeviceUnattached(void)
197 {
198 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
199 }
200
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.
203 */
204 void EVENT_USB_DeviceEnumerationComplete(void)
205 {
206 LEDs_SetAllLEDs(LEDMASK_USB_READY);
207 }
208
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)
211 {
212 USB_ShutDown();
213
214 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
215 for(;;);
216 }
217
218 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
219 * enumerating an attached USB device.
220 */
221 void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
222 {
223 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
224 }
225
226 /** Reads in and discards the next report from the attached device. */
227 void DiscardNextReport(void)
228 {
229 /* Select and unfreeze HID data IN pipe */
230 Pipe_SelectPipe(HID_DATA_IN_PIPE);
231 Pipe_Unfreeze();
232
233 /* Check to see if a packet has been received */
234 if (!(Pipe_IsINReceived()))
235 {
236 /* Refreeze HID data IN pipe */
237 Pipe_Freeze();
238
239 return;
240 }
241
242 /* Clear the IN endpoint, ready for next data packet */
243 Pipe_ClearIN();
244
245 /* Refreeze HID data IN pipe */
246 Pipe_Freeze();
247 }
248
249 /** Writes a report to the attached device.
250 *
251 * \param ReportOUTData Buffer containing the report to send to the device
252 * \param ReportLength Length of the report to send
253 */
254 void WriteNextReport(uint8_t* ReportOUTData, uint16_t ReportLength)
255 {
256 /* Select and unfreeze HID data OUT pipe */
257 Pipe_SelectPipe(HID_DATA_OUT_PIPE);
258
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())
262 {
263 Pipe_Unfreeze();
264
265 /* Ensure pipe is ready to be written to before continuing */
266 if (!(Pipe_IsOUTReady()))
267 {
268 /* Refreeze the data OUT pipe */
269 Pipe_Freeze();
270
271 return;
272 }
273
274 /* Write out HID report data */
275 Pipe_Write_Stream_LE(ReportOUTData, ReportLength);
276
277 /* Clear the OUT endpoint, send last data packet */
278 Pipe_ClearOUT();
279
280 /* Refreeze the data OUT pipe */
281 Pipe_Freeze();
282 }
283 else
284 {
285 /* Class specific request to send a HID report to the device */
286 USB_ControlRequest = (USB_Request_Header_t)
287 {
288 .bmRequestType = 0x21,
289 .bRequest = 0x09,
290 .wValue = 0x02,
291 .wIndex = 0x01,
292 .wLength = ReportLength,
293 };
294
295 /* Select the control pipe for the request transfer */
296 Pipe_SelectPipe(PIPE_CONTROLPIPE);
297
298 /* Send the request to the device */
299 USB_Host_SendControlRequest(ReportOUTData);
300 }
301 }
302
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.
305 */
306 void HID_Host_Task(void)
307 {
308 uint8_t ErrorCode;
309
310 /* Switch to determine what user-application handled host state the host state machine is in */
311 switch (USB_HostState)
312 {
313 case HOST_STATE_Addressed:
314 /* Get and process the configuration descriptor data */
315 if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
316 {
317 /* Indicate error status */
318 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
319
320 /* Wait until USB device disconnected */
321 while (USB_IsConnected);
322 break;
323 }
324
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)
327 {
328 /* Indicate error status */
329 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
330
331 /* Wait until USB device disconnected */
332 while (USB_IsConnected);
333 break;
334 }
335
336 USB_HostState = HOST_STATE_Configured;
337 break;
338 case HOST_STATE_Configured:
339 USB_HostState = HOST_STATE_Ready;
340 break;
341 case HOST_STATE_Ready:
342 DiscardNextReport();
343
344 break;
345 }
346 }