pidesktop icon indicating copy to clipboard operation
pidesktop copied to clipboard

Change python code to gpio zero?

Open hoopsurfer opened this issue 6 years ago • 2 comments

gpiozero might provide cleaner code in the long run. Note the nice pin numbering explaination.

gpiozero pinout

hoopsurfer avatar Mar 22 '19 05:03 hoopsurfer

I was able to start the mcu poweroff timer but I wan unable to read the button presses. Here's my current code:

Code
#!/usr/bin/env python
#
# pd-powerkey.py - monitor GPIO to detect power key press from Power MCU (PCU)
#

import time,os,sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib') # Import gpiozero in LibreELEC
from gpiozero import Button,LED

print("pidesktop: power button service initializing")

pwrBtn = Button(13) # PCU to Pi - detect power key pressed

pcu = LED(6) # Pi to PCU - start/stop shutdown timer
pcu.off() # tell PCU we are alive
pcu.on() # cause blink by starting shutdown timer
time.sleep(0.5)
pcu.off() # clear timer we really are alive

# callback function
def powerkey_pressed(channels):
    print("pidesktop: power button press detected, initiating shutdown")
    os.system("sync")
    os.system("shutdown -h now")
    sys.exit()

# wait for power key press
pwrBtn.when_pressed = powerkey_pressed
print("pidesktop: power button monitor enabled")

# idle - TODO: use wait
while True:
    time.sleep(10)

The button reports high both pressed and released so I don't know where to go from here.

jiriks74 avatar Jun 19 '24 11:06 jiriks74

I made it work. I'll publish it soon.

jiriks74 avatar Jun 19 '24 14:06 jiriks74