Skip to content

Commit

Permalink
Merge pull request #1140 from OpenBCI/june2023-networking-refactor
Browse files Browse the repository at this point in the history
Refactor Networking Widget - June 2023
  • Loading branch information
retiutut authored Jun 26, 2023
2 parents bf3372b + c6b44ae commit 7b971f2
Show file tree
Hide file tree
Showing 10 changed files with 1,314 additions and 1,356 deletions.
7 changes: 4 additions & 3 deletions Networking-Test-Kit/LSL/lslStreamTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# create a new inlet to read from the stream
inlet = StreamInlet(streams[0])
duration = 5
duration = 10

sleep(1)

Expand All @@ -24,12 +24,13 @@ def testLSLSamplingRate():
# get chunks of samples
chunk, timestamp = inlet.pull_chunk()
if chunk:
print("\nNew chunk!")
numChunks += 1
# print( len(chunk) )
totalNumSamples += len(chunk)
# print(chunk);
# print(chunk)
for sample in chunk:
# print(sample)
print(sample)
validSamples += 1

print( "Number of Chunks and Samples == {} , {}".format(numChunks, totalNumSamples) )
Expand Down
10 changes: 5 additions & 5 deletions Networking-Test-Kit/LSL/lslStreamTest_3Streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import time

numStreams = 3
duration_seconds = 20
duration_seconds = 10
# first resolve an EEG stream on the lab network
print("looking for an EEG stream...")
stream_1 = resolve_stream('type', 'EEG')
Expand All @@ -19,8 +19,8 @@

# create a new inlet to read from the stream
inlet = StreamInlet(stream_1[0])
intlet_2 = StreamInlet(stream_2[0])
intlet_3 = StreamInlet(stream_3[0])
inlet_2 = StreamInlet(stream_2[0])
inlet_3 = StreamInlet(stream_3[0])

def testLSLSamplingRates():
print( "Testing Sampling Rates for {} seconds".format(duration_seconds) )
Expand All @@ -40,13 +40,13 @@ def testLSLSamplingRates():
num_samples_1 += 1
#print(sample)
elif i == 1:
chunk, timestamps_2 = intlet_2.pull_chunk()
chunk, timestamps_2 = inlet_2.pull_chunk()
if timestamps_2:
for sample in chunk:
num_samples_2 += 1
#print(sample)
elif i == 2:
chunk, timestamps_3 = intlet_3.pull_chunk()
chunk, timestamps_3 = inlet_3.pull_chunk()
if timestamps_3:
for sample in chunk:
num_samples_3 += 1
Expand Down
36 changes: 36 additions & 0 deletions Networking-Test-Kit/LSL/lslStreamTest_PullSample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Here we show that we can use push_sample to send and pull_chunk to receive a sample."""
import time
from pylsl import StreamInlet, resolve_stream
from time import sleep

# first resolve an EEG stream on the lab network
print("looking for an EEG stream...")
streams = resolve_stream('type', 'FOCUS')

# create a new inlet to read from the stream
inlet = StreamInlet(streams[0])
duration = 5

sleep(1)

def testLSLSamplingRate():
start = time.time()
totalNumSamples = 0
validSamples = 0
numChunks = 0
print( "Testing Sampling Rates..." )

while time.time() <= start + duration:
# print(time.time())
# get chunks of samples
sample, timestamp = inlet.pull_chunk()
if sample:
print(sample)
validSamples += 1

#print( "Number of Chunks and Samples == {} , {}".format(numChunks, totalNumSamples) )
#print( "Valid Samples and Duration == {} / {}".format(validSamples, duration) )
print( "Avg Sampling Rate == {}".format(validSamples / duration) )


testLSLSamplingRate()
42 changes: 42 additions & 0 deletions Networking-Test-Kit/OSC/Complex OSC Receive.pd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#N canvas -1158 177 945 588 12;
#X obj 179 241 netreceive -u -b;
#X msg 179 179 listen 0;
#X obj 179 270 oscparse;
#X obj 262 148 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000;
#X obj 179 149 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000;
#X msg 262 179 listen 12345;
#X obj 179 343 route openbci;
#X obj 179 302 list trim;
#X obj 354 250 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000;
#X obj 354 311 list prepend symbol;
#X obj 354 340 list trim;
#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;
#X text 437 213 Clicking the bang would change the routing address to the new value., f 21;
#X text 437 339 After data is routed \, we are left with a list of floats that can be unpacked., f 21;
#X text 149 508 Happy Hacking! We're excited to see what our global Community can continue to create using OpenBCI hardware and software! -RW, f 62;
#X text 48 256 PureData adds the "list" selector after our binary data is parsed., f 16;
#X text 152 107 Turn off netreceive, f 10;
#X text 249 107 Start recieving on named port, f 16;
#X text 647 22 Example Incoming OSC Packet;
#X obj 179 376 route time-series-raw;
#X msg 354 283 time-series-raw;
#X obj 52 427 print rawmsg;
#X obj 179 419 route ch0 ch1 ch2 ch3;
#X obj 179 461 print channel0;
#X text 40 61 openbci time-series-raw ch0 -3.92592 -3.88553 -3.26396 -3.62399 -2.70645 -1.6476 -0.789745 0.749153 1.55314 3.081, f 114;
#X text 32 18 OpenBCI OSC Reciever Example June 21 \, 2023 Richard Waltman, f 31;
#X connect 0 0 2 0;
#X connect 1 0 0 0;
#X connect 2 0 7 0;
#X connect 3 0 5 0;
#X connect 4 0 1 0;
#X connect 5 0 0 0;
#X connect 6 0 19 0;
#X connect 7 0 6 0;
#X connect 7 0 21 0;
#X connect 8 0 20 0;
#X connect 9 0 10 0;
#X connect 10 0 19 1;
#X connect 19 0 22 0;
#X connect 20 0 9 0;
#X connect 22 0 23 0;
46 changes: 46 additions & 0 deletions Networking-Test-Kit/OSC/Simple OSC Receive.pd
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#N canvas 3103 183 661 588 12;
#X obj 179 241 netreceive -u -b;
#X msg 179 179 listen 0;
#X obj 179 270 oscparse;
#X obj 262 148 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000;
#X obj 179 149 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000;
#X msg 262 179 listen 12345;
#X obj 179 343 route openbci;
#X obj 179 302 list trim;
#X obj 354 250 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000;
#X obj 354 311 list prepend symbol;
#X obj 354 340 list trim;
#X obj 69 389 print data;
#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;
#X text 437 213 Clicking the bang would change the routing address to the new value., f 21;
#X text 437 339 After data is routed \, we are left with a list of floats that can be unpacked., f 21;
#X text 149 508 Happy Hacking! We're excited to see what our global Community can continue to create using OpenBCI hardware and software! -RW, f 62;
#X text 48 256 PureData adds the "list" selector after our binary data is parsed., f 16;
#X text 152 107 Turn off netreceive, f 10;
#X text 249 107 Start recieving on named port, f 16;
#X text 34 25 OpenBCI OSC Reciever Example June 14 \, 2023 Richard Waltman, f 31;
#X obj 179 383 route accelerometer;
#X msg 354 283 accelerometer;
#X obj 179 421 route x y z, f 21;
#X floatatom 179 466 5 0 0 0 - - - 0;
#X floatatom 275 466 5 0 0 0 - - - 0;
#X floatatom 227 466 5 0 0 0 - - - 0;
#X text 394 19 Example Incoming OSC Packet;
#X text 359 41 openbci accelerometer x 0.986508;
#X connect 0 0 2 0;
#X connect 1 0 0 0;
#X connect 2 0 7 0;
#X connect 3 0 5 0;
#X connect 4 0 1 0;
#X connect 5 0 0 0;
#X connect 6 0 20 0;
#X connect 7 0 6 0;
#X connect 7 0 11 0;
#X connect 8 0 21 0;
#X connect 9 0 10 0;
#X connect 10 0 20 1;
#X connect 20 0 22 0;
#X connect 21 0 9 0;
#X connect 22 0 23 0;
#X connect 22 1 25 0;
#X connect 22 2 24 0;
4 changes: 2 additions & 2 deletions Networking-Test-Kit/OSC/osc_receive.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def close_file(*args):
# Set up necessary parameters from command line
dispatcher = dispatcher.Dispatcher()
if args.option=="print":
dispatcher.map("/openbci", print_message)
dispatcher.map(args.address, print_message)
signal.signal(signal.SIGINT, exit_print)

elif args.option=="record":
Expand All @@ -68,7 +68,7 @@ def close_file(*args):
textfile.write("time,address,messages\n")
textfile.write("-------------------------\n")
print("Recording to %s" % filename)
dispatcher.map("/openbci", record_to_file)
dispatcher.map(args.address, record_to_file)
signal.signal(signal.SIGINT, close_file)

# Display server attributes
Expand Down
110 changes: 110 additions & 0 deletions Networking-Test-Kit/UDP/udp_receive_timeSeriesAllChannels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import socket
import sys
import time
import argparse
import signal
import struct
import os
import json

numSamples = 0

# Print received message to console
def print_message(*args):
try:
# print(args[0]) #added to see raw data
obj = json.loads(args[0].decode())
print(obj.get('data'))
numSamplesInChannelOne = len(obj.get('data')[0])
global numSamples
print("NumSamplesInPacket_ChannelOne == " + str(numSamplesInChannelOne))
numSamples += numSamplesInChannelOne
if obj:
return True
else:
return False
except BaseException as e:
print(e)
return False
# print("(%s) RECEIVED MESSAGE: " % time.time() +
# ''.join(str(struct.unpack('>%df' % int(length), args[0]))))

# Clean exit from print mode
def exit_print(signal, frame):
print("Closing listener")
sys.exit(0)

# Record received message in text file
def record_to_file(*args):
textfile.write(str(time.time()) + ",")
#textfile.write(args[0])
obj = json.loads(args[0].decode())
#print(obj.get('data'))
textfile.write(json.dumps(obj))
textfile.write("\n")

# Save recording, clean exit from record mode
def close_file(*args):
print("\nFILE SAVED")
textfile.close()
sys.exit(0)

if __name__ == "__main__":
# Collect command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("--ip",
default="127.0.0.1", help="The ip to listen on")
parser.add_argument("--port",
type=int, default=12345, help="The port to listen on")
parser.add_argument("--address",default="/openbci", help="address to listen to")
parser.add_argument("--option",default="print",help="Debugger option")
parser.add_argument("--len",default=9,help="Debugger option")
args = parser.parse_args()

# Set up necessary parameters from command line
length = int(args.len)
if args.option=="print":
signal.signal(signal.SIGINT, exit_print)
elif args.option=="record":
i = 0
while os.path.exists("udp_test%s.txt" % i):
i += 1
filename = "udp_test%i.txt" % i
textfile = open(filename, "w")
textfile.write("time,address,messages\n")
textfile.write("-------------------------\n")
print("Recording to %s" % filename)
signal.signal(signal.SIGINT, close_file)

# Connect to socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = (args.ip, args.port)
sock.bind(server_address)

# Display socket attributes
print('--------------------')
print("-- UDP LISTENER -- ")
print('--------------------')
print("IP:", args.ip)
print("PORT:", args.port)
print('--------------------')
print("%s option selected" % args.option)

# Receive messages
print("Listening...")
start = time.time()

duration = 10
while time.time() <= start + duration:
data, addr = sock.recvfrom(20000) # buffer size is 20000 bytes
if args.option=="print":
print_message(data)
# numSamples += 1
elif args.option=="record":
record_to_file(data)
numSamples += 1

print( "Samples == {}".format(numSamples) )
print( "Duration == {}".format(duration) )
print( "Avg Sampling Rate == {}".format(numSamples / duration) )
7 changes: 6 additions & 1 deletion OpenBCI_GUI/OpenBCI_GUI.pde
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ import java.awt.AWTException;
import netP5.*; // for OSC
import oscP5.*; // for OSC
import hypermedia.net.*; //for UDP
import java.nio.ByteBuffer; //for UDP
import java.nio.ByteBuffer; //for BDF file writing
import edu.ucsd.sccn.LSL; //for LSL
import com.fazecast.jSerialComm.*; //Helps distinguish serial ports on Windows
import org.apache.commons.lang3.time.StopWatch;
import http.requests.*;
import java.util.concurrent.atomic.AtomicBoolean;


//------------------------------------------------------------------------
Expand Down Expand Up @@ -417,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 @@ -518,6 +520,9 @@ synchronized void draw() {
initSystem();
reinitRequested = false;
}
if (systemMode == SYSTEMMODE_POSTINIT) {
w_networking.compareAndSetNetworkingFrameLocks();
}
} else if (systemMode == SYSTEMMODE_INTROANIMATION) {
if (settings.introAnimationInit == 0) {
settings.introAnimationInit = millis();
Expand Down
Loading

0 comments on commit 7b971f2

Please sign in to comment.