3 * Wait for Interrupt test program
5 * Copyright (c) 2012 Gordon Henderson.
6 ***********************************************************************
7 * This file is part of wiringPi:
8 * https://projects.drogon.net/raspberry-pi/wiringpi/
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.
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.
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 ***********************************************************************
29 // A 'key' which we can lock and unlock - values are 0 through 3
30 // This is interpreted internally as a pthread_mutex by wiringPi
31 // which is hiding some of that to make life simple.
35 // What BCM_GPIO input are we using?
36 // GPIO 0 is one of the I2C pins with an on-board pull-up
40 // Debounce time in mS
42 #define DEBOUNCE_TIME 100
46 // Global variable to count interrupts
47 // Should be declared volatile to make sure the compiler doesn't cache it.
49 static volatile int globalCounter = 0 ;
54 * This is a thread created using the wiringPi simplified threading
55 * mechanism. It will wait on an interrupt on the button and increment
57 *********************************************************************************
63 int debounceTime = 0 ;
65 (void)piHiPri (10) ; // Set this thread to be high priority
66 digitalWrite (18, 1) ;
70 if (waitForInterrupt (BUTTON_PIN, -1) > 0) // Got it
75 if (millis () < debounceTime)
77 debounceTime = millis () + DEBOUNCE_TIME ;
81 // We have a valid one
83 digitalWrite (17, state) ;
88 piUnlock (COUNT_KEY) ;
90 // Wait for key to be released
92 while (digitalRead (0) == LOW)
95 debounceTime = millis () + DEBOUNCE_TIME ;
103 * Demo a crude but effective way to initialise the hardware
104 *********************************************************************************
110 // Use the gpio program to initialise the hardware
111 // (This is the crude, but effective bit)
113 system ("gpio edge 0 falling") ;
114 system ("gpio export 17 out") ;
115 system ("gpio export 18 out") ;
119 wiringPiSetupSys () ;
121 // Fire off our interrupt handler
123 piThreadCreate (waitForIt) ;
125 digitalWrite (17, 0) ;
131 *********************************************************************************
136 int lastCounter = 0 ;
143 printf ("Waiting ... ") ; fflush (stdout) ;
145 while (myCounter == lastCounter)
148 myCounter = globalCounter ;
149 piUnlock (COUNT_KEY) ;
153 printf (" Done. myCounter: %5d\n", myCounter) ;
154 lastCounter = myCounter ;