diff --git a/examples/buttons3.c b/examples/buttons3.c new file mode 100644 index 0000000..a53c083 --- /dev/null +++ b/examples/buttons3.c @@ -0,0 +1,125 @@ +/* + * buttons3.c: + * Multiple button handling using wiringPiISRmulti. + * + *********************************************************************** + * This file is part of wiringPi: + * https://github.com/nuncio-bitis/WiringPi + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + *by the Free Software Foundation, either version 3 of the License, or (at your + *option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include +#include +#include + +//********************************************************************************************************************** + +// GPIOs (BCM) that buttons are attached to: +static const int buttons[] = { 26, 16, 20, 21 }; +static const int nButtons = (sizeof (buttons) / sizeof (int)); + +static int terminate_process = 0; + +//********************************************************************************************************************** + +/** + * Intercepts and handles signals from QNX + * This function is called when the SIGTERM signal is raised by QNX + */ +static void Signal_handler(int sig) +{ + printf("Received signal %d\n", sig); + + // Signal process to exit. + terminate_process = 1; +} + +//********************************************************************************************************************** + +static int getButtonIndex (int pin) +{ + for (int i = 0; i < nButtons; ++i) + { + if (buttons[i] == pin) + { + return i; + } + } + return -1; +} + +//********************************************************************************************************************** + +static int nInts = 0; + +static void Button_Wait (int pin) +{ + nInts++; + printf ("%s: Interrupt on pin %d; Button #%d; nInts=%d\n", __FUNCTION__, pin, 1+getButtonIndex(pin), nInts); +} + +//********************************************************************************************************************** + +int main (int argc, char *argv[]) +{ + // Set the handler for SIGTERM (15) + signal(SIGTERM, Signal_handler); + signal(SIGHUP, Signal_handler); + signal(SIGINT, Signal_handler); + signal(SIGQUIT, Signal_handler); + signal(SIGTRAP, Signal_handler); + signal(SIGABRT, Signal_handler); + signal(SIGALRM, Signal_handler); + signal(SIGUSR1, Signal_handler); + signal(SIGUSR2, Signal_handler); + + printf ("Raspberry Pi Button Test\n"); + + // ---------------------------------------------------- + + wiringPiSetupGpio (); + + // Setup the inputs + for (int i = 0; i < nButtons; ++i) + { + pinMode (buttons[i], INPUT); + pullUpDnControl (buttons[i], PUD_UP); + } + + // ---------------------------------------------------- + + // Set up a callback for when any of these buttons is pressed/released + // Note: there is no debounce proterction here. + // You may see multiple interrupts with one button. + if (wiringPiISRmulti ((int *)buttons, nButtons, INT_EDGE_RISING, &Button_Wait) < 0) + { + fprintf (stderr, "%s: Unable to setup ISR: %s\n", argv[0], strerror(errno)); + return EXIT_FAILURE; + } + + nInts = 0; + while (!terminate_process) + { + delayMs(100); + } + + return EXIT_SUCCESS; +} + +//**********************************************************************************************************************