LowPowerLab Forum

Hardware support => ATXRaspi => Topic started by: tonyp7 on November 12, 2016, 01:55:06 AM

Title: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: tonyp7 on November 12, 2016, 01:55:06 AM
EDIT: FINAL WORKING VERSION

What is shutdownirq.py?
shutdownirq.py is a drop in replacement for shutdowncheck.sh.

Instead of polling for the shutdown signal from the ATXRaspi to go high, it will simply register an interrupt on that pin. shutdownirq.py frees about 1% of your CPU processing time, and that's a lot.

Once the button is pressed, and due to quirks with the ATXRaspi (no falling edge sent back once the button press is released), I had to resort to polling to detect a button press longer than 2s. Interestingly, during my trials and errors I've found out that the ATXRaspi will kill the power no matter what after 50s. It basically leave you 48s to issue the sudo poweroff or die on you.

Next steps:
This is a nice and working replacement, but more could be done, such as converting this small Python script to C and making the whole solution a lot lighter on your system. To be continued!
Title: Re: ATXRaspi with interrupts: shutdownirq.py
Post by: tonyp7 on November 12, 2016, 10:46:15 PM
Updated final working version that behaves exactly like shutdowncheck.sh  :)
Title: Re: ATXRaspi with interrupts: shutdownirq.py
Post by: Felix on November 14, 2016, 10:44:28 AM
Awesome, I have to check this out :)
I already posted to your other topic before I read this, but thanks a lot!
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: Felix on November 17, 2016, 10:27:30 AM
This script seems to work great. 0% CPU :)

I decreased this setting to 1s:
REBOOTPULSEMAXIMUM = 1.0 #reboot pulse signal should be at most this long (seconds)

Did you have a reason to have it as 2s instead?

I may add this script as an alternate to the existing polling one.
Do you know if this will run on OpenELEC and other distributions?
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: tonyp7 on November 17, 2016, 09:34:26 PM
>Did you have a reason to have it as 2s instead?

Not sure, I was under the impression you had to press the button for 2s to trigger a shutdown. 1s is fine!

Do you know if this will run on OpenELEC and other distributions?

Unfortunately no, it will not run on OpenELEC out of the box because it seems like they removed RPi.GPIO library from their distro. You'd have to manually install the lib first. Probably the best way to circumvent with would be to package the script with it or just convert this script to C (not sure if asking users to make binaries is any simpler!)

I believe ALL distros supported by the Raspberry foundation come with the GPIO Python lib (like Librelec). Openelec might be a special snowflake.
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: Felix on November 17, 2016, 11:07:30 PM
Well anyway I updated my setup script (https://github.com/LowPowerLab/ATX-Raspi/blob/master/shutdownchecksetup.sh) and now the default is the python script with adjustments.
Checkout the readme (https://github.com/LowPowerLab/ATX-Raspi/) and try it out, let me know what you think.
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: tonyp7 on November 18, 2016, 07:38:24 AM
Hey Felix,

Just an error here:
         print "            REBOOT request from GPIO", SHUTDOWN, ", recycling Rpi ..."
Should be BOOT instead of SHUTDOWN.

but yeah looking good!
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: Felix on November 18, 2016, 08:17:34 AM
Quote from: tonyp7 on November 18, 2016, 07:38:24 AM
Just an error here:
         print "            REBOOT request from GPIO", SHUTDOWN, ", recycling Rpi ..."
Should be BOOT instead of SHUTDOWN.
Actually that's the SHUTDOWN signal, used in both cases (HIGH pulse for reboot, or permanent HIGH for shutdown).
Thanks for the feedback and contributions!
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: tonyp7 on November 18, 2016, 09:43:50 PM
My bad you're right. All good then! Happy to contribute a small bit!
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: Felix on November 20, 2016, 08:44:10 AM
Someone reported this issue but I don't see it:

https://github.com/LowPowerLab/ATX-Raspi/issues/3

Is it possible because they're using sh and not bash?
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: tonyp7 on November 24, 2016, 09:37:19 PM
Sorry Felix haven't had time to troubleshoot this --I'll get to it this weekend.
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: Felix on November 25, 2016, 10:10:47 AM
No worries, I think it's because the person was using sh, when the setup script is intended for bash.
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: tonyp7 on November 30, 2016, 12:45:31 AM
Hey Felix,

I confirm:  bash installs it correctly. sh recreates the issue.

Tested on Ubuntu 16.04.1 LTS.
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: Felix on November 30, 2016, 08:18:49 AM
Thanks,
Are there instances where a user is forced to use sh or bash is not even available? I don't know of any such case on raspbian/debian.
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: tonyp7 on December 08, 2016, 08:47:04 AM
Hey Felix,

Sorry I was off a few days. "sh" does not exist per say. On most distro sh is a symlink to bash. On debian sh symlinks to dash, another shell.

Bash is installed on all distros as far as I know.
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: Felix on December 08, 2016, 08:50:17 AM
Quote from: tonyp7 on December 08, 2016, 08:47:04 AM
On most distro sh is a symlink to bash. On debian sh symlinks to dash, another shell.

Ok so then how come:

QuoteI confirm:  bash installs it correctly. sh recreates the issue.
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: tonyp7 on December 09, 2016, 11:01:26 PM
Because Debian's sh runs /bin/dash, which exhibits this different behavior with the way it handles \n in a script file. If you forcefully runs bash on the script it works fine.
Title: Re: ATXRaspi shutdowncheck script with interrupts: shutdownirq.py
Post by: Felix on December 12, 2016, 09:02:18 AM
I see, would there be an easy way to make the script universal?