-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vectronix Terrapin-X laser rangefinder protocol
- Loading branch information
Showing
4 changed files
with
438 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/platypii/baseline/lasers/rangefinder/Crc16.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.platypii.baseline.lasers.rangefinder; | ||
|
||
class Crc16 { | ||
/** | ||
* Computes the CRC16 checksum for a given byte array. | ||
*/ | ||
public static short crc16(byte[] byteArray) { | ||
int crc = 0xffff; | ||
|
||
// Process bytes in pairs (LSB first) | ||
for (int i = 0; i < byteArray.length; i++) { | ||
int b = byteArray[i] & 0xff; | ||
crc ^= b; | ||
|
||
// Process each bit | ||
for (int j = 0; j < 8; j++) { | ||
if ((crc & 0x0001) != 0) { | ||
crc = (crc >> 1) ^ 0x8408; // 0x8408 is reversed polynomial | ||
} else { | ||
crc = crc >> 1; | ||
} | ||
} | ||
} | ||
|
||
// XOR with 0xfff | ||
crc ^= 0xffff; | ||
|
||
return (short) crc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
app/src/main/java/com/platypii/baseline/lasers/rangefinder/TerraSentenceIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.platypii.baseline.lasers.rangefinder; | ||
|
||
import android.util.Log; | ||
import androidx.annotation.NonNull; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
// Sentences start with 7e and end with 7e | ||
// Sometimes 1 sentence takes 2 messages | ||
// Sometimes 2 sentences come in 1 message | ||
class TerraSentenceIterator implements Iterator<byte[]> { | ||
private static final String TAG = "TerraSentenceIterator"; | ||
|
||
@NonNull | ||
private final List<Byte> byteBuffer = new ArrayList<>(); | ||
|
||
// Sentences ready to read | ||
@NonNull | ||
private final List<byte[]> sentences = new ArrayList<>(); | ||
|
||
private int state = 0; | ||
|
||
void addBytes(@NonNull byte[] bytes) { | ||
for (byte b : bytes) { | ||
addByte(b); | ||
} | ||
} | ||
|
||
private void addByte(byte b) { | ||
byteBuffer.add(b); | ||
if (state == 0) { | ||
if (b == 0x7e) state = 1; | ||
else Log.e(TAG, "missing preamble"); | ||
} else if (state == 1) { | ||
if (b == 0x7e) { | ||
addSentence(); | ||
state = 0; | ||
} | ||
} | ||
} | ||
|
||
private void addSentence() { | ||
byte[] sent = new byte[byteBuffer.size() - 2]; | ||
for (int i = 0; i < byteBuffer.size() - 2; i++) { | ||
sent[i] = byteBuffer.get(i + 1); | ||
} | ||
sentences.add(sent); | ||
byteBuffer.clear(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return !sentences.isEmpty(); | ||
} | ||
|
||
@Override | ||
public byte[] next() { | ||
return sentences.remove(0); | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
Oops, something went wrong.