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.
 
 
 
 
 

192 linhas
4.8 KiB

  1. /*
  2. * wfi.c:
  3. * Wait for Interrupt test program
  4. *
  5. * This program demonstrates the use of the waitForInterrupt()
  6. * function in wiringPi. It listens to a button input on
  7. * BCM_GPIO pin 17 (wiringPi pin 0)
  8. *
  9. * The biggest issue with this method is that it really only works
  10. * well in Sys mode.
  11. *
  12. * Jan 2013: This way of doing things is sort of deprecated now, see
  13. * the wiringPiISR() function instead and the isr.c test program here.
  14. *
  15. * Copyright (c) 2012-2013 Gordon Henderson.
  16. ***********************************************************************
  17. * This file is part of wiringPi:
  18. * https://github.com/WiringPi/WiringPi/
  19. *
  20. * wiringPi is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Lesser General Public License as published by
  22. * the Free Software Foundation, either version 3 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * wiringPi is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Lesser General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Lesser General Public License
  31. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  32. ***********************************************************************
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <signal.h>
  37. #include <pthread.h>
  38. #include <wiringPi.h>
  39. static int terminate_process = 0;
  40. static void Signal_handler(int sig);
  41. // A 'key' which we can lock and unlock - values are 0 through 3
  42. // This is interpreted internally as a pthread_mutex by wiringPi
  43. // which is hiding some of that to make life simple.
  44. #define COUNT_KEY 0
  45. // What BCM_GPIO input are we using?
  46. #define BUTTON_PIN 26
  47. // Debounce time in mS
  48. #define DEBOUNCE_TIME 100
  49. // globalCounter:
  50. // Global variable to count interrupts
  51. // Should be declared volatile to make sure the compiler doesn't cache it.
  52. static volatile int globalCounter = 0;
  53. /*
  54. * waitForIt:
  55. * This is a thread created using the wiringPi simplified threading
  56. * mechanism. It will wait on an interrupt on the button and increment
  57. * a counter.
  58. *********************************************************************************
  59. */
  60. PI_THREAD (waitForIt)
  61. {
  62. int state = 0;
  63. int debounceTime = 0;
  64. (void)piHiPri (10); // Set this thread to be high priority
  65. (void)digitalRead (BUTTON_PIN); // pre-read pin to avoid false trigger
  66. while (!terminate_process)
  67. {
  68. if (waitForInterrupt (BUTTON_PIN, -1) > 0) // Got it
  69. {
  70. // Bouncing?
  71. if (millis() < debounceTime)
  72. {
  73. debounceTime = millis() + DEBOUNCE_TIME;
  74. continue;
  75. }
  76. // We have a valid one
  77. state ^= 1;
  78. piLock (COUNT_KEY);
  79. ++globalCounter;
  80. piUnlock (COUNT_KEY);
  81. // Wait for key to be released
  82. while (digitalRead (BUTTON_PIN) == LOW)
  83. {
  84. delayMs (10);
  85. }
  86. debounceTime = millis() + DEBOUNCE_TIME;
  87. }
  88. }
  89. return (void *) NULL;
  90. }
  91. /*
  92. * main
  93. *********************************************************************************
  94. */
  95. int main (void)
  96. {
  97. int lastCounter = 0;
  98. int myCounter = 0;
  99. // ----------------------------------
  100. // Demo a crude but effective way to initialise the hardware
  101. // Use the gpio program to initialise the hardware
  102. // (This is the crude, but effective)
  103. system ("gpio edge 26 falling");
  104. // Setup wiringPi
  105. wiringPiSetupSys();
  106. // Fire off our interrupt handler
  107. int tid = piThreadCreate (waitForIt);
  108. // ----------------------------------
  109. // Set the handler for SIGTERM (15)
  110. signal(SIGTERM, Signal_handler);
  111. signal(SIGINT, Signal_handler);
  112. signal(SIGQUIT, Signal_handler);
  113. signal(SIGTRAP, Signal_handler);
  114. signal(SIGABRT, Signal_handler);
  115. signal(SIGALRM, Signal_handler);
  116. signal(SIGUSR1, Signal_handler);
  117. signal(SIGUSR2, Signal_handler);
  118. while (myCounter < 25)
  119. {
  120. printf ("Waiting ... ");
  121. fflush (stdout);
  122. while (myCounter == lastCounter)
  123. {
  124. piLock (COUNT_KEY);
  125. myCounter = globalCounter;
  126. piUnlock (COUNT_KEY);
  127. delayMs(75);
  128. }
  129. if (terminate_process) break;
  130. printf (" Done. myCounter: %5d\n", myCounter);
  131. lastCounter = myCounter;
  132. }
  133. pthread_join(tid, NULL);
  134. printf ("Done.\n");
  135. return EXIT_SUCCESS;
  136. }
  137. //**********************************************************************************************************************
  138. /**
  139. * Intercepts and handles signals from QNX
  140. * This function is called when the SIGTERM signal is raised by QNX
  141. */
  142. void Signal_handler(int sig)
  143. {
  144. printf("Received signal %d\n", sig);
  145. // Signal process to exit.
  146. terminate_process = 1;
  147. globalCounter++;
  148. }
  149. //**********************************************************************************************************************