You are not logged in.

#1 2009-12-01 19:59:58

rent0n
Member
From: Italy
Registered: 2009-10-29
Posts: 457
Website

Correct my LCD brightness bash script!

Hi there big_smile

I'm trying to write a script to control my LCD brightness because Arch always set it to 70% or so at startup.
I would like to have full brightness when the AC adapter is present and to have a decrease in brightness when I'm running on battery.

It seems to work quite well but I would like to have suggestions on how to improve it because I have not bash scripting / programming skills at all! big_smile

#!/bin/bash

if cat /proc/acpi/ac_adapter/ACAD/state | grep on-line
then
    echo 9 | sudo tee /sys/class/backlight/acpi_video0/brightness
    echo 9 | sudo tee /sys/class/backlight/acpi_video1/brightness
    echo 9 | sudo tee /sys/devices/virtual/backlight/acpi_video0/brightness
    echo 9 | sudo tee /sys/devices/virtual/backlight/acpi_video1/brightness
    echo 100 | sudo tee /proc/acpi/video/GFX0/DD03/brightness
    echo 100 | sudo tee /proc/acpi/video/GFX0/DD04/brightness
    xbacklight -set 100
    exit 0
else
    echo 4 | sudo tee /sys/class/backlight/acpi_video0/brightness
    echo 4 | sudo tee /sys/class/backlight/acpi_video1/brightness
    echo 4 | sudo tee /sys/devices/virtual/backlight/acpi_video0/brightness
    echo 4 | sudo tee /sys/devices/virtual/backlight/acpi_video1/brightness
    echo 40 | sudo tee /proc/acpi/video/GFX0/DD03/brightness
    echo 40 | sudo tee /proc/acpi/video/GFX0/DD04/brightness
    xbacklight -set 40
    exit 1
fi

What do you think of it? Maybe it's too redundant?
Give me your feedback! Thanks! smile

Last edited by rent0n (2009-12-01 20:00:59)


rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686

Offline

#2 2009-12-01 22:05:48

jelly
Administrator
From: /dev/null
Registered: 2008-06-10
Posts: 714

Re: Correct my LCD brightness bash script!

echo 4 | sudo tee /sys/class/backlight/acpi_video0/brightness
Can't you replace that with:

echo 4 > sudo tee /sys/class/backlight/acpi_video0/brightness

Offline

#3 2009-12-01 22:20:22

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Correct my LCD brightness bash script!

rent0n wrote:

What do you think of it? Maybe it's too redundant?
Give me your feedback! Thanks! smile

Yes, I would use functions.

#!/bin/bash

backlight()
{   # usage: backlight int
    local files=(
        /sys/class/backlight/acpi_video0/brightness
        /sys/class/backlight/acpi_video1/brightness
        /sys/devices/virtual/backlight/acpi_video0/brightness
        /sys/devices/virtual/backlight/acpi_video1/brightness
    )
    echo $1 | sudo tee ${files[@]}
}

video()
{   # usage: video int
    local files=(
        /proc/acpi/video/GFX0/DD03/brightness
        /proc/acpi/video/GFX0/DD04/brightness
    )
    echo $1 | sudo tee ${files[@]}
}

if cat /proc/acpi/ac_adapter/ACAD/state | grep -q on-line
then
    backlight 9
    video 100
    xbacklight -set 100
    exit 0
else
    backlight 4
    video 40
    xbacklight -set 40
    exit 1
fi

I haven't tested this nor can I comment on the brightness interface stuff.

Edit 2: I would remove the sudoes and call the whole script using sudo.

Edit:

jelly wrote:

echo 4 | sudo tee /sys/class/backlight/acpi_video0/brightness
Can't you replace that with:

echo 4 > sudo tee /sys/class/backlight/acpi_video0/brightness

No, sudo is a command, not a file.

Last edited by fsckd (2009-12-01 22:26:13)


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#4 2009-12-01 22:36:29

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Correct my LCD brightness bash script!

Ugh, my network is not great today.

Anyways, two links that can help you in learning bash:

http://tldp.org/LDP/Bash-Beginners-Guide/html/

http://tldp.org/LDP/abs/html/

Edit: Explanation of my changes above.

I added two functions, backlight and video which each take a single argument (I believe should be an integer). The files are listed as arrays using the array_var=( item1 item2 ... itemn ) syntax. This allows for using the array as arguments to a command thanks to array expansion, like so, command ${array_var[@]}. Finally I added -q to the grep to silence it. See grep's manual for more scintillating options. Hope that helps.

Last edited by fsckd (2009-12-01 22:45:14)


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#5 2009-12-02 07:34:32

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Correct my LCD brightness bash script!

Does acpid still work?  I tried to start the daemon and get (yeah, put it before hal,dbun in the daemon array):

acpid: can't open /proc/acpi/event: Device or resource busy

The reason I talk about this is because acpid is used for power management events and it may be able to be used for mapping to your brightness keys.  If that doesn't work you can use xbindkeys.  If that isn't enough, a udev event can be created that detects when your battery is plugged in/out and trigger an event.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#6 2009-12-02 11:34:06

rent0n
Member
From: Italy
Registered: 2009-10-29
Posts: 457
Website

Re: Correct my LCD brightness bash script!

@fsckd: thank you very much for your help, this would definitely improve my script. However, it sounds a bit more complicated to me now. smile I really would like to learn bash scripting, if only I have enough time! roll

@Gen2ly: Well, actually, my brightness keys are working perfectly. The problem was just to set the default brightness level at startup because arch always sets it to 70% or so. cool


rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686

Offline

#7 2009-12-02 21:25:16

NeoXP
Member
From: MS Matrix
Registered: 2009-01-09
Posts: 206
Website

Re: Correct my LCD brightness bash script!

Don't know if it is of any use to you, but I'm using acpid to handle this. The part regarding screen brightness is handled in the following part of my handler.sh:

    ac_adapter)
        if [ `awk '{print $2}' /proc/acpi/ac_adapter/C1E8/state` == "off-line" ];
        then
            logger "AC Adapter disconnected"
            echo 7 > /sys/class/backlight/acpi_video0/brightness
        else
            logger "AC Adpater connected"
            echo 10 > /sys/class/backlight/acpi_video0/brightness
        fi
    ;;

This works quite well for me.


Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."

Offline

#8 2009-12-02 22:11:28

rent0n
Member
From: Italy
Registered: 2009-10-29
Posts: 457
Website

Re: Correct my LCD brightness bash script!

NeoXP wrote:

Don't know if it is of any use to you, but I'm using acpid to handle this. The part regarding screen brightness is handled in the following part of my handler.sh:

    ac_adapter)
        if [ `awk '{print $2}' /proc/acpi/ac_adapter/C1E8/state` == "off-line" ];
        then
            logger "AC Adapter disconnected"
            echo 7 > /sys/class/backlight/acpi_video0/brightness
        else
            logger "AC Adpater connected"
            echo 10 > /sys/class/backlight/acpi_video0/brightness
        fi
    ;;

This works quite well for me.

Well this is interesting...Acpid is probably the best way to handle these issues.
I can't understand what the "logger" part is doing, can you please explain it?

Last edited by rent0n (2009-12-02 22:12:06)


rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686

Offline

#9 2009-12-02 22:20:21

NeoXP
Member
From: MS Matrix
Registered: 2009-01-09
Posts: 206
Website

Re: Correct my LCD brightness bash script!

rent0n wrote:
NeoXP wrote:

Don't know if it is of any use to you, but I'm using acpid to handle this. The part regarding screen brightness is handled in the following part of my handler.sh:

...

This works quite well for me.

Well this is interesting...Acpid is probably the best way to handle these issues.
I can't understand what the "logger" part is doing, can you please explain it?

It logs the text between the quotes in my messages.log and in my everything.log. If something goes wrong on my system it helps me find clues.


Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."

Offline

#10 2009-12-03 17:35:38

rent0n
Member
From: Italy
Registered: 2009-10-29
Posts: 457
Website

Re: Correct my LCD brightness bash script!

NeoXP wrote:

It logs the text between the quotes in my messages.log and in my everything.log. If something goes wrong on my system it helps me find clues.

I was thinking that your solution should work well when you switch between ac and battery power, however it shouldn't handle LCD brightness at startup, when you boot your laptop, because no acpi event is triggered. Am I wrong?
In my case, I want to set the correct brightness level when I power up my laptop and, for this reason, I placed my script in my autostarted program list. Xfce-power-manager already reduces screen brightness when I switch from ac to battery power.


rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686

Offline

#11 2009-12-05 15:53:39

NeoXP
Member
From: MS Matrix
Registered: 2009-01-09
Posts: 206
Website

Re: Correct my LCD brightness bash script!

rent0n wrote:

I was thinking that your solution should work well when you switch between ac and battery power, however it shouldn't handle LCD brightness at startup, when you boot your laptop, because no acpi event is triggered. Am I wrong?
In my case, I want to set the correct brightness level when I power up my laptop and, for this reason, I placed my script in my autostarted program list. Xfce-power-manager already reduces screen brightness when I switch from ac to battery power.

I think you're right, there is no difference in brightness if I boot plugged or unplugged. Your script is indeed a good idea to set brightness at boot time.

Thx.


Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."

Offline

Board footer

Powered by FluxBB