   Wire.write(deselectI2C, sizeof(deselectI2C));
   Wire.endTransmission();
   delay(1);
-  //readAndDisplay(3);  
-  
-  delay(1000);
+  //readAndDisplay(3); 
+ 
+  runMsg(); 
+  
+  delay(1000); // for debugging, separate into phases of read/process/wait
 }
 
 

#define PROTO_OVERHEAD 5
// 5 bytes for NDEF header (in URI format)
#define HEADER_LEN 5

  int msgStart = 1 + HEADER_LEN;
  int msgEnd   = 1 + HEADER_LEN + (len-HEADER_LEN);
  
  Wire.requestFrom(NFC_ADDR_7BIT, len+PROTO_OVERHEAD);
  idx = 0;
  while (Wire.available())
  {
    byte b = Wire.read();
    if (idx >= msgStart && idx <= msgEnd)
    {
  //    //Serial.write(b); //TODO process this character
    }
    idx++;
  }
  //Serial.println();  
  //readAndDisplay(len+PROTO_OVERHEAD);
  

  return wCrc;
}


/* This is an example of how to extract an NDEF text message.
 * The code calls startMsg() when it thinks it is receiving a message. 
 * This is the point to clear any buffers or reset any index counters.
 * Then it calls processMsg() for every byte received. You should
 * process the byte then increment your indexes.
 * Finally it calls endMsg() when finished.
 * You should now tidy anything up such as terminate buffers.
 * calling runMsg() should playback the sequence.
 */
 
/* This example stores delay lengths in a buffer. Odd indexes are the ON time in ms,
 * event indexes are the OFF time in ms. 0 marks the end of the buffer.
 * ASCII encoding is as follows:
 * '0' => don't use
 * '1' => 100ms
 * '2' => 200ms
 * ...
 * '9' => 900ms
 *
 * You can just about flash morse code using this.
 */
 
#define MSG_MAX 32
int msgIdx;
char msg[MSG_MAX+1];

void startMsg()
{
  msgIdx = 0;
}

void processMsg(byte b)
{
  char ch = (char) b;
  if (msgIdx<MSG_MAX)
  {
    msg[msgIdx++] = ch;
  }
}

void endMsg()
{
  msg[msgIdx] = 0; /* Make sure buffer is terminated */
}

void runMsg()
{
  for (int i=0; i<MSG_MAX; i++)
  {
    char ch = msg[i];
    if (ch == 0)
    {
      break; /* early terminated message */
    }
    if (ch < '1' || ch > '9')
    {
      ch = '1';
    }
    
    int time =  ((int)(ch - '0')) * 100;
    if ((i & 0x01) == 0) // odd message, turn LED on
    {
      digitalWrite(USER, HIGH);
    }
    else // even message, turn LED off
    {
      digitalWrite(USER, LOW);
    }
    delay(time);
  }
  // Always leave LED off at end
  digitalWrite(USER, LOW);
}

/* END OF FILE */

