sandysound
Posts: 64
Joined: Thu Sep 14, 2017 6:05 pm

Questions regarding shutdown-ledboard.py example in MagPi

Sat May 16, 2020 6:12 pm

Have been trying to use this script from MagPi: https://magpi.raspberrypi.org/articles/ ... spberry-pi

Its from a github:https://github.com/TonyLHansen/raspberr ... edboard.py

Code: Select all

#!/usr/bin/env python3
from gpiozero import Button, LEDBoard
from signal import pause
import warnings, os, sys

offGPIO = int(sys.argv[1]) if len(sys.argv) >= 2 else 21
offtime = int(sys.argv[2]) if len(sys.argv) >= 3 else 6
mintime = 1       # notice switch after mintime seconds
actledGPIO = 47   # activity LED
powerledGPIO = 35 # power LED

def shutdown(b):
    # find how long the button has been held
    p = b.pressed_time
    # blink rate will increase the longer we hold
    # the button down. E.g., at 2 seconds, use 1/4 second rate.
    leds.blink(on_time=0.5/p, off_time=0.5/p)
    if p > offtime:
        os.system("sudo poweroff")

def when_pressed():
    # start blinking with 1/2 second rate
    leds.blink(on_time=0.5, off_time=0.5)

def when_released():
    # be sure to turn the LEDs off if we release early
    leds.off()

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    leds = LEDBoard(actledGPIO, powerledGPIO)

btn = Button(offGPIO, hold_time=mintime, hold_repeat=True)
btn.when_held = shutdown
btn.when_pressed = when_pressed
btn.when_released = when_released
pause()
The problem I am having is figuring out how to manipulate it, I would like to be able to adjust the amount of time the button is pressed for before initiating a shutdown. How do i do that in this script?

And do I need to run it as python3 or will python be fine?

The script does work for me, but there is a long pause between initiating shutdown and it actually happening when compared to other scripts.

Also, what is the difference between running this as a cron job and using rc.local?

klricks
Posts: 7047
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Questions regarding shutdown-ledboard.py example in MagPi

Sun May 17, 2020 2:37 am

The program has 2 command line parameters which allow the user to input variables without having to make changes to the code.

Code: Select all

offGPIO = int(sys.argv[1]) if len(sys.argv) >= 2 else 21
offtime = int(sys.argv[2]) if len(sys.argv) >= 3 else 6
The 1st one is the GPIO number for the button and the 2nd is the hold time before shutdown.
If you do not specify the 2 numbers then it will default to use GPIO 21 and 6 seconds.
For example if you want to use GPIO 5 instead and 10 seconds hold time:

Code: Select all

python3 shutdown-ledboard.py 5 10
Or you could modify the defaults in the code:

Code: Select all

offGPIO = int(sys.argv[1]) if len(sys.argv) >= 2 else 5
offtime = int(sys.argv[2]) if len(sys.argv) >= 3 else 10
You need to use python3 for Python 3 (python for Python 2 code).

cron allows you to run a program or command at a certain time of day, week, month etc. (Endless possibilities)
For example you could set cron to start a program every Wednesday at 15:00:00 etc.
There is also a cron setting @reboot to run the program or command once at boot time.
@reboot does not always work especially for programs that need GUI as the program will attempt to run before the desktop is ready.

/etc/rc.local is much easier to use but wont work for GUI programs.

For GUI programs (and non GUI) use autostart:

Code: Select all

/etc/xdg/lxsession/LXDE-pi/autostart
Be sure to use the full paths in your commands in rc.local or autostart:

Code: Select all

python3 /home/pi/shutdown-ledboard.py 21 3
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

sandysound
Posts: 64
Joined: Thu Sep 14, 2017 6:05 pm

Re: Questions regarding shutdown-ledboard.py example in MagPi

Mon May 18, 2020 11:16 am

Ahhhhhhhh! Thankyou for your clear and concise answer!
Much obliged!

RpiMatt
Posts: 1
Joined: Thu May 28, 2020 1:41 pm

Re:Upgrading Raspberry Pi OS (formerly Raspbian)

Thu May 28, 2020 1:46 pm

Can I upgrade from the 32-bit Raspberry Pi OS to the all-new 64-bit Raspberry Pi OS when the 64-bit is more stable?

User avatar
DougieLawson
Posts: 38553
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Re:Upgrading Raspberry Pi OS (formerly Raspbian)

Thu May 28, 2020 4:58 pm

RpiMatt wrote:
Thu May 28, 2020 1:46 pm
Can I upgrade from the 32-bit Raspberry Pi OS to the all-new 64-bit Raspberry Pi OS when the 64-bit is more stable?
It's a bit early to ask since the 64bit stuff is still in beta testing.

The way I'm planning to do it is with a fresh install for the machines that are 64bit ready, so I don't keep any clag, cruft and junk from Raspbian (aka RaspiOS32).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “The MagPi”