uint32_t Adafruit_NFCShield_I2C::targetTxRx(char *DataOut,char *DataIn)
{
    ///////////////////////////////////// Receiving from Initiator ///////////////////////////
    pn532_packetbuffer[0] = PN532_COMMAND_TGGETDATA;
    if (! sendCommandCheckAck(pn532_packetbuffer, 1))
        return false;
    
    // read data packet
    wirereaddata(pn532_packetbuffer, 18+6);
    
#ifdef PN532DEBUG
    Serial.println();
    // check the response
    Serial.println("TARGET_RX");
    for(uint8_t i=0;i<18+6;i++)
    {
        Serial.print(pn532_packetbuffer[i], HEX); Serial.print(" ");
    }
#endif
    
    for(uint8_t iter=8;iter<(8+16);iter++)
    {
        DataIn[iter-8] = pn532_packetbuffer[iter]; //data received from initiator
    }
    
    ///////////////////////////////////// Sending to Initiator ///////////////////////////
    if(pn532_packetbuffer[7] == 0x00) //If no errors in receiving, send data.
    {
        pn532_packetbuffer[0] = PN532_COMMAND_TGSETDATA;
        for(uint8_t iter=(1+0);iter<(1+16);iter++)
        {
            pn532_packetbuffer[iter] = DataOut[iter-1]; //pack the data to send to target
        }
        
        if (! sendCommandCheckAck(pn532_packetbuffer, 17))
            return false;
        
        // read data packet
        wirereaddata(pn532_packetbuffer, 2+6);
        
#ifdef PN532DEBUG
        Serial.println();
        // check the response
        Serial.println("TARGET_TX");
        for(uint8_t i=0;i<2+6;i++)
        {
            Serial.print(pn532_packetbuffer[i], HEX); Serial.print(" ");
        }
#endif
        
        return (pn532_packetbuffer[7] == 0x00); //No error
    }
    
}
uint32_t Adafruit_NFCShield_I2C::initiatorTxRx(char *DataOut,char *DataIn)
{
    pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
    pn532_packetbuffer[1] = 0x01; //Target 01
    
    for(uint8_t iter=(2+0);iter<(2+16);iter++)
    {
        pn532_packetbuffer[iter] = DataOut[iter-2]; //pack the data to send to target
    }
    
    if (! sendCommandCheckAck(pn532_packetbuffer, 18))
        return false;
    
    // read data packet
    wirereaddata(pn532_packetbuffer, 18+6);
    
#ifdef PN532DEBUG
    Serial.println();
    // check the response
    Serial.println("INITIATOR_TXRX");
    for(uint8_t i=0;i<18+6;i++)
    {
        Serial.print(pn532_packetbuffer[i], HEX); Serial.print(" ");
    }
#endif
    
    for(uint8_t iter=8;iter<(8+16);iter++)
    {
        DataIn[iter-8] = pn532_packetbuffer[iter]; //data received from target
    }
    
    return (pn532_packetbuffer[7] == 0x00); //No error
}
uint32_t Adafruit_NFCShield_I2C::configurePeerAsInitiator(uint8_t baudrate /* Any number from 0-2. 0 for 106kbps or 1 for 201kbps or 2 for 424kbps */) {
    
    pn532_packetbuffer[0] = PN532_COMMAND_INJUMPFORDEP;
    pn532_packetbuffer[1] = 0x01; //Active Mode
    pn532_packetbuffer[2] = baudrate; // Use 1 or 2. //0 i.e 106kps is not supported yet
    pn532_packetbuffer[3] = 0x01; //Indicates Optional Payload is present
    
    //Polling request payload
    pn532_packetbuffer[4] = 0x00;
    pn532_packetbuffer[5] = 0xFF;
    pn532_packetbuffer[6] = 0xFF;
    pn532_packetbuffer[7] = 0x00;
    pn532_packetbuffer[8] = 0x00;
    
    if (! sendCommandCheckAck(pn532_packetbuffer, 9))
        return false;
    
    // read data packet
    wirereaddata(pn532_packetbuffer, 19+6);
    
#ifdef PN532DEBUG
    Serial.println();
    // check the response
    Serial.println("PEER_INITIATOR");
    for(uint8_t i=0;i<19+6;i++)
    {
        Serial.print(pn532_packetbuffer[i], HEX); Serial.print(" ");
    }
#endif
    
    return (pn532_packetbuffer[7] == 0x00); //No error
    
}
uint32_t Adafruit_NFCShield_I2C::getFirmwareVersion(void) {
  uint32_t response;

  pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;

  if (! sendCommandCheckAck(pn532_packetbuffer, 1))
    return 0;
	
  // read data packet
  wirereaddata(pn532_packetbuffer, 12);
  
  // check some basic stuff
  if (0 != strncmp((char *)pn532_packetbuffer, (char *)pn532response_firmwarevers, 6)) {
    #ifdef PN532DEBUG
    Serial.println("Firmware doesn't match!");
	#endif
    return 0;
  }
  
  response = pn532_packetbuffer[7];
  response <<= 8;
  response |= pn532_packetbuffer[8];
  response <<= 8;
  response |= pn532_packetbuffer[9];
  response <<= 8;
  response |= pn532_packetbuffer[10];

  return response;
}
boolean Adafruit_NFCShield_I2C::readackframe(void) {
  uint8_t ackbuff[6];
  
  wirereaddata(ackbuff, 6);
    
  return (0 == strncmp((char *)ackbuff, (char *)pn532ack, 6));
}
uint32_t Adafruit_NFCShield_I2C::readMemoryBlock(uint8_t cardnumber /*1 or 2*/,uint8_t blockaddress /*0 to 63*/, uint8_t * block) {
    pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
    pn532_packetbuffer[1] = cardnumber;  // either card 1 or 2 (tested for card 1)
    pn532_packetbuffer[2] = MIFARE_CMD_READ;
    pn532_packetbuffer[3] = blockaddress; //This address can be 0-63 for MIFARE 1K card
    
    if (! sendCommandCheckAck(pn532_packetbuffer, 4))
        return false;
    
    // read data packet
    wirereaddata(pn532_packetbuffer, 18+6);
    // check some basic stuff
#ifdef PN532DEBUG
    Serial.println("READ");
#endif
    for(uint8_t i=8;i<18+6;i++)
    {
        block[i-8] = pn532_packetbuffer[i];
#ifdef PN532DEBUG
        Serial.print(pn532_packetbuffer[i], HEX); Serial.print(" ");
#endif
    }
    if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00))
    {
        return true; //read successful
    }
    else
        
    {
        return false;
    }
    
}
uint8_t Adafruit_NFCShield_I2C::mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data)
{
  #ifdef MIFAREDEBUG
  Serial.print("Trying to write 16 bytes to block ");Serial.println(blockNumber);
  #endif
  
  /* Prepare the first command */
  pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
  pn532_packetbuffer[1] = 1;                      /* Card number */
  pn532_packetbuffer[2] = MIFARE_CMD_WRITE;       /* Mifare Write command = 0xA0 */
  pn532_packetbuffer[3] = blockNumber;            /* Block Number (0..63 for 1K, 0..255 for 4K) */
  memcpy (pn532_packetbuffer+4, data, 16);          /* Data Payload */

  /* Send the command */
  if (! sendCommandCheckAck(pn532_packetbuffer, 20))
  {
    #ifdef MIFAREDEBUG
    Serial.println("Failed to receive ACK for write command");
    #endif
    return 0;
  }  
  delay(10);
  
  /* Read the response packet */
  wirereaddata(pn532_packetbuffer, 26);

  return 1;  
}
boolean Adafruit_NFCShield_I2C::writeGPIO(uint8_t pinstate) {
  uint8_t errorbit;

  // Make sure pinstate does not try to toggle P32 or P34
  pinstate |= (1 << PN532_GPIO_P32) | (1 << PN532_GPIO_P34);
  
  // Fill command buffer
  pn532_packetbuffer[0] = PN532_COMMAND_WRITEGPIO;
  pn532_packetbuffer[1] = PN532_GPIO_VALIDATIONBIT | pinstate;  // P3 Pins
  pn532_packetbuffer[2] = 0x00;    // P7 GPIO Pins (not used ... taken by I2C)

  #ifdef PN532DEBUG
    Serial.print("Writing P3 GPIO: "); Serial.println(pn532_packetbuffer[1], HEX);
  #endif

  // Send the WRITEGPIO command (0x0E)  
  if (! sendCommandCheckAck(pn532_packetbuffer, 3))
    return 0x0;
  
  // Read response packet (00 00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0F) DATACHECKSUM)
  wirereaddata(pn532_packetbuffer, 8);

  #ifdef PN532DEBUG
    Serial.print("Received: ");
    PrintHex(pn532_packetbuffer, 8);
    Serial.println("");
  #endif  
  
  return  (pn532_packetbuffer[6] == 0x0F);
}
//Do not write to Sector Trailer Block unless you know what you are doing.
uint32_t Adafruit_NFCShield_I2C::writeMemoryBlock(uint8_t cardnumber /*1 or 2*/,uint8_t blockaddress /*0 to 63*/, uint8_t * block) {
    pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
    pn532_packetbuffer[1] = cardnumber;  // either card 1 or 2 (tested for card 1)
    pn532_packetbuffer[2] = MIFARE_CMD_WRITE;
    pn532_packetbuffer[3] = blockaddress;
    
    for(uint8_t byte=0; byte <16; byte++)
    {
        pn532_packetbuffer[4+byte] = block[byte];
    }
    
    if (! sendCommandCheckAck(pn532_packetbuffer, 20))
        return false;
    // read data packet
    wirereaddata(pn532_packetbuffer, 2+6);
    
#ifdef PN532DEBUG
    // check some basic stuff
    Serial.println("WRITE");
    for(uint8_t i=0;i<2+6;i++)
    {
        Serial.print(pn532_packetbuffer[i], HEX); Serial.println(" ");
    }
#endif
    
    if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00))
    {
        return true; //write successful
    }
    else
    {
        return false;
    }
}
boolean Adafruit_NFCShield_I2C::SAMConfig(void) {
  pn532_packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION;
  pn532_packetbuffer[1] = 0x01; // normal mode;
  pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second
  pn532_packetbuffer[3] = 0x01; // use IRQ pin!
  
  if (! sendCommandCheckAck(pn532_packetbuffer, 4))
     return false;

  // read data packet
  wirereaddata(pn532_packetbuffer, 8);
  
  return  (pn532_packetbuffer[6] == 0x15);
}
uint32_t Adafruit_NFCShield_I2C::configurePeerAsTarget() {
    
    byte pbuffer[38] =      { PN532_COMMAND_TGINITASTARGET,
        0x00,
        0x08, 0x00, //SENS_RES
        0x12, 0x34, 0x56, //NFCID1
        0x60, //SEL_RES
        
        0x01, 0xFE, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, // NFCID2t
        0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, //PAD
        0xFF, 0xFF, //System Code
        
        0xAA, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, //NFCID3t: Change this to desired value
        
        0x00,  //Length of general bytes
        0x00  //Length of historical bytes
    };
    

    
    for(uint8_t iter=0;iter<38;iter++)
    {
        pn532_packetbuffer[iter] = pbuffer[iter];
    }
    
    if (! sendCommandCheckAck(pn532_packetbuffer, 38)){
#ifdef PN532DEBUG
        Serial.println("failed to recieve send ACK");
#endif
        return 0;
    }
    delay(6000);
    Serial.print("___");
    // read data packet
    wirereaddata(pn532_packetbuffer, 18+6);
    Serial.print("___");    
#ifdef PN532DEBUG
//    Serial.println();
    // check some basic stuff
    
//    Serial.println("PEER_TARGET");
//    for(uint8_t i=0;i<18+6;i++)
//    {
//        Serial.print(pn532_packetbuffer[i], HEX); Serial.println(" ");
//    }
#endif
    
    return (pn532_packetbuffer[23] == 0x00); //No error as it received all response
}
uint8_t Adafruit_NFCShield_I2C::mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData)
{
  uint8_t len;
  uint8_t i;
  
  // Hang on to the key and uid data
  memcpy (_key, keyData, 6); 
  memcpy (_uid, uid, uidLen); 
  _uidLen = uidLen;  

  #ifdef MIFAREDEBUG
  Serial.print("Trying to authenticate card ");
  Adafruit_NFCShield_I2C::PrintHex(_uid, _uidLen);
  Serial.print("Using authentication KEY ");Serial.print(keyNumber ? 'B' : 'A');Serial.print(": ");
  Adafruit_NFCShield_I2C::PrintHex(_key, 6);
  #endif
  
  // Prepare the authentication command //
  pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;   /* Data Exchange Header */
  pn532_packetbuffer[1] = 1;                              /* Max card numbers */
  pn532_packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_B : MIFARE_CMD_AUTH_A;
  pn532_packetbuffer[3] = blockNumber;                    /* Block Number (1K = 0..63, 4K = 0..255 */
  memcpy (pn532_packetbuffer+4, _key, 6);
  for (i = 0; i < _uidLen; i++)
  {
    pn532_packetbuffer[10+i] = _uid[i];                /* 4 byte card ID */
  }

  if (! sendCommandCheckAck(pn532_packetbuffer, 10+_uidLen))
    return 0;

  // Read the response packet
  wirereaddata(pn532_packetbuffer, 12);
  
  // Check if the response is valid and we are authenticated???
  // for an auth success it should be bytes 5-7: 0xD5 0x41 0x00
  // Mifare auth error is technically byte 7: 0x14 but anything other and 0x00 is not good
  if (pn532_packetbuffer[7] != 0x00)
  {
    #ifdef PN532DEBUG
    Serial.print("Authentification failed: ");
    Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 12);
    #endif
    return 0;
  }  
  
  return 1;
}
uint8_t Adafruit_NFCShield_I2C::mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data)
{
  #ifdef MIFAREDEBUG
  Serial.print("Trying to read 16 bytes from block ");Serial.println(blockNumber);
  #endif
  
  /* Prepare the command */
  pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
  pn532_packetbuffer[1] = 1;                      /* Card number */
  pn532_packetbuffer[2] = MIFARE_CMD_READ;        /* Mifare Read command = 0x30 */
  pn532_packetbuffer[3] = blockNumber;            /* Block Number (0..63 for 1K, 0..255 for 4K) */

  /* Send the command */
  if (! sendCommandCheckAck(pn532_packetbuffer, 4))
  {
    #ifdef MIFAREDEBUG
    Serial.println("Failed to receive ACK for read command");
    #endif
    return 0;
  }

  /* Read the response packet */
  wirereaddata(pn532_packetbuffer, 26);

  /* If byte 8 isn't 0x00 we probably have an error */
  if (pn532_packetbuffer[7] != 0x00)
  {
    #ifdef MIFAREDEBUG
    Serial.println("Unexpected response");
    Adafruit_NFCShield_I2C::PrintHexChar(pn532_packetbuffer, 26);
    #endif
    return 0;
  }
    
  /* Copy the 16 data bytes to the output buffer        */
  /* Block content starts at byte 9 of a valid response */
  memcpy (data, pn532_packetbuffer+8, 16);

  /* Display data for debug if requested */
  #ifdef MIFAREDEBUG
    Serial.print("Block ");
    Serial.println(blockNumber);
    Adafruit_NFCShield_I2C::PrintHexChar(data, 16);
  #endif

  return 1;  
}
uint8_t Adafruit_NFCShield_I2C::readGPIO(void) {
  pn532_packetbuffer[0] = PN532_COMMAND_READGPIO;

  // Send the READGPIO command (0x0C)  
  if (! sendCommandCheckAck(pn532_packetbuffer, 1))
    return 0x0;
  
  // Read response packet (00 00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0D) P3 P7 IO1 DATACHECKSUM)
  wirereaddata(pn532_packetbuffer, 11);

  /* READGPIO response should be in the following format:
  
    byte            Description
    -------------   ------------------------------------------
    b0..6           Frame header and preamble
    b7              P3 GPIO Pins
    b8              P7 GPIO Pins (not used ... taken by I2C)
    b9              Interface Mode Pins (not used ... bus select pins) 
    b10             checksum */
  
  #ifdef PN532DEBUG
    Serial.print("Received: ");
    PrintHex(pn532_packetbuffer, 11);
    Serial.println("");
    Serial.print("P3 GPIO: 0x"); Serial.println(pn532_packetbuffer[7], HEX);
    Serial.print("P7 GPIO: 0x"); Serial.println(pn532_packetbuffer[8], HEX);
    Serial.print("IO GPIO: 0x"); Serial.println(pn532_packetbuffer[9], HEX);
    // Note: You can use the IO GPIO value to detect the serial bus being used
    switch(pn532_packetbuffer[9])
    {
      case 0x00:    // Using UART
        Serial.println("Using UART (IO = 0x00)");
        break;
      case 0x01:    // Using I2C 
        Serial.println("Using I2C (IO = 0x01)");
        break;
      case 0x02:    // Using I2C
        Serial.println("Using I2C (IO = 0x02)");
        break;
    }
  #endif

  return pn532_packetbuffer[6];
}
uint8_t Adafruit_NFCShield_I2C::mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData)
{
  uint8_t len;
  uint8_t i;
  
  // Hang on to the key and uid data
  memcpy (_key, keyData, 6); 
  memcpy (_uid, uid, uidLen); 
  _uidLen = uidLen;  

  #ifdef MIFAREDEBUG
  Serial.print("Trying to authenticate card ");
  Adafruit_NFCShield_I2C::PrintHex(_uid, _uidLen);
  Serial.print("Using authentication KEY ");Serial.print(keyNumber ? 'B' : 'A');Serial.print(": ");
  Adafruit_NFCShield_I2C::PrintHex(_key, 6);
  #endif
  
  // Prepare the authentication command //
  pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;   /* Data Exchange Header */
  pn532_packetbuffer[1] = 1;                              /* Max card numbers */
  pn532_packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_A : MIFARE_CMD_AUTH_B;
  pn532_packetbuffer[3] = blockNumber;                    /* Block Number (1K = 0..63, 4K = 0..255 */
  memcpy (pn532_packetbuffer+4, _key, 6);
  for (i = 0; i < _uidLen; i++)
  {
    pn532_packetbuffer[10+i] = _uid[i];                /* 4 byte card ID */
  }

  if (! sendCommandCheckAck(pn532_packetbuffer, 10+_uidLen))
    return 0;

  // Read the response packet
  wirereaddata(pn532_packetbuffer, 12);
  // ToDo: How to check if the response is valid and we are authenticated???
  // #ifdef PN532DEBUG
  //   Serial.println("Authentification failed%s");
  // #endif
  // return 0;

  return 1;
}
uint8_t Adafruit_NFCShield_I2C::mifareultralight_ReadPage (uint8_t page, uint8_t * buffer)
{
  if (page >= 64)
  {
    #ifdef MIFAREDEBUG
    Serial.println("Page value out of range");
    #endif
    return 0;
  }

  #ifdef MIFAREDEBUG
    Serial.print("Reading page ");Serial.println(page);
  #endif

  /* Prepare the command */
  pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
  pn532_packetbuffer[1] = 1;                   /* Card number */
  pn532_packetbuffer[2] = MIFARE_CMD_READ;     /* Mifare Read command = 0x30 */
  pn532_packetbuffer[3] = page;                /* Page Number (0..63 in most cases) */

  /* Send the command */
  if (! sendCommandCheckAck(pn532_packetbuffer, 4))
  {
    #ifdef MIFAREDEBUG
    Serial.println("Failed to receive ACK for write command");
    #endif
    return 0;
  }
  
  /* Read the response packet */
  wirereaddata(pn532_packetbuffer, 26);
  #ifdef MIFAREDEBUG
    Serial.println("Received: ");
    Adafruit_NFCShield_I2C::PrintHexChar(pn532_packetbuffer, 26);
  #endif

  /* If byte 8 isn't 0x00 we probably have an error */
  if (pn532_packetbuffer[7] == 0x00)
  {
    /* Copy the 4 data bytes to the output buffer         */
    /* Block content starts at byte 9 of a valid response */
    /* Note that the command actually reads 16 byte or 4  */
    /* pages at a time ... we simply discard the last 12  */
    /* bytes                                              */
    memcpy (buffer, pn532_packetbuffer+8, 4);
  }
  else
  {
    #ifdef MIFAREDEBUG
      Serial.println("Unexpected response reading block: ");
      Adafruit_NFCShield_I2C::PrintHexChar(pn532_packetbuffer, 26);
    #endif
    return 0;
  }

  /* Display data for debug if requested */
  #ifdef MIFAREDEBUG
    Serial.print("Page ");Serial.print(page);Serial.println(":");
    Adafruit_NFCShield_I2C::PrintHexChar(buffer, 4);
  #endif

  // Return OK signal
  return 1;
}
boolean Adafruit_NFCShield_I2C::readPassiveTargetID(uint8_t cardbaudrate, uint8_t * uid, uint8_t * uidLength) {
  pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
  pn532_packetbuffer[1] = 1;  // max 1 cards at once (we can set this to 2 later)
  pn532_packetbuffer[2] = cardbaudrate;
  
  if (! sendCommandCheckAck(pn532_packetbuffer, 3))
  {
    #ifdef PN532DEBUG
	Serial.println("No card(s) read");
	#endif
    return 0x0;  // no cards read
  }
  
  // Wait for a card to enter the field
  uint8_t status = PN532_I2C_BUSY;
  #ifdef PN532DEBUG
  Serial.println("Waiting for IRQ (indicates card presence)");
  #endif
  while (wirereadstatus() != PN532_I2C_READY)
  {
	delay(10);
  }

  #ifdef PN532DEBUG
  Serial.println("Found a card"); 
  #endif
 
  // read data packet
  wirereaddata(pn532_packetbuffer, 20);
  
  // check some basic stuff
  /* ISO14443A card response should be in the following format:
  
    byte            Description
    -------------   ------------------------------------------
    b0..6           Frame header and preamble
    b7              Tags Found
    b8              Tag Number (only one used in this example)
    b9..10          SENS_RES
    b11             SEL_RES
    b12             NFCID Length
    b13..NFCIDLen   NFCID                                      */
  
#ifdef MIFAREDEBUG
    Serial.print("Found "); Serial.print(pn532_packetbuffer[7], DEC); Serial.println(" tags");
#endif
  if (pn532_packetbuffer[7] != 1) 
    return 0;
    
  uint16_t sens_res = pn532_packetbuffer[9];
  sens_res <<= 8;
  sens_res |= pn532_packetbuffer[10];
#ifdef MIFAREDEBUG
    Serial.print("ATQA: 0x");  Serial.println(sens_res, HEX); 
    Serial.print("SAK: 0x");  Serial.println(pn532_packetbuffer[11], HEX); 
#endif
  
  /* Card appears to be Mifare Classic */
  *uidLength = pn532_packetbuffer[12];
#ifdef MIFAREDEBUG
    Serial.print("UID:"); 
#endif
  for (uint8_t i=0; i < pn532_packetbuffer[12]; i++) 
  {
    uid[i] = pn532_packetbuffer[13+i];
#ifdef MIFAREDEBUG
      Serial.print(" 0x");Serial.print(uid[i], HEX); 
#endif
  }
#ifdef MIFAREDEBUG
    Serial.println();
#endif

  return 1;
}
boolean Adafruit_NFCShield_I2C::inListPassiveTarget() {
  pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
  pn532_packetbuffer[1] = 1;
  pn532_packetbuffer[2] = 0;
  
  #ifdef PN532DEBUG 
    Serial.print("About to inList passive target");
  #endif

  if (!sendCommandCheckAck(pn532_packetbuffer,3,1000)) {
    #ifdef PN532DEBUG
      Serial.println("Could not send inlist message");
    #endif
    return false;
  }

  if (!waitUntilReady(30000)) {
    return false;
  }

  wirereaddata(pn532_packetbuffer,sizeof(pn532_packetbuffer));
  
  if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && pn532_packetbuffer[2] == 0xff) {
    uint8_t length = pn532_packetbuffer[3];
    if (pn532_packetbuffer[4]!=(uint8_t)(~length+1)) {
      #ifdef PN532DEBUG
        Serial.println("Length check invalid");
        Serial.println(length,HEX);
        Serial.println((~length)+1,HEX);
      #endif
      return false;
    }
    if (pn532_packetbuffer[5]==PN532_PN532TOHOST && pn532_packetbuffer[6]==PN532_RESPONSE_INLISTPASSIVETARGET) {
      if (pn532_packetbuffer[7] != 1) {
        #ifdef PN532DEBUG
        Serial.println("Unhandled number of targets inlisted");
        #endif
        Serial.println("Number of tags inlisted:");
        Serial.println(pn532_packetbuffer[7]);
        return false;
      }
      
      inListedTag = pn532_packetbuffer[8];
      Serial.print("Tag number: ");
      Serial.println(inListedTag);
      
      return true;
    } else {
      #ifdef PN532DEBUG
        Serial.print("Unexpected response to inlist passive host");
      #endif
      return false;
    } 
  } 
  else {
    #ifdef PN532DEBUG
      Serial.println("Preamble missing");
    #endif
    return false;
  }

  return true;
}
boolean Adafruit_NFCShield_I2C::inDataExchange(uint8_t * send, uint8_t sendLength, uint8_t * response, uint8_t * responseLength) {
  if (sendLength > PN532_PACKBUFFSIZ -2) {
    #ifdef PN532DEBUG
      Serial.println("APDU length too long for packet buffer");
    #endif
    return false;
  }
  uint8_t i;
  
  pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE;
  pn532_packetbuffer[1] = inListedTag;
  for (i=0; i<sendLength; ++i) {
    pn532_packetbuffer[i+2] = send[i];
  }
  
  if (!sendCommandCheckAck(pn532_packetbuffer,sendLength+2,1000)) {
    #ifdef PN532DEBUG
      Serial.println("Could not send ADPU");
    #endif
    return false;
  }

  if (!waitUntilReady(1000)) {
    #ifdef PN532DEBUG
      Serial.println("Response never received for ADPU...");
    #endif
    return false;
  }

  wirereaddata(pn532_packetbuffer,sizeof(pn532_packetbuffer));
  
  if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && pn532_packetbuffer[2] == 0xff) {
    uint8_t length = pn532_packetbuffer[3];
    if (pn532_packetbuffer[4]!=(uint8_t)(~length+1)) {
      #ifdef PN532DEBUG
        Serial.println("Length check invalid");
        Serial.println(length,HEX);
        Serial.println((~length)+1,HEX);
      #endif
      return false;
    }
    if (pn532_packetbuffer[5]==PN532_PN532TOHOST && pn532_packetbuffer[6]==PN532_RESPONSE_INDATAEXCHANGE) {
      if ((pn532_packetbuffer[7] & 0x3f)!=0) {
        #ifdef PN532DEBUG
          Serial.println("Status code indicates an error");
        #endif
        return false;
      }
      
      length -= 3;
      
      if (length > *responseLength) {
        length = *responseLength; // silent truncation...
      }
      
      for (i=0; i<length; ++i) {
        response[i] = pn532_packetbuffer[8+i];
      }
      *responseLength = length;
      
      return true;
    } 
    else {
      Serial.print("Don't know how to handle this command: ");
      Serial.println(pn532_packetbuffer[6],HEX);
      return false;
    } 
  } 
  else {
    Serial.println("Preamble missing");
    return false;
  }
}