3 * Wait for Interrupt test program
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)
9 * The biggest issue with this method is that it really only works
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.
15 * Copyright (c) 2012-2013 Gordon Henderson.
16 ***********************************************************************
17 * This file is part of wiringPi:
18 * https://projects.drogon.net/raspberry-pi/wiringpi/
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.
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.
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 ***********************************************************************
39 // A 'key' which we can lock and unlock - values are 0 through 3
40 // This is interpreted internally as a pthread_mutex by wiringPi
41 // which is hiding some of that to make life simple.
45 // What BCM_GPIO input are we using?
49 // Debounce time in mS
51 #define DEBOUNCE_TIME 100
55 // Global variable to count interrupts
56 // Should be declared volatile to make sure the compiler doesn't cache it.
58 static volatile int globalCounter = 0 ;
63 * This is a thread created using the wiringPi simplified threading
64 * mechanism. It will wait on an interrupt on the button and increment
66 *********************************************************************************
72 int debounceTime = 0 ;
74 (void)piHiPri (10) ; // Set this thread to be high priority
78 if (waitForInterrupt (BUTTON_PIN, -1) > 0) // Got it
82 if (millis () < debounceTime)
84 debounceTime = millis () + DEBOUNCE_TIME ;
88 // We have a valid one
94 piUnlock (COUNT_KEY) ;
96 // Wait for key to be released
98 while (digitalRead (BUTTON_PIN) == LOW)
101 debounceTime = millis () + DEBOUNCE_TIME ;
109 * Demo a crude but effective way to initialise the hardware
110 *********************************************************************************
116 // Use the gpio program to initialise the hardware
117 // (This is the crude, but effective)
119 system ("gpio edge 17 falling") ;
123 wiringPiSetupSys () ;
125 // Fire off our interrupt handler
127 piThreadCreate (waitForIt) ;
134 *********************************************************************************
139 int lastCounter = 0 ;
146 printf ("Waiting ... ") ; fflush (stdout) ;
148 while (myCounter == lastCounter)
151 myCounter = globalCounter ;
152 piUnlock (COUNT_KEY) ;
156 printf (" Done. myCounter: %5d\n", myCounter) ;
157 lastCounter = myCounter ;