6
6
7
7
# Initialize global variables
8
8
inlet = None
9
- data = np .zeros ((6 , 2000 )) # Buffer to hold the last 2000 samples for each channel
9
+ data = None
10
+ num_channels = 6 # Default number of channels
11
+ curves = []
12
+ plots = []
10
13
11
14
def update_plots ():
12
15
global data
@@ -17,14 +20,14 @@ def update_plots():
17
20
# Update data buffer
18
21
for sample in samples :
19
22
data = np .roll (data , - 1 , axis = 1 ) # Shift data left
20
- data [:, - 1 ] = sample # Add new channel data to the right end of the array
23
+ data [:, - 1 ] = sample [: num_channels ] # Add new channel data to the right end of the array
21
24
22
25
# Update the curves with the new data
23
- for i in range (6 ):
26
+ for i in range (num_channels ):
24
27
curves [i ].setData (data [i ])
25
28
26
29
def plot_lsl_data ():
27
- global inlet
30
+ global inlet , num_channels , data
28
31
print ("Looking for LSL Stream." )
29
32
streams = resolve_stream ('name' , 'BioAmpDataStream' )
30
33
@@ -33,6 +36,15 @@ def plot_lsl_data():
33
36
return
34
37
35
38
inlet = StreamInlet (streams [0 ])
39
+
40
+ # Get the number of channels from the stream
41
+ info = inlet .info ()
42
+ num_channels = info .channel_count ()
43
+ print (f"Detected { num_channels } channels." )
44
+
45
+ # Initialize data buffer based on the number of channels
46
+ data = np .zeros ((num_channels , 2000 )) # Buffer to hold the last 2000 samples for each channel
47
+
36
48
init_gui ()
37
49
38
50
def init_gui ():
@@ -47,15 +59,16 @@ def init_gui():
47
59
pg .setConfigOption ('background' , 'w' ) # Background color
48
60
pg .setConfigOption ('foreground' , 'k' ) # Foreground color
49
61
50
- # Create plots for each channel (6 in total)
62
+ # Create plots for each channel based on num_channels
51
63
global plots , curves
52
64
plots = []
53
65
curves = []
54
66
colors = ['#D10054' , '#007A8C' , '#0A6847' , '#674188' , '#E65C19' , '#2E073F' ] # Different colors for each channel
55
- for i in range (6 ):
67
+ for i in range (num_channels ):
56
68
plot = pg .PlotWidget (title = f"Channel { i + 1 } " ) # Create a plot widget for each channel
57
69
layout .addWidget (plot ) # Add the plot to the layout
58
- curve = plot .plot (pen = colors [i ]) # Create a curve (line) for plotting data
70
+ color = colors [i % len (colors )] # Cycle colors if fewer colors than channels
71
+ curve = plot .plot (pen = color ) # Create a curve (line) for plotting data
59
72
curve .setDownsampling (auto = True ) # Automatically downsample if needed
60
73
curve .setClipToView (True ) # Clip the data to the view
61
74
plots .append (plot ) # Store the plot
@@ -65,7 +78,7 @@ def init_gui():
65
78
status_bar = QtWidgets .QHBoxLayout ()
66
79
67
80
# LSL status label
68
- lsl_label = QtWidgets .QLabel ("LSL Status: Connected" )
81
+ lsl_label = QtWidgets .QLabel (f "LSL Status: Connected ( { num_channels } channels detected) " )
69
82
status_bar .addWidget (lsl_label )
70
83
71
84
layout .addLayout (status_bar ) # Add the status bar to the layout
0 commit comments