MissileLauncher.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. USB Missile Launcher Demo
  3. Copyright (C) Dave Fletcher, 2010.
  4. fletch at fletchtronics dot net
  5. Based on research by Scott Weston at
  6. http://code.google.com/p/pymissile
  7. */
  8. /*
  9. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  10. Copyright 2010 Dave Fletcher (fletch [at] fletchtronics [dot] net)
  11. Permission to use, copy, modify, distribute, and sell this
  12. software and its documentation for any purpose is hereby granted
  13. without fee, provided that the above copyright notice appear in
  14. all copies and that both that the copyright notice and this
  15. permission notice and warranty disclaimer appear in supporting
  16. documentation, and that the name of the author not be used in
  17. advertising or publicity pertaining to distribution of the
  18. software without specific, written prior permission.
  19. The author disclaims all warranties with regard to this
  20. software, including all implied warranties of merchantability
  21. and fitness. In no event shall the author be liable for any
  22. special, indirect or consequential damages or any damages
  23. whatsoever resulting from loss of use, data or profits, whether
  24. in an action of contract, negligence or other tortious action,
  25. arising out of or in connection with the use or performance of
  26. this software.
  27. */
  28. /*
  29. * Missile Launcher host. This is a host driver for the popular USB-controller table top toy missile launchers,
  30. * which can typically aim and fire small foam "missiles" from a spring-loaded turret. This project controls the
  31. * launcher via a joystick and button to aim and fire missiles at targets without a PC.
  32. */
  33. /** \file
  34. *
  35. * Main source file for the MissileLauncher application. This file contains the main tasks of
  36. * the application and is responsible for the initial application hardware configuration as well
  37. * as the sending of commands to the attached launcher toy.
  38. */
  39. #include "MissileLauncher.h"
  40. /** Launcher first init command report data sequence */
  41. static const uint8_t CMD_INITA[8] = { 85, 83, 66, 67, 0, 0, 4, 0 };
  42. /** Launcher second init command report data sequence */
  43. static const uint8_t CMD_INITB[8] = { 85, 83, 66, 67, 0, 64, 2, 0 };
  44. /** Launcher command report data sequence to stop all movement */
  45. static const uint8_t CMD_STOP[8] = { 0, 0, 0, 0, 0, 0, 8, 8 };
  46. /** Launcher command report data sequence to move left */
  47. static const uint8_t CMD_LEFT[8] = { 0, 1, 0, 0, 0, 0, 8, 8 };
  48. /** Launcher command report data sequence to move right */
  49. static const uint8_t CMD_RIGHT[8] = { 0, 0, 1, 0, 0, 0, 8, 8 };
  50. /** Launcher command report data sequence to move up */
  51. static const uint8_t CMD_UP[8] = { 0, 0, 0, 1, 0, 0, 8, 8 };
  52. /** Launcher command report data sequence to move down */
  53. static const uint8_t CMD_DOWN[8] = { 0, 0, 0, 0, 1, 0, 8, 8 };
  54. /** Launcher command report data sequence to move left and up */
  55. static const uint8_t CMD_LEFTUP[8] = { 0, 1, 0, 1, 0, 0, 8, 8 };
  56. /** Launcher command report data sequence to move right and up */
  57. static const uint8_t CMD_RIGHTUP[8] = { 0, 0, 1, 1, 0, 0, 8, 8 };
  58. /** Launcher command report data sequence to move left and down */
  59. static const uint8_t CMD_LEFTDOWN[8] = { 0, 1, 0, 0, 1, 0, 8, 8 };
  60. /** Launcher command report data sequence to move right and down */
  61. static const uint8_t CMD_RIGHTDOWN[8] = { 0, 0, 1, 0, 1, 0, 8, 8 };
  62. /** Launcher command report data sequence to fire a missile */
  63. static const uint8_t CMD_FIRE[8] = { 0, 0, 0, 0, 0, 1, 8, 8 };
  64. /** Last command sent to the launcher, to determine what new command (if any) must be sent */
  65. static const uint8_t* CmdState;
  66. /** Buffer to hold a command to send to the launcher */
  67. static uint8_t CmdBuffer[LAUNCHER_CMD_BUFFER_SIZE];
  68. /** Main program entry point. This routine configures the hardware required by the application, then
  69. * enters a loop to run the application tasks in sequence.
  70. */
  71. int main(void)
  72. {
  73. SetupHardware();
  74. CmdState = CMD_STOP;
  75. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  76. GlobalInterruptEnable();
  77. for (;;)
  78. {
  79. Read_Joystick_Status();
  80. DiscardNextReport();
  81. USB_USBTask();
  82. }
  83. }
  84. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  85. void SetupHardware(void)
  86. {
  87. #if (ARCH == ARCH_AVR8)
  88. /* Disable watchdog if enabled by bootloader/fuses */
  89. MCUSR &= ~(1 << WDRF);
  90. wdt_disable();
  91. /* Disable clock division */
  92. clock_prescale_set(clock_div_1);
  93. #endif
  94. /* Hardware Initialization */
  95. LEDs_Init();
  96. USB_Init();
  97. Joystick_Init();
  98. Buttons_Init();
  99. }
  100. /** Reads the joystick and button status, sending commands to the launcher as needed. */
  101. void Read_Joystick_Status(void)
  102. {
  103. uint8_t JoyStatus_LCL = Joystick_GetStatus();
  104. uint8_t Buttons_LCL = Buttons_GetStatus();
  105. if (Buttons_LCL & BUTTONS_BUTTON1)
  106. Send_Command(CMD_FIRE);
  107. else if (JoyStatus_LCL & JOY_UP)
  108. Send_Command(CMD_UP);
  109. else if (JoyStatus_LCL & JOY_DOWN)
  110. Send_Command(CMD_DOWN);
  111. else if (JoyStatus_LCL & JOY_LEFT)
  112. Send_Command(CMD_LEFT);
  113. else if (JoyStatus_LCL & JOY_RIGHT)
  114. Send_Command(CMD_RIGHT);
  115. else if (CmdState != CMD_STOP)
  116. Send_Command(CMD_STOP);
  117. }
  118. /** Lower level send routine, copies report into a larger buffer and sends.
  119. *
  120. * \param[in] Report Report data to send.
  121. * \param[in] ReportSize Report length in bytes.
  122. */
  123. void Send_Command_Report(const uint8_t* const Report,
  124. const uint16_t ReportSize)
  125. {
  126. memcpy(CmdBuffer, Report, 8);
  127. WriteNextReport(CmdBuffer, ReportSize);
  128. }
  129. /** Sends one of the \c CMD_* command constants to the attached device.
  130. *
  131. * \param[in] Command One of the command constants.
  132. */
  133. void Send_Command(const uint8_t* const Command)
  134. {
  135. if ((CmdState == CMD_STOP && Command != CMD_STOP) ||
  136. (CmdState != CMD_STOP && Command == CMD_STOP))
  137. {
  138. LEDs_ToggleLEDs(LEDS_LED4);
  139. Send_Command_Report(CMD_INITA, 8);
  140. Send_Command_Report(CMD_INITB, 8);
  141. Send_Command_Report(Command, LAUNCHER_CMD_BUFFER_SIZE);
  142. }
  143. CmdState = Command;
  144. }
  145. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  146. * starts the library USB task to begin the enumeration and USB management process.
  147. */
  148. void EVENT_USB_Host_DeviceAttached(void)
  149. {
  150. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  151. }
  152. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  153. * stops the library USB task management process.
  154. */
  155. void EVENT_USB_Host_DeviceUnattached(void)
  156. {
  157. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  158. }
  159. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  160. * enumerated by the host and is now ready to be used by the application.
  161. */
  162. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  163. {
  164. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  165. /* Get and process the configuration descriptor data */
  166. if (ProcessConfigurationDescriptor() != SuccessfulConfigRead)
  167. {
  168. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  169. return;
  170. }
  171. /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
  172. if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
  173. {
  174. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  175. return;
  176. }
  177. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  178. }
  179. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  180. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  181. {
  182. USB_Disable();
  183. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  184. for(;;);
  185. }
  186. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  187. * enumerating an attached USB device.
  188. */
  189. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  190. const uint8_t SubErrorCode)
  191. {
  192. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  193. }
  194. /** Reads in and discards the next report from the attached device. */
  195. void DiscardNextReport(void)
  196. {
  197. if (USB_HostState != HOST_STATE_Configured)
  198. return;
  199. /* Select and unfreeze HID data IN pipe */
  200. Pipe_SelectPipe(HID_DATA_IN_PIPE);
  201. Pipe_Unfreeze();
  202. /* Check to see if a packet has been received */
  203. if (!(Pipe_IsINReceived()))
  204. {
  205. /* Refreeze HID data IN pipe */
  206. Pipe_Freeze();
  207. return;
  208. }
  209. /* Clear the IN endpoint, ready for next data packet */
  210. Pipe_ClearIN();
  211. /* Refreeze HID data IN pipe */
  212. Pipe_Freeze();
  213. }
  214. /** Writes a report to the attached device.
  215. *
  216. * \param[in] ReportOUTData Buffer containing the report to send to the device
  217. * \param[in] ReportLength Length of the report to send
  218. */
  219. void WriteNextReport(uint8_t* const ReportOUTData,
  220. const uint16_t ReportLength)
  221. {
  222. if (USB_HostState != HOST_STATE_Configured)
  223. return;
  224. /* Select and unfreeze HID data OUT pipe */
  225. Pipe_SelectPipe(HID_DATA_OUT_PIPE);
  226. /* Not all HID devices have an OUT endpoint (some require OUT reports to be sent over the
  227. * control endpoint instead) - check to see if the OUT endpoint has been initialized */
  228. if (Pipe_IsConfigured())
  229. {
  230. Pipe_Unfreeze();
  231. /* Ensure pipe is ready to be written to before continuing */
  232. if (!(Pipe_IsOUTReady()))
  233. {
  234. /* Refreeze the data OUT pipe */
  235. Pipe_Freeze();
  236. return;
  237. }
  238. /* Write out HID report data */
  239. Pipe_Write_Stream_LE(ReportOUTData, ReportLength, NULL);
  240. /* Clear the OUT endpoint, send last data packet */
  241. Pipe_ClearOUT();
  242. /* Refreeze the data OUT pipe */
  243. Pipe_Freeze();
  244. }
  245. else
  246. {
  247. /* Class specific request to send a HID report to the device */
  248. USB_ControlRequest = (USB_Request_Header_t)
  249. {
  250. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  251. .bRequest = HID_REQ_SetReport,
  252. .wValue = 0x02,
  253. .wIndex = 0x01,
  254. .wLength = ReportLength,
  255. };
  256. /* Select the control pipe for the request transfer */
  257. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  258. /* Send the request to the device */
  259. USB_Host_SendControlRequest(ReportOUTData);
  260. }
  261. }