Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Power button script does not work
#1
Tongue 
Hello, 

First I want to congratulate developers for releasing ROPI 4.1, it is an amazing distro. On the other hand, I want to apologize for my bad English, it is not my native language.

Today I have been working on a script that every time is executed, it puts ROPI to sleep if ROPI is running and wakes up ROPI if it is sleeping. The idea is to read the status of the CPU inside a file: if the CPU is set to "powersave" then the script wakes up ROPI and if the CPU is set to "interactive", the script puts ROPI to sleep. 

The file that is read is "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor" and the script is the next:


Code:
#!/bin/bash

set -e
set -u

filename="/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"

while IFS= read -r line; do
   if [[ $line == interactive ]] ; then
       sudo sh -c "echo 4 > /sys/class/graphics/fb0/blank"
       sudo sh -c "echo 255 > /sys/class/leds/red_led/brightness"
       sudo sh -c "echo 0 > /sys/class/leds/green_led/brightness"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor"
       sudo pkill -f bgmusic.py
       sudo killall -q mplayer
       sudo service acpid restart
   fi
   if [[ $line == powersave ]] ; then

       sudo sh -c "echo 0 > /sys/class/graphics/fb0/blank"
       sudo sh -c "echo 0 > /sys/class/leds/red_led/brightness"
       sudo sh -c "echo 255 > /sys/class/leds/green_led/brightness"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor"
       python /home/pi/RetrOrangePi/Background_Music/bgmusic.py &
   fi
done < "$filename"

This script is called "TurnOnOff.sh" and is located at "/home/pi/RetrOrangePi/Power_Button" with other power scripts.

Then I assign this script to the OrangePi power button, modifying the next file "/etc/acpi/events/button_power":

Code:
event=button/power
action=/bin/sh /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh

if you run this script on a shell with a SSH client (I use putty), it works perfectly and flawlessly (even when ROPI is sleeping) but when the power button of the OrangePi is pressed, the script does not work, it looks like it cannot enter in the while loop.  Angry

I still don´t understand why when ROPI is asleep, the loop fails, and now i am running out of ideas.

Can someone help me to solve this issue?

All help is highly appreciated.  Blush
Reply
#2
I'm traveling at the moment, I'll try when I get home in a couple of days
Reply
#3
(02-10-2018, 01:47 AM)yopyop Wrote: Hello, 

First I want to congratulate developers for releasing ROPI 4.1, it is an amazing distro. On the other hand, I want to apologize for my bad English, it is not my native language.

Today I have been working on a script that every time is executed, it puts ROPI to sleep if ROPI is running and wakes up ROPI if it is sleeping. The idea is to read the status of the CPU inside a file: if the CPU is set to "powersave" then the script wakes up ROPI and if the CPU is set to "interactive", the script puts ROPI to sleep. 

The file that is read is "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor" and the script is the next:


Code:
#!/bin/bash

set -e
set -u

filename="/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"

while IFS= read -r line; do
   if [[ $line == interactive ]] ; then
       sudo sh -c "echo 4 > /sys/class/graphics/fb0/blank"
       sudo sh -c "echo 255 > /sys/class/leds/red_led/brightness"
       sudo sh -c "echo 0 > /sys/class/leds/green_led/brightness"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor"
       sudo pkill -f bgmusic.py
       sudo killall -q mplayer
       sudo service acpid restart
   fi
   if [[ $line == powersave ]] ; then

       sudo sh -c "echo 0 > /sys/class/graphics/fb0/blank"
       sudo sh -c "echo 0 > /sys/class/leds/red_led/brightness"
       sudo sh -c "echo 255 > /sys/class/leds/green_led/brightness"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor"
       python /home/pi/RetrOrangePi/Background_Music/bgmusic.py &
   fi
done < "$filename"

This script is called "TurnOnOff.sh" and is located at "/home/pi/RetrOrangePi/Power_Button" with other power scripts.

Then I assign this script to the OrangePi power button, modifying the next file "/etc/acpi/events/button_power":

Code:
event=button/power
action=/bin/sh /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh

if you run this script on a shell with a SSH client (I use putty), it works perfectly and flawlessly (even when ROPI is sleeping) but when the power button of the OrangePi is pressed, the script does not work, it looks like it cannot enter in the while loop.  Angry

I still don´t understand why when ROPI is asleep, the loop fails, and now i am running out of ideas.

Can someone help me to solve this issue?

All help is highly appreciated.  Blush

Hi, the script permission are set for user pi and group pi ?
Reply
#4
Thumbs Up 
yes, the permissions were set to be freely used by any user with chmod.


I found a solution to the problem, if you try to to run next command:

Code:
/bin/sh /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh


The shell gives the next error code:

Code:
/home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh: 9: /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh: [[: not found
/home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh: 21: /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh: [[: not found

But if you run the next code ('action=' at the beginning):

Code:
action=/bin/sh /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh

Then the script runs nicely.

So I change the next file "/etc/acpi/events/button_power":

Code:
event=button/power
action=action=/bin/sh /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh

The only difference between this version and the last one is the extra '=action' code.

With this modification, the script runs, even using the Orangepi button. So it is nice.  Cool Cool

With this script, the Orangepi button is a switch button to wake up the ROPI or put it to sleep.

My only problem now is that every time I sent the ROPI to sleep through the emulationstation menu, the file "/etc/acpi/events/button_power" is changed automatically. 

How can I change what the sleep menu option does in emulationstation?

Thanks a lot for the support!
Reply
#5
(02-17-2018, 04:02 AM)yopyop Wrote: My only problem now is that every time I sent the ROPI to sleep through the emulationstation menu, the file "/etc/acpi/events/button_power" is changed automatically. 

How can I change what the sleep menu option does in emulationstation?

Thanks a lot for the support!

edit /home/pi/RetrOrangePi/Power_Button/Sleep_mode.sh and remove the first line (that changes acpi/button_power) otherwise you would have to recompile EmulationStation code.
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  RetrOrangePi 4.1 - Orange Pi Zero GPIO - Getting GPIO to work bharathram21391 15 1,312 05-15-2018, 04:58 PM
Last Post: alexkidd
  Fba dont work CPS2 Games. Help batistars30 3 483 04-03-2018, 08:46 PM
Last Post: alexkidd
  Update ES Kiosk-Kid mode script broke my ES KeyDet 15 3,058 01-15-2018, 10:45 PM
Last Post: alexkidd
  nothing will work cossie123 1 427 01-14-2018, 12:25 PM
Last Post: alexkidd
  splashScreen dont' work Ropi 4.1 caioandrade 2 923 12-14-2017, 12:35 AM
Last Post: caioandrade
  WiFi doesn't work on ROPi OPi PC+ USB dongle zachxa 5 1,443 12-11-2017, 12:11 AM
Last Post: alexkidd

Forum Jump:


Users browsing this thread: 1 Guest(s)