Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

126 righe
3.8 KiB

  1. /*
  2. * buttons3.c:
  3. * Multiple button handling using wiringPiISRmulti.
  4. *
  5. ***********************************************************************
  6. * This file is part of wiringPi:
  7. * https://github.com/nuncio-bitis/WiringPi
  8. *
  9. * wiringPi is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published
  11. *by the Free Software Foundation, either version 3 of the License, or (at your
  12. *option) any later version.
  13. *
  14. * wiringPi is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  21. ***********************************************************************
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <signal.h>
  27. #include <errno.h>
  28. #include <wiringPi.h>
  29. //**********************************************************************************************************************
  30. // GPIOs (BCM) that buttons are attached to:
  31. static const int buttons[] = { 26, 16, 20, 21 };
  32. static const int nButtons = (sizeof (buttons) / sizeof (int));
  33. static int terminate_process = 0;
  34. //**********************************************************************************************************************
  35. /**
  36. * Intercepts and handles signals from QNX
  37. * This function is called when the SIGTERM signal is raised by QNX
  38. */
  39. static void Signal_handler(int sig)
  40. {
  41. printf("Received signal %d\n", sig);
  42. // Signal process to exit.
  43. terminate_process = 1;
  44. }
  45. //**********************************************************************************************************************
  46. static int getButtonIndex (int pin)
  47. {
  48. for (int i = 0; i < nButtons; ++i)
  49. {
  50. if (buttons[i] == pin)
  51. {
  52. return i;
  53. }
  54. }
  55. return -1;
  56. }
  57. //**********************************************************************************************************************
  58. static int nInts = 0;
  59. static void Button_Wait (int pin)
  60. {
  61. nInts++;
  62. printf ("%s: Interrupt on pin %d; Button #%d; nInts=%d\n", __FUNCTION__, pin, 1+getButtonIndex(pin), nInts);
  63. }
  64. //**********************************************************************************************************************
  65. int main (int argc, char *argv[])
  66. {
  67. // Set the handler for SIGTERM (15)
  68. signal(SIGTERM, Signal_handler);
  69. signal(SIGHUP, Signal_handler);
  70. signal(SIGINT, Signal_handler);
  71. signal(SIGQUIT, Signal_handler);
  72. signal(SIGTRAP, Signal_handler);
  73. signal(SIGABRT, Signal_handler);
  74. signal(SIGALRM, Signal_handler);
  75. signal(SIGUSR1, Signal_handler);
  76. signal(SIGUSR2, Signal_handler);
  77. printf ("Raspberry Pi Button Test\n");
  78. // ----------------------------------------------------
  79. wiringPiSetupGpio ();
  80. // Setup the inputs
  81. for (int i = 0; i < nButtons; ++i)
  82. {
  83. pinMode (buttons[i], INPUT);
  84. pullUpDnControl (buttons[i], PUD_UP);
  85. }
  86. // ----------------------------------------------------
  87. // Set up a callback for when any of these buttons is pressed/released
  88. // Note: there is no debounce proterction here.
  89. // You may see multiple interrupts with one button.
  90. if (wiringPiISRmulti ((int *)buttons, nButtons, INT_EDGE_RISING, &Button_Wait) < 0)
  91. {
  92. fprintf (stderr, "%s: Unable to setup ISR: %s\n", argv[0], strerror(errno));
  93. return EXIT_FAILURE;
  94. }
  95. nInts = 0;
  96. while (!terminate_process)
  97. {
  98. delayMs(100);
  99. }
  100. return EXIT_SUCCESS;
  101. }
  102. //**********************************************************************************************************************