Fixed LowLevel Keyboard demo not saving the issued report only after it has been...
[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 * enters a loop to run the application tasks in sequence.
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 uint8_t Buttons_LCL = Buttons_GetStatus();
141
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);
154 }
155
156 /** Lower level send routine, copies report into a larger buffer and sends.
157 *
158 * \param[in] Report Report data to send.
159 * \param[in] ReportSize Report length in bytes.
160 */
161 void Send_Command_Report(uint8_t *Report, uint16_t ReportSize)
162 {
163 memcpy(CmdBuffer, Report, 8);
164 WriteNextReport(CmdBuffer, ReportSize);
165 }
166
167 /** Sends one of the CMD_* command constants to the attached device.
168 *
169 * \param[in] Command One of the command constants.
170 */
171 void Send_Command(uint8_t* Command)
172 {
173 if ((CmdState == CMD_STOP && Command != CMD_STOP) ||
174 (CmdState != CMD_STOP && Command == CMD_STOP))
175 {
176 LEDs_ToggleLEDs(LEDS_LED4);
177
178 Send_Command_Report(CMD_INITA, 8);
179 Send_Command_Report(CMD_INITB, 8);
180 Send_Command_Report(Command, LAUNCHER_CMD_BUFFER_SIZE);
181 }
182
183 CmdState = Command;
184 }
185
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.
188 */
189 void EVENT_USB_Host_DeviceAttached(void)
190 {
191 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
192 }
193
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.
196 */
197 void EVENT_USB_Host_DeviceUnattached(void)
198 {
199 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
200 }
201
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.
204 */
205 void EVENT_USB_Host_DeviceEnumerationComplete(void)
206 {
207 LEDs_SetAllLEDs(LEDMASK_USB_READY);
208 }
209
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)
212 {
213 USB_ShutDown();
214
215 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
216 for(;;);
217 }
218
219 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
220 * enumerating an attached USB device.
221 */
222 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
223 {
224 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
225 }
226
227 /** Reads in and discards the next report from the attached device. */
228 void DiscardNextReport(void)
229 {
230 /* Select and unfreeze HID data IN pipe */
231 Pipe_SelectPipe(HID_DATA_IN_PIPE);
232 Pipe_Unfreeze();
233
234 /* Check to see if a packet has been received */
235 if (!(Pipe_IsINReceived()))
236 {
237 /* Refreeze HID data IN pipe */
238 Pipe_Freeze();
239
240 return;
241 }
242
243 /* Clear the IN endpoint, ready for next data packet */
244 Pipe_ClearIN();
245
246 /* Refreeze HID data IN pipe */
247 Pipe_Freeze();
248 }
249
250 /** Writes a report to the attached device.
251 *
252 * \param[in] ReportOUTData Buffer containing the report to send to the device
253 * \param[in] ReportLength Length of the report to send
254 */
255 void WriteNextReport(uint8_t* ReportOUTData, uint16_t ReportLength)
256 {
257 /* Select and unfreeze HID data OUT pipe */
258 Pipe_SelectPipe(HID_DATA_OUT_PIPE);
259
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())
263 {
264 Pipe_Unfreeze();
265
266 /* Ensure pipe is ready to be written to before continuing */
267 if (!(Pipe_IsOUTReady()))
268 {
269 /* Refreeze the data OUT pipe */
270 Pipe_Freeze();
271
272 return;
273 }
274
275 /* Write out HID report data */
276 Pipe_Write_Stream_LE(ReportOUTData, ReportLength);
277
278 /* Clear the OUT endpoint, send last data packet */
279 Pipe_ClearOUT();
280
281 /* Refreeze the data OUT pipe */
282 Pipe_Freeze();
283 }
284 else
285 {
286 /* Class specific request to send a HID report to the device */
287 USB_ControlRequest = (USB_Request_Header_t)
288 {
289 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
290 .bRequest = 0x09,
291 .wValue = 0x02,
292 .wIndex = 0x01,
293 .wLength = ReportLength,
294 };
295
296 /* Select the control pipe for the request transfer */
297 Pipe_SelectPipe(PIPE_CONTROLPIPE);
298
299 /* Send the request to the device */
300 USB_Host_SendControlRequest(ReportOUTData);
301 }
302 }
303
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.
306 */
307 void HID_Host_Task(void)
308 {
309 uint8_t ErrorCode;
310
311 /* Switch to determine what user-application handled host state the host state machine is in */
312 switch (USB_HostState)
313 {
314 case HOST_STATE_Addressed:
315 /* Get and process the configuration descriptor data */
316 if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
317 {
318 /* Indicate error status */
319 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
320
321 /* Wait until USB device disconnected */
322 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
323 break;
324 }
325
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)
328 {
329 /* Indicate error status */
330 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
331
332 /* Wait until USB device disconnected */
333 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
334 break;
335 }
336
337 USB_HostState = HOST_STATE_Configured;
338 break;
339 case HOST_STATE_Configured:
340 DiscardNextReport();
341
342 break;
343 }
344 }