|
44 | 44 | focus_speed_downward = 5
|
45 | 45 | focus_timeout = 2
|
46 | 46 | focus_threshold = None
|
| 47 | +calibration_duration = 10 |
47 | 48 |
|
48 | 49 | sprite_count = 10
|
49 | 50 | 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):
|
95 | 96 | screen = pygame.display.set_mode((800, 600))
|
96 | 97 | pygame.display.set_caption('Beetle Game')
|
97 | 98 |
|
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) |
100 | 119 |
|
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() |
124 | 133 |
|
125 | 134 | # Show Game Start Message
|
126 | 135 | show_message("Game Starting...", 1)
|
|
0 commit comments