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
- FAN_COOLDOWN_MINUTES = 5
- FAN_COOLDOWN_MS = FAN_COOLDOWN_MINUTES * 60 * 1000
- FORCE_SHUTDOWN_HOLD_MS = 5000 # 5 seconds for the two-button combo
- # --- Initial State ---
- motherboard_power_control.high()
- power_ok_signal.low()
- case_led.low()
- fan_relay_control.high() # Energize relay to keep fans OFF initially.
- print("Pico controller initialized (v2 logic). Waiting for commands.")
- # --- Helper Functions ---
- def pulse_motherboard_power():
- """Simulates a short, momentary power button press."""
- print("Pulsing motherboard power control...")
- 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 LOW...")
- 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()
- 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 # Reset timer to prevent re-triggering
- time.sleep(5) # Wait for PC to fully power off
- else:
- force_shutdown_timer_start = 0 # Reset timer if combo is broken
- # --- Normal Shutdown Logic (Only if force combo is not active) ---
- 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: # Button was just released
- 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) # Debounce to wait for PC to start turning on
- time.sleep_ms(50)
Advertisement
Add Comment
Please, Sign In to add comment