Skip to content

Commit f52cadc

Browse files
committed
Refactor parts of NetworkStreamOut for developer readability and per code review comments
1 parent df7f022 commit f52cadc

1 file changed

Lines changed: 103 additions & 85 deletions

File tree

OpenBCI_GUI/NetworkStreamOut.pde

Lines changed: 103 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -133,39 +133,47 @@ class NetworkStreamOut extends Thread {
133133
}
134134

135135
public void run() {
136-
if (!this.protocol.equals("LSL")) {
137-
openNetwork();
138-
while (this.isStreaming) {
139-
if (!currentBoard.isStreaming()) {
136+
if (this.protocol.equals("LSL")) {
137+
runLSL();
138+
} else {
139+
runUdpOscSerial();
140+
}
141+
}
142+
143+
private void runLSL() {
144+
if (currentBoard.isStreaming()) {
145+
// This method has been updated to reduce duplicate packets - RW 3/15/23
146+
if (checkForData()) {
147+
sendData();
148+
}
149+
} else {
150+
try {
151+
Thread.sleep(1);
152+
} catch (InterruptedException e) {
153+
println(e.getMessage());
154+
}
155+
}
156+
}
157+
158+
private void runUdpOscSerial() {
159+
openNetwork();
160+
while (this.isStreaming) {
161+
if (currentBoard.isStreaming()) {
162+
if (checkForData()) {
163+
sendData();
164+
} else {
140165
try {
141166
Thread.sleep(1);
142167
} catch (InterruptedException e) {
143168
println(e.getMessage());
144169
}
145-
} else {
146-
if (checkForData()) {
147-
sendData();
148-
} else {
149-
try {
150-
Thread.sleep(1);
151-
} catch (InterruptedException e) {
152-
println(e.getMessage());
153-
}
154-
}
155170
}
156-
}
157-
} else if (this.protocol.equals("LSL")) {
158-
if (!currentBoard.isStreaming()) {
171+
} else {
159172
try {
160173
Thread.sleep(1);
161174
} catch (InterruptedException e) {
162175
println(e.getMessage());
163176
}
164-
} else {
165-
// This method has been updated to reduce duplicate packets - RW 3/15/23
166-
if (checkForData()) {
167-
sendData();
168-
}
169177
}
170178
}
171179
}
@@ -221,43 +229,55 @@ class NetworkStreamOut extends Thread {
221229
}
222230

223231
private void sendData() {
224-
if (this.dataType.equals("TimeSeriesFilt") || this.dataType.equals("TimeSeriesRaw")) {
225-
sendTimeSeriesData();
226-
} else if (this.dataType.equals("Focus")) {
227-
sendFocusData();
228-
} else if (this.dataType.equals("FFT")) {
229-
sendFFTData();
230-
} else if (this.dataType.equals("EMG")) {
231-
sendEMGData();
232-
} else if (this.dataType.equals("AvgBandPower")) {
233-
sendNormalizedPowerBandData();
234-
} else if (this.dataType.equals("BandPower")) {
235-
sendPowerBandData();
236-
} else if (this.dataType.equals("Accel/Aux")) {
237-
if (currentBoard instanceof AccelerometerCapableBoard) {
238-
AccelerometerCapableBoard accelBoard = (AccelerometerCapableBoard) currentBoard;
239-
if (accelBoard.isAccelerometerActive()) {
240-
sendAccelerometerData();
232+
switch (this.dataType) {
233+
case "TimeSeriesRaw":
234+
case "TimeSeriesFilt":
235+
sendTimeSeriesData();
236+
break;
237+
case "Focus":
238+
sendFocusData();
239+
break;
240+
case "FFT":
241+
sendFFTData();
242+
break;
243+
case "EMG":
244+
sendEMGData();
245+
break;
246+
case "AvgBandPower":
247+
sendNormalizedPowerBandData();
248+
break;
249+
case "BandPower":
250+
sendPowerBandData();
251+
break;
252+
case "Accel/Aux":
253+
if (currentBoard instanceof AccelerometerCapableBoard) {
254+
AccelerometerCapableBoard accelBoard = (AccelerometerCapableBoard) currentBoard;
255+
if (accelBoard.isAccelerometerActive()) {
256+
sendAccelerometerData();
257+
}
241258
}
242-
}
243-
if (currentBoard instanceof AnalogCapableBoard) {
244-
AnalogCapableBoard analogBoard = (AnalogCapableBoard) currentBoard;
245-
if (analogBoard.isAnalogActive()) {
246-
sendAnalogReadData();
259+
if (currentBoard instanceof AnalogCapableBoard) {
260+
AnalogCapableBoard analogBoard = (AnalogCapableBoard) currentBoard;
261+
if (analogBoard.isAnalogActive()) {
262+
sendAnalogReadData();
263+
}
247264
}
248-
}
249-
if (currentBoard instanceof DigitalCapableBoard) {
250-
DigitalCapableBoard digitalBoard = (DigitalCapableBoard) currentBoard;
251-
if (digitalBoard.isDigitalActive()) {
252-
sendDigitalReadData();
265+
if (currentBoard instanceof DigitalCapableBoard) {
266+
DigitalCapableBoard digitalBoard = (DigitalCapableBoard) currentBoard;
267+
if (digitalBoard.isDigitalActive()) {
268+
sendDigitalReadData();
269+
}
253270
}
254-
}
255-
} else if (this.dataType.equals("Pulse")) {
256-
sendPulseData();
257-
} else if (this.dataType.equals("EMGJoystick")) {
258-
sendEMGJoystickData();
259-
} else if (this.dataType.equals("Marker")) {
260-
sendMarkerData();
271+
break;
272+
case "Pulse":
273+
sendPulseData();
274+
break;
275+
case "EMGJoystick":
276+
sendEMGJoystickData();
277+
break;
278+
case "Marker":
279+
sendMarkerData();
280+
break;
261281
}
262282
}
263283

@@ -384,32 +404,32 @@ class NetworkStreamOut extends Thread {
384404

385405
// Send out 1 or 0 as an integer over all networking data types for "Focus" data
386406
private void sendFocusData() {
387-
final int IS_METRIC = w_focus.getMetricExceedsThreshold();
407+
final int metricValue = w_focus.getMetricExceedsThreshold();
388408
if (this.protocol.equals("OSC")) {
389409
msg.clearArguments();
390410
msg.setAddrPattern(baseOscAddress + "/focus");
391-
msg.add(IS_METRIC);
411+
msg.add(metricValue);
392412
try {
393413
this.osc.send(msg, this.oscNetAddress);
394414
} catch (Exception e) {
395415
println(e.getMessage());
396416
}
397417
} else if (this.protocol.equals("UDP")) {
398418
StringBuilder sb = new StringBuilder("{\"type\":\"focus\",\"data\":");
399-
sb.append(str(IS_METRIC));
419+
sb.append(str(metricValue));
400420
sb.append("}\r\n");
401421
try {
402422
this.udp.send(sb.toString(), this.ip, this.port);
403423
} catch (Exception e) {
404424
println(e.getMessage());
405425
}
406426
} else if (this.protocol.equals("LSL")) {
407-
float[] output = new float[] { (float) IS_METRIC };
427+
float[] output = new float[] { (float) metricValue };
408428
outlet_data.push_sample(output);
409429
// Serial
410430
} else if (this.protocol.equals("Serial")) {
411431
StringBuilder sb = new StringBuilder();
412-
sb.append(IS_METRIC);
432+
sb.append(metricValue);
413433
sb.append("\n");
414434
try {
415435
// println("SerialMessage: " + serialMessage);
@@ -486,16 +506,16 @@ class NetworkStreamOut extends Thread {
486506
}
487507

488508
private void sendPowerBandData() {
489-
// UNFILTERED & FILTERED ... influenced globally by the FFT filters dropdown
490-
// just like the FFT data
491-
final int NUM_BAND_POWERS = 5; // DELTA, THETA, ALPHA, BETA, GAMMA
509+
// UNFILTERED & FILTERED: Influenced globally by the FFT filters dropdown
510+
// Band Power order: DELTA, THETA, ALPHA, BETA, GAMMA
511+
final int NUM_BAND_POWERS = 5;
492512

493513
if (this.protocol.equals("OSC")) {
494-
for (int i = 0; i < numExgChannels; i++) {
514+
for (int channel = 0; channel < numExgChannels; channel++) {
495515
msg.clearArguments();
496-
msg.setAddrPattern(baseOscAddress + "/band-power/" + i);
497-
for (int j = 0; j < NUM_BAND_POWERS; j++) {
498-
msg.add(dataProcessing.avgPowerInBins[i][j]); // [CHAN][BAND]
516+
msg.setAddrPattern(baseOscAddress + "/band-power/" + channel);
517+
for (int band = 0; band < NUM_BAND_POWERS; band++) {
518+
msg.add(dataProcessing.avgPowerInBins[channel][band]); // [CHAN][BAND]
499519
}
500520
try {
501521
this.osc.send(msg, this.oscNetAddress);
@@ -506,14 +526,14 @@ class NetworkStreamOut extends Thread {
506526
} else if (this.protocol.equals("UDP")) {
507527
// DELTA, THETA, ALPHA, BETA, GAMMA
508528
String outputter = "{\"type\":\"bandPower\",\"data\":[[";
509-
for (int i = 0; i < numExgChannels; i++) {
510-
for (int j = 0; j < NUM_BAND_POWERS; j++) {
511-
outputter += str(dataProcessing.avgPowerInBins[i][j]); // [CHAN][BAND]
512-
if (j != NUM_BAND_POWERS - 1) {
529+
for (int channel = 0; channel < numExgChannels; channel++) {
530+
for (int band = 0; band < NUM_BAND_POWERS; band++) {
531+
outputter += str(dataProcessing.avgPowerInBins[channel][band]); // [CHAN][BAND]
532+
if (band != NUM_BAND_POWERS - 1) {
513533
outputter += ",";
514534
}
515535
}
516-
if (i != numExgChannels - 1) {
536+
if (channel != numExgChannels - 1) {
517537
outputter += "],[";
518538
} else {
519539
outputter += "]]}\r\n";
@@ -546,13 +566,15 @@ class NetworkStreamOut extends Thread {
546566
}
547567

548568
} else if (this.protocol.equals("Serial")) {
549-
for (int i = 0; i < numExgChannels; i++) {
550-
serialMessage = "[" + (i + 1) + ","; // clear message
551-
for (int j = 0; j < NUM_BAND_POWERS; j++) {
552-
float power_band = dataProcessing.avgPowerInBins[i][j];
553-
String power_band_3dec = threeDecimalPlaces.format(power_band);
554-
serialMessage += power_band_3dec;
555-
if (j < NUM_BAND_POWERS - 1) {
569+
570+
// Send out band powers for each channel sequentially
571+
for (int channel = 0; channel < numExgChannels; channel++) {
572+
serialMessage = "[" + (channel + 1) + ","; // clear message
573+
for (int band = 0; band < NUM_BAND_POWERS; band++) {
574+
float value = dataProcessing.avgPowerInBins[channel][band];
575+
String valueFormatted = threeDecimalPlaces.format(value);
576+
serialMessage += valueFormatted;
577+
if (band < NUM_BAND_POWERS - 1) {
556578
serialMessage += ","; // add a comma to serialMessage to separate chan values, as long as it
557579
// isn't last value...
558580
}
@@ -565,6 +587,7 @@ class NetworkStreamOut extends Thread {
565587
println(e.getMessage());
566588
}
567589
}
590+
568591
}
569592
}
570593

@@ -1202,13 +1225,9 @@ class NetworkStreamOut extends Thread {
12021225
private void openNetwork() {
12031226
println("Networking: " + getAttributes());
12041227
if (this.protocol.equals("OSC")) {
1205-
// Possibly enter a nice custom exception here
1206-
// try {
12071228
this.osc = new OscP5(this, this.port + 1000);
12081229
this.oscNetAddress = new NetAddress(this.ip, this.port);
12091230
this.msg = new OscMessage(this.baseOscAddress);
1210-
// } catch (Exception e) {
1211-
// }
12121231
} else if (this.protocol.equals("UDP")) {
12131232
this.udp = new UDP(this);
12141233
this.udp.setBuffer(20000);
@@ -1221,7 +1240,6 @@ class NetworkStreamOut extends Thread {
12211240
currentBoard.getSampleRate(), LSL.ChannelFormat.float32, stream_id);
12221241
outlet_data = new LSL.StreamOutlet(info_data);
12231242
} else if (this.protocol.equals("Serial")) {
1224-
// Open Serial Port! %%%%%
12251243
try {
12261244
serial_networking = new processing.serial.Serial(this.pApplet, this.portName, this.baudRate);
12271245
serial_networking.clear();

0 commit comments

Comments
 (0)