-

CommentStreams:Bde16dada61dbb5d25cab17c52c7c11c

From Geekworm Wiki
< X728-script
Revision as of 16:22, 9 December 2024 by 195.192.96.124 (talk) (Migrated comment #5365)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

@Lisa it does, i found a way to reprogramm "x728-v2.x-plsd-gpiod.py" and save it in "sudo cp -f ./x728-v2.x-plsd-gpiod.py /usr/local/bin/"

  1. !/usr/bin/env python

import gpiod import time import subprocess

  1. Define the chip and lines

chipname = "gpiochip0" # Please use gpiochip0 if using Raspberry Pi 4/3 hardware

  1. chipname = "gpiochip4" # Please use gpiochip4 if using Raspberry Pi 5 hardware

line_offset = 6 out_line_offset = 26

  1. Open the GPIO chip

chip = gpiod.Chip(chipname)

  1. Get the input line

line = chip.get_line(line_offset) line.request(consumer="my_program", type=gpiod.LINE_REQ_EV_BOTH_EDGES)

  1. Get the output line

out_line = chip.get_line(out_line_offset) out_line.request(consumer="my_program", type=gpiod.LINE_REQ_DIR_OUT)

def print_event(event):

   if event.type == gpiod.LineEvent.RISING_EDGE:
       print("---AC Power Loss OR Power Adapter Failure---")
       print("Shutdown in 5 seconds")
       time.sleep(5)
       out_line.set_value(1)
       time.sleep(3)
       out_line.set_value(0)
       
       # Perform safe shutdown
       subprocess.run(["sudo", "shutdown", "-h", "now"])
   elif event.type == gpiod.LineEvent.FALLING_EDGE:
       print("---AC Power OK, Power Adapter OK---")

try:

   # Wait for events indefinitely
   while True:
       event = line.event_wait()        
       if event:            
           event = line.event_read()
           print_event(event)

except KeyboardInterrupt:

   print("Exiting...")

finally:

   chip.close()

than just create a service.unit and save it in: sudo cp -f ./safe_shutdown.service /lib/systemd/system/

[Unit] Description=Run Python Power Management Daemon

[Service] Type=simple ExecStart=/usr/bin/python3 /usr/local/bin/x728-v2.x-plsd-gpiod.py Restart=always

User=root Group=root

[Install] WantedBy=multi-user.target

sudo systemctl daemon-reload sudo chmod +x /usr/local/bin/xPWR.sh sudo chmod +x /usr/local/bin/x728-v2.x-plsd-gpiod.py sudo systemctl enable xPWR sudo systemctl enable x728-v2.x-plsd-gpiod.py sudo systemctl start xPWR sudo systemctl start x728-v2.x-plsd-gpiod.py

Now everybody can use the raspberry with something you switch off to turn the raspberry off, for example a screen.