[TriEmbed] catching SIGINT/SIGHUP

Alex Davis alexd at matrixwide.com
Mon Sep 28 10:55:59 CDT 2015


Anyone have experience with unix c/c++ coding and catching interrupt
signals in the code?

In my instance, I am using the rpi-rgb-led-matrix (led-matrix.h) library
on a Pi2. You don't want to just exit a program using it, as you
'freeze' the matrix and it ends up at the last line scanned looking very
bright in a way which must be overdriving the LEDs.

I'm using LIRC to fire off different matrix utilities and sometimes I've
got to to kill one before running another.

I'm no unix code expert, but I found an example using <signal.h> which
at least works. Here's a simple example:

#include <signal.h>

volatile bool interrupt_received = false;
static void InterruptHandler(int signo) {
    interrupt_received = true;
}

int main(int argc, char *argv[]) {

    signal(SIGTERM, InterruptHandler);
    signal(SIGINT, InterruptHandler);

    while (!interrupt_received)
    {
        // do your rgb_matrix::DrawText(canvas, font, x, y +
        font.baseline(), color, line) here
        // or whatever
    }
    fprintf(stderr, "Caught signal. Exiting.\n");

    // shut down the matrix
    // or whatever your cleanup tasks need be
    matrix->Clear();
    delete matrix   

    exit(0);
    return 0;
}

Is there a better way to do this? I think this is the easiest/laziest
but I'm open to doing it better.

-- 
"The theater of noise is proof of our potential."
|\ |  (¯  \/ |¯\  |V| |\ ¯|¯ |¯) | \/ | | | |¯\ (¯   /¯  /\ |V|
|-||_ (_  /\ |_/ @| | |-| |  | \ | /\ |^| | |_/ (_ . \_  \/ | |

You won't find me on Facebook.




More information about the TriEmbed mailing list