Go Down

Topic: Need Help Understanding PS2X Library (Read 46 times) previous topic - next topic


I bought an Arduino Tank recently that uses the PS2 controller and receiver. It comes with the PS2X_lib Library and I couldn't get a good grasp of what some of the functions do. I have attached the code of the library.


In particular, how does unsigned char _gamepad_shiftinout(char byte)
works? And about the static variables that are assigned before the declaration of the functions, whats the reason of assigning these numbers to these variables?
Below are the relevant portions of the library:   

Code: [Select]
static byte enter_config[]={0x01,0x43,0x00,0x01,0x00};
static byte set_mode[]={0x01,0x44,0x00,0x01,0x03,0x00,0x00,0x00,0x00};
static byte set_bytes_large[]={0x01,0x4F,0x00,0xFF,0xFF,0x03,0x00,0x00,0x00};
static byte exit_config[]={0x01,0x43,0x00,0x00,0x5A,0x5A,0x5A,0x5A,0x5A};
static byte enable_rumble[]={0x01,0x4D,0x00,0x00,0x01};
static byte type_read[]={0x01,0x45,0x00,0x5A,0x5A,0x5A,0x5A,0x5A,0x5A};

Code: [Select]

unsigned char PS2X::_gamepad_shiftinout (char byte) {
   unsigned char tmp = 0;
   for(unsigned char i=0;i<8;i++) {
      if(CHK(byte,i)) CMD_SET();
      else CMD_CLR();

      //if(DAT_CHK()) SET(tmp,i);
      if(DAT_CHK()) bitSet(tmp,i);

   return tmp;

I want to understand this so that I can know how these other two functions work, void reconfig_gamepad() and void sendCommandString(). Below are the relevant portions of the library:

Code: [Select]
void PS2X::sendCommandString(byte string[], byte len) {
  byte temp[len];
  ATT_CLR(); // low enable joystick

  for (int y=0; y < len; y++)
    temp[y] = _gamepad_shiftinout(string[y]);

  ATT_SET(); //high disable joystick
  delay(read_delay); //wait a few

  Serial.println("OUT:IN Configure");
  for(int i=0; i<len; i++) {
    Serial.print(string[i], HEX);
    Serial.print(temp[i], HEX);
    Serial.print(" ");
  ATT_CLR(); // low enable joystick
  for (int y=0; y < len; y++)
  ATT_SET(); //high disable joystick
  delay(read_delay);                  //wait a few

Code: [Select]
void PS2X::reconfig_gamepad(){
  sendCommandString(enter_config, sizeof(enter_config));
  sendCommandString(set_mode, sizeof(set_mode));
  if (en_Rumble)
    sendCommandString(enable_rumble, sizeof(enable_rumble));
  if (en_Pressures)
    sendCommandString(set_bytes_large, sizeof(set_bytes_large));
  sendCommandString(exit_config, sizeof(exit_config));

Go Up