Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Writing to a file to change LED's state

Status
Not open for further replies.

electroRF

Member
Hi,

In the BBB (beagleboneblack), the following program changes the LED's state:

Code:
int main()
{
    FILE *led;

    led = fopen("/sys/class/leds/beaglebone:green:usr3/brightness", "w");

    while(1)
    {
        fprintf(led,"%d",1);
        fflush(led);
        pauseSec(1);
        fprintf(led,"%d",0);
        fflush(led);
        pauseSec(1);
    }

    fclose(led);
    return 0;
}

How does it work?

Is there an interrupt when the file "brightness" is written, which changes the LED's state according to the input?

Or other method for checking the file's content?

Thank you.
 
"/sys" is a special filesystem designed for ordinary programs to communicate with Kernel. There's no real files behind it. When you read/write to it, it goes directly from/to the Kernel.
 
FYI, the code after the while(1) loop will never get executed and is therefore not needed.

Mike.
 
Thank you NorthGuy and Mike! :)

NorthGuy
Thanks, it makes sense.
It is correct for all the files in /sys folder and its sub folders?

I thought there would be system-calls to interface Linux Kernel, are there?

Pommie
Oh you're right.
How then this process ever gets terminated?
When it does terminate, would it close the "file"?


* BTW, what is the fflush(led); for?
 
Last edited:
Thank you NorthGuy!

I'm going to get a project in job which involves working with VxWorks.

As I've never worked with OS before, I'm practicing Linux.

Do you have an advice how I could practice multi-process programming?
 
Hi NorthGuy!

I actually meant for both, I wanna practice both multi-threaded process and multi-process programming.
 
Status
Not open for further replies.

Latest threads

Back
Top