Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

119 řádky
3.4 KiB

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