TinyCircuits

Can someone explain this joytsick code?

w

Can someone explain this joytsick code?
« on: February 05, 2017, 12:14:05 AM »
Hey, I'm trying to understand what's going on in this joystick feedback function. I'm all new to this and would appreciate some annotation comments, inline. I'm ultimately trying to understand the X and Y ranges for the joystick and why he's doing what he's doing here. Thanks!

void getJoystick(){
  Wire.requestFrom(0x22,6);
  int data[4];
  for(int i=0;i<4;i++){
    data=Wire.read();
  }
  byte lsb=Wire.read();
  byte buttons=~Wire.read();
  leftButton=buttons&4;
  rightButton=buttons&8;
  for(int i=0;i<4;i++){
    data<<=2;
    data|= ((lsb>>(i*2))&3);
    data-=511;
  }
  RX=data[0];
  RY=-data[1];
  LX=-data[2];
  LY=data[3];
}