neophob/PixelController — pull request #88 by eosrei (2014-12-02 07:04:35+00:00)
Title: tpm2serial: Add serialWaitRead() to block on byte reads.

I've got this running reliably on an Arduino Uno R2 with 50 RGB WS2811 LEDs on two separate computers. A major problem was the serial latency issue. I avoided it by writing a `serialWaitRead()` function which checks the return of the [Serial.read](http://arduino.cc/en/Serial/Read) call and loops until something other than -1 is returned.

```arduino
int serialWaitRead() {
int inByte = Serial.read();

// Loop until a byte is read.
while (inByte == -1) {
inByte = Serial.read();
}
return inByte;
}
```

I added the `showError()` function to set all LEDs red. It is called when the Arduino is receiving invalid data and DEBUG is defined. If the data is all bad and `readCommand()` constantly returns a negative, then the LEDs just stay red. If only some of the data is bad, then the LEDs flicker red. (IDK why, but random packets have large incorrect frame size header values?) Summary: Comment DEBUG and you won't notice the dropped frames.

FYI *jpnevulator* has worked well for me on Linux to watch the Arduino's debug responses:
```
jpnevulator --ascii --timing-print --tty /dev/ttyACM0 --read
```

I renamed some of the variables to for clarity (I hope that's alright!):
* packetBuffer -> frameData
* psize -> frameSize
* recvNr-> byteCount

Lastly, there is some formatting and additional documentation.