Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import machine
- import time
- # --- Pin Definitions ---
- # Inputs
- power_button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP)
- reset_button = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP)
- pc_state_sensor = machine.Pin(22, machine.Pin.IN)
- # Outputs
- case_led = machine.Pin(16, machine.Pin.OUT)
- motherboard_power_control = machine.Pin(12, machine.Pin.OUT)
- power_ok_signal = machine.Pin(17, machine.Pin.OUT)
- fan_relay_control = machine.Pin(18, machine.Pin.OUT)
- # --- Constants ---
- POWER_OK_DELAY_MS = 300 # ATX spec: 100-500ms
- FAN_COOLDOWN_MINUTES = 5
- FAN_COOLDOWN_MS = FAN_COOLDOWN_MINUTES * 60 * 1000
- FORCE_SHUTDOWN_HOLD_MS = 5000 # 5 seconds
- # --- Initial State ---
- motherboard_power_control.high() # Set HIGH to keep active-low relay OFF
- power_ok_signal.low()
- case_led.low()
- fan_relay_control.high() # Set HIGH to keep active-low fan relay OFF
- print("Pico controller initialized. Waiting for commands.")
- # --- Helper Functions ---
- def pulse_motherboard_power():
- """Simulates a short, momentary power button press via the relay."""
- print("Pulsing motherboard power control relay...")
- motherboard_power_control.low()
- time.sleep_ms(500)
- motherboard_power_control.high()
- def force_motherboard_power_off():
- """Simulates holding the power button down to force a shutdown."""
- print("FORCING SHUTDOWN: Holding motherboard power control relay ON...")
- motherboard_power_control.low()
- time.sleep(7) # Hold for 7 seconds to ensure force-off
- motherboard_power_control.high()
- # --- Main Loop ---
- power_press_time = 0
- force_shutdown_timer_start = 0
- shutdown_fan_timer_start = 0
- pc_is_on_flag = pc_state_sensor.value() == 1
- while True:
- current_pc_state = pc_state_sensor.value() == 1
- # --- State Change Detection (Handles PWR_OK and Fans) ---
- if current_pc_state and not pc_is_on_flag: # PC just turned ON
- print("PC turned ON.")
- fan_relay_control.low() # Turn fans ON
- shutdown_fan_timer_start = 0
- print(f"Waiting {POWER_OK_DELAY_MS}ms to send PWR_OK...")
- time.sleep_ms(POWER_OK_DELAY_MS)
- power_ok_signal.high()
- print("PWR_OK signal sent.")
- elif not current_pc_state and pc_is_on_flag: # PC just turned OFF
- print("PC turned OFF.")
- power_ok_signal.low()
- print(f"Starting {FAN_COOLDOWN_MINUTES}-minute fan cooldown.")
- shutdown_fan_timer_start = time.ticks_ms()
- pc_is_on_flag = current_pc_state
- # --- Cooldown Timer Logic ---
- if shutdown_fan_timer_start != 0:
- if time.ticks_diff(time.ticks_ms(), shutdown_fan_timer_start) >= FAN_COOLDOWN_MS:
- print("Cooldown complete. Turning fans OFF.")
- fan_relay_control.high()
- shutdown_fan_timer_start = 0
- case_led.value(pc_is_on_flag)
- # --- Button Logic ---
- power_button_pressed = power_button.value() == 0
- reset_button_pressed = reset_button.value() == 0
- if pc_is_on_flag:
- # Forced Shutdown Logic (Only when PC is ON)
- if power_button_pressed and reset_button_pressed:
- if force_shutdown_timer_start == 0:
- force_shutdown_timer_start = time.ticks_ms()
- print("Forced shutdown combo detected. Hold for 5 seconds...")
- if time.ticks_diff(time.ticks_ms(), force_shutdown_timer_start) >= FORCE_SHUTDOWN_HOLD_MS:
- force_motherboard_power_off()
- force_shutdown_timer_start = 0
- time.sleep(5)
- else:
- force_shutdown_timer_start = 0
- # Normal Shutdown Logic
- if power_button_pressed and not reset_button_pressed:
- if power_press_time == 0:
- power_press_time = time.ticks_ms()
- else:
- if power_press_time != 0:
- print("Case power button pressed. Sending clean shutdown signal.")
- pulse_motherboard_power()
- power_press_time = 0
- else: # Power ON Logic (Only when PC is OFF)
- if power_button_pressed:
- if power_press_time == 0:
- power_press_time = time.ticks_ms()
- else:
- if power_press_time != 0:
- print("Case power button pressed. Sending power ON signal.")
- pulse_motherboard_power()
- power_press_time = 0
- time.sleep(3)
- time.sleep_ms(50)
Advertisement
Add Comment
Please, Sign In to add comment