Skip to content

Commit 504490e

Browse files
committed
Implement basic vision interface.
1 parent 351501a commit 504490e

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed
Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,72 @@
11
package org.strykeforce.thirdcoast.deadeye;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
import java.nio.ByteBuffer;
7+
import java.nio.ByteOrder;
48
import java.util.Arrays;
59

610
public class DeadeyeMessage {
711

8-
public static final int TYPE_FRAME_DATA = 0xDEADDA7A;
9-
public static final int TYPE_PING = 0xDEADBACC;
10-
public static final int TYPE_PONG = 0xDEADCCAB;
12+
public static final ByteOrder BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;
13+
14+
public static final byte ERROR_BYTE = (byte) 0;
15+
public static final byte HEARTBEAT_BYTE = (byte) 1;
16+
public static final byte[] HEARTBEAT_BYTES = {HEARTBEAT_BYTE};
17+
public static final byte DATA_BYTE = (byte) 2;
18+
public static final byte NODATA_BYTE = (byte) 3;
19+
20+
public final Type type;
1121

12-
public final int type;
13-
public final double[] data = new double[4];
1422
public final int latency;
23+
public final double[] data = new double[4];
24+
25+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
1526

16-
DeadeyeMessage(byte[] bytes) {
17-
ByteBuffer buffer = ByteBuffer.wrap(bytes);
18-
buffer.order(DeadeyeService.BYTE_ORDER);
19-
buffer.rewind();
20-
type = buffer.getInt();
27+
public DeadeyeMessage(byte[] bytes) {
28+
29+
byte type = bytes.length > 0 ? bytes[0] : ERROR_BYTE;
2130

2231
switch (type) {
23-
case TYPE_FRAME_DATA:
32+
case HEARTBEAT_BYTE:
33+
this.type = Type.HEARTBEAT;
34+
break;
35+
case DATA_BYTE:
36+
this.type = Type.DATA;
37+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
38+
buffer.order(BYTE_ORDER);
39+
buffer.position(Integer.BYTES); // skip over 1 integer
2440
latency = buffer.getInt();
2541
for (int i = 0; i < 4; i++) {
2642
data[i] = buffer.getDouble();
2743
}
28-
// System.out.println("FRAME DATA:" + Arrays.toString(data));
2944
return;
30-
31-
case TYPE_PONG:
32-
// System.out.println("PONG DATA");
45+
case NODATA_BYTE:
46+
this.type = Type.NODATA;
3347
break;
34-
3548
default:
36-
System.out.println("UNKNOWN TYPE: " + type);
49+
this.type = Type.ERROR;
3750
}
3851
latency = 0;
3952
}
4053

4154
@Override
4255
public String toString() {
43-
return "VisionData{"
56+
return "DeadeyeMessage{"
4457
+ "type="
45-
+ String.format("0x%08X", type)
46-
+ ", data="
47-
+ Arrays.toString(data)
58+
+ type
4859
+ ", latency="
4960
+ latency
61+
+ ", data="
62+
+ Arrays.toString(data)
5063
+ '}';
5164
}
65+
66+
public enum Type {
67+
HEARTBEAT,
68+
DATA,
69+
NODATA,
70+
ERROR
71+
}
5272
}

deadeye/src/main/java/org/strykeforce/thirdcoast/deadeye/DeadeyeService.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,18 @@
99

1010
import java.net.DatagramPacket;
1111
import java.net.InetSocketAddress;
12-
import java.nio.ByteBuffer;
13-
import java.nio.ByteOrder;
1412

1513
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1614
import static org.strykeforce.thirdcoast.deadeye.ConnectionEvent.CONNECTED;
1715
import static org.strykeforce.thirdcoast.deadeye.ConnectionEvent.DISCONNECTED;
16+
import static org.strykeforce.thirdcoast.deadeye.DeadeyeMessage.Type.HEARTBEAT;
1817

1918
public class DeadeyeService {
20-
public static final ByteOrder BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;
2119

2220
private static final int PING_INTERVAL = 100;
2321
private static final int PONG_LIMIT = PING_INTERVAL * 4;
2422
private static final int PORT = 5555;
2523

26-
private static final byte[] PING_BYTES =
27-
ByteBuffer.allocate(4).order(BYTE_ORDER).putInt(DeadeyeMessage.TYPE_PING).array();
28-
2924
private final Logger logger = LoggerFactory.getLogger(this.getClass());
3025

3126
// this IP address is hardcoded for tethering in Android 6.0 Marshmallow
@@ -44,7 +39,7 @@ public DeadeyeService() {
4439

4540
// send pings
4641
Observable.interval(PING_INTERVAL, MILLISECONDS)
47-
.map(i -> PING_BYTES)
42+
.map(i -> DeadeyeMessage.HEARTBEAT_BYTES)
4843
.subscribe(RxUdp.observerSendingTo(ADDRESS));
4944

5045
// monitor pongs
@@ -58,7 +53,9 @@ public DeadeyeService() {
5853
.share();
5954

6055
pongs =
61-
messageObservable.filter(vd -> vd.type == DeadeyeMessage.TYPE_PONG).timestamp(MILLISECONDS);
56+
messageObservable
57+
.filter(deadeyeMessage -> deadeyeMessage.type == HEARTBEAT)
58+
.timestamp(MILLISECONDS);
6259

6360
heartbeat = Observable.interval(PING_INTERVAL / 2, MILLISECONDS).timestamp(MILLISECONDS);
6461

@@ -81,6 +78,10 @@ public void enableConnectionEventLogging(boolean enable) {
8178
}
8279
}
8380

81+
public Observable<ConnectionEvent> getConnectionEventObservable() {
82+
return connectionEventObservable;
83+
}
84+
8485
public Observable<DeadeyeMessage> getMessageObservable() {
8586
return messageObservable;
8687
}

0 commit comments

Comments
 (0)