1515 *
1616 * https://github.com/PseudoResonance/Pixy2JavaAPI
1717 *
18- * @author PseudoResonance
18+ * @author PseudoResonance (Josh Otake)
1919 *
2020 * ORIGINAL HEADER -
2121 * https://github.com/charmedlabs/pixy2/blob/master/src/host/arduino/libraries/Pixy2/TPixy2.h
@@ -104,8 +104,10 @@ public enum LinkType {
104104 */
105105 private Pixy2 (Link link ) {
106106 this .link = link ;
107+ // Initializes send/return buffer and payload buffer
107108 buffer = new byte [PIXY_BUFFERSIZE + PIXY_SEND_HEADER_SIZE ];
108109 bufferPayload = new byte [PIXY_BUFFERSIZE ];
110+ // Initializes tracker objects
109111 this .ccc = new Pixy2CCC (this );
110112 this .line = new Pixy2Line (this );
111113 this .video = new Pixy2Video (this );
@@ -119,9 +121,12 @@ private Pixy2(Link link) {
119121 * @return Pixy2 error code
120122 */
121123 public int init (int argument ) {
124+ // Opens link
122125 int ret = link .open (argument );
123126 if (ret >= 0 ) {
127+ // Tries to connect, times out if unable to communicate after 5 seconds
124128 for (long t = System .currentTimeMillis (); System .currentTimeMillis () - t < 5000 ;) {
129+ // Gets version and resolution to check if communication is successful and stores for future use
125130 if (getVersion () >= 0 ) {
126131 getResolution ();
127132 return PIXY_RESULT_OK ;
@@ -137,7 +142,7 @@ public int init(int argument) {
137142 }
138143
139144 /**
140- * Initializes Pixy2 and waits for startup to complete using default argument
145+ * Initializes Pixy2 and waits for startup to complete using default link argument
141146 * value
142147 *
143148 * @return Pixy2 error code
@@ -330,22 +335,22 @@ public int getFrameHeight() {
330335 }
331336
332337 /**
333- * Synchronizes communication with Pixy2
338+ * Looks for Pixy2 communication synchronization bytes to find start of message
334339 *
335340 * @return Pixy2 error code
336341 */
337342 private byte getSync () {
338343 int i , attempts , cprev , res , start , ret ;
339344 byte [] c = new byte [1 ];
340345
341- // parse bytes until we find sync
346+ // Parse incoming bytes until sync bytes are found
342347 for (i = attempts = cprev = 0 ; true ; i ++) {
343348 res = link .receive (c , 1 ) & 0xff ;
344349 if (res >= PIXY_RESULT_OK ) {
345350 ret = c [0 ] & 0xff ;
346- // since we're using little endian, previous byte is least significant byte
351+ // Since we're using little endian, previous byte is least significant byte
347352 start = cprev ;
348- // current byte is most significant byte
353+ // Current byte is most significant byte
349354 start |= ret << 8 ;
350355 cprev = ret ;
351356 if (start == PIXY_CHECKSUM_SYNC ) {
@@ -359,7 +364,7 @@ private byte getSync() {
359364 }
360365 // If we've read some bytes and no sync, then wait and try again.
361366 // And do that several more times before we give up.
362- // Pixy guarantees to respond within 100us.
367+ // Pixy2 guarantees to respond within 100us.
363368 if (i >= 4 ) {
364369 if (attempts >= 4 )
365370 return PIXY_RESULT_ERROR ;
@@ -374,7 +379,7 @@ private byte getSync() {
374379 }
375380
376381 /**
377- * Gets stored Pixy2 Version info or retrieves from Pixy2 if not present
382+ * Gets stored Pixy2 {@link Version} info or retrieves from Pixy2 if not present
378383 *
379384 * @return Pixy2 Version Info
380385 */
@@ -385,18 +390,21 @@ public Version getVersionInfo() {
385390 }
386391
387392 /**
388- * Receives packet from Pixy2 to buffer
393+ * Receives packet from Pixy2 and outputs to buffer for further processing
389394 *
390395 * @return Length of bytes received or Pixy2 error code
391396 */
392397 protected int receivePacket () {
393398 int csSerial , res ;
394399 Checksum csCalc = new Checksum ();
395400
401+ // Waits for sync bytes
396402 res = getSync ();
397403 if (res < 0 )
404+ // Sync not found
398405 return res ;
399406 if (m_cs ) {
407+ // Checksum sync
400408 res = link .receive (buffer , 4 );
401409 if (res < 0 )
402410 return res ;
@@ -406,20 +414,24 @@ protected int receivePacket() {
406414
407415 csSerial = ((buffer [3 ] & 0xff ) << 8 ) | (buffer [2 ] & 0xff );
408416
417+ // Receives message from buffer
409418 res = link .receive (buffer , length , csCalc );
410419
411420 if (res < 0 )
412421 return res ;
422+ // Checks for accuracy with checksum
413423 if (csSerial != csCalc .getChecksum ())
414424 return PIXY_RESULT_CHECKSUM_ERROR ;
415425 } else {
426+ // Non-checksum sync
416427 res = link .receive (buffer , 2 );
417428 if (res < 0 )
418429 return res ;
419430
420- type = buffer [0 ];
421- length = buffer [1 ];
431+ type = buffer [0 ] & 0xff ;
432+ length = buffer [1 ] & 0xff ;
422433
434+ // Receives message from buffer
423435 res = link .receive (buffer , length );
424436
425437 if (res < 0 )
@@ -434,30 +446,32 @@ protected int receivePacket() {
434446 * @return Length of bytes sent or Pixy2 error code
435447 */
436448 protected int sendPacket () {
437- // write header info at beginning of buffer
449+ // Write header info at beginning of buffer
438450 buffer [0 ] = (byte ) (PIXY_NO_CHECKSUM_SYNC & 0xff );
439451 buffer [1 ] = (byte ) ((PIXY_NO_CHECKSUM_SYNC >> 8 ) & 0xff );
440452 buffer [2 ] = (byte ) type ;
441453 buffer [3 ] = (byte ) length ;
442- // send whole thing -- header and data in one call
454+ // Add payload data to buffer
443455 for (int i = 0 ; i < length ; i ++) {
444456 buffer [4 + i ] = bufferPayload [i ];
445457 }
458+ // Send buffer
446459 return link .send (buffer , (byte ) (length + PIXY_SEND_HEADER_SIZE ));
447460 }
448461
449462 /**
450463 * Sends change program packet to Pixy2
451464 *
452- * @param prog Program data
465+ * @param prog Program name
453466 *
454467 * @return Pixy2 error code
455468 */
456469 public byte changeProg (char [] prog ) {
457470 int res = 0 ;
458471
459- // poll for program to change
472+ // Poll for program to change
460473 while (true ) {
474+ // Truncates supplied program name, or adds empty characters after to indicate end of string
461475 for (int i = 0 ; i < PIXY_MAX_PROGNAME ; i ++) {
462476 if (i < prog .length )
463477 bufferPayload [i ] = (byte ) prog [i ];
@@ -471,12 +485,13 @@ public byte changeProg(char[] prog) {
471485 res = ((buffer [3 ] & 0xff ) << 24 ) | ((buffer [2 ] & 0xff ) << 16 ) | ((buffer [1 ] & 0xff ) << 8 )
472486 | (buffer [0 ] & 0xff );
473487 if (res > 0 ) {
474- getResolution (); // get resolution so we have it
475- return PIXY_RESULT_OK ; // success
488+ getResolution (); // Get resolution for future use
489+ return PIXY_RESULT_OK ; // Success
476490 }
477491 } else
478- return PIXY_RESULT_ERROR ; // some kind of bitstream error
492+ return PIXY_RESULT_ERROR ; // Some kind of bitstream error
479493 try {
494+ // Timeout to try again
480495 TimeUnit .MICROSECONDS .sleep (1000 );
481496 } catch (InterruptedException e ) {
482497 }
@@ -486,7 +501,7 @@ public byte changeProg(char[] prog) {
486501 /**
487502 * Gets version info from Pixy2
488503 *
489- * @return Pixy2 error code
504+ * @return Buffer length or Pixy2 error code
490505 */
491506 public int getVersion () {
492507 length = 0 ;
@@ -495,11 +510,11 @@ public int getVersion() {
495510 if (receivePacket () == 0 ) {
496511 if (type == PIXY_TYPE_RESPONSE_VERSION ) {
497512 version = new Version (buffer );
498- return length ;
513+ return length ; // Success
499514 } else if (type == PIXY_TYPE_RESPONSE_ERROR )
500515 return PIXY_RESULT_BUSY ;
501516 }
502- return PIXY_RESULT_ERROR ; // some kind of bitstream error
517+ return PIXY_RESULT_ERROR ; // Some kind of bitstream error
503518 }
504519
505520 /**
@@ -509,18 +524,18 @@ public int getVersion() {
509524 */
510525 public byte getResolution () {
511526 length = 1 ;
512- bufferPayload [0 ] = 0 ; // for future types of queries
527+ bufferPayload [0 ] = 0 ; // Adds empty byte to payload as placeholder for future queries
513528 type = PIXY_TYPE_REQUEST_RESOLUTION ;
514529 sendPacket ();
515530 if (receivePacket () == 0 ) {
516531 if (type == PIXY_TYPE_RESPONSE_RESOLUTION ) {
517532 frameWidth = ((buffer [1 ] & 0xff ) << 8 ) | (buffer [0 ] & 0xff );
518533 frameHeight = ((buffer [3 ] & 0xff ) << 8 ) | (buffer [2 ] & 0xff );
519- return PIXY_RESULT_OK ; // success
534+ return PIXY_RESULT_OK ; // Success
520535 } else
521536 return PIXY_RESULT_ERROR ;
522537 } else
523- return PIXY_RESULT_ERROR ; // some kind of bitstream error
538+ return PIXY_RESULT_ERROR ; // Some kind of bitstream error
524539 }
525540
526541 /**
@@ -533,7 +548,7 @@ public byte getResolution() {
533548 public byte setCameraBrightness (int brightness ) {
534549 int res ;
535550
536- // Limits brightness between the min and max
551+ // Limits brightness between the 0 and 255
537552 brightness = (brightness >= 255 ? 255 : (brightness <= 0 ? 0 : brightness ));
538553
539554 bufferPayload [0 ] = (byte ) brightness ;
@@ -543,9 +558,9 @@ public byte setCameraBrightness(int brightness) {
543558 if (receivePacket () == 0 && type == PIXY_TYPE_RESPONSE_RESULT && length == 4 ) {
544559 res = ((buffer [3 ] & 0xff ) << 24 ) | ((buffer [2 ] & 0xff ) << 16 ) | ((buffer [1 ] & 0xff ) << 8 )
545560 | (buffer [0 ] & 0xff );
546- return (byte ) res ;
561+ return (byte ) res ; // Success
547562 } else
548- return PIXY_RESULT_ERROR ; // some kind of bitstream error
563+ return PIXY_RESULT_ERROR ; // Some kind of bitstream error
549564 }
550565
551566 /**
@@ -559,7 +574,7 @@ public byte setCameraBrightness(int brightness) {
559574 public byte setServos (int pan , int tilt ) {
560575 int res ;
561576
562- // Limits servo values between the defined min and max
577+ // Limits servo values between 0 and 1000
563578 pan = (pan >= PIXY_RCS_MAX_POS ? PIXY_RCS_MAX_POS : (pan <= PIXY_RCS_MIN_POS ? PIXY_RCS_MIN_POS : pan ));
564579 tilt = (tilt >= PIXY_RCS_MAX_POS ? PIXY_RCS_MAX_POS : (tilt <= PIXY_RCS_MIN_POS ? PIXY_RCS_MIN_POS : tilt ));
565580
@@ -573,9 +588,9 @@ public byte setServos(int pan, int tilt) {
573588 if (receivePacket () == 0 && type == PIXY_TYPE_RESPONSE_RESULT && length == 4 ) {
574589 res = ((buffer [3 ] & 0xff ) << 24 ) | ((buffer [2 ] & 0xff ) << 16 ) | ((buffer [1 ] & 0xff ) << 8 )
575590 | (buffer [0 ] & 0xff );
576- return (byte ) res ;
591+ return (byte ) res ; // Success
577592 } else
578- return PIXY_RESULT_ERROR ; // some kind of bitstream error
593+ return PIXY_RESULT_ERROR ; // Some kind of bitstream error
579594 }
580595
581596 /**
@@ -612,7 +627,7 @@ public byte setLED(int rgb) {
612627 public byte setLED (int r , int g , int b ) {
613628 int res ;
614629
615- // Limits rgb values between the min and max
630+ // Limits rgb values between 0 and 255
616631 r = (r >= 255 ? 255 : (r <= 0 ? 0 : r ));
617632 g = (g >= 255 ? 255 : (g <= 0 ? 0 : g ));
618633 b = (b >= 255 ? 255 : (b <= 0 ? 0 : b ));
@@ -626,16 +641,18 @@ public byte setLED(int r, int g, int b) {
626641 if (receivePacket () == 0 && type == PIXY_TYPE_RESPONSE_RESULT && length == 4 ) {
627642 res = ((buffer [3 ] & 0xff ) << 24 ) | ((buffer [2 ] & 0xff ) << 16 ) | ((buffer [1 ] & 0xff ) << 8 )
628643 | (buffer [0 ] & 0xff );
629- return (byte ) res ;
644+ return (byte ) res ; // Success
630645 } else
631- return PIXY_RESULT_ERROR ; // some kind of bitstream error
646+ return PIXY_RESULT_ERROR ; // Some kind of bitstream error
632647 }
633648
634649 /**
635650 * Turns Pixy2 light source on/off
636651 *
637- * @param upper Turns white LEDs on/off
638- * @param lower Sets RGB values to on/off
652+ * Use 1 to indicate on, 0 to indicate off
653+ *
654+ * @param upper Byte indicating status of white LEDs
655+ * @param lower Byte indicating status of RGB LED
639656 *
640657 * @return Pixy2 error code
641658 */
@@ -650,15 +667,15 @@ public byte setLamp(byte upper, byte lower) {
650667 if (receivePacket () == 0 && type == PIXY_TYPE_RESPONSE_RESULT && length == 4 ) {
651668 res = ((buffer [3 ] & 0xff ) << 24 ) | ((buffer [2 ] & 0xff ) << 16 ) | ((buffer [1 ] & 0xff ) << 8 )
652669 | (buffer [0 ] & 0xff );
653- return (byte ) res ;
670+ return (byte ) res ; // Success
654671 } else
655- return PIXY_RESULT_ERROR ; // some kind of bitstream error
672+ return PIXY_RESULT_ERROR ; // Some kind of bitstream error
656673 }
657674
658675 /**
659676 * Gets Pixy2 camera framerate between 2-62fps
660677 *
661- * @return Positive number for framerate or negative for Pixy2 error code
678+ * @return Framerate or Pixy2 error code
662679 */
663680 public byte getFPS () {
664681 int res ;
@@ -669,23 +686,37 @@ public byte getFPS() {
669686 if (receivePacket () == 0 && type == PIXY_TYPE_RESPONSE_RESULT && length == 4 ) {
670687 res = ((buffer [3 ] & 0xff ) << 24 ) | ((buffer [2 ] & 0xff ) << 16 ) | ((buffer [1 ] & 0xff ) << 8 )
671688 | (buffer [0 ] & 0xff );
672- return (byte ) res ;
689+ return (byte ) res ; // Success
673690 } else
674- return PIXY_RESULT_ERROR ; // some kind of bitstream error
691+ return PIXY_RESULT_ERROR ; // Some kind of bitstream error
675692 }
676693
694+ // Checksum holder class
677695 public class Checksum {
678696
679697 int cs = 0 ;
680698
699+ /**
700+ * Adds byte to checksum
701+ *
702+ * @param b Byte to be added
703+ */
681704 public void updateChecksum (int b ) {
682705 cs += b ;
683706 }
684707
708+ /**
709+ * Returns calculated checksum
710+ *
711+ * @return Calculated checksum
712+ */
685713 public int getChecksum () {
686714 return cs ;
687715 }
688716
717+ /**
718+ * Resets checksum
719+ */
689720 public void reset () {
690721 cs = 0 ;
691722 }
0 commit comments