Advertisement
shadowbrony33

ArduinoRapidFire - v0.09 SUPPORT FOR I2C

Apr 17th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.81 KB | None | 0 0
  1. // Note that this code has only been set up for XBOX 360 Gampads ONLY as of right now.
  2.  
  3. #include <SPI.h>
  4. #include <Wire.h>
  5. #include <gfxfont.h>
  6. #include <Adafruit_GFX.h>
  7. #include <Adafruit_SSD1306.h>
  8.  
  9. #define OLED_RESET 4
  10. Adafruit_SSD1306 display(OLED_RESET);
  11.  
  12. #define NUMFLAKES 10
  13. #define XPOS 0
  14. #define YPOS 1
  15. #define DELTAY 2
  16.  
  17.  
  18. #define LOGO16_GLCD_HEIGHT 16
  19. #define LOGO16_GLCD_WIDTH  16
  20. static const unsigned char PROGMEM logo16_glcd_bmp[] =
  21. { B00000000, B11000000,
  22.   B00000001, B11000000,
  23.   B00000001, B11000000,
  24.   B00000011, B11100000,
  25.   B11110011, B11100000,
  26.   B11111110, B11111000,
  27.   B01111110, B11111111,
  28.   B00110011, B10011111,
  29.   B00011111, B11111100,
  30.   B00001101, B01110000,
  31.   B00011011, B10100000,
  32.   B00111111, B11100000,
  33.   B00111111, B11110000,
  34.   B01111100, B11110000,
  35.   B01110000, B01110000,
  36.   B00000000, B00110000 };
  37.  
  38. #if (SSD1306_LCDHEIGHT != 64)
  39. #error("Height incorrect, please fix Adafruit_SSD1306.h!");
  40. #endif
  41.  
  42. // Pins
  43. int DPAD_UP_IN = 2, DPAD_DOWN_IN = 3, DPAD_LEFT_IN = 6, DPAD_RIGHT_IN = 5; // DPAD Input Pins
  44. int A_BUTTON_IN= 9, B_BUTTON_IN = 10, X_BUTTON_IN = 8, Y_BUTTON_IN = 7; // ABXY Input Pins
  45.   int A_BUTTON_OUT = A0, B_BUTTON_OUT = A1, X_BUTTON_OUT = A6, Y_BUTTON_OUT = A7; // ABXY Output Pins
  46.  
  47. int R_TRIGGER_IN = A3,L_TRIGGER_IN = A2; // Trigger Input Pins
  48.   int R_TRIGGER_OUT = 4; // Trigger Output Pins
  49.  
  50. int R_BUMPER_IN = 11, L_BUMPER_IN = 12; // Bumper Input Pins
  51.  
  52. int LED = 13; // On-Board LED
  53.    
  54. // Input Values from L & R Triggers
  55. int R_TRIGGER_VAL, L_TRIGGER_VAL;
  56.   int R_TRIGGER, L_TRIGGER;
  57.  
  58. // Input Values from Bumpers
  59. int L_BUMPER, R_BUMPER;
  60.  
  61. // Input values from ABXY
  62. int A_BUTTON, B_BUTTON, X_BUTTON, Y_BUTTON;
  63.  
  64. // Input values from DPAD
  65. int DPAD_UP, DPAD_DOWN, DPAD_LEFT, DPAD_RIGHT;
  66.  
  67. // Other Variables
  68. const int Profiles = 2;   // Number of Profiles (RapidFire)
  69. const int Macro_Profiles = 2; // Number of Profiles (Macro)
  70.  
  71. int Profile = 0;  // Current Profile (RapidFire)
  72. int Macro_Profile = 0; // Current Profile (Macro)
  73.  
  74. int DELAY[Profiles][2] = { {40, 30} , {90, 30} } ;   // Array containing delay timings for each profile (RapidFire)
  75. int MACRO_DELAY[Macro_Profiles][2] = { {30, 30} , {30, 30} }; // Array containing delay timings for each profile (Macro)
  76.  
  77.  
  78.  
  79. void setup()
  80.   {
  81.   // Setting up Pins
  82.   pinMode(A_BUTTON_IN, INPUT);
  83.     pinMode(A_BUTTON_IN, LOW); // Enables Pull-Down Resistor to prevent value from floating
  84.       pinMode(A_BUTTON_OUT, OUTPUT);
  85.   pinMode(B_BUTTON_IN, INPUT);
  86.     pinMode(B_BUTTON_IN, LOW);
  87.       pinMode(B_BUTTON_OUT, OUTPUT);
  88.   pinMode(X_BUTTON_IN, INPUT);
  89.     pinMode(X_BUTTON_IN, LOW);
  90.       pinMode(X_BUTTON_OUT, OUTPUT);
  91.   pinMode(Y_BUTTON_IN, INPUT);
  92.     pinMode(Y_BUTTON_IN, LOW);
  93.       pinMode(Y_BUTTON_OUT, OUTPUT);
  94.  
  95.   pinMode(DPAD_UP_IN, INPUT);
  96.     pinMode(DPAD_UP_IN, LOW);
  97.   pinMode(DPAD_DOWN_IN, INPUT);
  98.     pinMode(DPAD_DOWN_IN, LOW);
  99.   pinMode(DPAD_LEFT_IN, INPUT);
  100.     pinMode(DPAD_LEFT_IN, LOW);    
  101.   pinMode(DPAD_RIGHT_IN, INPUT);
  102.     pinMode(DPAD_RIGHT_IN, LOW);
  103.    
  104.   pinMode(R_BUMPER_IN, INPUT);
  105.     pinMode(R_BUMPER_IN, LOW);
  106.   pinMode(L_BUMPER_IN, INPUT);
  107.     pinMode(L_BUMPER_IN, LOW);
  108.        
  109.   pinMode(R_TRIGGER_IN, INPUT);
  110.     pinMode(R_TRIGGER_OUT, OUTPUT);
  111.   pinMode(L_TRIGGER_IN, INPUT);
  112.  
  113.   // Initializing Serial Comunication
  114.   Serial.begin(9600);
  115.   display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)
  116.  
  117.   // internally, this will display the splashscreen.
  118.   display.display();
  119.   delay(2000);
  120.   display.clearDisplay();
  121.   }
  122.  
  123.  
  124.  
  125. void loop()
  126.   {
  127.     GrabInputs(); // Reads and stores values from I/O
  128.    
  129.     RapidFire(R_TRIGGER, 30, R_TRIGGER_OUT); // Trigger, Min Threshold, Trigger Output
  130.    
  131.     SetDelay(L_TRIGGER, 30, DPAD_UP, DPAD_DOWN, DPAD_RIGHT, DPAD_LEFT, 10, 300, 1, 0); // Trigger Input, Delay_In_Up Button, Delay_In_Down Button, Delay_Out_Up Button, Delay_Out_Down Button, MinDelay, MaxDelay, Print via Serial, Print via IIC
  132.    
  133.     SetProfile(L_TRIGGER, 30, Y_BUTTON, 1, 0); // Trigger Input, Min Trigger Input Value, Button Input, Print via Serial, Print via IIC
  134.        
  135.     SetMacroProfile(L_TRIGGER, 30, R_BUMPER, L_BUMPER); // Trigger Input, Min Trigger Input Value, Select Next Profile, Select Previous Profile
  136.    
  137.     PrintDebug(3, 300); // 0 = Off 1 = Triggers 2 = ABXY 3 = Bumpers 4 = DPAD, Refresh Delay
  138.   }
  139.  
  140.  
  141.  
  142. void RapidFire(int TRIGGER_INPUT, int i, int TRIGGER_OUTPUT)
  143.   {
  144.     if (TRIGGER_INPUT > i)
  145.     {
  146.       digitalWrite(TRIGGER_OUTPUT, LOW); // Sends signal that the trigger is pressed
  147.         delay(DELAY[Profile][0]);
  148.        
  149.       digitalWrite(TRIGGER_OUTPUT, HIGH); // Sends signal that the trigger is released
  150.         delay(DELAY[Profile][1]);
  151.     }
  152.   }
  153.  
  154.  
  155.  
  156. void SetDelay(int TRIGGER_INPUT, int i, int DELAY_IN_UP, int DELAY_IN_DOWN, int DELAY_OUT_UP, int DELAY_OUT_DOWN, int MinDelay, int MaxDelay, bool PrintSerial, bool PrintIIC)
  157.   {  
  158.     if (TRIGGER_INPUT > i && DELAY_IN_UP == LOW && DELAY[Profile][0] < MaxDelay) // Checks for if the L-Trigger & DPAD-UP is being pressed and that DELAY_IN in below MaxDelay
  159.       {
  160.         DELAY[Profile][0] += 10;
  161.          
  162.         PrintStats(PrintSerial, PrintIIC);
  163.          
  164.         delay(300);
  165.       }
  166.    
  167.     if (TRIGGER_INPUT > i && DELAY_IN_DOWN == LOW && DELAY[Profile][0] > MinDelay) // Checks for if the L-Trigger & DPAD-DOWN is being pressed and that DELAY_IN in above MinDelay
  168.       {
  169.         DELAY[Profile][0] -= 10;
  170.          
  171.         PrintStats(PrintSerial, PrintIIC);
  172.          
  173.         delay(300);
  174.       }
  175.      
  176.     if (TRIGGER_INPUT > i && DELAY_OUT_UP == LOW && DELAY[Profile][1] < MaxDelay) // Checks for if the L-Trigger & DPAD-UP is being pressed and that DELAY_OUT in below MaxDelay
  177.       {
  178.         DELAY[Profile][1] += 5;
  179.          
  180.         PrintStats(PrintSerial, PrintIIC);
  181.          
  182.         delay(300);
  183.       }
  184.  
  185.     if (TRIGGER_INPUT > i && DELAY_OUT_DOWN == LOW && DELAY[Profile][1] > MinDelay) // Checks for if the L-Trigger & DPAD-DOWN is being pressed and that DELAY_IN in above MinValue
  186.       {
  187.         DELAY[Profile][1] -= 5;
  188.          
  189.         PrintStats(PrintSerial, PrintIIC);
  190.          
  191.         delay(300);
  192.       }
  193.  }
  194.  
  195.  
  196.  
  197. void SetProfile(int TRIGGER_INPUT, int i, int BUTTON_INPUT, bool PrintSerial, bool PrintIIC)
  198.   {
  199.     if (BUTTON_INPUT == LOW && TRIGGER_INPUT > i && Profile <= Profiles)
  200.       {
  201.         Profile++;
  202.        
  203.         if (Profile == Profiles)
  204.           {
  205.             Profile = 0;
  206.           }
  207.        
  208.         PrintStats(PrintSerial, PrintIIC);
  209.          
  210.         delay(300);
  211.       }
  212.   }
  213.  
  214.  
  215.  
  216. void PrintStats(bool PrintSerial, bool PrintIIC)
  217.   {  
  218.     if (PrintSerial == 1)
  219.       {
  220.         Serial.print("Profile ");
  221.           Serial.println(Profile);
  222.          
  223.           Serial.print("\t");
  224.          
  225.         Serial.print("Delay_In = ");
  226.           Serial.println(DELAY[Profile][0]);
  227.          
  228.           Serial.print("\t");
  229.          
  230.         Serial.print("Delay_Out = ");
  231.           Serial.println(DELAY[Profile][1]);
  232.       }
  233.  
  234.     if (PrintIIC == 1)
  235.       {
  236.         display.setTextSize(1);
  237.         display.setTextColor(WHITE);
  238.         display.setCursor(0,0);
  239.    
  240.         display.print("Profile ");
  241.           display.println(Profile);
  242.          
  243.           display.print("\t");
  244.          
  245.         display.print("Delay_In = ");
  246.           display.println(DELAY[Profile][0]);
  247.          
  248.           display.print("\t");
  249.          
  250.         display.print("Delay_Out = ");
  251.           display.println(DELAY[Profile][1]);
  252.        
  253.         display.display();
  254.         display.clearDisplay();
  255.       }
  256.   }
  257.  
  258.  
  259.  
  260. void PrintDebug(int i, int REFRESH_DELAY)
  261. {
  262.   switch (i)
  263.     {
  264.       case 1 : Serial.print("Right Trigger = ");
  265.                 Serial.println(R_TRIGGER);        
  266.                Serial.print("Left Trigger = ");
  267.                 Serial.println(L_TRIGGER);      
  268.                Serial.println();
  269.                break;
  270.                  
  271.       case 2 : Serial.print("A Button = ");
  272.                  Serial.println(A_BUTTON);
  273.                Serial.print("B Button = ");
  274.                  Serial.println(B_BUTTON);
  275.                Serial.print("X Button = ");
  276.                  Serial.println(X_BUTTON);
  277.                Serial.print("Y Button = ");
  278.                  Serial.println(Y_BUTTON);
  279.                Serial.println();
  280.                break;
  281.  
  282.       case 3 : Serial.print("R Bumper = ");
  283.                 Serial.println(R_BUMPER);
  284.                Serial.print("L Bumper = ");
  285.                 Serial.println(L_BUMPER);
  286.                Serial.println();
  287.                break;
  288.  
  289.       case 4 : Serial.print("DPAD UP = ");
  290.                 Serial.println(DPAD_UP);
  291.                Serial.print("DPAD DOWN = ");
  292.                 Serial.println(DPAD_DOWN);
  293.                Serial.print("DPAD LEFT = ");
  294.                 Serial.println(DPAD_LEFT);
  295.                Serial.print("DPAD RIGHT = ");
  296.                 Serial.println(DPAD_RIGHT);
  297.                Serial.println();
  298.                break;
  299.     }
  300.    
  301.   if(i > 0)
  302.     {
  303.        delay(REFRESH_DELAY);
  304.     }
  305. }
  306.  
  307.  
  308.  
  309. void GrabInputs()
  310.   {
  311.     // Reading value from L & R Triggers
  312.     R_TRIGGER_VAL = analogRead(R_TRIGGER_IN);
  313.     R_TRIGGER = map(R_TRIGGER_VAL, 0, 1023, 0, 255); // Maps values from R-Trigger to a value between 0 and 255
  314.    
  315.     L_TRIGGER_VAL = analogRead(L_TRIGGER_IN);
  316.     L_TRIGGER = map(L_TRIGGER_VAL, 0, 1023, 0, 255);  // Maps values from L-Trigger to a value between 0 and 255
  317.    
  318.     // Reading Values from L & R Bumpers
  319.     R_BUMPER = digitalRead(R_BUMPER_IN);
  320.     L_BUMPER = digitalRead(L_BUMPER_IN);
  321.      
  322.     // Reading values from ABXY
  323.     A_BUTTON = digitalRead(A_BUTTON_IN);
  324.     B_BUTTON = digitalRead(B_BUTTON_IN);
  325.     X_BUTTON = digitalRead(X_BUTTON_IN);
  326.     Y_BUTTON = digitalRead(Y_BUTTON_IN);
  327.    
  328.     // Reading values from DPAD
  329.     DPAD_UP = digitalRead(DPAD_UP_IN); // Returns HIGH when not pressed and LOW when pressed
  330.     DPAD_DOWN = digitalRead(DPAD_DOWN_IN); // Returns HIGH when not pressed and LOW when pressed
  331.     DPAD_LEFT = digitalRead(DPAD_LEFT_IN); // Returns HIGH when not pressed and LOW when pressed
  332.     DPAD_RIGHT = digitalRead(DPAD_RIGHT_IN); // Returns HIGH when not pressed and LOW when pressed
  333.   }
  334.  
  335.  
  336. void SetMacroProfile(int TRIGGER_INPUT, int i, int PROFILE_NEXT, int PROFILE_BACK)
  337.   {
  338.  
  339.    
  340.     if (TRIGGER_INPUT > i && PROFILE_NEXT == LOW && Macro_Profile < Macro_Profiles)
  341.       {
  342.         Macro_Profile++;
  343.        
  344.         if (Macro_Profile == Macro_Profiles)
  345.           {
  346.             Macro_Profile = 0;
  347.           }
  348.        
  349.         PrintMacroStats();
  350.        
  351.         delay(300);
  352.       }
  353.      
  354.     if (TRIGGER_INPUT > i && PROFILE_BACK == LOW && Macro_Profile > 0)
  355.       {
  356.         Macro_Profile--;
  357.        
  358.         PrintMacroStats();
  359.        
  360.         delay(300);
  361.       }
  362.   }
  363.  
  364.  
  365.  
  366. void Macro(int TRIGGER_INPUT, int i, int BUTTON_INPUT, int BUTTON_OUTPUT, int NUM_LOOPS)
  367.   {
  368.     int LOOP = 0;  
  369.    
  370.     if (TRIGGER_INPUT > i && BUTTON_INPUT == LOW)
  371.       {
  372.         if (NUM_LOOPS == -1)
  373.         {
  374.           ButtonOutput(BUTTON_OUTPUT);
  375.         }
  376.        
  377.         if (NUM_LOOPS >= 1 && LOOP <= NUM_LOOPS)
  378.         {
  379.           ButtonOutput(BUTTON_OUTPUT);
  380.          
  381.           LOOP++;
  382.         }
  383.       }
  384.   }
  385.  
  386.  
  387.  
  388. void PrintMacroStats()
  389.   {
  390.     Serial.print("Macro Profile ");
  391.       Serial.println(Macro_Profile);
  392.      
  393.       Serial.print("\t");
  394.      
  395.     Serial.print("Delay_In = ");
  396.       Serial.println(MACRO_DELAY[Macro_Profile][0]);
  397.      
  398.       Serial.print("\t");
  399.      
  400.     Serial.print("Delay_Out = ");
  401.       Serial.println(MACRO_DELAY[Macro_Profile][1]);
  402.   }
  403.  
  404.  
  405.  
  406. void ButtonOutput(int BUTTON_OUTPUT)
  407.   {
  408.     digitalWrite(BUTTON_OUTPUT, HIGH);
  409.       delay(MACRO_DELAY[Macro_Profile][0]);
  410.     digitalWrite(BUTTON_OUTPUT, LOW);
  411.       delay(MACRO_DELAY[Macro_Profile][1]);
  412.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement