Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

133 linhas
4.0 KiB

  1. /*
  2. * buttons2.c:
  3. * Button polling, with debounce.
  4. * Note: This tries to handle all buttons simultaneously.
  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. int main (void)
  36. {
  37. int i;
  38. // Set the handler for SIGTERM (15)
  39. signal(SIGTERM, Signal_handler);
  40. signal(SIGHUP, Signal_handler);
  41. signal(SIGINT, Signal_handler);
  42. signal(SIGQUIT, Signal_handler);
  43. signal(SIGTRAP, Signal_handler);
  44. signal(SIGABRT, Signal_handler);
  45. signal(SIGALRM, Signal_handler);
  46. signal(SIGUSR1, Signal_handler);
  47. signal(SIGUSR2, Signal_handler);
  48. printf ("Raspberry Pi Button Test\n");
  49. wiringPiSetupGpio ();
  50. // Setup the inputs
  51. for (i = 0; i < nButtons; ++i)
  52. {
  53. pinMode (buttons[i], INPUT);
  54. pullUpDnControl (buttons[i], PUD_UP);
  55. }
  56. int buttonCounts[] = {0, 0, 0, 0};
  57. int buttonVal[] = {0, 0, 0, 0};
  58. const int threshold = 10;
  59. while (!terminate_process)
  60. {
  61. for (i = 0; i < nButtons; ++i)
  62. {
  63. // If released, look for press
  64. if (buttonVal[i] == 0)
  65. {
  66. if (digitalRead (buttons[i]) == LOW) // Low is pushed
  67. {
  68. if (buttonCounts[i] < threshold)
  69. {
  70. buttonCounts[i]++;
  71. }
  72. else
  73. {
  74. buttonVal[i] = 1; // declare pushed
  75. printf ("Button %d pressed\n", buttons[i]);
  76. }
  77. }
  78. }
  79. delayMs(1);
  80. // If pushed, look for release
  81. if (buttonVal[i] == 1)
  82. {
  83. if (digitalRead (buttons[i]) == HIGH)
  84. {
  85. if (buttonCounts[i] > 0)
  86. {
  87. buttonCounts[i]--;
  88. }
  89. else
  90. {
  91. buttonVal[i] = 0; // declare released
  92. printf ("Button %d released\n", buttons[i]);
  93. }
  94. }
  95. }
  96. }
  97. }
  98. return EXIT_SUCCESS;
  99. }
  100. //**********************************************************************************************************************
  101. /**
  102. * Intercepts and handles signals from QNX
  103. * This function is called when the SIGTERM signal is raised by QNX
  104. */
  105. void Signal_handler(int sig)
  106. {
  107. printf("Received signal %d\n", sig);
  108. // Signal process to exit.
  109. terminate_process = 1;
  110. }
  111. //**********************************************************************************************************************