Skip to content

Commit 4df8c92

Browse files
committed
Vectronix Terrapin-X laser rangefinder protocol
1 parent 1d830c4 commit 4df8c92

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.platypii.baseline.lasers.rangefinder;
2+
3+
class Crc16 {
4+
/**
5+
* Computes the CRC16 checksum for a given byte array.
6+
*
7+
* @param byteArray The byte array to compute the CRC for.
8+
* @return The computed CRC16 checksum as an integer.
9+
*/
10+
public static int crc16(byte[] byteArray) {
11+
int crc = 65535;
12+
for (byte b : byteArray) {
13+
int index = (crc ^ (b & 0xff)) & 0xff;
14+
crc = (crc >> 8) ^ CRC16_LOOKUP_TABLE[index];
15+
}
16+
return crc;
17+
}
18+
19+
private static final int[] CRC16_LOOKUP_TABLE = {
20+
0, 4489, 8978, 12955, 17956, 22445, 25910, 29887,
21+
35912, 40385, 44890, 48851, 51820, 56293, 59774, 63735,
22+
4225, 264, 13203, 8730, 22181, 18220, 30135, 25662,
23+
40137, 36160, 49115, 44626, 56045, 52068, 63999, 59510,
24+
8450, 12427, 528, 5017, 26406, 30383, 17460, 21949,
25+
44362, 48323, 36440, 40913, 60270, 64231, 51324, 55797,
26+
12675, 8202, 4753, 792, 30631, 26158, 21685, 17724,
27+
48587, 44098, 40665, 36688, 64495, 60006, 55549, 51572,
28+
16900, 21389, 24854, 28831, 1056, 5545, 10034, 14011,
29+
52812, 57285, 60766, 64727, 34920, 39393, 43898, 47859,
30+
21125, 17164, 29079, 24606, 5281, 1320, 14259, 9786,
31+
57037, 53060, 64991, 60502, 39145, 35168, 48123, 43634,
32+
25350, 29327, 16404, 20893, 9506, 13483, 1584, 6073,
33+
61262, 65223, 52316, 56789, 43370, 47331, 35448, 39921,
34+
29575, 25102, 20629, 16668, 13731, 9258, 5809, 1848,
35+
65487, 60998, 56541, 52564, 47595, 43106, 39673, 35696,
36+
33800, 38273, 42778, 46739, 49708, 54181, 57662, 61623,
37+
2112, 6601, 11090, 15067, 20068, 24557, 28022, 31999,
38+
38025, 34048, 47003, 42514, 53933, 49956, 61887, 57398,
39+
6337, 2376, 15315, 10842, 24293, 20332, 32247, 27774,
40+
42250, 46211, 34328, 38801, 58158, 62119, 49212, 53685,
41+
10562, 14539, 2640, 7129, 28518, 32495, 19572, 24061,
42+
46475, 41986, 38553, 34576, 62383, 57894, 53437, 49460,
43+
14787, 10314, 6865, 2904, 32743, 28270, 23797, 19836,
44+
50700, 55173, 58654, 62615, 32808, 37281, 41786, 45747,
45+
19012, 23501, 26966, 30943, 3168, 7657, 12146, 16123,
46+
54925, 50948, 62879, 58390, 37033, 33056, 46011, 41522,
47+
23237, 19276, 31191, 26718, 7393, 3432, 16371, 11898,
48+
59150, 63111, 50204, 54677, 41258, 45219, 33336, 37809,
49+
27462, 31439, 18516, 23005, 11618, 15595, 3696, 8185,
50+
63375, 58886, 54429, 50452, 45483, 40994, 37561, 33584,
51+
31687, 27214, 22741, 18780, 15843, 11370, 7921, 3960
52+
};
53+
}

app/src/main/java/com/platypii/baseline/lasers/rangefinder/RangefinderService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class RangefinderService {
3030
private final BleProtocol protocols[] = {
3131
new ATNProtocol(),
3232
new SigSauerProtocol(),
33+
new TerrapinProtocol(),
3334
new UineyeProtocol()
3435
};
3536

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.platypii.baseline.lasers.rangefinder;
2+
3+
import com.platypii.baseline.bluetooth.BleException;
4+
import com.platypii.baseline.bluetooth.BleProtocol;
5+
import com.platypii.baseline.bluetooth.BluetoothUtil;
6+
import com.platypii.baseline.lasers.LaserMeasurement;
7+
import com.platypii.baseline.util.Exceptions;
8+
9+
import android.bluetooth.le.ScanRecord;
10+
import android.os.ParcelUuid;
11+
import android.util.Log;
12+
import androidx.annotation.NonNull;
13+
import androidx.annotation.Nullable;
14+
import com.welie.blessed.BluetoothPeripheral;
15+
import com.welie.blessed.WriteType;
16+
import java.util.Arrays;
17+
import java.util.List;
18+
import java.util.UUID;
19+
import org.greenrobot.eventbus.EventBus;
20+
21+
import static com.platypii.baseline.bluetooth.BluetoothUtil.byteArrayToHex;
22+
import static com.platypii.baseline.bluetooth.BluetoothUtil.toManufacturerString;
23+
24+
/**
25+
* This class contains ids, commands, and decoders for Vectronix Terrapin-X laser rangefinders.
26+
*/
27+
class TerrapinProtocol extends BleProtocol {
28+
private static final String TAG = "TerrapinProtocol";
29+
30+
// Manufacturer ID
31+
private static final int manufacturerId1 = 1164;
32+
private static final byte[] manufacturerData1 = {1, -96, -1, -1, -1, -1, 0}; // 01-a0-ff-ff-ff-ff-00
33+
34+
// Terrapin service
35+
private static final UUID terrapinService = UUID.fromString("81480000-b0b7-4074-8a24-ae554e5cdbc4");
36+
// Terrapin characteristic: read, indicate
37+
private static final UUID terrapinCharacteristic1 = UUID.fromString("81480100-b0b7-4074-8a24-ae554e5cdbc4");
38+
// Terrapin characteristic: notify, write
39+
private static final UUID terrapinCharacteristic2 = UUID.fromString("81480200-b0b7-4074-8a24-ae554e5cdbc4");
40+
41+
private static final String factoryModeSecretKey = "b6987833";
42+
43+
// Say hello to laser
44+
private static final byte[] commandStartMeasurement = {0, 0, 1, 16}; // 7e-00-00-01-10-7e
45+
46+
private static final byte[] commandGetLastRange = {0, 0, 2, 16}; // 7e-00-00-02-10-7e
47+
48+
@Override
49+
public void onServicesDiscovered(@NonNull BluetoothPeripheral peripheral) {
50+
try {
51+
// Request rangefinder service
52+
Log.i(TAG, "app -> rf: subscribe");
53+
peripheral.setNotify(terrapinService, terrapinCharacteristic1, true);
54+
} catch (Throwable e) {
55+
Log.e(TAG, "rangefinder handshake exception", e);
56+
}
57+
}
58+
59+
@Override
60+
public void processBytes(@NonNull BluetoothPeripheral peripheral, @NonNull byte[] value) {
61+
if (value[0] != 0x7e || value[value.length - 1] != 0x7e) {
62+
Log.w(TAG, "rf -> app: invalid command " + byteArrayToHex(value));
63+
return;
64+
}
65+
66+
// Remove frame
67+
final byte[] frame = Arrays.copyOfRange(value, 1, value.length - 1);
68+
69+
// Check checksum
70+
final int checksum = frame[frame.length - 1] + (frame[frame.length - 2] << 8);
71+
if (Crc16.crc16(frame) != checksum) {
72+
Log.w(TAG, "rf -> app: invalid checksum " + byteArrayToHex(frame) + " " + Crc16.crc16(frame) + " != " + checksum);
73+
}
74+
75+
// Packet types
76+
if (frame[0] == 0 && frame[1] == 0) {
77+
// Packet type command
78+
final int command = frame[2] + (frame[3] << 8);
79+
final byte[] payload = Arrays.copyOfRange(frame, 4, frame.length - 2);
80+
if (command == 2) {
81+
Log.i(TAG, "rf -> app: button press " + byteArrayToHex(payload));
82+
startMeasurement(peripheral);
83+
BluetoothUtil.sleep(1000);
84+
getLastRange(peripheral);
85+
} else {
86+
Log.i(TAG, "rf -> app: command unknown " + command);
87+
}
88+
} else if (frame[0] == 3 && frame[1] == 0) {
89+
// Packet type data
90+
Log.i(TAG, "rf -> app: data " + byteArrayToHex(frame));
91+
} else if (frame[0] == 4 && frame[1] == 0) {
92+
Log.i(TAG, "rf -> app: ack " + byteArrayToHex(frame));
93+
} else if (frame[0] == 5 && frame[1] == 0) {
94+
Log.i(TAG, "rf -> app: nack " + byteArrayToHex(frame));
95+
} else {
96+
Log.w(TAG, "rf -> app: unknown " + byteArrayToHex(frame));
97+
}
98+
}
99+
100+
private void startMeasurement(@NonNull BluetoothPeripheral peripheral) {
101+
Log.i(TAG, "app -> rf: start measurement");
102+
sendCommand(peripheral, commandStartMeasurement);
103+
}
104+
105+
private void getLastRange(@NonNull BluetoothPeripheral peripheral) {
106+
Log.i(TAG, "app -> rf: get last range");
107+
sendCommand(peripheral, commandGetLastRange);
108+
}
109+
110+
private void sendCommand(@NonNull BluetoothPeripheral peripheral, @NonNull byte[] command) {
111+
// Compute checksum
112+
final int checksum = Crc16.crc16(command);
113+
final byte[] frame = new byte[command.length + 4];
114+
frame[0] = 0x7e;
115+
System.arraycopy(command, 0, frame, 1, command.length);
116+
frame[command.length + 1] = (byte) (checksum & 0xff);
117+
frame[command.length + 2] = (byte) ((checksum >> 8) & 0xff);
118+
frame[command.length + 3] = 0x7e;
119+
Log.i(TAG, "app -> rf: send command " + byteArrayToHex(frame));
120+
peripheral.writeCharacteristic(terrapinService, terrapinCharacteristic2, frame, WriteType.WITH_RESPONSE);
121+
}
122+
123+
/**
124+
* Return true iff a bluetooth scan result looks like a rangefinder
125+
*/
126+
@Override
127+
public boolean canParse(@NonNull BluetoothPeripheral peripheral, @Nullable ScanRecord record) {
128+
final String deviceName = peripheral.getName();
129+
if (record != null && Arrays.equals(record.getManufacturerSpecificData(manufacturerId1), manufacturerData1)) {
130+
return true; // Manufacturer match (kenny's laser)
131+
} else if (
132+
(record != null && hasRangefinderService(record))
133+
|| deviceName.startsWith("FastM")
134+
|| deviceName.startsWith("Terrapin")) {
135+
// Send manufacturer data to firebase
136+
final String mfg = toManufacturerString(record);
137+
Exceptions.report(new BleException("Terrapin laser unknown mfg data: " + deviceName + " " + mfg));
138+
return true;
139+
} else {
140+
return false;
141+
}
142+
}
143+
144+
private boolean hasRangefinderService(@NonNull ScanRecord record) {
145+
final List<ParcelUuid> uuids = record.getServiceUuids();
146+
return uuids != null && uuids.contains(new ParcelUuid(terrapinService));
147+
}
148+
}

0 commit comments

Comments
 (0)