setpci - Making a script to manually change brightness
Solution 1
Xbacklight doesn't work on my netbook; the backlight isn't visible to the ACPI subsystem. I've hacked these two shell scripts together, though:
brightup.sh:
#!/bin/bash
var1=`sudo setpci -s 00:02.0 f4.b | tr [:lower:] [:upper:]`;
var2=$(echo "obase=16;ibase=16;${var1} + 8" |bc)
sudo setpci -s 00:02.0 f4.b=$var2
brightdown.sh:
#!/bin/bash
var1=`sudo setpci -s 00:02.0 f4.b | tr [:lower:] [:upper:]`;
var2=$(echo "obase=16;ibase=16;${var1} - 8" |bc)
sudo setpci -s 00:02.0 f4.b=$var2
The problem I'm having is that it requires sudo. The end goal of this is to set these two shell scripts to run as keyboard shortcuts, so I need to figure out how to set setpci to be able to run without a password with sudo. I know, it's probably insecure, and a kludge, but I don't see any other options at this point.
also, I apologize for necroing this thread, but these are two scripts I've been looking for on the interwebs ever since I switched back to using Linux on a daily basis.
Solution 2
I ended up making a few scripts to solve my problem.
- I bound the following three scripts to convenient keys (CTRL+ALT+PGUP for brightness up, CTRL+ALT+PGDN for brigthtness down and CTRL+ALT+HOME for restoring brigtness to a default value).
The scripts require sudoless setpci (you can probably play with some chmods after looking at errors you get by running setpci to avoid editing sudoers) via a line like:
androith ALL=(ALL) NOPASSWD: /usr/bin/setpci
Finally, you will have to hold a "state file" in something akin to /home/androith/.scripts/brightness/state containing only a state (BF to begin with is OK).
brightness-up.sh
#!/bin/bash
# Get the stored state from file
exec < /home/androith/.scripts/brightness/state # stdin replaced by this file
read state # first line of file goes to state variable
# Increase the state, up to FF
new_state=$(echo "$[0x$state+0x10]") # in decimal
if [ "$new_state" -gt 255 ] # 255 = FF in hex
then
echo "Already at maximal brightness."
else
state=$(echo "obase=16; $new_state" | bc) # put into hex
echo "Setting brightness to $state and storing state..."
sudo setpci -s 00:02.0 F4.B=$state # passwordless sudo
echo $state > /home/androith/.scripts/brightness/state
fi
brightness-down.sh
#!/bin/bash
# Get the stored state from file
exec < /home/androith/.scripts/brightness/state # stdin replaced by this file
read state # first line of file goes to state variable
# Decrease the state, down to FF
new_state=$(echo "$[0x$state-0x10]") # in decimal
if [ "$new_state" -lt 15 ] # 15 = 0F in hex
then
echo "Already at minimal brightness."
else
state=$(echo "obase=16; $new_state" | bc) # update and put into hex
echo "Setting brightness to $state and storing state..."
sudo setpci -s 00:02.0 F4.B=$state # passwordless sudo
echo $state > /home/androith/.scripts/brightness/state
fi
brightness-default.sh
#!/bin/bash
# Set brightness to default value
state=AF
sudo setpci -s 00:02.0 F4.B=$state # passwordless sudo
echo $state > /home/androith/.scripts/brightness/state
Solution 3
There is a little program called xbacklight. It can get and set the brightness of your screen. If you have a webcam, you can even let it set your brightness automatically by taking a picture, getting the average brightness and setting the screen brightness accordingly.
image='/home/kim/brightness.jpeg'
streamer -c /dev/video0 -b 16 -o $image >/dev/null 2>/dev/null && \
meanline=`convert $image -colorspace gray -verbose info: | grep '^\s*mean: '`
if [[ $meanline =~ \((.*)\) ]]; then
brightness="${BASH_REMATCH[1]}"
brightness=`echo "$brightness * 200" | bc`
echo $brightness
xbacklight -set $brightness
xbacklight
fi
rm -f "$image"
MarkovCh1
Updated on September 18, 2022Comments
-
MarkovCh1 over 1 year
I can use something like
setpci -s 00:02.0 F4.B=XX
replacing 00:02.0 with your device address (obtained via lspci | grep VGA) and XX with a value between 00 and FF (FF = 100%, 7F = 50%, etc...) to manually change my brightness from the terminal (as root, or with proper chmods).
I wish to make a script to manually change my brightness via keystrokes. To do so, I need to get the current state (XX above) in order to relay to the script the current brightness of the screen.
How can I obtain the current state?
-
MarkovCh1 almost 13 yearsTwo complaints: 1) I don't like automation of brightness; and more importantly 2) xbacklight won't work for me. This is why I have to interact via setpci directly.
-
Kim almost 13 yearsif xbacklight doesn't work for you, why not file a bug?
-
MarkovCh1 almost 13 yearsIt's due to the kernel settings, I think...
-
slm over 11 yearsGood first post.