cribbageSTARSHIP

pico-pc-ctrl

Sep 29th, 2025
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. import machine
  2. import time
  3.  
  4. # --- Pin Definitions ---
  5. # Inputs
  6. power_button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP)
  7. reset_button = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP)
  8. pc_state_sensor = machine.Pin(22, machine.Pin.IN)
  9.  
  10. # Outputs
  11. case_led = machine.Pin(16, machine.Pin.OUT)
  12. motherboard_power_control = machine.Pin(12, machine.Pin.OUT)
  13. power_ok_signal = machine.Pin(17, machine.Pin.OUT)
  14. fan_relay_control = machine.Pin(18, machine.Pin.OUT)
  15.  
  16. # --- Constants ---
  17. POWER_OK_DELAY_MS = 300
  18. FAN_COOLDOWN_MINUTES = 5
  19. FAN_COOLDOWN_MS = FAN_COOLDOWN_MINUTES * 60 * 1000
  20. FORCE_SHUTDOWN_HOLD_MS = 5000 # 5 seconds for the two-button combo
  21.  
  22. # --- Initial State ---
  23. motherboard_power_control.high()
  24. power_ok_signal.low()
  25. case_led.low()
  26. fan_relay_control.high() # Energize relay to keep fans OFF initially.
  27. print("Pico controller initialized (v2 logic). Waiting for commands.")
  28.  
  29. # --- Helper Functions ---
  30. def pulse_motherboard_power():
  31. """Simulates a short, momentary power button press."""
  32. print("Pulsing motherboard power control...")
  33. motherboard_power_control.low()
  34. time.sleep_ms(500)
  35. motherboard_power_control.high()
  36.  
  37. def force_motherboard_power_off():
  38. """Simulates holding the power button down to force a shutdown."""
  39. print("FORCING SHUTDOWN: Holding motherboard power control LOW...")
  40. motherboard_power_control.low()
  41. time.sleep(7) # Hold for 7 seconds to ensure force-off
  42. motherboard_power_control.high()
  43.  
  44. # --- Main Loop ---
  45. power_press_time = 0
  46. force_shutdown_timer_start = 0
  47. shutdown_fan_timer_start = 0
  48. pc_is_on_flag = pc_state_sensor.value() == 1
  49.  
  50. while True:
  51. current_pc_state = pc_state_sensor.value() == 1
  52.  
  53. # --- State Change Detection (Handles PWR_OK and Fans) ---
  54. if current_pc_state and not pc_is_on_flag: # PC just turned ON
  55. print("PC turned ON.")
  56. fan_relay_control.low()
  57. shutdown_fan_timer_start = 0
  58. print(f"Waiting {POWER_OK_DELAY_MS}ms to send PWR_OK...")
  59. time.sleep_ms(POWER_OK_DELAY_MS)
  60. power_ok_signal.high()
  61. print("PWR_OK signal sent.")
  62.  
  63. elif not current_pc_state and pc_is_on_flag: # PC just turned OFF
  64. print("PC turned OFF.")
  65. power_ok_signal.low()
  66. print(f"Starting {FAN_COOLDOWN_MINUTES}-minute fan cooldown.")
  67. shutdown_fan_timer_start = time.ticks_ms()
  68.  
  69. pc_is_on_flag = current_pc_state
  70.  
  71. # --- Cooldown Timer Logic ---
  72. if shutdown_fan_timer_start != 0:
  73. if time.ticks_diff(time.ticks_ms(), shutdown_fan_timer_start) >= FAN_COOLDOWN_MS:
  74. print("Cooldown complete. Turning fans OFF.")
  75. fan_relay_control.high()
  76. shutdown_fan_timer_start = 0
  77.  
  78. case_led.value(pc_is_on_flag)
  79.  
  80. # --- Button Logic ---
  81. power_button_pressed = power_button.value() == 0
  82. reset_button_pressed = reset_button.value() == 0
  83.  
  84. if pc_is_on_flag:
  85. # --- Forced Shutdown Logic (Only when PC is ON) ---
  86. if power_button_pressed and reset_button_pressed:
  87. if force_shutdown_timer_start == 0:
  88. force_shutdown_timer_start = time.ticks_ms()
  89. print("Forced shutdown combo detected. Hold for 5 seconds...")
  90.  
  91. if time.ticks_diff(time.ticks_ms(), force_shutdown_timer_start) >= FORCE_SHUTDOWN_HOLD_MS:
  92. force_motherboard_power_off()
  93. force_shutdown_timer_start = 0 # Reset timer to prevent re-triggering
  94. time.sleep(5) # Wait for PC to fully power off
  95. else:
  96. force_shutdown_timer_start = 0 # Reset timer if combo is broken
  97.  
  98. # --- Normal Shutdown Logic (Only if force combo is not active) ---
  99. if power_button_pressed and not reset_button_pressed:
  100. if power_press_time == 0:
  101. power_press_time = time.ticks_ms()
  102. else:
  103. if power_press_time != 0: # Button was just released
  104. print("Case power button pressed. Sending clean shutdown signal.")
  105. pulse_motherboard_power()
  106. power_press_time = 0
  107.  
  108. else: # --- Power ON Logic (Only when PC is OFF) ---
  109. if power_button_pressed:
  110. if power_press_time == 0:
  111. power_press_time = time.ticks_ms()
  112. else:
  113. if power_press_time != 0:
  114. print("Case power button pressed. Sending power ON signal.")
  115. pulse_motherboard_power()
  116. power_press_time = 0
  117. time.sleep(3) # Debounce to wait for PC to start turning on
  118.  
  119. time.sleep_ms(50)
Advertisement
Add Comment
Please, Sign In to add comment