Skip to content

Commit deedac5

Browse files
OtakeOtake
authored andcommitted
Corrected Slight Errors
C++ Strncpy Implementation from Jared Brogni for Pixy2 changeProg SPILink Checksum calculation error Pixy2CCC C++ Struct implementation typo
1 parent f853ee6 commit deedac5

3 files changed

Lines changed: 18 additions & 15 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ protected int receivePacket() {
378378
res = getSync();
379379
if (res < 0)
380380
return res;
381-
382381
if (m_cs) {
383382
res = link.receive(buffer, 4);
384383
if (res < 0)
@@ -390,9 +389,9 @@ protected int receivePacket() {
390389
csSerial = ((buffer[3] & 0xff) << 8) | (buffer[2] & 0xff);
391390

392391
res = link.receive(buffer, length, csCalc);
392+
393393
if (res < 0)
394394
return res;
395-
396395
if (csSerial != csCalc.getChecksum())
397396
return PIXY_RESULT_CHECKSUM_ERROR;
398397
} else {
@@ -441,7 +440,10 @@ public byte changeProg(char[] prog) {
441440
// poll for program to change
442441
while (true) {
443442
for (int i = 0; i < PIXY_MAX_PROGNAME; i++) {
444-
bufferPayload[i] = (byte) prog[i];
443+
if (i < prog.length)
444+
bufferPayload[i] = (byte) prog[i];
445+
else
446+
bufferPayload[i] = Character.MIN_VALUE;
445447
}
446448
length = PIXY_MAX_PROGNAME;
447449
type = PIXY_TYPE_REQUEST_CHANGE_PROG;

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ protected Pixy2CCC(Pixy2 pixy) {
8080
* @return Pixy2 error code
8181
*/
8282
public int getBlocks(boolean wait, int sigmap, int maxBlocks) {
83-
blocks.clear();
8483
long start = System.currentTimeMillis();
8584

8685
while (true) {
@@ -94,14 +93,15 @@ public int getBlocks(boolean wait, int sigmap, int maxBlocks) {
9493
pixy.sendPacket();
9594
if (pixy.receivePacket() == 0) {
9695
if (pixy.type == CCC_RESPONSE_BLOCKS) {
97-
for (int i = 0; (i + 1) * 14 < pixy.buffer.length; i++) {
98-
Block b = new Block(((pixy.buffer[i * 8 + 1] & 0xff) << 8) | (pixy.buffer[i * 8] & 0xff),
99-
((pixy.buffer[i * 8 + 3] & 0xff) << 8) | (pixy.buffer[i * 8 + 2] & 0xff),
100-
((pixy.buffer[i * 8 + 5] & 0xff) << 8) | (pixy.buffer[i * 8 + 4] & 0xff),
101-
((pixy.buffer[i * 8 + 7] & 0xff) << 8) | (pixy.buffer[i * 8 + 6] & 0xff),
102-
((pixy.buffer[i * 8 + 9] & 0xff) << 8) | (pixy.buffer[i * 8 + 8] & 0xff),
103-
(pixy.buffer[i * 8 + 11] << 8) | pixy.buffer[i * 8 + 10], pixy.buffer[i * 8 + 12],
104-
pixy.buffer[i * 8 + 13]);
96+
blocks = new ArrayList<Block>();
97+
for (int i = 0; i + 13 < pixy.buffer.length; i += 14) {
98+
Block b = new Block(((pixy.buffer[i + 1] & 0xff) << 8) | (pixy.buffer[i] & 0xff),
99+
((pixy.buffer[i + 3] & 0xff) << 8) | (pixy.buffer[i + 2] & 0xff),
100+
((pixy.buffer[i + 5] & 0xff) << 8) | (pixy.buffer[i + 4] & 0xff),
101+
((pixy.buffer[i + 7] & 0xff) << 8) | (pixy.buffer[i + 6] & 0xff),
102+
((pixy.buffer[i + 9] & 0xff) << 8) | (pixy.buffer[i + 8] & 0xff),
103+
((pixy.buffer[i + 11] & 0xff) << 8) | (pixy.buffer[i + 10] & 0xff),
104+
(pixy.buffer[i + 12] & 0xff), (pixy.buffer[i + 13] & 0xff));
105105
blocks.add(b);
106106
}
107107
return blocks.size();
@@ -115,8 +115,9 @@ public int getBlocks(boolean wait, int sigmap, int maxBlocks) {
115115
}
116116

117117
}
118-
} else
118+
} else {
119119
return Pixy2.PIXY_RESULT_ERROR; // some kind of bitstream error
120+
}
120121
if (System.currentTimeMillis() - start > 500) {
121122
return Pixy2.PIXY_RESULT_ERROR; // timeout to prevent lockup
122123
}

src/main/java/io/github/pseudoresonance/pixy2api/links/SPILink.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ public int receive(byte[] buffer, int length, Checksum cs) {
9292
cs.reset();
9393
spi.read(false, buffer, length);
9494
if (cs != null)
95-
for (byte b : buffer) {
96-
int csb = b & 0xff;
95+
for (int i = 0; i < length; i++) {
96+
int csb = buffer[i] & 0xff;
9797
cs.updateChecksum(csb);
9898
}
9999
return length;

0 commit comments

Comments
 (0)