1

I know the trigger for on-board leds can be controlled from the command-line (or a script) I have a python program that I want to temporarily take over the system led blink it a little and return control to the trigger it had previously. I know I can read the current trigger by cat /sys/class/leds/led0/trigger and set it by echo mmc0 >/sys/class/leds/led0/trigger.

When I tried to do:

f=open("/sys/class/leds/led1/trigger","w")
f.write("input")

Nothing changed even though python did not complain (as long as it was started as root, which is another problem in itself).

Edit: I tried treating /sys/class/leds/led1/trigger like a regular ASCII file and opened it using sudo vi, erased the whole line with 'none' and it worked. Tried again and replaced the line with 'input' and it went back to its original trigger. So it seems like it can be treated like a regular text file. (I don't fully understand how it works just what it seems like)

I know that I can call:

os.system("echo input | sudo tee /sys/class/leds/led1/trigger > /dev/null")

However since the program is in python I was wondering if it can be controlled from within python without making a system call and how to do that.

2
  • The lines you list seem fine so far, it would be a good idea to close the file after the write. The files behave similar to regular files, that is the reason why you can use echo and cat.
    – RalfFriedl
    May 3, 2019 at 16:53
  • @RalfFriedl They are fine -except they don't work!- I would close the file after but that code is not being used because it does nothing. It seems that file is a little different than a regular file but I really don't know.
    – FoxSam12
    May 3, 2019 at 19:47

1 Answer 1

0

Something like:

from sys import stderr,exit
from time import sleep

def toggle(hled,value,seconds):
    hled.write(value)
    hled.flush()
    hled.seek(0,0)
    sleep(seconds)

def playseq(led,times,secon,secoff):
    hled = open(LED[led], 'w')
    while (times > 0):
        toggle(hled,'1',secon)
        toggle(hled,'0',secoff)
        times-=1
    hled.close()

################################
### MAIN
################################
if __name__ == '__main__':
    if (geteuid() != 0):
        stderr.write('You must be root to run this\n')
        exit(9)
    GREEN   = 0
    RED     = 1
    LED     = {0: '/sys/class/leds/led0/brightness', 1: '/sys/class/leds/led1/brightness'}   # GREEN,RED
    # blink green 5 times, on for 0.1 sec, off for 0.2 sec
    playseq(GREEN,5,0.1,0.2)
    sleep(1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.