wfi.c 3.5 KB

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