Advertisement
Guest User

Untitled

a guest
Nov 6th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Note: 2018 default plus shield drop with D-pad and R analog lightshield only
  2. //This section is for user inputted data to make the code personalized to the particular controller
  3.  
  4. //enter values of cardinals here (be cognizant of signs)
  5. #define n__notch_x_value  0.0000
  6. #define n__notch_y_value  1.0000
  7.  
  8. #define e__notch_x_value  1.0000
  9. #define e__notch_y_value  0.0000
  10.  
  11. #define s__notch_x_value  0.0000
  12. #define s__notch_y_value -1.0000
  13.  
  14. #define w__notch_x_value -1.0000
  15. #define w__notch_y_value  0.0000
  16.  
  17. //enter values for shield drop notches here
  18. #define sw_notch_x_value -0.6625
  19. #define sw_notch_y_value -0.7375
  20.  
  21. #define se_notch_x_value  0.6625
  22. #define se_notch_y_value -0.7350
  23.  
  24. /*
  25. once you have installed the mod, upload the code and go to live analog inputs display
  26. hold dpad down for 3 seconds to turn off the mod then record the notch values
  27. doing the steps in this order ensures the most accurate calibration
  28.  
  29. to return to a stock controller hold dpad down for 3 seconds
  30. to set to dolphin mode hold dpad up for 3 seconds
  31. */
  32.  
  33. #include "Nintendo.h"
  34. CGamecubeController controller(0); //sets RX0 on arduino to read data from controller
  35. CGamecubeConsole console(1);       //sets TX1 on arduino to write data to console
  36. Gamecube_Report_t gcc;             //structure for controller state
  37. Gamecube_Data_t data;
  38. struct{int8_t ax, ay, cx, cy; uint8_t l, r;}ini;
  39. bool shield, dolphin=0, off=0, cal=1, button;
  40. struct{float n, e, eh, el, s, w, se, sw;}g;
  41. struct{uint8_t db:5, cr:5;}buf;
  42. struct{bool u, d, l, r;}perf;
  43. struct{uint8_t l, r;}ls;
  44. int8_t ax, ay, cx, cy;
  45. float r, deg, cr;
  46. uint8_t cycles=3;
  47. uint16_t mode;
  48. uint32_t n, c;
  49.  
  50. void mods(){       //to remove mods delete any lines that you do not want here
  51.   //anglesfixed();   //reallocates angles properly based on the given cardinal notches
  52.   //perfectangles(); //reduces deadzone of cardinals and gives steepest/shallowest angles when on or near the gate
  53.   //maxvectors();    //snaps sufficiently high cardinal inputs to vectors of 1.0 magnitude of analog stick and c stick
  54.   shielddrops();   //gives an 8 degree range of shield dropping centered on SW and SE gates
  55.   backdash();      //fixes dashback by imposing a 1 frame buffer upon tilt turn values
  56.   backdashooc();   //allows more leniency for dash back out of crouch
  57.   dolphinfix();    //ensures close to 0 values are reported as 0 on the sticks to fix dolphin calibration and fixes poll speed issues
  58.   sdbutton();      //maps a digital button to shield drop input
  59.   rlightshield();
  60.   nocode();        //function to disable all code if dpad down is held for 3 seconds (unplug controller to reactivate)
  61. } //more mods to come!
  62.  
  63. void anglesfixed(){
  64.   if(deg>g.el&&deg<g.n)      deg = map(deg, g.el, g.n,    0,  90);
  65.   else if(deg>g.n&&deg<g.w)  deg = map(deg, g.n,  g.w,   90, 180);
  66.   else if(deg>g.w&&deg<g.s)  deg = map(deg, g.w,  g.s,  180, 270);
  67.   else if(deg>g.s&&deg<g.eh) deg = map(deg, g.s,  g.eh, 270, 360);
  68.   else                  deg = map(deg-g.eh, g.el, g.n,    0,  90);
  69.   perf.u = deg> 73 && deg<107; perf.d = deg>254 && deg<287;
  70.   perf.l = deg>163 && deg<197; perf.r = deg>343 || deg< 17;
  71.   gcc.xAxis=128+r*cos(deg/57.3);  gcc.yAxis=128+r*sin(deg/57.3);  
  72. }
  73.  
  74. void perfectangles(){
  75.   if(r>75){
  76.     if(perf.u){gcc.xAxis = (ax>0)?151:105; gcc.yAxis = 204;}
  77.     if(perf.r){gcc.yAxis = (ay>0)?151:105; gcc.xAxis = 204;}
  78.     if(perf.d){gcc.xAxis = (ax>0)?151:105; gcc.yAxis =  52;}
  79.     if(perf.l){gcc.yAxis = (ay>0)?151:105; gcc.xAxis =  52;}
  80.   }
  81. }
  82.  
  83. void maxvectors(){
  84.   if(r>75){
  85.     if(arc(g.n)<6){gcc.xAxis = 128; gcc.yAxis = 255;}
  86.     if(arc(g.e)<6){gcc.xAxis = 255; gcc.yAxis = 128;}
  87.     if(arc(g.s)<6){gcc.xAxis = 128; gcc.yAxis =   1;}
  88.     if(arc(g.w)<6){gcc.xAxis =   1; gcc.yAxis = 128;}
  89.   }
  90.   if(abs(cx)>75&&abs(cy)<23){gcc.cxAxis = (cx>0)?255:1; gcc.cyAxis = 128;}
  91.   if(abs(cy)>75&&abs(cx)<23){gcc.cyAxis = (cy>0)?255:1; gcc.cxAxis = 128;}
  92. }
  93.  
  94. void shielddrops(){
  95.   shield = gcc.l||gcc.r||ls.l>74||ls.r>74||gcc.z;
  96.   if(shield){
  97.     if(ay<0&&r>72){
  98.       if(arc(g.sw)<4){gcc.yAxis = 73; gcc.xAxis =  71;}
  99.       if(arc(g.se)<4){gcc.yAxis = 73; gcc.xAxis = 185;}
  100.     }
  101.   }
  102. }
  103.  
  104. void backdash(){
  105.   button=gcc.a||gcc.b||gcc.x||gcc.y||gcc.z||gcc.l||gcc.r||ls.l>74||ls.r>74;
  106.   if(abs(ay)<23&&!button){ if(abs(ax)<23)buf.db = cycles;
  107.     if(buf.db>0){buf.db--; if(abs(ax)<64) gcc.xAxis = 128+ax*(abs(ax)<23);}
  108.   }else buf.db = 0;
  109. }
  110.  
  111. void backdashooc(){
  112.   if(ay<23){
  113.     if(ay<-49) buf.cr = cycles;
  114.     if(buf.cr>0){buf.cr--; if(ay>-50) gcc.yAxis = 78;}
  115.   }else buf.cr = 0;
  116. }
  117.  
  118. void rlightshield(){
  119.   if(ls.r>90) {gcc.right = 90;}
  120. }
  121.  
  122. void dolphinfix(){
  123.   if(r<8)         {gcc.xAxis  = 128; gcc.yAxis  = 128;}
  124.   if(mag(cx,cy)<8){gcc.cxAxis = 128; gcc.cyAxis = 128;}
  125.   if(gcc.dup&&mode<1500) dolphin = dolphin||(mode++>1000);
  126.   else mode = 0; cycles = 3 + (16*dolphin);
  127. }
  128.  
  129. void sdbutton(){
  130.   if(shield){
  131.     if(gcc.dpad>0) {gcc.yAxis = 73; if(ax>73)gcc.xAxis=180; if(ax<-73)gcc.xAxis=73;}
  132.   }
  133. }
  134.  
  135. void nocode(){
  136.   if(gcc.ddown){
  137.     if(n == 0) n = millis();
  138.     off = off||(millis()-n>500);
  139.   }else n = 0;
  140. }
  141.  
  142. void recalibrate(){
  143.   if(cal){ cal = gcc.x&&gcc.y&&gcc.start;
  144.     ini.ax = gcc.xAxis -128; ini.ay = gcc.yAxis -128; //gets offset from analog stick in nuetral  
  145.     ini.cx = gcc.cxAxis-128; ini.cy = gcc.cyAxis-128; //gets offset from c stick in nuetral
  146.     ini.l  = gcc.left;       ini.r  = gcc.right;      //gets offset from analog triggers in nuetral
  147.   }else if(gcc.x&&gcc.y&&gcc.start){
  148.     if(c == 0) c = millis();
  149.     cal = millis()-c>500;
  150.   }else c = 0;
  151. }
  152.  
  153. void calibration(){
  154.   ax = constrain(gcc.xAxis -128-ini.ax,-128,127); //offsets from nuetral position of analog stick x axis
  155.   ay = constrain(gcc.yAxis -128-ini.ay,-128,127); //offsets from nuetral position of analog stick y axis
  156.   cx = constrain(gcc.cxAxis-128-ini.cx,-128,127); //offsets from nuetral position of c stick x axis
  157.   cy = constrain(gcc.cyAxis-128-ini.cy,-128,127); //offsets from nuetral position of c stick y axis
  158.   r  = mag(ax, ay); deg  = ang(ax, ay);           //obtains polar coordinates for analog stick
  159.   cr = mag(cx, cy);                               //obtains magnitude of c stick value
  160.   ls.l = constrain(gcc.left -ini.l,0,255);        //fixes left trigger calibration
  161.   ls.r = constrain(gcc.right-ini.r,0,255);        //fixes right trigger calibration
  162.   gcc.left = ls.l; gcc.right = ls.r;              //sets proper analog shield values  
  163.   gcc.xAxis  = 128+ax; gcc.yAxis  = 128+ay;       //reports analog stick values
  164.   gcc.cxAxis = 128+cx; gcc.cyAxis = 128+cy;       //reports c stick values
  165.   recalibrate();                                  //allows holding x+y+start for 3 seconds to recalibrate
  166. }
  167.  
  168. float ang(float x, float y){return atan2(y,x)*57.3+360*(y<0);}        //returns angle in degrees when given x and y components
  169. float mag(char  x, char  y){return sqrt(sq(x)+sq(y));}                //returns vector magnitude when given x and y components
  170. bool  mid(float val, float n1, float n2){return val>n1&&val<n2;}      //returns whether val is between n1 and n2
  171. float arc(float val){return abs(180-abs(abs(deg-val)-180));}          //returns length of arc between the deg and val
  172. int   dis(float val){return abs(fmod(val,90)-90*(fmod(val,90)>45));}  //returns how far off the given angle is from a cardinal
  173. float map(long val, float in, float ix, float on, float ox){return (val-in)*(ox-on)/(ix-in)+on;}
  174.  
  175. void setup(){
  176.   gcc.origin=0;gcc.errlatch=0;gcc.high1=0;gcc.errstat=0; //init values
  177.   g.n  = ang(n__notch_x_value, n__notch_y_value);        //calculates angle of N notch
  178.   g.e  = ang(e__notch_x_value, e__notch_y_value);        //calculates angle of E notch
  179.   g.s  = ang(s__notch_x_value, s__notch_y_value);        //calculates angle of S notch
  180.   g.w  = ang(w__notch_x_value, w__notch_y_value);        //calculates angle of W notch
  181.   g.sw = ang(sw_notch_x_value, sw_notch_y_value);        //calculates angle of SW notch
  182.   g.se = ang(se_notch_x_value, se_notch_y_value);        //calculates angle of SE notch
  183.   g.el = g.e-360*(g.e>180); g.eh = g.e+360*(g.e<180);    //gets east gate in 2 notations
  184.   controller.read(); gcc = controller.getReport();       //reads controller once for calibration
  185.   recalibrate();                                         //calibrates the controller for initial plug in
  186. }
  187.  
  188. void loop(){
  189.   controller.read();                        //reads the controller
  190.   data = defaultGamecubeData;               //this line is necessary for proper rumble
  191.   gcc = controller.getReport();             //gets a report of the controller read
  192.   calibration(); recalibrate();             //fixes normal calibration and allows resetting with x+y+start
  193.   if(!off) mods();                          //implements all the mods (remove this line to unmod the controller)
  194.   data.report = gcc; console.write(data);   //sends controller data to the console
  195.   controller.setRumble(data.status.rumble); //allows for rumble
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement