Advertisement
Guest User

Joystick.c

a guest
Jan 23rd, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.39 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. // make && sudo dfu-programmer atmega16u2 erase && sudo dfu-programmer atmega16u2 flash Joystick.hex
  22.  
  23. // make && ./teensy_loader_cli -mmcu=atmega32u4 -w Joystick.hex
  24.  
  25. #include "Joystick.h"
  26.  
  27. typedef enum {
  28.     UP,
  29.     DOWN,
  30.     LEFT,
  31.     RIGHT,
  32.     X,
  33.     Y,
  34.     A,
  35.     B,
  36.     L,
  37.     R,
  38.     THROW,
  39.     NOTHING,
  40.     PLUS,
  41.     MINUS,
  42.     TRIGGERS
  43. } Buttons_t;
  44.  
  45. typedef struct {
  46.     Buttons_t button;
  47.     uint16_t duration;
  48. } command;
  49.  
  50. static const command step[] = {
  51.     // Setup controller
  52.     { NOTHING,  250 },
  53.     { TRIGGERS,   5 },
  54.     { NOTHING,  100 },
  55.     { TRIGGERS,   5 },
  56.     { NOTHING,  100 },
  57.     { A,          5 },
  58.     { NOTHING,   50 },
  59.  
  60.     // Pay Vending Machine
  61.     { NOTHING,  250 }
  62.     { A,          5 },
  63.     { NOTHING,  150 },
  64.     { Right,      5 },
  65.     { NOTHING,  150 },
  66.     { A,          5 },
  67.     { NOTHING,  150 },
  68.     { A,          5 },
  69.     { NOTHING,  255 }
  70.  
  71.     // Wait before looping
  72.     // { NOTHING,   50 },
  73. };
  74.  
  75. // Main entry point.
  76. int main(void) {
  77.     // We'll start by performing hardware and peripheral setup.
  78.     SetupHardware();
  79.     // We'll then enable global interrupts for our use.
  80.     GlobalInterruptEnable();
  81.     // Once that's done, we'll enter an infinite loop.
  82.     for (;;)
  83.     {
  84.         // We need to run our task to process and deliver data for our IN and OUT endpoints.
  85.         HID_Task();
  86.         // We also need to run the main USB management task.
  87.         USB_USBTask();
  88.     }
  89. }
  90.  
  91. // Configures hardware and peripherals, such as the USB peripherals.
  92. void SetupHardware(void) {
  93.     // We need to disable watchdog if enabled by bootloader/fuses.
  94.     MCUSR &= ~(1 << WDRF);
  95.     wdt_disable();
  96.  
  97.     // We need to disable clock division before initializing the USB hardware.
  98.     clock_prescale_set(clock_div_1);
  99.     // We can then initialize our hardware and peripherals, including the USB stack.
  100.  
  101.     #ifdef ALERT_WHEN_DONE
  102.     // Both PORTD and PORTB will be used for the optional LED flashing and buzzer.
  103.     #warning LED and Buzzer functionality enabled. All pins on both PORTB and \
  104. PORTD will toggle when printing is done.
  105.     DDRD  = 0xFF; //Teensy uses PORTD
  106.     PORTD =  0x0;
  107.                   //We'll just flash all pins on both ports since the UNO R3
  108.     DDRB  = 0xFF; //uses PORTB. Micro can use either or, but both give us 2 LEDs
  109.     PORTB =  0x0; //The ATmega328P on the UNO will be resetting, so unplug it?
  110.     #endif
  111.     // The USB stack should be initialized last.
  112.     USB_Init();
  113. }
  114.  
  115. // Fired to indicate that the device is enumerating.
  116. void EVENT_USB_Device_Connect(void) {
  117.     // We can indicate that we're enumerating here (via status LEDs, sound, etc.).
  118. }
  119.  
  120. // Fired to indicate that the device is no longer connected to a host.
  121. void EVENT_USB_Device_Disconnect(void) {
  122.     // We can indicate that our device is not ready (via status LEDs, sound, etc.).
  123. }
  124.  
  125. // Fired when the host set the current configuration of the USB device after enumeration.
  126. void EVENT_USB_Device_ConfigurationChanged(void) {
  127.     bool ConfigSuccess = true;
  128.  
  129.     // We setup the HID report endpoints.
  130.     ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_OUT_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
  131.     ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_IN_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
  132.  
  133.     // We can read ConfigSuccess to indicate a success or failure at this point.
  134. }
  135.  
  136. // Process control requests sent to the device from the USB host.
  137. void EVENT_USB_Device_ControlRequest(void) {
  138.     // We can handle two control requests: a GetReport and a SetReport.
  139.  
  140.     // Not used here, it looks like we don't receive control request from the Switch.
  141. }
  142.  
  143. // Process and deliver data from IN and OUT endpoints.
  144. void HID_Task(void) {
  145.     // If the device isn't connected and properly configured, we can't do anything here.
  146.     if (USB_DeviceState != DEVICE_STATE_Configured)
  147.         return;
  148.  
  149.     // We'll start with the OUT endpoint.
  150.     Endpoint_SelectEndpoint(JOYSTICK_OUT_EPADDR);
  151.     // We'll check to see if we received something on the OUT endpoint.
  152.     if (Endpoint_IsOUTReceived())
  153.     {
  154.         // If we did, and the packet has data, we'll react to it.
  155.         if (Endpoint_IsReadWriteAllowed())
  156.         {
  157.             // We'll create a place to store our data received from the host.
  158.             USB_JoystickReport_Output_t JoystickOutputData;
  159.             // We'll then take in that data, setting it up in our storage.
  160.             while(Endpoint_Read_Stream_LE(&JoystickOutputData, sizeof(JoystickOutputData), NULL) != ENDPOINT_RWSTREAM_NoError);
  161.             // At this point, we can react to this data.
  162.  
  163.             // However, since we're not doing anything with this data, we abandon it.
  164.         }
  165.         // Regardless of whether we reacted to the data, we acknowledge an OUT packet on this endpoint.
  166.         Endpoint_ClearOUT();
  167.     }
  168.  
  169.     // We'll then move on to the IN endpoint.
  170.     Endpoint_SelectEndpoint(JOYSTICK_IN_EPADDR);
  171.     // We first check to see if the host is ready to accept data.
  172.     if (Endpoint_IsINReady())
  173.     {
  174.         // We'll create an empty report.
  175.         USB_JoystickReport_Input_t JoystickInputData;
  176.         // We'll then populate this report with what we want to send to the host.
  177.         GetNextReport(&JoystickInputData);
  178.         // Once populated, we can output this data to the host. We do this by first writing the data to the control stream.
  179.         while(Endpoint_Write_Stream_LE(&JoystickInputData, sizeof(JoystickInputData), NULL) != ENDPOINT_RWSTREAM_NoError);
  180.         // We then send an IN packet on this endpoint.
  181.         Endpoint_ClearIN();
  182.     }
  183. }
  184.  
  185. typedef enum {
  186.     SYNC_CONTROLLER,
  187.     SYNC_POSITION,
  188.     BREATHE,
  189.     PROCESS,
  190.     CLEANUP,
  191.     DONE
  192. } State_t;
  193. State_t state = SYNC_CONTROLLER;
  194.  
  195. #define ECHOES 2
  196. int echoes = 0;
  197. USB_JoystickReport_Input_t last_report;
  198.  
  199. int report_count = 0;
  200. int xpos = 0;
  201. int ypos = 0;
  202. int bufindex = 0;
  203. int duration_count = 0;
  204. int portsval = 0;
  205.  
  206. // Prepare the next report for the host.
  207. void GetNextReport(USB_JoystickReport_Input_t* const ReportData) {
  208.  
  209.     // Prepare an empty report
  210.     memset(ReportData, 0, sizeof(USB_JoystickReport_Input_t));
  211.     ReportData->LX = STICK_CENTER;
  212.     ReportData->LY = STICK_CENTER;
  213.     ReportData->RX = STICK_CENTER;
  214.     ReportData->RY = STICK_CENTER;
  215.     ReportData->HAT = HAT_CENTER;
  216.  
  217.     // Repeat ECHOES times the last report
  218.     if (echoes > 0)
  219.     {
  220.         memcpy(ReportData, &last_report, sizeof(USB_JoystickReport_Input_t));
  221.         echoes--;
  222.         return;
  223.     }
  224.  
  225.     // States and moves management
  226.     switch (state)
  227.     {
  228.  
  229.         case SYNC_CONTROLLER:
  230.             state = BREATHE;
  231.             break;
  232.  
  233.         case SYNC_POSITION:
  234.             bufindex = 0;
  235.  
  236.  
  237.             ReportData->Button = 0;
  238.             ReportData->LX = STICK_CENTER;
  239.             ReportData->LY = STICK_CENTER;
  240.             ReportData->RX = STICK_CENTER;
  241.             ReportData->RY = STICK_CENTER;
  242.             ReportData->HAT = HAT_CENTER;
  243.  
  244.  
  245.             state = BREATHE;
  246.             break;
  247.  
  248.         case BREATHE:
  249.             state = PROCESS;
  250.             break;
  251.  
  252.         case PROCESS:
  253.  
  254.             switch (step[bufindex].button)
  255.             {
  256.  
  257.                 case UP:
  258.                     ReportData->LY = STICK_MIN;            
  259.                     break;
  260.  
  261.                 case LEFT:
  262.                     ReportData->LX = STICK_MIN;            
  263.                     break;
  264.  
  265.                 case DOWN:
  266.                     ReportData->LY = STICK_MAX;            
  267.                     break;
  268.  
  269.                 case RIGHT:
  270.                     ReportData->LX = STICK_MAX;            
  271.                     break;
  272.  
  273.                 case PLUS:
  274.                     ReportData->Button |= SWITCH_PLUS;
  275.                     break;
  276.  
  277.                 case MINUS:
  278.                     ReportData->Button |= SWITCH_MINUS;
  279.                     break;
  280.  
  281.                 case A:
  282.                     ReportData->Button |= SWITCH_A;
  283.                     break;
  284.  
  285.                 case B:
  286.                     ReportData->Button |= SWITCH_B;
  287.                     break;
  288.  
  289.                 case X:
  290.                     ReportData->Button |= SWITCH_X;
  291.                     break;
  292.  
  293.                 case Y:
  294.                     ReportData->Button |= SWITCH_Y;
  295.                     break;
  296.  
  297.                 case R:
  298.                     ReportData->Button |= SWITCH_R;
  299.                     break;
  300.  
  301.                 case L:
  302.                     ReportData->Button |= SWITCH_L;
  303.                     break;
  304.  
  305.                 case THROW:
  306.                     ReportData->LY = STICK_MIN;            
  307.                     ReportData->Button |= SWITCH_R;
  308.                     break;
  309.  
  310.                 case TRIGGERS:
  311.                     ReportData->Button |= SWITCH_L | SWITCH_R;
  312.                     break;
  313.  
  314.                 default:
  315.                     ReportData->LX = STICK_CENTER;
  316.                     ReportData->LY = STICK_CENTER;
  317.                     ReportData->RX = STICK_CENTER;
  318.                     ReportData->RY = STICK_CENTER;
  319.                     ReportData->HAT = HAT_CENTER;
  320.                     break;
  321.             }
  322.  
  323.             duration_count++;
  324.  
  325.             if (duration_count > step[bufindex].duration)
  326.             {
  327.                 bufindex++;
  328.                 duration_count = 0;            
  329.             }
  330.  
  331.  
  332.             if (bufindex > (int)( sizeof(step) / sizeof(step[0])) - 1)
  333.             {
  334.  
  335.                 // state = CLEANUP;
  336.  
  337.                 bufindex = 7;
  338.                 duration_count = 0;
  339.  
  340.                 state = BREATHE;
  341.  
  342.                 ReportData->LX = STICK_CENTER;
  343.                 ReportData->LY = STICK_CENTER;
  344.                 ReportData->RX = STICK_CENTER;
  345.                 ReportData->RY = STICK_CENTER;
  346.                 ReportData->HAT = HAT_CENTER;
  347.  
  348.             }
  349.  
  350.             break;
  351.  
  352.         case CLEANUP:
  353.             state = DONE;
  354.             break;
  355.  
  356.         case DONE:
  357.             #ifdef ALERT_WHEN_DONE
  358.             portsval = ~portsval;
  359.             PORTD = portsval; //flash LED(s) and sound buzzer if attached
  360.             PORTB = portsval;
  361.             _delay_ms(250);
  362.             #endif
  363.             return;
  364.     }
  365.  
  366.  
  367.     // Prepare to echo this report
  368.     memcpy(&last_report, ReportData, sizeof(USB_JoystickReport_Input_t));
  369.     echoes = ECHOES;
  370.  
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement