您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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://projects.drogon.net/raspberry-pi/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 <wiringPi.h>
  37. // A 'key' which we can lock and unlock - values are 0 through 3
  38. // This is interpreted internally as a pthread_mutex by wiringPi
  39. // which is hiding some of that to make life simple.
  40. #define COUNT_KEY 0
  41. // What BCM_GPIO input are we using?
  42. #define BUTTON_PIN 17
  43. // Debounce time in mS
  44. #define DEBOUNCE_TIME 100
  45. // globalCounter:
  46. // Global variable to count interrupts
  47. // Should be declared volatile to make sure the compiler doesn't cache it.
  48. static volatile int globalCounter = 0 ;
  49. /*
  50. * waitForIt:
  51. * This is a thread created using the wiringPi simplified threading
  52. * mechanism. It will wait on an interrupt on the button and increment
  53. * a counter.
  54. *********************************************************************************
  55. */
  56. PI_THREAD (waitForIt)
  57. {
  58. int state = 0 ;
  59. int debounceTime = 0 ;
  60. (void)piHiPri (10) ; // Set this thread to be high priority
  61. for (;;)
  62. {
  63. if (waitForInterrupt (BUTTON_PIN, -1) > 0) // Got it
  64. {
  65. // Bouncing?
  66. if (millis () < debounceTime)
  67. {
  68. debounceTime = millis () + DEBOUNCE_TIME ;
  69. continue ;
  70. }
  71. // We have a valid one
  72. state ^= 1 ;
  73. piLock (COUNT_KEY) ;
  74. ++globalCounter ;
  75. piUnlock (COUNT_KEY) ;
  76. // Wait for key to be released
  77. while (digitalRead (BUTTON_PIN) == LOW)
  78. delay (1) ;
  79. debounceTime = millis () + DEBOUNCE_TIME ;
  80. }
  81. }
  82. }
  83. /*
  84. * setup:
  85. * Demo a crude but effective way to initialise the hardware
  86. *********************************************************************************
  87. */
  88. void setup (void)
  89. {
  90. // Use the gpio program to initialise the hardware
  91. // (This is the crude, but effective)
  92. system ("gpio edge 17 falling") ;
  93. // Setup wiringPi
  94. wiringPiSetupSys () ;
  95. // Fire off our interrupt handler
  96. piThreadCreate (waitForIt) ;
  97. }
  98. /*
  99. * main
  100. *********************************************************************************
  101. */
  102. int main (void)
  103. {
  104. int lastCounter = 0 ;
  105. int myCounter = 0 ;
  106. setup () ;
  107. for (;;)
  108. {
  109. printf ("Waiting ... ") ; fflush (stdout) ;
  110. while (myCounter == lastCounter)
  111. {
  112. piLock (COUNT_KEY) ;
  113. myCounter = globalCounter ;
  114. piUnlock (COUNT_KEY) ;
  115. delay (500) ;
  116. }
  117. printf (" Done. myCounter: %5d\n", myCounter) ;
  118. lastCounter = myCounter ;
  119. }
  120. return 0 ;
  121. }