From 041bcc09a6805a5ee2a743d18322ad79ba32a124 Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 8 Aug 2025 01:11:58 +0200 Subject: [PATCH] MainCode/adalm1000_logger.py aktualisiert Working except of switching devices D --- MainCode/adalm1000_logger.py | 112 ++++++++++++++++++++++++----------- 1 file changed, 79 insertions(+), 33 deletions(-) diff --git a/MainCode/adalm1000_logger.py b/MainCode/adalm1000_logger.py index 2a23d1d..0f72afa 100644 --- a/MainCode/adalm1000_logger.py +++ b/MainCode/adalm1000_logger.py @@ -256,6 +256,10 @@ class MeasurementThread(QThread): self.device.channels['A'].constant(0) except Exception as e: print(f"Error stopping device: {e}") + self.wait(500) # Wait for thread to finish + + def is_measuring(self): + return self.measurement_thread is not None and self.measurement_thread.isRunning() def run(self): """Measurement loop with enhanced recovery""" @@ -993,7 +997,6 @@ class BatteryTester(QMainWindow): padding: 4px 8px; border-radius: 4px; min-height: 28px; - background-color: {self.accent_color}; color: {self.fg_color}; }} QPushButton:checked {{ @@ -1009,7 +1012,7 @@ class BatteryTester(QMainWindow): self.toggle_button = QPushButton("START") self.toggle_button.setCheckable(True) self.toggle_button.setStyleSheet(button_style + f""" - background-color: {self.accent_color}; + background-color: {self.success_color}; min-width: 120px; """) self.toggle_button.clicked.connect(self.toggle_test) @@ -1026,8 +1029,8 @@ class BatteryTester(QMainWindow): self.record_button = QPushButton("● Start Recording") self.record_button.setCheckable(True) self.record_button.setStyleSheet(button_style.replace( - self.accent_color, self.success_color - )) + "background-color", "background-color", 1 + ) + f"background-color: {self.success_color};") self.record_button.clicked.connect(self.toggle_global_recording) button_layout.addWidget(self.record_button) self.record_button.hide() @@ -1056,7 +1059,7 @@ class BatteryTester(QMainWindow): def apply_button_style(self): """Apply consistent button styling based on current state""" if self.toggle_button.isChecked(): - # Stop state + # Stop state - red self.toggle_button.setStyleSheet(f""" QPushButton {{ background-color: {self.warning_color}; @@ -1072,10 +1075,10 @@ class BatteryTester(QMainWindow): }} """) else: - # Start state + # Start state - green self.toggle_button.setStyleSheet(f""" QPushButton {{ - background-color: {self.accent_color}; + background-color: {self.success_color}; color: {self.fg_color}; font-weight: bold; border: none; @@ -1084,7 +1087,33 @@ class BatteryTester(QMainWindow): min-height: 28px; }} QPushButton:hover {{ - background-color: #{self.darker_color(self.accent_color)}; + background-color: #{self.darker_color(self.success_color)}; + }} + """) + + # Update record button separately + if self.record_button.isChecked(): + self.record_button.setStyleSheet(f""" + QPushButton {{ + background-color: {self.warning_color}; + color: white; + font-weight: bold; + border: none; + border-radius: 4px; + padding: 4px 8px; + min-height: 28px; + }} + """) + else: + self.record_button.setStyleSheet(f""" + QPushButton {{ + background-color: {self.success_color}; + color: {self.fg_color}; + font-weight: bold; + border: none; + border-radius: 4px; + padding: 4px 8px; + min-height: 28px; }} """) @@ -1114,7 +1143,7 @@ class BatteryTester(QMainWindow): if all_success: self.record_button.setText("■ Stop Recording") - self.record_button.setStyleSheet(f"background-color: {self.warning_color};") + self.apply_button_style() # Update style self.status_bar.showMessage("Recording started for all connected devices") else: self.global_recording = False @@ -1126,7 +1155,7 @@ class BatteryTester(QMainWindow): self.finalize_device_log_file(device) # Changed from _stop_device_recording self.record_button.setText("● Start Recording") - self.record_button.setStyleSheet(f"background-color: {self.success_color};") + self.apply_button_style() # Update style self.status_bar.showMessage("Recording stopped for all devices") def safe_execute(func): @@ -1140,13 +1169,12 @@ class BatteryTester(QMainWindow): return wrapper @safe_execute - def toggle_test(self): - if self.toggle_button.isChecked(): - self.apply_button_style() # Add this line + def toggle_test(self, checked): + if checked: self.start_test() else: - self.apply_button_style() # Add this line self.stop_test() + self.apply_button_style() def change_mode(self, mode_name): """Change between different test modes""" @@ -1250,6 +1278,7 @@ class BatteryTester(QMainWindow): if self.create_cycle_log_file(): dev.is_recording = True # Set device's recording state self.record_button.setText("Stop Recording") + self.apply_button_style() # Update style self.status_bar.showMessage("Live recording started") # Ensure monitoring is running if not self.test_running: @@ -1269,6 +1298,7 @@ class BatteryTester(QMainWindow): self.finalize_log_file() dev.is_recording = False # Clear device's recording state self.record_button.setText("Start Recording") + self.apply_button_style() # Update style self.status_bar.showMessage("Live recording stopped") except Exception as e: print(f"Error stopping recording: {e}") @@ -1413,6 +1443,10 @@ class BatteryTester(QMainWindow): self.status_light.setStyleSheet(f""" background-color: {color}; border-radius: 10px; + min-width: 12px; + max-width: 12px; + min-height: 12px; + max-height: 12px; """) QApplication.processEvents() @@ -1509,9 +1543,8 @@ class BatteryTester(QMainWindow): if hasattr(self.active_device, 'measurement_thread'): thread = self.active_device.measurement_thread try: - if thread.isRunning(): - thread.stop() - thread.wait(500) + thread.stop() # Properly stop the thread + thread.wait(500) # Wait for clean shutdown thread.update_signal.disconnect() thread.error_signal.disconnect() except (RuntimeError, TypeError) as e: @@ -1524,20 +1557,30 @@ class BatteryTester(QMainWindow): # Activate new device self.active_device = self.devices[serial] + # Ensure measurement thread exists and is properly configured + if not hasattr(self.active_device, 'measurement_thread') or not self.active_device.measurement_thread: + self.active_device.start_measurement(self.interval) + # Connect signals to new device - if hasattr(self.active_device, 'measurement_thread'): - try: - self.measurement_thread = self.active_device.measurement_thread - self.measurement_thread.update_signal.connect(self.update_measurements) - self.measurement_thread.error_signal.connect(self.handle_device_error) - - # Start measurement only AFTER connecting signals - if not self.measurement_thread.isRunning(): - self.active_device.start_measurement(self.interval) - print("Measurement thread running?", self.measurement_thread.isRunning()) - except Exception as e: - print(f"Error connecting to new device: {e}") - return + self.measurement_thread = self.active_device.measurement_thread + + # Disconnect any existing connections first + try: + self.measurement_thread.update_signal.disconnect() + except (TypeError, RuntimeError): + pass + try: + self.measurement_thread.error_signal.disconnect() + except (TypeError, RuntimeError): + pass + + # Reconnect signals + self.measurement_thread.update_signal.connect(self.update_measurements) + self.measurement_thread.error_signal.connect(self.handle_device_error) + + # Ensure thread is running + if not self.measurement_thread.isRunning(): + self.active_device.start_measurement(self.interval) # Update UI with current device data self.update_ui_from_active_device() @@ -1546,6 +1589,7 @@ class BatteryTester(QMainWindow): # Update recording button state self.record_button.setChecked(self.global_recording) self.record_button.setText("Stop Recording" if self.global_recording else "Start Recording") + self.apply_button_style() # Ensure proper colors def update_ui_from_active_device(self): dev = self.active_device @@ -1700,6 +1744,7 @@ class BatteryTester(QMainWindow): if not self.active_device: QMessageBox.warning(self, "No Device", "No ADALM1000 device selected.") self.toggle_button.setChecked(False) + self.apply_button_style() return dev_manager = self.active_device @@ -1739,6 +1784,7 @@ class BatteryTester(QMainWindow): except Exception as e: QMessageBox.critical(self, "Device Error", f"Failed to reset device: {e}") self.toggle_button.setChecked(False) + self.apply_button_style() return # Reset test variables @@ -2398,12 +2444,12 @@ class BatteryTester(QMainWindow): self.apply_button_style() self.toggle_button.setStyleSheet(f""" QPushButton {{ - background-color: {self.status_colors["active"]}; + background-color: {self.success_color}; color: {self.fg_color}; font-weight: bold; }} QPushButton:checked {{ - background-color: {self.status_colors["warning"]}; + background-color: {self.warning_color}; }} QPushButton:disabled {{ background-color: #4C566A; @@ -2422,7 +2468,7 @@ class BatteryTester(QMainWindow): self.toggle_button.setText(text) self.toggle_button.setStyleSheet(f""" QPushButton {{ - background-color: {self.accent_color}; + background-color: {self.success_color}; color: {self.fg_color}; font-weight: bold; }}