Skip to content

Commit

Permalink
Refactor Networking Widget and Fix TIme Series Output
Browse files Browse the repository at this point in the history
  • Loading branch information
retiutut committed Jun 8, 2023
1 parent 50bba0e commit 43818a5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def print_message(*args):
else:
return False
except BaseException as e:
print(e)
print(e)
return False
# print("(%s) RECEIVED MESSAGE: " % time.time() +
# ''.join(str(struct.unpack('>%df' % int(length), args[0]))))
Expand Down
7 changes: 4 additions & 3 deletions OpenBCI_GUI/OpenBCI_GUI.pde
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,6 @@ boolean isFFTFiltered = true; //yes by default ... this is used in dataProcessin
StringBuilder globalScreenResolution;
StringBuilder globalScreenDPI;

AtomicBoolean networkingFrameLock = new AtomicBoolean(false);

//------------------------------------------------------------------------
// Global Functions
//------------------------------------------------------------------------
Expand Down Expand Up @@ -420,6 +418,7 @@ void setup() {
if (isMac()) {
checkIsMacFullDetail();
}
println("JVM Version: " + System.getProperty("java.version"));
println("Welcome to the Processing-based OpenBCI GUI!"); //Welcome line.
println("For more information, please visit: https://docs.openbci.com/Software/OpenBCISoftware/GUIDocs/");

Expand Down Expand Up @@ -521,7 +520,9 @@ synchronized void draw() {
initSystem();
reinitRequested = false;
}
networkingFrameLock.compareAndSet(false, true);
if (systemMode == SYSTEMMODE_POSTINIT) {
w_networking.networkingFrameLock.compareAndSet(false, true);
}
} else if (systemMode == SYSTEMMODE_INTROANIMATION) {
if (settings.introAnimationInit == 0) {
settings.introAnimationInit = millis();
Expand Down
131 changes: 57 additions & 74 deletions OpenBCI_GUI/W_Networking.pde
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,16 @@ class W_Networking extends Widget {
boolean layoutIsVisible = false;

private LinkedList<double[]> dataAccumulationQueue;
private LinkedList<float[]> dataAccumulationQueue_Filtered;
private LinkedList<float[]> dataAccumulationQueueFiltered;
public float[][] dataBufferToSend;
public float[][] dataBufferToSend_Filtered;
public boolean newTimeSeriesDataToSend = false;
public boolean newTimeSeriesDataToSendFiltered = false;
public AtomicBoolean networkingFrameLock = new AtomicBoolean(false);
public AtomicBoolean newTimeSeriesDataToSend = new AtomicBoolean(false);
public AtomicBoolean newTimeSeriesDataToSendFiltered = new AtomicBoolean(false);

HashMap<String, Object> cp5Map = new HashMap<String, Object>();
public HashMap<String, Object> cp5Map = new HashMap<String, Object>();

List<controlP5.Controller> cp5ElementsToCheck;
private List<controlP5.Controller> cp5ElementsToCheck;

W_Networking(PApplet _parent) {
super(_parent);
Expand Down Expand Up @@ -169,7 +170,7 @@ class W_Networking extends Widget {
dataBufferToSend = new float[currentBoard.getNumEXGChannels()][nPointsPerUpdate];
dataAccumulationQueue = new LinkedList<double[]>();
dataBufferToSend_Filtered = new float[currentBoard.getNumEXGChannels()][nPointsPerUpdate];
dataAccumulationQueue_Filtered = new LinkedList<float[]>();
dataAccumulationQueueFiltered = new LinkedList<float[]>();

cp5ElementsToCheck = new ArrayList<controlP5.Controller>();
cp5ElementsToCheck.add((controlP5.Controller)guideButton);
Expand Down Expand Up @@ -312,14 +313,14 @@ class W_Networking extends Widget {
//println("CHAN== "+iChan+" || SAMPLE== "+iSample+" DATA=="+sample[iChan]);
}
dataAccumulationQueue.add(sample);
dataAccumulationQueue_Filtered.add(sample_filtered);
dataAccumulationQueueFiltered.add(sample_filtered);
}
}

public void checkIfEnoughDataToSend() {
newTimeSeriesDataToSend = dataAccumulationQueue.size() >= nPointsPerUpdate;
newTimeSeriesDataToSend.set(dataAccumulationQueue.size() >= nPointsPerUpdate);

if (newTimeSeriesDataToSend) {
if (newTimeSeriesDataToSend.get()) {
for (int iSample = 0; iSample < nPointsPerUpdate; iSample++) {
double[] sample = dataAccumulationQueue.pop();

Expand All @@ -329,11 +330,11 @@ class W_Networking extends Widget {
}
}

newTimeSeriesDataToSendFiltered = dataAccumulationQueue_Filtered.size() >= nPointsPerUpdate;
newTimeSeriesDataToSendFiltered.set(dataAccumulationQueueFiltered.size() >= nPointsPerUpdate);

if (newTimeSeriesDataToSendFiltered) {
if (newTimeSeriesDataToSendFiltered.get()) {
for (int iSample = 0; iSample < nPointsPerUpdate; iSample++) {
float[] sample = dataAccumulationQueue_Filtered.pop();
float[] sample = dataAccumulationQueueFiltered.pop();

for (int iChan = 0; iChan < sample.length; iChan++) {
dataBufferToSend_Filtered[iChan][iSample] = sample[iChan];
Expand Down Expand Up @@ -1227,52 +1228,49 @@ class W_Networking extends Widget {
////////////////////////////////////////////////////////////////

class Stream extends Thread {
String protocol;
String dataType;
String ip;
int port;
String address;
boolean filter;
String streamType;
String streamName;
int nChanLSL;
int numChan = 0;
DecimalFormat threeDecimalPlaces;
DecimalFormat fourLeadingPlaces;

Boolean isStreaming;
Boolean newData = false;
private String protocol;
private String dataType;
private String ip;
private int port;
private String address;
private boolean filter;
private String streamType;
private String streamName;
private int nChanLSL;
private int numChan = 0;
private DecimalFormat threeDecimalPlaces;
private DecimalFormat fourLeadingPlaces;

public Boolean isStreaming;
// Data buffers set dynamically in updateNumChan()
int start;
float[] dataToSend;
private int start;
private float[] dataToSend;
private double[][] previousFrameData;
private float[] previousDataSingleChannel;
private float[][] previousDataAllChannels;

private int samplesSent = 0;
private int sampleRateClock = 0;
private int sampleRateClockInterval = 10000;

//OSC Objects
OscP5 osc;
NetAddress netaddress;
OscMessage msg;
private OscP5 osc;
private NetAddress netaddress;
private OscMessage msg;
//UDP Objects
UDP udp;
ByteBuffer buffer;
private UDP udp;
private ByteBuffer buffer;
// LSL objects
LSL.StreamInfo info_data;
LSL.StreamOutlet outlet_data;
LSL.StreamInfo info_aux;
LSL.StreamOutlet outlet_aux;
private LSL.StreamInfo info_data;
private LSL.StreamOutlet outlet_data;
private LSL.StreamInfo info_aux;
private LSL.StreamOutlet outlet_aux;

// Serial objects %%%%%
processing.serial.Serial serial_networking;
String portName;
int baudRate;
String serialMessage = "";
private processing.serial.Serial serial_networking;
private String portName;
private int baudRate;
private String serialMessage = "";

PApplet pApplet;
private PApplet pApplet;

private void updateNumChan(int _numChan) {
numChan = _numChan;
Expand Down Expand Up @@ -1311,8 +1309,6 @@ class Stream extends Thread {
this.filter = filter;
this.isStreaming = false;
updateNumChan(_nchan);

previousDataAllChannels = new float[currentBoard.getNumEXGChannels()][nPointsPerUpdate];

//Force decimal formatting for all Locales
Locale currentLocale = Locale.getDefault();
Expand Down Expand Up @@ -1423,14 +1419,14 @@ class Stream extends Thread {
synchronized Boolean checkForData() {
if (this.dataType.equals("TimeSeries")) {
if (this.filter == false) {
return w_networking.newTimeSeriesDataToSend;
return w_networking.newTimeSeriesDataToSend.compareAndSet(true, false);
} else {
return w_networking.newTimeSeriesDataToSendFiltered;
return w_networking.newTimeSeriesDataToSendFiltered.compareAndSet(true, false);
}
}

if (networkingFrameLock.get()) {
networkingFrameLock.set(false);
if (w_networking.networkingFrameLock.get()) {
w_networking.networkingFrameLock.set(false);
return true;
} else {
return false;
Expand Down Expand Up @@ -1491,17 +1487,6 @@ class Stream extends Thread {
}
}

if (Arrays.deepEquals(newDataFromBuffer, previousDataAllChannels)) {
//It's nearly impossible for 2+ channels of data's last 10 samples to be identical.
//This helps prove that new data is available.
//If the previous data is equal to the new data, return. If not, proceed and send out new data.
return;
}

for (int i = 0; i < newDataFromBuffer.length; i++) {
System.arraycopy(newDataFromBuffer[i], 0, previousDataAllChannels[i], 0, newDataFromBuffer[i].length);
}

/*
//Debugging
if (sampleRateClock == 0) sampleRateClock = millis();
Expand All @@ -1518,7 +1503,6 @@ class Stream extends Thread {
samplesSent = 0;
}
*/


if (this.protocol.equals("UDP")) {

Expand All @@ -1539,8 +1523,6 @@ class Stream extends Thread {
//End of entire packet
output.append("]}\r\n");

//println(output.toString());

try {
this.udp.send(output.toString(), this.ip, this.port);
} catch (Exception e) {
Expand Down Expand Up @@ -1573,20 +1555,21 @@ class Stream extends Thread {

} else if (this.protocol.equals("Serial")) {

for (int i=0;i<nPointsPerUpdate;i++) {
for (int i = 0; i < nPointsPerUpdate; i++) {
serialMessage = "["; //clear message
for (int j=0;j<numChan;j++) {
for (int j = 0; j < numChan; j++) {
float chan_uV = w_networking.dataBufferToSend[j][i];//get chan uV float value and truncate to 3 decimal places
String chan_uV_3dec = String.format("%.3f", chan_uV);
serialMessage += chan_uV_3dec;// serialMesage += //add 3 decimal float chan uV value as string to serialMessage
if (j < numChan-1) {
serialMessage += ","; //add a comma to serialMessage to separate chan values, as long as it isn't last value...
StringBuilder chan_uV_3dec = new StringBuilder(String.format("%.3f", chan_uV));
serialMessage.append(chan_uV_3dec);// serialMesage += //add 3 decimal float chan uV value as string to serialMessage
if (j < numChan - 1) {
serialMessage.append(","); //add a comma to serialMessage to separate chan values, as long as it isn't last value...
}
}
serialMessage += "]"; //close the message w/ "]"
serialMessage.append("]"); //close the message w/ "]"
try {
// println(serialMessage);
this.serial_networking.write(serialMessage); //write message to serial
//Write message to serial
this.serial_networking.write(serialMessage.toString());
//println(serialMesage.toString());
} catch (Exception e) {
println(e.getMessage());
}
Expand Down

0 comments on commit 43818a5

Please sign in to comment.