Skip to content

Commit ed35af3

Browse files
committed
Handle LSL stream disconnection by closing app after 2s of no data.
1 parent 96724df commit ed35af3

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

ffteeg.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from scipy.signal import butter, iirnotch, lfilter, lfilter_zi
1010
from scipy.fft import fft
1111
import math
12+
import time
1213

1314
class EEGMonitor(QMainWindow):
1415
def __init__(self):
@@ -17,6 +18,9 @@ def __init__(self):
1718
self.setWindowTitle("Real-Time EEG Monitor with FFT and Brainwave Power")
1819
self.setGeometry(100, 100, 1200, 800)
1920

21+
self.stream_active = True # Flag to check if the stream is active
22+
self.last_data_time = None # Variable to store the last data time
23+
2024
# Main layout split into two halves: top for EEG, bottom for FFT and Brainwaves
2125
self.central_widget = QWidget()
2226
self.main_layout = QVBoxLayout(self.central_widget)
@@ -108,6 +112,7 @@ def __init__(self):
108112
def update_plot(self):
109113
samples, _ = self.inlet.pull_chunk(timeout=0.0)
110114
if samples:
115+
self.last_data_time = time.time() # Store the last data time
111116
for sample in samples:
112117
raw_point = sample[0]
113118

@@ -129,6 +134,13 @@ def update_plot(self):
129134
time_axis = np.linspace(0, 4, len(plot_data))
130135
self.eeg_curve.setData(time_axis, plot_data)
131136

137+
else:
138+
if self.last_data_time and (time.time() - self.last_data_time) > 2:
139+
self.stream_active = False
140+
print("LSL stream disconnected!")
141+
self.timer.stop()
142+
self.close()
143+
132144
def process_fft_and_brainpower(self):
133145
window = np.hanning(len(self.moving_window))
134146
buffer_windowed = np.array(self.moving_window) * window

0 commit comments

Comments
 (0)