So, this is what I was told about the two loops.
"check if the index is getting so high that you can’t align and see an entire frame." I think this is that it takes too long to parse all the data so we split it up? Looking back at documentation, this is how the code should look. Now with comments!
Code:
// set the number of bytes to get from the pixycam each read cycle. The pixycam outputs 14 byte blocks
// of data with an extra 2 bytes between frames per Object Block Format Figure
int maxBytes=64;
// declare the object data variables
int xPosition = 0;
int yPosition = 0;
int width = 0;
int height = 0;
// declare a byte array to store the data from the camera
byte[] pixyValues = new byte[maxBytes];
// the remainder of this snippet should be placed in a loop where the data is also used.
// a while loop is suggested where the loop exits when the target is identified or a break button is
// depressed on the OI
boolean target = false;
boolean oiExit = false;
while (!target && !oiExit){
// read the array of data from the camera
RobotMap.pixyi2c.readOnly(pixyValues, 64);
// check for a null array and don’t try to parse bad data
if (pixyValues != null) {
int i = 0;
// parse the data to move the index pointer (i) to the start of a frame
// i is incremented until the first two bytes (i and i+1) match the sync bytes (0x55 and 0xaa)
// Note: In Java, the and operation with 0xff is key to matching the 0xaa because the byte array is
// automatically filled by Java with leading 1s that make the number -86
while (!((pixyValues[i] & 0xff) == 0x55) && (pixyValues[i + 1] & 0xff) == 0xaa) && i < 50) { i++; }
i++;
// check if the index is getting so high that you can’t align and see an entire frame. Ensure it isn’t
if (i > 50) i = 49;
// parse away the second set of sync bytes
while (!((pixyValues[i] & 0xff) == 0x55) && (pixyValues[i + 1] & 0xff) == 0xaa) && i < 50) { i++; }
// build the target data from the framed data
xPosition = (char) (((pixyValues[i + 7] & 0xff) << 8) | (pixyValues[i + 6] & 0xff));
yPosition = (char) (((pixyValues[i + 9] & 0xff) << 8) | (pixyValues[i + 8] & 0xff));
width = (char) (((pixyValues[i + 11] & 0xff) << 8) | (pixyValues[i + 10 & 0xff));
height = (char) (((pixyValues[i + 13] & 0xff) << 8) | (pixyValues[i + 12] & 0xff));
}
Hope this helps
