Skip to content

Commit 96724df

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

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

beetle.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pylsl
33
import numpy as np
44
import time
5+
import sys
56
from pylsl import StreamInlet, resolve_streams, resolve_byprop
67
from scipy.signal import iirnotch, butter, lfilter
78
import math
@@ -12,8 +13,13 @@
1213
streams = resolve_streams()
1314
available_streams = [s.name() for s in streams]
1415

16+
last_data_time = None
17+
stream_active = True
18+
inlet = None # Initialize inlet variable
19+
1520
if not available_streams:
1621
print("No LSL streams found!")
22+
sys.exit(1)
1723

1824
for stream_name in available_streams:
1925
print(f"Trying to connect to {stream_name}...")
@@ -28,6 +34,8 @@
2834

2935
if inlet is None:
3036
print("Could not connect to any stream.")
37+
sys.exit(1)
38+
3139
sampling_rate = int(inlet.info().nominal_srate())
3240
print(f"Sampling rate: {sampling_rate} Hz")
3341

@@ -70,6 +78,11 @@ def show_message(message, duration=3):
7078
screen.blit(text, text_rect)
7179
pygame.display.update()
7280

81+
for event in pygame.event.get():
82+
if event.type == pygame.QUIT:
83+
pygame.quit()
84+
sys.exit()
85+
7386
# Apply filters
7487
def apply_filters(eeg_point):
7588
filtered = lfilter(b_notch, a_notch, [eeg_point])
@@ -97,7 +110,7 @@ def calculate_focus_level(eeg_data, sampling_rate=500):
97110
pygame.display.set_caption('Beetle Game')
98111

99112
def calibrate():
100-
global focus_threshold
113+
global focus_threshold, last_data_time
101114
calibration_data = []
102115

103116
font = pygame.font.SysFont("Arial", 36, bold=True)
@@ -119,8 +132,19 @@ def calibrate():
119132

120133
sample, _ = inlet.pull_sample(timeout=0.1)
121134
if sample:
135+
last_data_time = time.time()
122136
filtered_sample = apply_filters(sample[0])
123137
calibration_data.append(filtered_sample)
138+
else:
139+
if last_data_time and (time.time() - last_data_time) > 2:
140+
show_message("Connection lost! Exiting...", 2)
141+
pygame.quit()
142+
sys.exit(1)
143+
144+
for event in pygame.event.get():
145+
if event.type == pygame.QUIT:
146+
pygame.quit()
147+
sys.exit()
124148

125149
if len(calibration_data) >= buffer_size: # Ensure enough data was collected
126150
eeg_data = np.array(calibration_data)
@@ -170,8 +194,14 @@ def update_beetle_position(focus_level, is_focus_stable):
170194

171195
sample, _ = inlet.pull_sample(timeout=0.1)
172196
if sample:
197+
last_data_time = time.time()
173198
filtered_sample = apply_filters(sample[0])
174199
buffer.append(filtered_sample)
200+
else:
201+
if last_data_time and (time.time() - last_data_time) > 2:
202+
show_message("Connection lost! Exiting...", 2)
203+
running = False
204+
break
175205

176206
current_time = time.time()
177207

0 commit comments

Comments
 (0)