Skip to content

Commit 08829dd

Browse files
Fixed C++ Struct Math and Bitwise Math
Corrected Line Tracking C++ Struct Math thanks to Jared Brogni Corrected bitwise operation on 16bit angle value
1 parent 5c7f12d commit 08829dd

3 files changed

Lines changed: 13 additions & 10 deletions

File tree

src/main/java/io/github/pseudoresonance/pixy2api/Pixy2.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public enum LinkType {
8585
protected int length = 0;
8686
protected int type = 0;
8787
protected byte[] bufferPayload = null;
88-
protected int receiveLength = 0;
8988

9089
protected int frameWidth = -1;
9190
protected int frameHeight = -1;
@@ -390,7 +389,6 @@ protected int receivePacket() {
390389
csSerial = ((buffer[3] & 0xff) << 8) | (buffer[2] & 0xff);
391390

392391
res = link.receive(buffer, length, csCalc);
393-
receiveLength = length;
394392

395393
if (res < 0)
396394
return res;
@@ -405,7 +403,6 @@ protected int receivePacket() {
405403
length = buffer[1];
406404

407405
res = link.receive(buffer, length);
408-
receiveLength = length;
409406

410407
if (res < 0)
411408
return res;

src/main/java/io/github/pseudoresonance/pixy2api/Pixy2CCC.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public int getBlocks(boolean wait, int sigmap, int maxBlocks) {
9494
if (pixy.receivePacket() == 0) {
9595
if (pixy.type == CCC_RESPONSE_BLOCKS) {
9696
blocks = new ArrayList<Block>();
97-
for (int i = 0; i + 13 < pixy.receiveLength; i += 14) {
97+
for (int i = 0; i + 13 < pixy.length; i += 14) {
9898
Block b = new Block(((pixy.buffer[i + 1] & 0xff) << 8) | (pixy.buffer[i] & 0xff),
9999
((pixy.buffer[i + 3] & 0xff) << 8) | (pixy.buffer[i + 2] & 0xff),
100100
((pixy.buffer[i + 5] & 0xff) << 8) | (pixy.buffer[i + 4] & 0xff),

src/main/java/io/github/pseudoresonance/pixy2api/Pixy2Line.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)