Skip to content

Commit

Permalink
Update flysight2 protocol for 2024.11.11 release candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Dec 3, 2024
1 parent 3100506 commit 725ef5b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.7.2'
classpath 'com.android.tools.build:gradle:8.7.3'
classpath 'com.google.firebase:firebase-crashlytics-gradle:3.0.2'
classpath 'com.google.gms:google-services:4.4.2'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import androidx.annotation.Nullable;
import com.welie.blessed.BluetoothPeripheral;
import com.welie.blessed.GattStatus;
import com.welie.blessed.WriteType;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.UUID;

public class Flysight2Protocol extends BleProtocol {
Expand All @@ -24,11 +26,15 @@ public class Flysight2Protocol extends BleProtocol {
private static final UUID flysightService0 = UUID.fromString("00000000-cc7a-482a-984a-7f2ed5b3e58f");
private static final UUID flysightService1 = UUID.fromString("00000001-cc7a-482a-984a-7f2ed5b3e58f");
private static final UUID flysightService2 = UUID.fromString("00000002-cc7a-482a-984a-7f2ed5b3e58f");

// Flysight characteristics
private static final UUID flysightCharacteristicGNSS = UUID.fromString("00000000-8e22-4541-9d4c-21edae82ed19");
private static final UUID flysightCharacteristicTX = UUID.fromString("00000001-8e22-4541-9d4c-21edae82ed19");
private static final UUID flysightCharacteristicRX = UUID.fromString("00000002-8e22-4541-9d4c-21edae82ed19");

// Flysight commands
private static final byte[] flysightCommandHeartbeat = new byte[]{(byte) 0xfe};

private static final long gpsEpochMilliseconds = 315964800000L - 18000L; // January 6, 1980 - 18s
private static final long millisecondsPerWeek = 604800000L;

Expand All @@ -46,6 +52,8 @@ public boolean canParse(@NonNull BluetoothPeripheral peripheral, @Nullable ScanR
public void onServicesDiscovered(@NonNull BluetoothPeripheral peripheral) {
Log.i(TAG, "flysight services discovered " + peripheral.getCurrentMtu());
peripheral.requestMtu(256);
// Start heartbeat thread
startHeartbeat(peripheral, flysightService0, flysightCharacteristicRX);
}

@Override
Expand All @@ -58,6 +66,10 @@ public void onMtuChanged(@NonNull BluetoothPeripheral peripheral, int mtu, @NonN
@Override
public void processBytes(@NonNull BluetoothPeripheral peripheral, @NonNull byte[] value) {
try {
// FlySight2 RC pre-2024-11-11 didn't have flag byte
if (value.length == 29 && value[0] == -80) { // 0xb0
value = Arrays.copyOfRange(value, 1, value.length);
}
final ByteBuffer buf = ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN);
final int tow = buf.getInt(0); // gps time of week
final double lng = buf.getInt(4) * 1e-7;
Expand Down Expand Up @@ -92,4 +104,23 @@ public void processBytes(@NonNull BluetoothPeripheral peripheral, @NonNull byte[
Exceptions.report(e);
}
}

private void startHeartbeat(@NonNull BluetoothPeripheral peripheral, @NonNull UUID service, @NonNull UUID characteristic) {
// Start heartbeat thread
new Thread(() -> {
try {
while (true) {
// Send heartbeat every 14.5 seconds
if (peripheral.writeCharacteristic(service, characteristic, flysightCommandHeartbeat, WriteType.WITHOUT_RESPONSE)) {
Thread.sleep(14500);
} else {
Log.w(TAG, "Failed to send heartbeat, stopping heartbeat thread");
break;
}
}
} catch (InterruptedException e) {
Log.i(TAG, "Heartbeat thread interrupted");
}
}).start();
}
}

0 comments on commit 725ef5b

Please sign in to comment.