Geekmason
Posts: 5
Joined: Sat Jun 08, 2013 1:51 pm

Custom Case indicator LEDs

Sat Jun 08, 2013 2:08 pm

Hi,

I'm working on a customised case for my Pi. I have mounted two LEDs one for power and one (intended) for Activity just on the PCB of the Pi itself.

I have found wiring the power LED easy by connecting a the power led (with resistor) to GPIO 2 and 6.

An activity LED is not turning out quite as easy. I have searched around and it seems like I may have to program a GPIO pin to do this for me.

I'm pretty new at this (programming), so if anyone can drop me some hints on how this would be achievable that would be great.

pjc123
Posts: 921
Joined: Thu Mar 29, 2012 3:37 pm

Re: Custom Case indicator LEDs

Sat Jun 08, 2013 6:02 pm

Geekmason wrote: I have found wiring the power LED easy by connecting a the power led (with resistor) to GPIO 2 and 6.
I assume you mean physical pins 2 and 6, not GPIO 2 and 6? On my chassis I do the same thing, except I tap in at my 5V source before it reaches the pi, because I am powering other things as well.

However, it is more important for me to know when a shutdown command has completed (In particular that the file systems have unmounted), so I know when I can safely turn off the power switch on my chassis. You might find that handy as well. This can be done by connecting physical pin 8 to an LED through a resistor to GND. Actually, I do it a little differently, as I do with all my GPIOs; I use a transistor to turn on/off the 5V supply to the LED. When the pi is turned on the LED turns on, and when the shutdown is complete the LED turns off (That LED is independent of the power LED, which would still be on after a software shutdown).

I do not have an activity LED on my chassis as I have not critical need for that, but it would be a nice addition. I never tried it so I can't help you in that regard.
My Raspberry Pi Project Page:

https://www.flaminghellmet.com/launch/

Geekmason
Posts: 5
Joined: Sat Jun 08, 2013 1:51 pm

Re: Custom Case indicator LEDs

Sun Jun 09, 2013 1:48 am

Yes, i meant pins 2 and 6.

I have found further suggestions towards programming a pin to indicate activity in accordance with the processes that govern access to the SD card.
Haven't found any examples of code that can do this.

Geekmason
Posts: 5
Joined: Sat Jun 08, 2013 1:51 pm

Re: Custom Case indicator LEDs

Sun Jun 09, 2013 8:40 am

This thread might help.

http://www.raspberrypi.org/phpBB3/viewt ... 31&t=12530

Are the references to mmc0 led on the right track?

Geekmason
Posts: 5
Joined: Sat Jun 08, 2013 1:51 pm

Re: Custom Case indicator LEDs

Mon Jun 17, 2013 9:14 am

Still keen to learn what options exist to roll an "activity" LED off the GPIO.

/bump :)

Twana94
Posts: 5
Joined: Mon Oct 21, 2013 11:06 am

Re: Custom Case indicator LEDs

Mon Oct 21, 2013 11:17 am

I also have a project like this where I want to program one of the GPIOs to send the same signals, that GPIO16 send to the ACT LED, to a LED on my chassis. Have been searching the internet several days now but haven't found anything useful. I have a simple code that makes a LED blink but thats not really what I want. The link to the other forum thread didn't help much since I'm not looking for the ability to change the ACT LED, just add another LED.

texy
Forum Moderator
Forum Moderator
Posts: 5174
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Custom Case indicator LEDs

Mon Oct 21, 2013 11:30 am

Hi,
maybe, just maybe, you can treat GPIO16 as an input and read from it, then send it to the GPIO you are using for your chassis LED?
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
ragnarjensen
Posts: 332
Joined: Wed May 15, 2013 6:13 pm
Location: Stockholm, Sweden

Re: Custom Case indicator LEDs

Wed Jan 22, 2014 9:26 pm

A bit of necro-posting, I know. But anyway...
For many years, I have now and then used a program that uses the Scroll Lock LED on the keyboard as a HDD activity light.
http://members.optusnet.com.au/foonly/w ... e/hddled.c

I have butchered it for use on the RPi to use an LED connected to a GPIO pin instead.

EDIT 2016-10-18: Code now on Github https://github.com/RagnarJensen/PiLEDlights

--
Ragnar
Last edited by ragnarjensen on Tue Oct 18, 2016 7:22 pm, edited 5 times in total.

User avatar
ragnarjensen
Posts: 332
Joined: Wed May 15, 2013 6:13 pm
Location: Stockholm, Sweden

Re: Custom Case indicator LEDs

Mon Feb 03, 2014 9:26 pm

A variant of the above, that blinks an LED on network activity.


EDIT 2016-10-18: Code now on Github https://github.com/RagnarJensen/PiLEDlights

--
Ragnar
Last edited by ragnarjensen on Tue Oct 18, 2016 7:22 pm, edited 3 times in total.

boyoh
Posts: 1717
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Custom Case indicator LEDs

Mon Feb 03, 2014 11:31 pm

Some times with a little bit of thought
Some difficult problems can be over come
Is it possible to fit a small Mirror in the case
To reflect the LED's to a little slot in the case

Nothing Ventured Nothing Gained
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

User avatar
AndrewS
Posts: 3641
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK

Re: Custom Case indicator LEDs

Mon Jun 30, 2014 7:42 pm


djdtime
Posts: 5
Joined: Wed Oct 29, 2014 3:19 am

Re: Custom Case indicator LEDs

Sat Nov 01, 2014 11:28 pm

I had been trying both programs, the network LED stays on without flicking, in contrast with the one in the Raspberry, which flicks continuously. When the activity stops, then the LED goes off, now the activity LED barely flicks comparing to the one on the board, any ideas or recommendations?

Leonuk
Posts: 1
Joined: Thu Dec 18, 2014 11:35 pm

Re: Custom Case indicator LEDs

Thu Dec 18, 2014 11:38 pm

Thanks ragnarjensen both work nice for me :D

ragnarjensen wrote:A bit of necro-posting, I know. But anyway...
For many years, I have now and then used a program that uses the Scroll Lock LED on the keyboard as a HDD activity light.
http://members.optusnet.com.au/foonly/w ... e/hddled.c

I have butchered it for use on the RPi to use an LED connected to a GPIO pin instead.

Code: Select all

/*
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * For more information, please refer to <http://unlicense.org/>
 *
 **************************************************************************
 *
 * A disk activity light for the Raspberry Pi, using an LED connected to a GPIO pin.
 * Based on hddled.c - http://members.optusnet.com.au/foonly/whirlpool/code/hddled.c -
 * This program uses the WiringPi library by Gordon Henderson - http://wiringpi.com/ - Thanks, Gordon!
 * 
 *
 * To compile:
 *   gcc -Wall -O3 -o hddledPi hddledPi.c -lwiringPi
 *
 * Options:
 * -d, --detach               Detach from terminal (become a daemon)
 * -p, --pin=VALUE            GPIO pin (using wiringPi numbering scheme) where LED is connected (default: 10) 
 * -r, --refresh=VALUE        Refresh interval (default: 20 ms)
 *
 * Default LED Pin - wiringPi pin 10 is BCM_GPIO 8, physical pin 24 on the Pi's P1 header.
 * Note: This pin is also used for the SPI interface. If you have SPI add-ons connected,
 * you'll have to use the -p option to change it to another, unused pin.
 *
 * GPIO pin ----|>|----[330]----+
 *              LED             |
 *                             ===
 *                            Ground
 */


#define VMSTAT "/proc/vmstat"


#define _GNU_SOURCE

#include <argp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <wiringPi.h>


static unsigned int o_refresh = 20; /* milliseconds */
static unsigned int o_gpiopin = 10; /* wiringPi numbering scheme */
static int o_detach = 0;

static volatile sig_atomic_t running = 1;
static char *line = NULL;
static size_t len = 0;

/* Reread the vmstat file */
int activity(FILE *vmstat) {
        static unsigned int prev_pgpgin, prev_pgpgout;
        unsigned int pgpgin, pgpgout;
        int found_pgpgin, found_pgpgout;
        int result;

        /* Reload the vmstat file */
        result = TEMP_FAILURE_RETRY(fseek(vmstat, 0L, SEEK_SET));
        if (result) {
                perror("Could not rewind " VMSTAT);
                return result;
        }

        /* Clear glibc's buffer */
        result = TEMP_FAILURE_RETRY(fflush(vmstat));
        if (result) {
                perror("Could not flush input stream");
                return result;
        }

        /* Extract the I/O stats */
        found_pgpgin = found_pgpgout = 0;
        while (getline(&line, &len, vmstat) != -1 && errno != EINTR) {
                if (sscanf(line, "pgpgin %u", &pgpgin))
                        found_pgpgin++;
                else if (sscanf(line, "pgpgout %u", &pgpgout))
                        found_pgpgout++;
                if (found_pgpgin && found_pgpgout)
                        break;
        }
        if (!found_pgpgin || !found_pgpgout) {
                fprintf(stderr, "Could not find required lines in " VMSTAT);
                return -1;
        }

        /* Anything changed? */
        result =
                (prev_pgpgin  != pgpgin) ||
                (prev_pgpgout != pgpgout);
        prev_pgpgin = pgpgin;
        prev_pgpgout = pgpgout;

        return result;
}

/* Update the LED */
void led(int on) {
        static int current = 1; /* Ensure the LED turns off on first call */
        if (current == on)
                return;

                if (on) {
                        digitalWrite (o_gpiopin, HIGH);
                } else {
                        digitalWrite (o_gpiopin, LOW);
                }

        current = on;
}

/* Signal handler -- break out of the main loop */
void shutdown(int sig) {
        running = 0;
}

/* Argp parser function */
error_t parse_options(int key, char *arg, struct argp_state *state) {
        switch (key) {
        case 'd':
                o_detach = 1;
                break;
        case 'r':
                o_refresh = strtol(arg, NULL, 10);
                if (o_refresh < 10)
                        argp_failure(state, EXIT_FAILURE, 0,
                                "refresh interval must be at least 10");
                break;
        case 'p':
                o_gpiopin = strtol(arg, NULL, 10);
                if ((o_gpiopin < 0) || (o_gpiopin > 20))
                        argp_failure(state, EXIT_FAILURE, 0,
                                "pin number must be between 0 and 20");
                break;
        }
        return 0;
}

int main(int argc, char **argv) {
        struct argp_option options[] = {
                { "detach",  'd',      NULL, 0, "Detach from terminal" },
                { "pin",     'p',   "VALUE", 0, "GPIO pin where LED is connected (default: wiringPi pin 10, physical pin 24 on the P1 header)" },
                { "refresh", 'r',   "VALUE", 0, "Refresh interval (default: 20 ms)" },
                { 0 },
        };
        struct argp parser = {
                NULL, parse_options, NULL,
                "Show disk activity using an LED wired to a GPIO pin.",
                NULL, NULL, NULL
        };
        int status = EXIT_FAILURE;
        FILE *vmstat = NULL;
        struct timespec delay;

        /* Parse the command-line */
        parser.options = options;
        if (argp_parse(&parser, argc, argv, ARGP_NO_ARGS, NULL, NULL))
                goto out;

        delay.tv_sec = o_refresh / 1000;
        delay.tv_nsec = 1000000 * (o_refresh % 1000);

        wiringPiSetup () ;
        pinMode (o_gpiopin, OUTPUT) ;


        /* Open the vmstat file */
        vmstat = fopen(VMSTAT, "r");
        if (!vmstat) {
                perror("Could not open " VMSTAT " for reading");
                goto out;
        }

        /* Ensure the LED is off */
        led(LOW);

        /* Save the current I/O stat values */
        if (activity(vmstat) < 0)
                goto out;

        /* Detach from terminal? */
        if (o_detach) {
                pid_t child = fork();
                if (child < 0) {
                        perror("Could not detach from terminal");
                        goto out;
                }
                if (child) {
                        /* I am the parent */
                        status = EXIT_SUCCESS;
                        goto out;
                }
        }

        /* We catch these signals so we can clean up */
        {
                struct sigaction action;
                memset(&action, 0, sizeof(action));
                action.sa_handler = shutdown;
                sigemptyset(&action.sa_mask);
                action.sa_flags = 0; /* We block on usleep; don't use SA_RESTART */
                sigaction(SIGHUP, &action, NULL);
                sigaction(SIGINT, &action, NULL);
                sigaction(SIGTERM, &action, NULL);
        }

        /* Loop until signal received */
        while (running) {
                int a;
                if (nanosleep(&delay, NULL) < 0)
                        break;
                a = activity(vmstat);
                if (a < 0)
                        break;
                led(a);
        }

        /* Ensure the LED is off */
        led(LOW);

        status = EXIT_SUCCESS;

out:
        if (line) free(line);
        if (vmstat) fclose(vmstat);
        return status;
}
You'll need the WiringPi library in order to build the program.
Save the code above as hddledPi.c and build with
gcc -Wall -O3 -o hddledPi hddledPi.c -lwiringPi

I have only tested it on Raspbian, where it works fine.

EDIT 2014-05-14: updated code.
--
Ragnar

rern
Posts: 2
Joined: Tue Sep 06, 2016 6:14 am

Re: Custom Case indicator LEDs

Wed Sep 07, 2016 1:30 am

@ragnarjensen's actledPi in this post, Can we control the on-board leds, is even better.

Return to “General programming discussion”