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;
}
On Tue, Dec 1, 2015 at 12:51 AM, William Hermans <[email protected]> wrote:
> 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);
> }
>
> On Mon, Nov 30, 2015 at 11:24 PM, William Hermans <[email protected]>
> wrote:
>
>> #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 (C) 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.
>>
>>
>> On Mon, Nov 30, 2015 at 9:49 PM, William Hermans <[email protected]>
>> wrote:
>>
>>> 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");
>>> }
>>>
>>>
>>>
>>> On Mon, Nov 30, 2015 at 9:42 PM, William Hermans <[email protected]>
>>> wrote:
>>>
>>>> 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 . . .
>>>>
>>>> On Mon, Nov 30, 2015 at 9:39 PM, William Hermans <[email protected]>
>>>> wrote:
>>>>
>>>>> 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
>>>>>
>>>>>
>>>>> On Mon, Nov 30, 2015 at 8:09 PM, <[email protected]> wrote:
>>>>>
>>>>>> 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.
>>>>>>
>>>>>> --
>>>>>> For more options, visit http://beagleboard.org/discuss
>>>>>> ---
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "BeagleBoard" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to [email protected].
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>
--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.