Skip to content

Commit 6eb5897

Browse files
committed
Calibration now runs in a loop if data is insufficient, with a real-time countdown updating every second.
1 parent bc99650 commit 6eb5897

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

beetle.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
focus_speed_downward = 5
4545
focus_timeout = 2
4646
focus_threshold = None
47+
calibration_duration = 10
4748

4849
sprite_count = 10
4950
beetle_sprites = [pygame.image.load(f'media/Beetle{i}.png') for i in range(1, sprite_count + 1)]
@@ -95,32 +96,40 @@ def calculate_focus_level(eeg_data, sampling_rate=500):
9596
screen = pygame.display.set_mode((800, 600))
9697
pygame.display.set_caption('Beetle Game')
9798

98-
# Calibration Phase
99-
show_message("Sit still and relax for 10 seconds", 3)
99+
def calibrate():
100+
global focus_threshold
101+
calibration_data = []
102+
103+
font = pygame.font.SysFont("Arial", 36, bold=True)
104+
105+
start_time = time.time()
106+
while time.time() - start_time < calibration_duration:
107+
remaining_time = max(0, calibration_duration - int(time.time() - start_time))
108+
screen.fill((255, 255, 255))
109+
message = f"Sit still and relax for {remaining_time} seconds" # Update the message dynamically
110+
text = font.render(message, True, (0, 0, 0))
111+
text_rect = text.get_rect(center=(400, 300))
112+
screen.blit(text, text_rect)
113+
pygame.display.update()
114+
115+
sample, _ = inlet.pull_sample(timeout=0.1)
116+
if sample:
117+
filtered_sample = apply_filters(sample[0])
118+
calibration_data.append(filtered_sample)
100119

101-
print("Starting Calibration... Please relax and stay still for 10 seconds.")
102-
calibration_data = []
103-
calibration_duration = 10
104-
calibration_start_time = time.time()
105-
106-
while time.time() - calibration_start_time < calibration_duration:
107-
sample, _ = inlet.pull_sample(timeout=0.1)
108-
if sample:
109-
filtered_sample = apply_filters(sample[0])
110-
calibration_data.append(filtered_sample)
111-
112-
if len(calibration_data) >= buffer_size:
113-
eeg_data = np.array(calibration_data)
114-
baseline_focus_levels = [calculate_focus_level(eeg_data[i:i + buffer_size]) for i in range(0, len(eeg_data), buffer_size)]
115-
116-
mean_focus = np.mean(baseline_focus_levels)
117-
std_focus = np.std(baseline_focus_levels)
118-
119-
focus_threshold = mean_focus + 0.5 * std_focus
120-
print(f"Calibration Complete. Focus Threshold set at: {focus_threshold:.3f}")
121-
else:
122-
print("Calibration failed due to insufficient data.")
123-
exit()
120+
if len(calibration_data) >= buffer_size: # Ensure enough data was collected
121+
eeg_data = np.array(calibration_data)
122+
baseline_focus_levels = [calculate_focus_level(eeg_data[i:i + buffer_size]) for i in range(0, len(eeg_data), buffer_size)]
123+
mean_focus = np.mean(baseline_focus_levels)
124+
std_focus = np.std(baseline_focus_levels)
125+
126+
focus_threshold = mean_focus + 0.5 * std_focus # Set focus threshold
127+
print(f"Calibration Complete. Focus Threshold set at: {focus_threshold:.3f}")
128+
else:
129+
print("Calibration failed due to insufficient data. Retrying...")
130+
calibrate() # Retry calibration if not enough data
131+
132+
calibrate()
124133

125134
# Show Game Start Message
126135
show_message("Game Starting...", 1)

0 commit comments

Comments
 (0)