#include <stdio.h>
#include <unistd.h>
int main(){
printf(“LED Flash Start\n”);
FILE *LEDHandle = NULL;
const char *LEDBrightness="/sys/class/leds/beaglebone:green:usr0/brightness";
int i;
for(i=0; i<10; i++){
if((LEDHandle = fopen(LEDBrightness, “r+”)) != NULL){
fwrite(“1”, sizeof(char), 1, LEDHandle);
fclose(LEDHandle);
}
usleep(1000000);
if((LEDHandle = fopen(LEDBrightness, “r+”)) != NULL){
fwrite(“0”, sizeof(char), 1, LEDHandle);
fclose(LEDHandle);
}
usleep(1000000);
}
printf(“LED Flash End\n”);
}
$ gcc --version
gcc (Debian 4.6.3-14) 4.6.3
Copyright © 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc test.c -o test
$ sudo ./test
LED Flash Start
LED Flash End
First, this works as expected. So a couple of notes. First, the iteration variable must be declared outside of the for loop as demonstrated in the code above. This is a C11 standard “feature” I believe. Second, this code will appear to work at first glance, if run without elevated permissions, but really, accessing /sys/class/leds/beaglebone:green:usr0/brightness in the code will fail. So, you have a few options:
- Run the code as root
- Run the code with sudo
- Change the GUID for the /sys/class/leds/beaglebone:green:usr0/brightness file.
The last option here I believe would have to be done every system up(boot), or you would need to create a udev rule. Essentially though, you need your regular user to be part of a group that has permissions to write to this file. Well actually since fopen() uses the mode r+, you probably need read permissions as well. r+ meaning read / update.