Untitled

 avatar
unknown
plain_text
a month ago
5.5 kB
2
Indexable
def on_press(self, event):
    # First check if settings menu is open
    root = self.winfo_toplevel()
    for widget in root.winfo_children():
        if isinstance(widget, ctk.CTkFrame):  # Main frame
            for child in widget.winfo_children():
                if isinstance(child, SettingsMenu) and child.is_open:
                    return  # Don't process press if settings menu is open
    
    # Check if this instance is in reset mode
    if hasattr(self, 'is_reset_mode') and self.is_reset_mode:
        return
            
    current_time = time.time()
    if current_time - self.last_press_time >= self.cooldown_period:
        self.is_pressed = True
        self.press_animation_active = True
        self.last_press_time = current_time
        self.can_press = False
        print(f"{self.bin_id} button pressed")
        
        # Find turntable controller
        turntable = None
        for widget in root.winfo_children():
            if isinstance(widget, ctk.CTkFrame):  # Main frame
                for child in widget.winfo_children():
                    if isinstance(child, ClassificationPrompt):
                        turntable = child.turntable
                        break
        
        if turntable and turntable.is_initialized:
            self.start_manual_fill(turntable)
        else:
            MessageDialog(
                root,
                TRANSLATIONS[root.LANGUAGE].get('please_wait', 'Please wait'),
                root.DARK_MODE
            )
    else:
        self.is_pressed = False
        self.press_animation_active = False

def start_manual_fill(self, turntable):
    """Handle manual filling of bins"""
    if hasattr(self, '_fill_active'):
        # If fill is active, close the lid
        self.close_lid(turntable)
        return
        
    # Create loading screen
    loading_screen = LoadingScreen(
        self.winfo_toplevel(),
        message=TRANSLATIONS[self.winfo_toplevel().LANGUAGE]['manual_fill_started'],
        dark_mode=self.dark_mode,
        language=self.winfo_toplevel().LANGUAGE
    )
    
    def perform_fill():
        try:
            # Move turntable to position
            print(f"Moving turntable to {self.bin_id}")
            target_position = turntable.positions[self.bin_id]
            steps, direction = turntable._calculate_steps(target_position)
            turntable._move_motor(steps, direction)
            turntable.current_position = target_position
            
            # Short delay after movement
            time.sleep(1)
            
            # Open servo
            print("Opening servo")
            turntable.hardware.servo_motor.angle = 90
            
            # Update loading screen message
            self.after(0, lambda: loading_screen.update_message(
                TRANSLATIONS[self.winfo_toplevel().LANGUAGE]['manual_fill_active']
            ))
            self.after(0, lambda: loading_screen.update_status(
                TRANSLATIONS[self.winfo_toplevel().LANGUAGE]['tap_to_close']
            ))
            
            # Set fill active flag
            self._fill_active = True
            
            # Start auto-close timer
            self._auto_close_timer = self.after(10000, lambda: self.close_lid(turntable))
            
        except Exception as e:
            print(f"Error during manual fill: {e}")
            self.after(0, loading_screen.close)
            
    # Start fill process in separate thread
    thread = threading.Thread(target=perform_fill)
    thread.daemon = True
    thread.start()

def close_lid(self, turntable):
    """Close the servo lid and measure fill level"""
    if hasattr(self, '_auto_close_timer'):
        self.after_cancel(self._auto_close_timer)
        del self._auto_close_timer
    
    if not hasattr(self, '_fill_active'):
        return
        
    loading_screen = LoadingScreen(
        self.winfo_toplevel(),
        message=TRANSLATIONS[self.winfo_toplevel().LANGUAGE]['manual_fill_closing'],
        dark_mode=self.dark_mode,
        language=self.winfo_toplevel().LANGUAGE
    )
    
    def close_and_measure():
        try:
            # Close servo
            turntable.hardware.servo_motor.angle = 0
            time.sleep(0.5)  # Wait for contents to settle
            
            # Measure fill level
            if hasattr(turntable, 'hardware'):
                readings = turntable.hardware.get_measurements()
                closest_reading, percentage = turntable.hardware.process_readings(readings)
                
                if percentage is not None:
                    turntable.hardware.update_fill_level(self.bin_id, percentage)
                    
                    def update_and_close():
                        if not self._destroyed:
                            self.set_fill_level(percentage)
                            CircularProgress.update_all_instances()
                        loading_screen.close()
                    
                    self.after(0, update_and_close)
            
            # Clear fill active flag
            del self._fill_active
            
        except Exception as e:
            print(f"Error closing lid: {e}")
            self.after(0, loading_screen.close)
    
    thread = threading.Thread(target=close_and_measure)
    thread.daemon = True
    thread.start()
Leave a Comment