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];
}

Re: Can someone explain this joytsick code?
« Reply #1 on: March 22, 2017, 03:51:01 PM »
Missed this- for future reference, the joystick board has a microcontroller that converts the joystick input into 10 bit values. The TinyDuino reads these values over I2C in 8 bit chunks, the fifth of which contains the least significant 2 bits of the 10 bit values. It then adds that to the other data, eventually assigning the 10 bit values to RX, RY, etc. The range of the joystick does need some calibration, they are not very accurate.