int led = 13;

void setup() {
  Serial.begin(9600);
  Serial.println(F("boot"));
  pinMode(led, OUTPUT);
//  for (int i=15; i<18; i++) {
//    pinMode(led, INPUT);
//  }
}

// RawHID packets are always 64 bytes
byte buffer[64];
elapsedMillis msUntilNextSend;
unsigned int packetCount = 0;

void loop() {
  int n;
  n = RawHID.recv(buffer, 0); // 0 timeout = do not wait
  if (n > 0) {
    // the computer sent a message.  Display the bits
    // of the first byte on pin 0 to 7.  Ignore the
    // other 63 bytes!
    Serial.print(F("Received packet, first byte: "));
    switch (buffer[0]) {
      case 'o':
        digitalWrite(led, HIGH);
        break;
      case 'c':
        digitalWrite(led, LOW);  
        break;
    }
  }
  // every 2 seconds, send a packet to the computer
  if (msUntilNextSend > 2000) {
    msUntilNextSend = msUntilNextSend - 2000;
    // first 2 bytes are a signature
    buffer[0] = 0xB0;
    buffer[1] = 0x07;
    // fill the rest with zeros
    for (int i=2; i<62; i++) {
      buffer[i] = 0;
    }
    // next 24 bytes are analog measurements
    for (int i=0; i<10; i++) {
      int val = digitalRead(i);
      buffer[i + 2] = val;
    }
    // and put a count of packets sent at the end
    buffer[62] = highByte(packetCount);
    buffer[63] = lowByte(packetCount);
    // actually send the packet
    n = RawHID.send(buffer, 100);
    if (n > 0) {
      Serial.print(F("Transmit packet "));
      Serial.println(packetCount);
      packetCount = packetCount + 1;
    } else {
      Serial.println(F("Unable to transmit packet"));
    }
  }
}