Skip to content

Commit 4f62c2f

Browse files
committed
Add simple OSC receiver pure data patch
1 parent 6218ed2 commit 4f62c2f

2 files changed

Lines changed: 114 additions & 61 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#N canvas 844 372 663 677 12;
2+
#X obj 179 241 netreceive -u -b;
3+
#X msg 179 179 listen 0;
4+
#X obj 179 270 oscparse;
5+
#X obj 262 148 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000;
6+
#X obj 179 149 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000;
7+
#X msg 262 179 listen 12345;
8+
#X floatatom 317 471 5 0 0 0 - - - 0;
9+
#X floatatom 225 471 5 0 0 0 - - - 0;
10+
#X floatatom 271 471 5 0 0 0 - - - 0;
11+
#X obj 179 343 route openbci;
12+
#X obj 179 302 list trim;
13+
#X msg 179 470;
14+
#X obj 354 250 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000;
15+
#X obj 354 311 list prepend symbol;
16+
#X obj 354 340 list trim;
17+
#X obj 92 431 print data;
18+
#X obj 179 383 route accelerometer;
19+
#X text 423 126 Enter the address you'd like to receive here. This is the same as the Address textfield in the GUI. Example: accelerometer, f 23;
20+
#X text 437 213 Clicking the bang would change the routing address to the new value., f 21;
21+
#X obj 179 417 unpack s f f f f f f f f, f 53;
22+
#X floatatom 455 471 5 0 0 0 - - - 0;
23+
#X floatatom 363 471 5 0 0 0 - - - 0;
24+
#X floatatom 409 471 5 0 0 0 - - - 0;
25+
#X floatatom 547 471 5 0 0 0 - - - 0;
26+
#X floatatom 501 471 5 0 0 0 - - - 0;
27+
#X text 437 339 After data is routed \, we are left with a list of floats that can be unpacked., f 21;
28+
#X text 119 534 Happy Hacking! We're excited to see what our global Community can continue to create using OpenBCI hardware and software! -RW, f 62;
29+
#X text 48 256 PureData adds the "list" selector after our binary data is parsed., f 16;
30+
#X text 297 24 Example Incoming OSC Packet: openbci accel 0.830266 0.842021 0.855461, f 41;
31+
#X msg 354 283 accelerometer;
32+
#X text 152 107 Turn off netreceive, f 10;
33+
#X text 249 107 Start recieving on named port, f 16;
34+
#X text 34 25 OpenBCI OSC Reciever Example June 14 \, 2023 Richard Waltman, f 31;
35+
#X connect 0 0 2 0;
36+
#X connect 1 0 0 0;
37+
#X connect 2 0 10 0;
38+
#X connect 3 0 5 0;
39+
#X connect 4 0 1 0;
40+
#X connect 5 0 0 0;
41+
#X connect 9 0 16 0;
42+
#X connect 10 0 9 0;
43+
#X connect 12 0 29 0;
44+
#X connect 13 0 14 0;
45+
#X connect 14 0 16 1;
46+
#X connect 16 0 15 0;
47+
#X connect 16 0 19 0;
48+
#X connect 19 0 11 0;
49+
#X connect 19 1 7 0;
50+
#X connect 19 2 8 0;
51+
#X connect 19 3 6 0;
52+
#X connect 19 4 21 0;
53+
#X connect 19 5 22 0;
54+
#X connect 19 6 20 0;
55+
#X connect 19 7 24 0;
56+
#X connect 19 8 23 0;
57+
#X connect 29 0 13 0;

OpenBCI_GUI/W_Networking.pde

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,70 +1921,66 @@ class Stream extends Thread {
19211921

19221922

19231923
void sendAccelerometerData() {
1924-
// UNFILTERED & FILTERED, Accel data is not affected by filters anyways
1925-
if (this.filter==false || this.filter==true) {
1926-
// OSC
1927-
if (this.protocol.equals("OSC")) {
1928-
for (int i = 0; i < NUM_ACCEL_DIMS; i++) {
1929-
msg.clearArguments();
1930-
msg.add(i+1);
1931-
//ADD Accelerometer data
1932-
msg.add(w_accelerometer.getLastAccelVal(i));
1933-
// println(i + " | " + w_accelerometer.getLastAccelVal(i));
1934-
try {
1935-
this.osc.send(msg,this.netaddress);
1936-
} catch (Exception e) {
1937-
println(e.getMessage());
1938-
}
1939-
}
1940-
// UDP
1941-
} else if (this.protocol.equals("UDP")) {
1942-
String outputter = "{\"type\":\"accelerometer\",\"data\":[";
1943-
for (int i = 0; i < NUM_ACCEL_DIMS; i++) {
1944-
float accelData = w_accelerometer.getLastAccelVal(i);
1945-
String accelData_3dec = threeDecimalPlaces.format(accelData);
1946-
//String accelData_3dec = String.format("%.3f", accelData); //This does not work in all international settings
1947-
outputter += accelData_3dec;
1948-
if (i != NUM_ACCEL_DIMS - 1) {
1949-
outputter += ",";
1950-
} else {
1951-
outputter += "]}\r\n";
1952-
}
1953-
}
1954-
try {
1955-
this.udp.send(outputter, this.ip, this.port);
1956-
} catch (Exception e) {
1957-
println(e.getMessage());
1958-
}
1959-
// LSL
1960-
} else if (this.protocol.equals("LSL")) {
1961-
for (int i = 0; i < NUM_ACCEL_DIMS; i++) {
1962-
dataToSend[i] = w_accelerometer.getLastAccelVal(i);
1963-
}
1964-
// Add timestamp to LSL Stream
1965-
outlet_data.push_sample(dataToSend);
1966-
} else if (this.protocol.equals("Serial")) {
1967-
// Data Format: +0.900,-0.042,+0.254\n
1968-
// 7 chars per axis, including \n char for Z
1969-
serialMessage = "";
1970-
for (int i = 0; i < NUM_ACCEL_DIMS; i++) {
1971-
float accelData = w_accelerometer.getLastAccelVal(i);
1972-
String accelData_3dec = String.format("%.3f", accelData);
1973-
if (accelData >= 0) serialMessage += "+";
1974-
serialMessage += accelData_3dec;
1975-
if (i != NUM_ACCEL_DIMS - 1) {
1976-
serialMessage += ",";
1977-
} else {
1978-
serialMessage += "\n";
1979-
}
1924+
//Accelerometer data is not affected by filters
1925+
// OSC
1926+
if (this.protocol.equals("OSC")) {
1927+
msg.clearArguments();
1928+
msg.setAddrPattern(address + "/accelerometer/");
1929+
for (int i = 0; i < NUM_ACCEL_DIMS; i++) {
1930+
msg.add(w_accelerometer.getLastAccelVal(i));
1931+
}
1932+
try {
1933+
this.osc.send(msg,this.netaddress);
1934+
} catch (Exception e) {
1935+
println(e.getMessage());
1936+
}
1937+
// UDP
1938+
} else if (this.protocol.equals("UDP")) {
1939+
String outputter = "{\"type\":\"accelerometer\",\"data\":[";
1940+
for (int i = 0; i < NUM_ACCEL_DIMS; i++) {
1941+
float accelData = w_accelerometer.getLastAccelVal(i);
1942+
String accelData_3dec = threeDecimalPlaces.format(accelData);
1943+
//String accelData_3dec = String.format("%.3f", accelData); //This does not work in all international settings
1944+
outputter += accelData_3dec;
1945+
if (i != NUM_ACCEL_DIMS - 1) {
1946+
outputter += ",";
1947+
} else {
1948+
outputter += "]}\r\n";
19801949
}
1981-
try {
1982-
//println(serialMessage);
1983-
this.serial_networking.write(serialMessage);
1984-
} catch (Exception e) {
1985-
println(e.getMessage());
1950+
}
1951+
try {
1952+
this.udp.send(outputter, this.ip, this.port);
1953+
} catch (Exception e) {
1954+
println(e.getMessage());
1955+
}
1956+
// LSL
1957+
} else if (this.protocol.equals("LSL")) {
1958+
for (int i = 0; i < NUM_ACCEL_DIMS; i++) {
1959+
dataToSend[i] = w_accelerometer.getLastAccelVal(i);
1960+
}
1961+
// Add timestamp to LSL Stream
1962+
outlet_data.push_sample(dataToSend);
1963+
} else if (this.protocol.equals("Serial")) {
1964+
// Data Format: +0.900,-0.042,+0.254\n
1965+
// 7 chars per axis, including \n char for Z
1966+
serialMessage = "";
1967+
for (int i = 0; i < NUM_ACCEL_DIMS; i++) {
1968+
float accelData = w_accelerometer.getLastAccelVal(i);
1969+
String accelData_3dec = String.format("%.3f", accelData);
1970+
if (accelData >= 0) serialMessage += "+";
1971+
serialMessage += accelData_3dec;
1972+
if (i != NUM_ACCEL_DIMS - 1) {
1973+
serialMessage += ",";
1974+
} else {
1975+
serialMessage += "\n";
19861976
}
19871977
}
1978+
try {
1979+
//println(serialMessage);
1980+
this.serial_networking.write(serialMessage);
1981+
} catch (Exception e) {
1982+
println(e.getMessage());
1983+
}
19881984
}
19891985
}
19901986

0 commit comments

Comments
 (0)