@@ -104,6 +104,8 @@ public byte getFeatures(byte type, byte features, boolean wait) {
104104 vectors = null ;
105105 intersections = null ;
106106 barcodes = null ;
107+
108+ long start = System .currentTimeMillis ();
107109
108110 while (true ) {
109111 // fill in request data
@@ -120,10 +122,10 @@ public byte getFeatures(byte type, byte features, boolean wait) {
120122 for (offset = 0 , res = 0 ; pixy .length > offset ; offset += fsize + 2 ) {
121123 ftype = pixy .buffer [offset ];
122124 fsize = pixy .buffer [offset + 1 ];
123- fdata = Arrays .copyOfRange (pixy .buffer , offset + 2 , pixy .receiveLength );
125+ fdata = Arrays .copyOfRange (pixy .buffer , offset + 2 , pixy .length );
124126 if (ftype == LINE_VECTOR ) {
125127 vectors = new Vector [(int ) Math .floor (fdata .length / 6 )];
126- for (int i = 0 ; (i + 1 ) * 6 < fdata .length ; i ++) {
128+ for (int i = 0 ; (i + 1 ) * 6 <= fdata .length ; i ++) {
127129 vectors [i ] = new Vector (fdata [(6 * i )] & 0xFF , fdata [(6 * i ) + 1 ] & 0xFF ,
128130 fdata [(6 * i ) + 2 ] & 0xFF , fdata [(6 * i ) + 3 ] & 0xFF , fdata [(6 * i ) + 4 ] & 0xFF ,
129131 fdata [(6 * i ) + 5 ] & 0xFF );
@@ -135,12 +137,13 @@ public byte getFeatures(byte type, byte features, boolean wait) {
135137 .floor (fdata .length / (4 + (4 * LINE_MAX_INTERSECTION_LINES )))];
136138 for (int i = 0 ; (i + 1 ) * size < fdata .length ; i ++) {
137139 IntersectionLine [] lines = new IntersectionLine [LINE_MAX_INTERSECTION_LINES ];
138- for (int l = 0 ; l < LINE_MAX_INTERSECTION_LINES ; l ++) {
140+ for (int l = 0 ; l <= LINE_MAX_INTERSECTION_LINES ; l ++) {
139141 int arr = ((size * i ) + 4 );
140142 int index = fdata [arr + (4 * l )];
141143 int reserved = fdata [arr + (4 * l ) + 1 ];
142- short angle = (short ) (((fdata [arr + (4 * l ) + 2 ] & 0xff ) << 8 )
143- | (fdata [arr + (4 * l ) + 3 ] & 0xff ));
144+ short angle = (short ) (
145+ ((fdata [arr + (4 * l ) + 3 ] & 0xff ) << 8 )
146+ | (fdata [arr + (4 * l ) + 2 ] & 0xff ));
144147 IntersectionLine intLine = new IntersectionLine (index , reserved , angle );
145148 lines [l ] = intLine ;
146149 }
@@ -151,7 +154,7 @@ public byte getFeatures(byte type, byte features, boolean wait) {
151154 res |= LINE_INTERSECTION ;
152155 } else if (ftype == LINE_BARCODE ) {
153156 barcodes = new Barcode [(int ) Math .floor (fdata .length / 4 )];
154- for (int i = 0 ; (i + 1 ) * 4 < fdata .length ; i ++) {
157+ for (int i = 0 ; (i + 1 ) * 4 <= fdata .length ; i ++) {
155158 barcodes [i ] = new Barcode (fdata [(4 * i )] & 0xFF , fdata [(4 * i ) + 1 ] & 0xFF ,
156159 fdata [(4 * i ) + 2 ] & 0xFF , fdata [(4 * i ) + 3 ] & 0xFF );
157160 }
@@ -170,6 +173,9 @@ else if (!wait) // we're busy
170173 } else
171174 return Pixy2 .PIXY_RESULT_ERROR ; // some kind of bitstream error
172175
176+ if (System .currentTimeMillis () - start > 500 ) {
177+ return Pixy2 .PIXY_RESULT_ERROR ; // timeout to prevent lockup
178+ }
173179 // If we're waiting for frame data, don't thrash Pixy with requests.
174180 // We can give up half a millisecond of latency (worst case)
175181 try {
0 commit comments