Ok something that deserves a little post
My problem:
I'm sometimes using my raspberry pi 400 (equivalent to pi 4b) on a 2 monitors setup (CRT because I'm a cool kid) : one to watch movies / TV Shows, the other to write some shit, which requires a bit of performance because I like it to be responsive
And sometimes it needs to act as a low power server, then it's mostly idle at least doesn't require that much power, and since it's powered most of the time by a solar panel, I'd like it to be... as power saving as possible.
My idea:
Using keyboard shortcut to switch it to low power, or high power depending on what we need
On low power:
- I want the frequency of the cpu to be as low as possible
- I want the USB to autosuspend quickly
- I want both my hdmi to be turned off
- I don't want any graphical apps
On high power:
- Frequency set to on demand
- USB always on
- Both HDMI must be on
- I need graphical apps
I went with three scripts
One to turn everything off:
#!/bin/bash
systemctl stop lightdm # bye bye graphic apps
cpufreq-set -g powersave # low frequencies... everywhere ! (could be useless thanks to powertop)
systemctl start powertop
powertop --auto-tune # will try to tune everything to low power
/usr/bin/vcgencmd display_power 0 2 #switch off HDMI 0
/usr/bin/vcgencmd display_power 0 7 #switch off HDMI 1
One to turn everything on:
/usr/bin/vcgencmd display_power 1 2 #switch on HDMI 0
/usr/bin/vcgencmd display_power 1 7 #switch on HDMI 1
cpufreq-set -g performance # high frequencies !
systemctl stop powertop
systemctl start lightdm # back to UI
echo 'on' > '/sys/bus/usb/devices/1-1.3.1/power/control' # manually setting some usb devices to on
echo 'on' > '/sys/bus/usb/devices/1-1.2/power/control' # manually setting some usb devices to on
echo 'on' > '/sys/bus/usb/devices/1-1.1/power/control' # manually setting some usb devices to on
cpufreq-set -u 1.80GHz # need to reset max frequency to my max raspi freq, you can check yours with cpufreq-info
One to monitor keyboard inputs:
#!/bin/bash
# Path to your keyboard event device
KEYBOARD_DEVICE=$1 # mine was "/dev/input/event5"
# Path to the script you want to execute
ON_SCRIPT="/home/salcie/turn_on.sh"
OFF_SCRIPT="/home/salcie/turn_off.sh"
# Monitor key events and execute the script when f7 or f8 is pressed
sudo evemu-record "$KEYBOARD_DEVICE" | while read -r event; do
echo "key "$event
if [[ $event == *"EV_KEY"* && $event == *"KEY_F7"* && $event == *" 0000"* ]]; then
echo "F7 key pressed. Turning on !"
timeout 30 "$ON_SCRIPT" &
fi
if [[ $event == *"EV_KEY"* && $event == *"KEY_F8"* && $event == *" 0000"* ]]; then
echo "F8 key pressed. Turning off..."
timeout 30 "$OFF_SCRIPT" &
fi
done
the script needs your input keyboard as argument for example
./monitor.sh /dev/input/event5
you can find it out with evtest
That's it... I guess
Will need to remember sources...
maybe those:
=> https://askubuntu.com/questions/185274/how-can-i-disable-usb-autosuspend-for-a-specific-device
=> https://www.reddit.com/r/raspberry_pi/comments/jyvj5q/comment/mfutkmo/