-
Notifications
You must be signed in to change notification settings - Fork 0
/
Receiver.pde
62 lines (52 loc) · 1.53 KB
/
Receiver.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import processing.core.PApplet;
import processing.serial.*;
class Receiver extends Thread {
private final String DEVICE = "/dev/tty.Jason-bt00-DevB";
private final int BAUD_RATE = 460800;
//
private final int PACKET_SIZE = 16;
private final int THRESH = 512;
private final int MASK = 0xff;
private final int INVALID = -1;
//
private PApplet parent;
private Data data;
private Serial serial;
private byte[] buffer = new byte[PACKET_SIZE];
//
public Receiver(PApplet parent, Data data) {
this.parent = parent;
this.data = data;
serial = new Serial(parent, DEVICE, BAUD_RATE);
this.start();
}
public void run() {
while(true){
while(serial.available() > 0) {
serial.readBytes(buffer);
int pos;
short val;
pos = FlagPos(0xf1);
if((pos != INVALID) && (pos < PACKET_SIZE - 1))
data.index = (int) buffer[pos + 1];
for(int i = 0; i < 3; i++) {
pos = FlagPos(0xf2 + i);
if((pos != INVALID) && (pos < PACKET_SIZE - 2)) {
val = (short)((buffer[pos + 1] & MASK) << 8 | (buffer[pos + 2] & MASK));
if((val < +THRESH) && (val > -THRESH))
data.acc[i] = val;
}
}
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {}
}
}
private int FlagPos(int flag) {
for(int i = 0; i < PACKET_SIZE; i++)
if(buffer[i] == (byte) flag)
return i;
return INVALID;
}
}