Flashing usr0 works in terminal but led on board doesn't change? C++

Hello,

I have been going through these steps: http://elinux.org/Beagleboard:C/C%2B%2B_Programming

I have successfully ssh’d into my BeagleBone Black using MobaXterm and typed the code in the above link into a file (I have tried this with geddit and nano now and get the same results with both). The code is supposed to write “LED Flash Start” to the terminal, flash USR0 on the board 10 times, and then write “LED Flash Finish”. The code compiles fine and when it runs it displays “LED Flash Start”, waits for awhile, and then displays “LED Flash Finish” but the USR0 never changes from it’s usual heartbeat flashing pattern. Any ideas on why the USR0 LED doesn’t work as it should?

Thanks in advance.

Probably because the OS think it still has control of that LED - And rightly so(because it does ). So disable the heartbeat pattern on USR0 first then try again. Also, if you’re not running that app as root, or sudo. It could fail silently, or possibly seg fault. The following disables the heartbeat pattern:

$ ls /sys/class/leds/beaglebone:green:usr0
brightness device max_brightness power subsystem trigger uevent
$ sudo su

echo “none” > /sys/class/leds/beaglebone:green:usr0/trigger

By the way, no idea why that code is compiled in g++, or uses the using keyword. Because it’s all C. Well, it does use cout for stdout too I suppose, but the code uses C style strings, instead of the C++ string classes . . .

This should compile using gcc, if you’re not interested in the C++ warts in that code . . . Although for a modern version of gcc, you may have to explicitly include another header or two.

 #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";
 
 for(int 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");
 }

#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:

  1. Run the code as root
  2. Run the code with sudo
  3. 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.

Anyway, this is how I probably would have done things . . .

#include <stdio.h>
#include <unistd.h>

#include <fcntl.h> /* Needed for O_WRONLY*/

int main(){

int fd = open("/sys/class/leds/beaglebone:green:usr0/brightness", O_WRONLY);
if(fd < 0)
printf(“Unable to open file, check permissions.\n”);
return 1;

int i;
while(i++ < 10){
write(fd, “1”, 1);
usleep(500000);

write(fd, “0”, 1);
usleep(250000);
}
close(fd);
}

errr, oooops, really bad form. Hence why I normally do not write late night code . . . Anyway, this is a “proper” way to do what I was attempting to demonstrate above.

#include <stdio.h>
#include <unistd.h>

#include <fcntl.h> /* Needed for O_WRONLY*/

int main(){

int fd = open("/sys/class/leds/beaglebone:green:usr0/brightness", O_WRONLY);
if(fd < 0) {
printf(“unable to open usr0 file, check permissions.\n”);
return 1;
}

int i = 0;
while(i++ < 10){
write(fd, “1”, 1);
usleep(500000);

write(fd, “0”, 1);
usleep(250000);
}
close(fd);
return 0;
}