Add uIP-split code to the Webserver project, so that each packet is split in half...
[pub/USBasp.git] / Projects / MissileLauncher / MissileLauncher.c
1 /*
2 USB Missile Launcher Demo
3 Copyright (C) Dave Fletcher, 2010.
4 fletch at fletchtronics dot net
5
6 Based on research by Scott Weston at
7 http://code.google.com/p/pymissile
8 */
9
10 /*
11 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Copyright 2010 Dave Fletcher (fletch [at] fletchtronics [dot] net)
13
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.
22
23 The author disclaim 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
30 this software.
31 */
32
33 /*
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.
37 */
38
39 /** \file
40 *
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.
44 */
45
46 #include "MissileLauncher.h"
47
48 /** Launcher first init command report data sequence */
49 uint8_t CMD_INITA[8] = { 85, 83, 66, 67, 0, 0, 4, 0 };
50
51 /** Launcher second init command report data sequence */
52 uint8_t CMD_INITB[8] = { 85, 83, 66, 67, 0, 64, 2, 0 };
53
54 /** Launcher command report data sequence to stop all movement */
55 uint8_t CMD_STOP[8] = { 0, 0, 0, 0, 0, 0, 8, 8 };
56
57 /** Launcher command report data sequence to move left */
58 uint8_t CMD_LEFT[8] = { 0, 1, 0, 0, 0, 0, 8, 8 };
59
60 /** Launcher command report data sequence to move right */
61 uint8_t CMD_RIGHT[8] = { 0, 0, 1, 0, 0, 0, 8, 8 };
62
63 /** Launcher command report data sequence to move up */
64 uint8_t CMD_UP[8] = { 0, 0, 0, 1, 0, 0, 8, 8 };
65
66 /** Launcher command report data sequence to move down */
67 uint8_t CMD_DOWN[8] = { 0, 0, 0, 0, 1, 0, 8, 8 };
68
69 /** Launcher command report data sequence to move left and up */
70 uint8_t CMD_LEFTUP[8] = { 0, 1, 0, 1, 0, 0, 8, 8 };
71
72 /** Launcher command report data sequence to move right and up */
73 uint8_t CMD_RIGHTUP[8] = { 0, 0, 1, 1, 0, 0, 8, 8 };
74
75 /** Launcher command report data sequence to move left and down */
76 uint8_t CMD_LEFTDOWN[8] = { 0, 1, 0, 0, 1, 0, 8, 8 };
77
78 /** Launcher command report data sequence to move right and down */
79 uint8_t CMD_RIGHTDOWN[8] = { 0, 0, 1, 0, 1, 0, 8, 8 };
80
81 /** Launcher command report data sequence to fire a missile */
82 uint8_t CMD_FIRE[8] = { 0, 0, 0, 0, 0, 1, 8, 8 };
83
84 /** Last command sent to the launcher, to determine what new command (if any) must be sent */
85 uint8_t* CmdState;
86
87 /** Buffer to hold a command to send to the launcher */
88 uint8_t CmdBuffer[LAUNCHER_CMD_BUFFER_SIZE];
89
90
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.
93 */
94 int main(void)
95 {
96 SetupHardware();
97
98 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
99
100 CmdState = CMD_STOP;
101
102 for (;;)
103 {
104 Read_Joystick_Status();
105
106 HID_Host_Task();
107 USB_USBTask();
108 }
109 }
110
111 /** Configures the board hardware and chip peripherals for the demo's functionality. */
112 void SetupHardware(void)
113 {
114 /* Disable watchdog if enabled by bootloader/fuses */
115 MCUSR &= ~(1 << WDRF);
116 wdt_disable();
117
118 /* Disable clock division */
119 clock_prescale_set(clock_div_1);
120
121 /* Hardware Initialization */
122 LEDs_Init();
123 USB_Init();
124 Joystick_Init();
125 Buttons_Init();
126 }
127
128 /** Reads the joystick and button status, sending commands to the launcher as needed. */
129 void Read_Joystick_Status(void)
130 {
131 uint8_t JoyStatus_LCL = Joystick_GetStatus();
132 uint8_t Buttons_LCL = Buttons_GetStatus();
133
134 if (Buttons_LCL & BUTTONS_BUTTON1)
135 Send_Command(CMD_FIRE);
136 else if (JoyStatus_LCL & JOY_UP)
137 Send_Command(CMD_UP);
138 else if (JoyStatus_LCL & JOY_DOWN)
139 Send_Command(CMD_DOWN);
140 else if (JoyStatus_LCL & JOY_LEFT)
141 Send_Command(CMD_LEFT);
142 else if (JoyStatus_LCL & JOY_RIGHT)
143 Send_Command(CMD_RIGHT);
144 else if (CmdState != CMD_STOP)
145 Send_Command(CMD_STOP);
146 }
147
148 /** Lower level send routine, copies report into a larger buffer and sends.
149 *
150 * \param[in] Report Report data to send.
151 * \param[in] ReportSize Report length in bytes.
152 */
153 void Send_Command_Report(uint8_t* const Report, const uint16_t ReportSize)
154 {
155 memcpy(CmdBuffer, Report, 8);
156 WriteNextReport(CmdBuffer, ReportSize);
157 }
158
159 /** Sends one of the CMD_* command constants to the attached device.
160 *
161 * \param[in] Command One of the command constants.
162 */
163 void Send_Command(uint8_t* const Command)
164 {
165 if ((CmdState == CMD_STOP && Command != CMD_STOP) ||
166 (CmdState != CMD_STOP && Command == CMD_STOP))
167 {
168 LEDs_ToggleLEDs(LEDS_LED4);
169
170 Send_Command_Report(CMD_INITA, 8);
171 Send_Command_Report(CMD_INITB, 8);
172 Send_Command_Report(Command, LAUNCHER_CMD_BUFFER_SIZE);
173 }
174
175 CmdState = Command;
176 }
177
178 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
179 * starts the library USB task to begin the enumeration and USB management process.
180 */
181 void EVENT_USB_Host_DeviceAttached(void)
182 {
183 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
184 }
185
186 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
187 * stops the library USB task management process.
188 */
189 void EVENT_USB_Host_DeviceUnattached(void)
190 {
191 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
192 }
193
194 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
195 * enumerated by the host and is now ready to be used by the application.
196 */
197 void EVENT_USB_Host_DeviceEnumerationComplete(void)
198 {
199 LEDs_SetAllLEDs(LEDMASK_USB_READY);
200 }
201
202 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
203 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
204 {
205 USB_ShutDown();
206
207 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
208 for(;;);
209 }
210
211 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
212 * enumerating an attached USB device.
213 */
214 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
215 {
216 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
217 }
218
219 /** Reads in and discards the next report from the attached device. */
220 void DiscardNextReport(void)
221 {
222 /* Select and unfreeze HID data IN pipe */
223 Pipe_SelectPipe(HID_DATA_IN_PIPE);
224 Pipe_Unfreeze();
225
226 /* Check to see if a packet has been received */
227 if (!(Pipe_IsINReceived()))
228 {
229 /* Refreeze HID data IN pipe */
230 Pipe_Freeze();
231
232 return;
233 }
234
235 /* Clear the IN endpoint, ready for next data packet */
236 Pipe_ClearIN();
237
238 /* Refreeze HID data IN pipe */
239 Pipe_Freeze();
240 }
241
242 /** Writes a report to the attached device.
243 *
244 * \param[in] ReportOUTData Buffer containing the report to send to the device
245 * \param[in] ReportLength Length of the report to send
246 */
247 void WriteNextReport(uint8_t* const ReportOUTData, const uint16_t ReportLength)
248 {
249 /* Select and unfreeze HID data OUT pipe */
250 Pipe_SelectPipe(HID_DATA_OUT_PIPE);
251
252 /* Not all HID devices have an OUT endpoint (some require OUT reports to be sent over the
253 * control endpoint instead) - check to see if the OUT endpoint has been initialized */
254 if (Pipe_IsConfigured())
255 {
256 Pipe_Unfreeze();
257
258 /* Ensure pipe is ready to be written to before continuing */
259 if (!(Pipe_IsOUTReady()))
260 {
261 /* Refreeze the data OUT pipe */
262 Pipe_Freeze();
263
264 return;
265 }
266
267 /* Write out HID report data */
268 Pipe_Write_Stream_LE(ReportOUTData, ReportLength);
269
270 /* Clear the OUT endpoint, send last data packet */
271 Pipe_ClearOUT();
272
273 /* Refreeze the data OUT pipe */
274 Pipe_Freeze();
275 }
276 else
277 {
278 /* Class specific request to send a HID report to the device */
279 USB_ControlRequest = (USB_Request_Header_t)
280 {
281 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
282 .bRequest = REQ_SetReport,
283 .wValue = 0x02,
284 .wIndex = 0x01,
285 .wLength = ReportLength,
286 };
287
288 /* Select the control pipe for the request transfer */
289 Pipe_SelectPipe(PIPE_CONTROLPIPE);
290
291 /* Send the request to the device */
292 USB_Host_SendControlRequest(ReportOUTData);
293 }
294 }
295
296 /** Task to set the configuration of the attached device after it has been enumerated, and to read and process
297 * HID reports from the device and to send reports if desired.
298 */
299 void HID_Host_Task(void)
300 {
301 uint8_t ErrorCode;
302
303 /* Switch to determine what user-application handled host state the host state machine is in */
304 switch (USB_HostState)
305 {
306 case HOST_STATE_Addressed:
307 /* Get and process the configuration descriptor data */
308 if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
309 {
310 /* Indicate error status */
311 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
312
313 /* Wait until USB device disconnected */
314 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
315 break;
316 }
317
318 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
319 if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
320 {
321 /* Indicate error status */
322 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
323
324 /* Wait until USB device disconnected */
325 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
326 break;
327 }
328
329 USB_HostState = HOST_STATE_Configured;
330 break;
331 case HOST_STATE_Configured:
332 DiscardNextReport();
333
334 break;
335 }
336 }