1

I'm working on a python project which should be able to control my backlight brightness. I working with Ubuntu 17.04 and I already locate where the file is that show my backlight brightness

/sys/class/backlight/acpi_video0/brightness

the command that I can use in the bash terminal to change the value is

sudo su -c 'echo 12 > /sys/class/backlight/acpi_video0/brightness'

but I have no clue how to implement this in a py project. Maybe this is also the wrong way to start.

Thank you Guys for probably helping me out.

  • Task 1: fix the permission of the files so you are able to change the brightness without `sudo`. – Klaus D. Oct 31 '17 at 01:31
  • Yes I have just figured out but I have also no idea how to do this. I have found something useful on here https://www.raspberrypi.org/forums/viewtopic.php?t=134390&p=894761 Let me search around a bit than i can post my code –  Oct 31 '17 at 01:33
  • This is sysfs not a real filesystem. You may not change its permissions. In Python just open the file, write to it and close it. – geckos Oct 31 '17 at 02:05

4 Answers4

1

you can use either os.system() or subprocess.Popen()

Not really recommended, but I see no harm in it for a personal project where the input isn't coming from an external source.. That being said, one should take care using this, because you will be executing straight from your command line, so anything your CLI can do, this can do. You have been warned.

Using os.system() (you might have to prepend the path to your shell to the command. This is usually /bin/bash in Linux.):

import os os.system('echo "your command goes here"')

if that doesn't work, then it should look something like:

os.system('/bin/bash echo "your command goes here"')

Using subprocess.Popen() (again, you might need to prepend the path to your shell before the rest of the command.:

import subprocess subprocess.Popen('echo "your command goes here"')

Once again, I will say, this is NOT recomended for frequent usage especially where outside sources may affect the output of the command being ran. Only use this when you KNOW what will be input into the command.

Jebby
  • 1,845
  • 1
  • 12
  • 25
1

So I do a bit of research and on a this site https://wiki.archlinux.org/index.php/backlight I have found the command

gdbus call --session --dest org.gnome.SettingsDaemon.Power --object-path /org/gnome/SettingsDaemon/Power --method org.freedesktop.DBus.Properties.Set org.gnome.SettingsDaemon.Power.Screen Brightness "<int32 50>"

I have no idea how this works but i changed my backlight.

It only works on gnome!! but because i use gnome and the application should be for gnome it is OK for me

my function now looks like this:

def change_brightness(self, value):
    os.system('gdbus call --session --dest org.gnome.SettingsDaemon.Power --object-path /org/gnome/SettingsDaemon/Power --method org.freedesktop.DBus.Properties.Set org.gnome.SettingsDaemon.Power.Screen Brightness "<int32 ' + str(value) + '>"')

the value must be between 0 and 100

1

In Ubuntu, I achieved this using xbacklight package and python's os.system() imported from os module.

Installation :

sudo apt install xbacklight

Python command:

os.system('xbacklight -set ' + str(value)) where value is the input.

Markandeya
  • 507
  • 4
  • 13
0

Try this:

def set_brightness(brightness):
    if int(brightness) > 15:
        raise TypeError("Need int 0 < and > 15")
    elif int(brightness) < 0:
        raise TypeError("Need int 0 < and > 15")
    with open("/sys/devices/pci0000:00/0000:00:02.0/backlight/acpi_video0/brightness","w") as bright:
         bright.write(str(brightness))
         bright.close()
set_brightness(0) #Brightness 0-15