Welcome! Log In Create A New Profile

Advanced

LED control on Pogoplug E02

Posted by BuckNaked 
LED control on Pogoplug E02
June 29, 2017 04:52PM
I'm chasing what I think should be an obvious issue, but I can't find it.

The host is a Pogoplug E02 running Debian jessie.

Logged into the system as root, the following two commands turn the green LED on and off as expected.

root@www:# echo "default-on" > "/sys/class/leds/status:green:health/trigger"
root@www:# echo "none" > "/sys/class/leds/status:green:health/trigger"

So here is a little program adapted from the one at https://github.com/suetanvil/ledflash/blob/master/ledflash.c, which to my mind should blink the green LED on and off a few times:

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <sys/stat.h>

#   define FLASHFILE   "/sys/class/leds/status:green:health/trigger"
#   define ON          "default-on"
#   define OFF         "none"

void main() {
    FILE *ledfile;
    int status;

  ledfile = fopen(FLASHFILE, "w");
  if (ledfile == NULL) {
    printf("Unable to open '" FLASHFILE "' for write\n");
    printf("Error: %d (%s)\n", errno, strerror(errno));
    exit(0);
  }

  status = fputs("none", ledfile);
  sleep(1);
  status = fputs("default-on", ledfile);
  sleep(1);
  status = fputs("none", ledfile);
  sleep(1);
  status = fputs("default-on", ledfile);
  sleep(1);
  status = fputs("none", ledfile);
  sleep(1);
  status = fputs("default-on", ledfile);
  sleep(1);
  status = fputs("none", ledfile);
  sleep(1);
  status = fputs("default-on", ledfile);
  sleep(1);
  status = fputs("none", ledfile);
  sleep(1);
  status = fputs("default-on", ledfile);
  sleep(1);
  status = fputs("none", ledfile);

  exit(0);
}

The program's effect is exactly nothing. No error on the LED device file open, no blinking LED.

Does anyone see what the problem is here?
Re: LED control on Pogoplug E02
June 29, 2017 09:50PM
I don't know why it doesn't work and I'd be interested to know myself, but try this:
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <sys/stat.h>

#   define FLASHFILE   "/sys/class/leds/status:green:health/trigger"
#   define ON          "default-on",10
#   define OFF         "none",4

void main() {
    int status;
    int fn;

  fn = open(FLASHFILE,O_WRONLY);
  if(fn < 1){
    printf("Unable to open '" FLASHFILE "' for write\n");
    printf("Error: %d (%s)\n\n", errno, strerror(errno));
    exit(0);
  }

  status = write(fn,"none",4);
  sleep(1);
  status = write(fn,"default-on",10);
  sleep(1);
  status = write(fn,"none",4);
  sleep(1);
  status = write(fn,"default-on",10);
  sleep(1);
  status = write(fn,"none",4);
  sleep(1);
  status = write(fn,"default-on",10);
  sleep(1);
  status = write(fn,"none",4);
  sleep(1);
  status = write(fn,"default-on",10);
  sleep(1);
  status = write(fn,"none",4);
  sleep(1);
  status = write(fn,"default-on",10);
  sleep(1);
  status = write(fn,"none",4);
  close(fn);

  exit(0);
}
-JT
Re: LED control on Pogoplug E02
June 29, 2017 09:58PM
That works great. I'll adopt the code into the original program.

(Is it a disgraceful abuse of 70 years of hard work, technology and computer science to send Morse code through an LED on a Pogoplug?)
Re: LED control on Pogoplug E02
June 29, 2017 10:30PM
BuckNaked Wrote:
-------------------------------------------------------
> That works great. I'll adopt the code into
> the original program.
>
> (Is it a disgraceful abuse of 70 years of hard wor
> k, technology and computer science to send Morse c
> ode through an LED on a Pogoplug?)

Yes it is :)

You can try to echo "heartbeat" to the health trigger (instead of "default-on"). And then turn it back to "default-on" after a period of time.

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: LED control on Pogoplug E02
June 30, 2017 10:15AM
Expanding on renojim's solution above ... the problem occurs due to linux file buffering / caching.

It would be possible to get around it by calling fflush after every fputs, but the resulting code would be ugly and I believe that using write is a better solution anyway.
Re: LED control on Pogoplug E02
June 30, 2017 05:01PM
Yes. It was a buffer flush problem.

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: