Advertisement
Guest User

autowat rank

a guest
Dec 10th, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.92 KB | None | 0 0
  1. /*
  2. Nintendo Switch Fightstick - Proof-of-Concept
  3.  
  4. Based on the LUFA library's Low-Level Joystick Demo
  5. (C) Dean Camera
  6. Based on the HORI's Pokken Tournament Pro Pad design
  7. (C) HORI
  8.  
  9. This project implements a modified version of HORI's Pokken Tournament Pro Pad
  10. USB descriptors to allow for the creation of custom controllers for the
  11. Nintendo Switch. This also works to a limited degree on the PS3.
  12.  
  13. Since System Update v3.0.0, the Nintendo Switch recognizes the Pokken
  14. Tournament Pro Pad as a Pro Controller. Physical design limitations prevent
  15. the Pokken Controller from functioning at the same level as the Pro
  16. Controller. However, by default most of the descriptors are there, with the
  17. exception of Home and Capture. Descriptor modification allows us to unlock
  18. these buttons for our use.
  19. */
  20.  
  21. #include "Joystick.h"
  22.  
  23. typedef enum {
  24. UP,
  25. DOWN,
  26. LEFT,
  27. RIGHT,
  28. X,
  29. Y,
  30. A,
  31. B,
  32. L,
  33. R,
  34. THROW,
  35. NOTHING,
  36. TRIGGERS,
  37. HOME,
  38. } Buttons_t;
  39.  
  40. typedef struct {
  41. Buttons_t button;
  42. uint16_t duration;
  43. } command;
  44.  
  45. static const command step[] = {
  46. // Setup controller
  47. /*
  48. { NOTHING, 250 },
  49. { TRIGGERS, 5 },
  50. { NOTHING, 200 },
  51. { TRIGGERS, 5 },
  52. { NOTHING, 200 },
  53. { A, 5 },
  54. { NOTHING, 250 },
  55. */
  56.  
  57. // Talk to Pondo
  58. { NOTHING, 100 },
  59. { A, 5 },
  60. { NOTHING, 30 },
  61. { B, 5 },
  62. { NOTHING, 30 },
  63. { B, 5 },
  64. { NOTHING, 30 },
  65. { B, 5 },
  66. { NOTHING, 150 }, // ワット回収
  67. { HOME, 5 },
  68. { NOTHING, 15 },
  69. { DOWN, 5 },
  70. { NOTHING, 15 },
  71. { RIGHT, 5 },
  72. { NOTHING, 15 },
  73. { RIGHT, 5 },
  74. { NOTHING, 15 },
  75. { RIGHT, 5 },
  76. { NOTHING, 15 },
  77. { RIGHT, 5 },
  78. { NOTHING, 15 },
  79. { A, 5 }, // 設定選択
  80. { NOTHING, 15 },
  81. { DOWN, 5 },
  82.  
  83. { DOWN, 5 },
  84.  
  85. { DOWN, 5 },
  86.  
  87. { DOWN, 5 },
  88.  
  89. { DOWN, 5 },
  90.  
  91. { DOWN, 5 },
  92.  
  93. { DOWN, 5 },
  94.  
  95. { DOWN, 5 },
  96.  
  97. { DOWN, 5 },
  98.  
  99. { DOWN, 5 },
  100.  
  101. { DOWN, 5 },
  102.  
  103. { DOWN, 5 },
  104.  
  105. { DOWN, 5 },
  106.  
  107. { DOWN, 5 },
  108. { NOTHING, 15 },
  109. { A, 5 }, // 設定>本体 選択
  110. { NOTHING, 15 },
  111. { DOWN, 5 },
  112. { NOTHING, 15 },
  113. { DOWN, 5 },
  114. { NOTHING, 15 },
  115. { DOWN, 5 },
  116. { NOTHING, 15 },
  117. { DOWN, 5 },
  118. { NOTHING, 15 },
  119. { A, 5 }, // 日付と時刻選択
  120. { NOTHING, 15 },
  121. { DOWN, 5 },
  122. { NOTHING, 15 },
  123. { DOWN, 5 },
  124. { NOTHING, 15 },
  125. { A, 5 },
  126. { NOTHING, 15 },
  127. { DOWN, 5 }, // ペナルティ回避
  128. { NOTHING, 15 },
  129. { RIGHT, 5 },
  130. { RIGHT, 5 },
  131. { RIGHT, 5 },
  132. { RIGHT, 5 },
  133. { RIGHT, 5 },
  134. { NOTHING, 15 },
  135. { A, 5 },
  136. { NOTHING, 15 },
  137. { A, 5 },
  138. { LEFT, 5 },
  139. { LEFT, 5 },
  140. { LEFT, 5 },
  141. { LEFT, 5 },
  142. { LEFT, 5 },
  143. { NOTHING, 15 },
  144. { UP, 5 }, // ときわたり
  145. { RIGHT, 5 },
  146. { RIGHT, 5 },
  147. { RIGHT, 5 },
  148. { RIGHT, 5 },
  149. { RIGHT, 5 },
  150. { NOTHING, 15 },
  151. { A, 5 },
  152. { HOME, 5 },
  153. { NOTHING, 15 },
  154. { HOME, 5 }, // ゲームに戻る
  155.  
  156.  
  157. };
  158.  
  159. // Main entry point.
  160. int main(void) {
  161. // We'll start by performing hardware and peripheral setup.
  162. SetupHardware();
  163. // We'll then enable global interrupts for our use.
  164. GlobalInterruptEnable();
  165. // Once that's done, we'll enter an infinite loop.
  166. for (;;)
  167. {
  168. // We need to run our task to process and deliver data for our IN and OUT endpoints.
  169. HID_Task();
  170. // We also need to run the main USB management task.
  171. USB_USBTask();
  172. }
  173. }
  174.  
  175. // Configures hardware and peripherals, such as the USB peripherals.
  176. void SetupHardware(void) {
  177. // We need to disable watchdog if enabled by bootloader/fuses.
  178. MCUSR &= ~(1 << WDRF);
  179. wdt_disable();
  180.  
  181. // We need to disable clock division before initializing the USB hardware.
  182. clock_prescale_set(clock_div_1);
  183. // We can then initialize our hardware and peripherals, including the USB stack.
  184.  
  185. #ifdef ALERT_WHEN_DONE
  186. // Both PORTD and PORTB will be used for the optional LED flashing and buzzer.
  187. #warning LED and Buzzer functionality enabled. All pins on both PORTB and \
  188. PORTD will toggle when printing is done.
  189. DDRD = 0xFF; //Teensy uses PORTD
  190. PORTD = 0x0;
  191. //We'll just flash all pins on both ports since the UNO R3
  192. DDRB = 0xFF; //uses PORTB. Micro can use either or, but both give us 2 LEDs
  193. PORTB = 0x0; //The ATmega328P on the UNO will be resetting, so unplug it?
  194. #endif
  195. // The USB stack should be initialized last.
  196. USB_Init();
  197. }
  198.  
  199. // Fired to indicate that the device is enumerating.
  200. void EVENT_USB_Device_Connect(void) {
  201. // We can indicate that we're enumerating here (via status LEDs, sound, etc.).
  202. }
  203.  
  204. // Fired to indicate that the device is no longer connected to a host.
  205. void EVENT_USB_Device_Disconnect(void) {
  206. // We can indicate that our device is not ready (via status LEDs, sound, etc.).
  207. }
  208.  
  209. // Fired when the host set the current configuration of the USB device after enumeration.
  210. void EVENT_USB_Device_ConfigurationChanged(void) {
  211. bool ConfigSuccess = true;
  212.  
  213. // We setup the HID report endpoints.
  214. ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_OUT_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
  215. ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_IN_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
  216.  
  217. // We can read ConfigSuccess to indicate a success or failure at this point.
  218. }
  219.  
  220. // Process control requests sent to the device from the USB host.
  221. void EVENT_USB_Device_ControlRequest(void) {
  222. // We can handle two control requests: a GetReport and a SetReport.
  223.  
  224. // Not used here, it looks like we don't receive control request from the Switch.
  225. }
  226.  
  227. // Process and deliver data from IN and OUT endpoints.
  228. void HID_Task(void) {
  229. // If the device isn't connected and properly configured, we can't do anything here.
  230. if (USB_DeviceState != DEVICE_STATE_Configured)
  231. return;
  232.  
  233. // We'll start with the OUT endpoint.
  234. Endpoint_SelectEndpoint(JOYSTICK_OUT_EPADDR);
  235. // We'll check to see if we received something on the OUT endpoint.
  236. if (Endpoint_IsOUTReceived())
  237. {
  238. // If we did, and the packet has data, we'll react to it.
  239. if (Endpoint_IsReadWriteAllowed())
  240. {
  241. // We'll create a place to store our data received from the host.
  242. USB_JoystickReport_Output_t JoystickOutputData;
  243. // We'll then take in that data, setting it up in our storage.
  244. while(Endpoint_Read_Stream_LE(&JoystickOutputData, sizeof(JoystickOutputData), NULL) != ENDPOINT_RWSTREAM_NoError);
  245. // At this point, we can react to this data.
  246.  
  247. // However, since we're not doing anything with this data, we abandon it.
  248. }
  249. // Regardless of whether we reacted to the data, we acknowledge an OUT packet on this endpoint.
  250. Endpoint_ClearOUT();
  251. }
  252.  
  253. // We'll then move on to the IN endpoint.
  254. Endpoint_SelectEndpoint(JOYSTICK_IN_EPADDR);
  255. // We first check to see if the host is ready to accept data.
  256. if (Endpoint_IsINReady())
  257. {
  258. // We'll create an empty report.
  259. USB_JoystickReport_Input_t JoystickInputData;
  260. // We'll then populate this report with what we want to send to the host.
  261. GetNextReport(&JoystickInputData);
  262. // Once populated, we can output this data to the host. We do this by first writing the data to the control stream.
  263. while(Endpoint_Write_Stream_LE(&JoystickInputData, sizeof(JoystickInputData), NULL) != ENDPOINT_RWSTREAM_NoError);
  264. // We then send an IN packet on this endpoint.
  265. Endpoint_ClearIN();
  266. }
  267. }
  268.  
  269. typedef enum {
  270. SYNC_CONTROLLER,
  271. SYNC_POSITION,
  272. BREATHE,
  273. PROCESS,
  274. CLEANUP,
  275. DONE
  276. } State_t;
  277. State_t state = SYNC_CONTROLLER;
  278.  
  279. #define ECHOES 2
  280. int echoes = 0;
  281. USB_JoystickReport_Input_t last_report;
  282.  
  283. int report_count = 0;
  284. int xpos = 0;
  285. int ypos = 0;
  286. int bufindex = 0;
  287. int duration_count = 0;
  288. int portsval = 0;
  289.  
  290. // Prepare the next report for the host.
  291. void GetNextReport(USB_JoystickReport_Input_t* const ReportData) {
  292.  
  293. // Prepare an empty report
  294. memset(ReportData, 0, sizeof(USB_JoystickReport_Input_t));
  295. ReportData->LX = STICK_CENTER;
  296. ReportData->LY = STICK_CENTER;
  297. ReportData->RX = STICK_CENTER;
  298. ReportData->RY = STICK_CENTER;
  299. ReportData->HAT = HAT_CENTER;
  300.  
  301. // Repeat ECHOES times the last report
  302. if (echoes > 0)
  303. {
  304. memcpy(ReportData, &last_report, sizeof(USB_JoystickReport_Input_t));
  305. echoes--;
  306. return;
  307. }
  308.  
  309. // States and moves management
  310. switch (state)
  311. {
  312.  
  313. case SYNC_CONTROLLER:
  314. state = BREATHE;
  315. break;
  316.  
  317. // case SYNC_CONTROLLER:
  318. // if (report_count > 550)
  319. // {
  320. // report_count = 0;
  321. // state = SYNC_POSITION;
  322. // }
  323. // else if (report_count == 250 || report_count == 200 || report_count == 325)
  324. // {
  325. // ReportData->Button |= SWITCH_L | SWITCH_R;
  326. // }
  327. // else if (report_count == 350 || report_count == 375 || report_count == 400)
  328. // {
  329. // ReportData->Button |= SWITCH_A;
  330. // }
  331. // else
  332. // {
  333. // ReportData->Button = 0;
  334. // ReportData->LX = STICK_CENTER;
  335. // ReportData->LY = STICK_CENTER;
  336. // ReportData->RX = STICK_CENTER;
  337. // ReportData->RY = STICK_CENTER;
  338. // ReportData->HAT = HAT_CENTER;
  339. // }
  340. // report_count++;
  341. // break;
  342.  
  343. case SYNC_POSITION:
  344. bufindex = 0;
  345.  
  346.  
  347. ReportData->Button = 0;
  348. ReportData->LX = STICK_CENTER;
  349. ReportData->LY = STICK_CENTER;
  350. ReportData->RX = STICK_CENTER;
  351. ReportData->RY = STICK_CENTER;
  352. ReportData->HAT = HAT_CENTER;
  353.  
  354.  
  355. state = BREATHE;
  356. break;
  357.  
  358. case BREATHE:
  359. state = PROCESS;
  360. break;
  361.  
  362. case PROCESS:
  363.  
  364. switch (step[bufindex].button)
  365. {
  366.  
  367. case UP:
  368. ReportData->LY = STICK_MIN;
  369. break;
  370.  
  371. case LEFT:
  372. ReportData->LX = STICK_MIN;
  373. break;
  374.  
  375. case DOWN:
  376. ReportData->LY = STICK_MAX;
  377. break;
  378.  
  379. case RIGHT:
  380. ReportData->LX = STICK_MAX;
  381. break;
  382.  
  383. case A:
  384. ReportData->Button |= SWITCH_A;
  385. break;
  386.  
  387. case B:
  388. ReportData->Button |= SWITCH_B;
  389. break;
  390.  
  391. case R:
  392. ReportData->Button |= SWITCH_R;
  393. break;
  394.  
  395. case THROW:
  396. ReportData->LY = STICK_MIN;
  397. ReportData->Button |= SWITCH_R;
  398. break;
  399.  
  400. case TRIGGERS:
  401. ReportData->Button |= SWITCH_L | SWITCH_R;
  402. break;
  403.  
  404. case HOME:
  405. ReportData->Button |= SWITCH_HOME;
  406. break;
  407. default:
  408. /*
  409. ReportData->LX = STICK_CENTER;
  410. ReportData->LY = STICK_CENTER;
  411. ReportData->RX = STICK_CENTER;
  412. ReportData->RY = STICK_CENTER;
  413. ReportData->HAT = HAT_CENTER;
  414. */
  415. break;
  416. }
  417.  
  418. duration_count++;
  419.  
  420. if (duration_count > step[bufindex].duration)
  421. {
  422. bufindex++;
  423. duration_count = 0;
  424. }
  425.  
  426.  
  427. if (bufindex > (int)( sizeof(step) / sizeof(step[0])) - 1)
  428. {
  429.  
  430. // state = CLEANUP;
  431.  
  432. bufindex = 7;
  433. duration_count = 0;
  434.  
  435. state = BREATHE;
  436.  
  437. ReportData->LX = STICK_CENTER;
  438. ReportData->LY = STICK_CENTER;
  439. ReportData->RX = STICK_CENTER;
  440. ReportData->RY = STICK_CENTER;
  441. ReportData->HAT = HAT_CENTER;
  442.  
  443.  
  444. // state = DONE;
  445. // state = BREATHE;
  446.  
  447. }
  448.  
  449. break;
  450.  
  451. case CLEANUP:
  452. state = DONE;
  453. break;
  454.  
  455. case DONE:
  456. #ifdef ALERT_WHEN_DONE
  457. portsval = ~portsval;
  458. PORTD = portsval; //flash LED(s) and sound buzzer if attached
  459. PORTB = portsval;
  460. _delay_ms(250);
  461. #endif
  462. return;
  463. }
  464.  
  465. // // Inking
  466. // if (state != SYNC_CONTROLLER && state != SYNC_POSITION)
  467. // if (pgm_read_byte(&(image_data[(xpos / 8) + (ypos * 40)])) & 1 << (xpos % 8))
  468. // ReportData->Button |= SWITCH_A;
  469.  
  470. // Prepare to echo this report
  471. memcpy(&last_report, ReportData, sizeof(USB_JoystickReport_Input_t));
  472. echoes = ECHOES;
  473.  
  474. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement