Advertisement
selrahc13

DanceForce XInput code for Sparkfun Pro Micro

May 16th, 2020
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.32 KB | None | 0 0
  1. /*  DanceForce pad code based on code provided by Promit Roy under Creative Commons CC-BY License
  2.  *  Modifications by Charles Scheidecker for Sparkfun Pro Micro board with XInput support
  3.  *  See https://github.com/dmadison/ArduinoXInput for XInput installation instructions
  4.  */
  5.  
  6. #include <XInput.h>
  7.  
  8. //The analog threshold value for triggering a button
  9. const int TriggerThreshold = 25;
  10.  
  11. //pin mappings for where things got soldered
  12. const int Pin_Up      = A0;
  13. const int Pin_Right   = A1;
  14. const int Pin_Down    = A2;
  15. const int Pin_Left    = A3;
  16. const int Pin_Back    = 9;
  17. const int Pin_Start   = 8;
  18. const int p[6] = {Pin_Up, Pin_Right, Pin_Down, Pin_Left, Pin_Start, Pin_Back};
  19.  
  20. // debounce vars
  21. unsigned long lastDebounceTime[6] = {0};
  22. bool lastState[6] = {false};
  23. const int debounceDelayPad = 5; // debounce time in ms, adjust as needed
  24. const int debounceDelayButton = 125; // debounce time in ms, adjust as needed
  25.  
  26. // state vars
  27. //analog read values
  28. int a[6] = {0};
  29. // Button states
  30. bool s[6] = {0};
  31.  
  32. void setup() {
  33.  
  34.   Serial.begin(38400);
  35.  
  36.   //The analog pins are configured with internal pull-up resistors, which makes for a very simple circuit
  37.   //However this method does not support useful pressure sensitivity adjustments
  38.   //By soldering 1K resistors as pull-ups on the board, you can make the buttons require more pressure
  39.   //The first version did that, but making the buttons more difficult didn't seem very desirable
  40.  
  41.   pinMode(Pin_Start,  INPUT_PULLUP);
  42.   pinMode(Pin_Back,   INPUT_PULLUP);
  43.   pinMode(Pin_Up,     INPUT_PULLUP);
  44.   pinMode(Pin_Right,  INPUT_PULLUP);
  45.   pinMode(Pin_Down,   INPUT_PULLUP);
  46.   pinMode(Pin_Left,   INPUT_PULLUP);
  47.  
  48.   XInput.setAutoSend(false);
  49.  
  50.   XInput.begin();
  51. }
  52.  
  53. void loop() {  
  54.   //read each pin, and set that Joystick button appropriately
  55.   for(int i = 0; i < 6; ++i)
  56.   {
  57.     a[i] = analogRead(p[i]);
  58.  
  59.     // normalize values to on or off
  60.     if (a[i] <= TriggerThreshold) {
  61.       s[i] = true;
  62.     } else {
  63.       s[i] = false;
  64.     }
  65.    
  66.     switch(p[i]) {
  67.       case Pin_Up:
  68.         if (debounced(i, debounceDelayPad)) {
  69.           XInput.setButton(BUTTON_Y, s[i]);
  70.         }
  71.         break;
  72.       case Pin_Right:
  73.         if (debounced(i, debounceDelayPad)) {
  74.           XInput.setButton(BUTTON_B, s[i]);
  75.         }
  76.         break;
  77.       case Pin_Down:
  78.         if (debounced(i, debounceDelayPad)) {
  79.           XInput.setButton(BUTTON_A, s[i]);
  80.         }
  81.         break;
  82.       case Pin_Left:
  83.         if (debounced(i, debounceDelayPad)) {
  84.           XInput.setButton(BUTTON_X, s[i]);
  85.         }
  86.         break;
  87.       case Pin_Start:
  88.         if (debounced(i, debounceDelayButton)) {
  89.           XInput.setButton(BUTTON_START, s[i]);
  90.         }
  91.         break;
  92.       case Pin_Back:
  93.         if (debounced(i, debounceDelayButton)) {
  94.           XInput.setButton(BUTTON_BACK, s[i]);
  95.         }
  96.         break;
  97.     }
  98.     lastState[i] = s[i];    
  99.   }
  100.  
  101.   XInput.send();
  102.   //Enable this block if you need to debug the electricals of the pad
  103.   //XInput library automatically prints debug information if a non-XInput board
  104.   //is selected. If you want raw debug data, use a standard board type and set
  105.   //if(0) to if(1)
  106.   #ifndef USB_XINPUT
  107.   if(1)
  108.   {
  109.     char buffer[80];
  110.     sprintf(buffer, "Pins: %d %d %d %d (%d %d)\n", a[0], a[1], a[2], a[3], a[4], a[5]);
  111.     sprintf(buffer, " Deb: %lu %lu %lu %lu (%lu %lu) %lu\n", lastDebounceTime[0], lastDebounceTime[1], lastDebounceTime[2], lastDebounceTime[3], lastDebounceTime[4], lastDebounceTime[5], millis());
  112.     Serial.println(buffer);
  113.     //delay(250);
  114.   }
  115.   #endif
  116.  
  117.   //This limits the pad to run at 200 Hz.
  118.   //delay(5);
  119. }
  120.  
  121. //returns true if a button doesn't need to be debounced
  122. bool debounced(int buttonIndex, int debounceDelay) {
  123.   // If the switch changed, due to noise or pressing:
  124.   if (s[buttonIndex] != lastState[buttonIndex]) {
  125.     // reset debounce timer
  126.     lastDebounceTime[buttonIndex] = millis();
  127.   }
  128.    
  129.   if ((millis() - lastDebounceTime[buttonIndex]) > debounceDelay) {
  130.     // whatever the reading is at, it's been there for longer than the debounce
  131.     // delay, so take it as the actual current state:
  132.  
  133.     // if the button state has changed:
  134.     if (s[buttonIndex] != lastState[buttonIndex]) {
  135.       lastState[buttonIndex] = s[buttonIndex];
  136.     }
  137.     return true;
  138.   }
  139.   return false;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement