|
1 | 1 | package org.strykeforce.thirdcoast.deadeye; |
2 | 2 |
|
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | + |
3 | 6 | import java.nio.ByteBuffer; |
| 7 | +import java.nio.ByteOrder; |
4 | 8 | import java.util.Arrays; |
5 | 9 |
|
6 | 10 | public class DeadeyeMessage { |
7 | 11 |
|
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; |
11 | 21 |
|
12 | | - public final int type; |
13 | | - public final double[] data = new double[4]; |
14 | 22 | public final int latency; |
| 23 | + public final double[] data = new double[4]; |
| 24 | + |
| 25 | + private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
15 | 26 |
|
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; |
21 | 30 |
|
22 | 31 | 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 |
24 | 40 | latency = buffer.getInt(); |
25 | 41 | for (int i = 0; i < 4; i++) { |
26 | 42 | data[i] = buffer.getDouble(); |
27 | 43 | } |
28 | | - // System.out.println("FRAME DATA:" + Arrays.toString(data)); |
29 | 44 | return; |
30 | | - |
31 | | - case TYPE_PONG: |
32 | | - // System.out.println("PONG DATA"); |
| 45 | + case NODATA_BYTE: |
| 46 | + this.type = Type.NODATA; |
33 | 47 | break; |
34 | | - |
35 | 48 | default: |
36 | | - System.out.println("UNKNOWN TYPE: " + type); |
| 49 | + this.type = Type.ERROR; |
37 | 50 | } |
38 | 51 | latency = 0; |
39 | 52 | } |
40 | 53 |
|
41 | 54 | @Override |
42 | 55 | public String toString() { |
43 | | - return "VisionData{" |
| 56 | + return "DeadeyeMessage{" |
44 | 57 | + "type=" |
45 | | - + String.format("0x%08X", type) |
46 | | - + ", data=" |
47 | | - + Arrays.toString(data) |
| 58 | + + type |
48 | 59 | + ", latency=" |
49 | 60 | + latency |
| 61 | + + ", data=" |
| 62 | + + Arrays.toString(data) |
50 | 63 | + '}'; |
51 | 64 | } |
| 65 | + |
| 66 | + public enum Type { |
| 67 | + HEARTBEAT, |
| 68 | + DATA, |
| 69 | + NODATA, |
| 70 | + ERROR |
| 71 | + } |
52 | 72 | } |
0 commit comments