nummy
Posts: 35
Joined: Tue Aug 14, 2012 12:07 am

Restart/Shutdown with Gertboard Pushbuttons

Fri Jul 26, 2013 11:40 pm

I have a Gertboard rev1 and I was wondering if there was an easy way to make the pushbuttons serve as a restart button and/or a halt button.

I have managed to open up buttons.c (from the gertboard_sw package) which loops and checks the button GPIO status ... and I can determine which pushbutton is pressed but then I'm not sure how to reboot/halt the system in C. I'd like to do 'shutdown -r now' if one button is pressed, and 'shutdown -h now' if another is pressed.

This would be extremely useful as I am running a headless RasPi.

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Restart/Shutdown with Gertboard Pushbuttons

Sat Jul 27, 2013 4:56 pm

The simplest is to execute the command using 'system'.
system("This command is executed");
In your case:
system("sudo shutdown -r now");
But you need a program always running in a loop to detect the buttons.
(You had better put a 'sleep' in the loop else it uses 100% CPU time)
There is interrupt code for the GPIO pins which is a more elegant solution.
I don't know where that is.

nummy
Posts: 35
Joined: Tue Aug 14, 2012 12:07 am

Re: Restart/Shutdown with Gertboard Pushbuttons

Sun Jul 28, 2013 3:58 am

great thank you!!!!

Return to “HATs and other add-ons”