Add MissleLauncher project to the /Projects/ makefile. Add missing MissleLauncher...
[pub/USBasp.git] / Projects / MissleLauncher / 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 Missle Launcher Demo
11 Copyright (C) Dave Fletcher, 2009.
12 fletch at fletchtronics dot net
13 */
14
15 /*
16 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
17 Copyright 2009 Dave Fletcher (fletch [at] fletchtronics [dot] net)
18
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.
27
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
35 this software.
36 */
37
38 /*
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.
42 */
43
44 /** \file
45 *
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.
49 */
50
51 #include "MissileLauncher.h"
52
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 };
66
67 uint8_t* CmdState;
68 uint8_t CmdBuffer[LAUNCHER_CMD_BUFFER_SIZE];
69
70 /** Main program entry point. This routine configures the hardware required by the application, then
71 * starts the scheduler to run the application tasks.
72 */
73 int main(void)
74 {
75 SetupHardware();
76
77 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
78
79 CmdState = CMD_STOP;
80
81 for (;;)
82 {
83 Read_Joystick_Status();
84
85 HID_Host_Task();
86 USB_USBTask();
87 }
88 }
89
90 /** Configures the board hardware and chip peripherals for the demo's functionality. */
91 void SetupHardware(void)
92 {
93 /* Disable watchdog if enabled by bootloader/fuses */
94 MCUSR &= ~(1 << WDRF);
95 wdt_disable();
96
97 /* Disable clock division */
98 clock_prescale_set(clock_div_1);
99
100 /* Hardware Initialization */
101 LEDs_Init();
102 USB_Init();
103 Joystick_Init();
104 Buttons_Init();
105 }
106
107 /** Reads the joystick and button status, sending commands to the launcher as needed. */
108 void Read_Joystick_Status(void)
109 {
110 uint8_t JoyStatus_LCL = Joystick_GetStatus();
111
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);
124 }
125
126 /** Lower level send routine, copies report into a larger buffer and sends.
127 *
128 * \param Report Report data to send.
129 * \param ReportSize Report length in bytes.
130 */
131 void Send_Command_Report(uint8_t *Report, uint16_t ReportSize)
132 {
133 memcpy(CmdBuffer, Report, 8);
134 WriteNextReport(CmdBuffer, ReportSize);
135 }
136
137 /** Send one of the CMD_* command constants listed above.
138 *
139 * \param Command One of the command constants.
140 */
141 void Send_Command(uint8_t* Command)
142 {
143 if ((CmdState == CMD_STOP && Command != CMD_STOP) ||
144 (CmdState != CMD_STOP && Command == CMD_STOP))
145 {
146 LEDs_ChangeLEDs(LEDS_LED4, ~LEDs_GetLEDs() & LEDS_LED4);
147
148 Send_Command_Report(CMD_INITA, 8);
149 Send_Command_Report(CMD_INITB, 8);
150 Send_Command_Report(Command, LAUNCHER_CMD_BUFFER_SIZE);
151 }
152
153 CmdState = Command;
154 }
155
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.
158 */
159 void EVENT_USB_DeviceAttached(void)
160 {
161 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
162 }
163
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.
166 */
167 void EVENT_USB_DeviceUnattached(void)
168 {
169 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
170 }
171
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.
174 */
175 void EVENT_USB_DeviceEnumerationComplete(void)
176 {
177 LEDs_SetAllLEDs(LEDMASK_USB_READY);
178 }
179
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)
182 {
183 USB_ShutDown();
184
185 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
186 for(;;);
187 }
188
189 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
190 * enumerating an attached USB device.
191 */
192 void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
193 {
194 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
195 }
196
197 /** Reads in and discards the next report from the attached device. */
198 void DiscardNextReport(void)
199 {
200 /* Select and unfreeze HID data IN pipe */
201 Pipe_SelectPipe(HID_DATA_IN_PIPE);
202 Pipe_Unfreeze();
203
204 /* Check to see if a packet has been received */
205 if (!(Pipe_IsINReceived()))
206 {
207 /* Refreeze HID data IN pipe */
208 Pipe_Freeze();
209
210 return;
211 }
212
213 /* Clear the IN endpoint, ready for next data packet */
214 Pipe_ClearIN();
215
216 /* Refreeze HID data IN pipe */
217 Pipe_Freeze();
218 }
219
220 /** Writes a report to the attached device.
221 *
222 * \param ReportOUTData Buffer containing the report to send to the device
223 * \param ReportLength Length of the report to send
224 */
225 void WriteNextReport(uint8_t* ReportOUTData, uint16_t ReportLength)
226 {
227 /* Select and unfreeze HID data OUT pipe */
228 Pipe_SelectPipe(HID_DATA_OUT_PIPE);
229
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())
233 {
234 Pipe_Unfreeze();
235
236 /* Ensure pipe is ready to be written to before continuing */
237 if (!(Pipe_IsOUTReady()))
238 {
239 /* Refreeze the data OUT pipe */
240 Pipe_Freeze();
241
242 return;
243 }
244
245 /* Write out HID report data */
246 Pipe_Write_Stream_LE(ReportOUTData, ReportLength);
247
248 /* Clear the OUT endpoint, send last data packet */
249 Pipe_ClearOUT();
250
251 /* Refreeze the data OUT pipe */
252 Pipe_Freeze();
253 }
254 else
255 {
256 /* Class specific request to send a HID report to the device */
257 USB_ControlRequest = (USB_Request_Header_t)
258 {
259 .bmRequestType = 0x21,
260 .bRequest = 0x09,
261 .wValue = 0x02,
262 .wIndex = 0x01,
263 .wLength = ReportLength,
264 };
265
266 /* Select the control pipe for the request transfer */
267 Pipe_SelectPipe(PIPE_CONTROLPIPE);
268
269 /* Send the request to the device */
270 USB_Host_SendControlRequest(ReportOUTData);
271 }
272 }
273
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.
276 */
277 void HID_Host_Task(void)
278 {
279 uint8_t ErrorCode;
280
281 /* Switch to determine what user-application handled host state the host state machine is in */
282 switch (USB_HostState)
283 {
284 case HOST_STATE_Addressed:
285 /* Get and process the configuration descriptor data */
286 if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
287 {
288 /* Indicate error status */
289 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
290
291 /* Wait until USB device disconnected */
292 while (USB_IsConnected);
293 break;
294 }
295
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)
298 {
299 /* Indicate error status */
300 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
301
302 /* Wait until USB device disconnected */
303 while (USB_IsConnected);
304 break;
305 }
306
307 USB_HostState = HOST_STATE_Configured;
308 break;
309 case HOST_STATE_Configured:
310 USB_HostState = HOST_STATE_Ready;
311 break;
312 case HOST_STATE_Ready:
313 DiscardNextReport();
314
315 break;
316 }
317 }