chiark / gitweb /
Build system: Add missing set -e
[wiringPi.git] / examples / wfi.c
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
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <wiringPi.h>
38
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.
42
43 #define COUNT_KEY       0
44
45 // What BCM_GPIO input are we using?
46
47 #define BUTTON_PIN      17
48
49 // Debounce time in mS
50
51 #define DEBOUNCE_TIME   100
52
53
54 // globalCounter:
55 //      Global variable to count interrupts
56 //      Should be declared volatile to make sure the compiler doesn't cache it.
57
58 static volatile int globalCounter = 0 ;
59
60
61 /*
62  * waitForIt:
63  *      This is a thread created using the wiringPi simplified threading
64  *      mechanism. It will wait on an interrupt on the button and increment
65  *      a counter.
66  *********************************************************************************
67  */
68
69 PI_THREAD (waitForIt)
70 {
71   int state = 0 ;
72   int debounceTime = 0 ;
73
74   (void)piHiPri (10) ;  // Set this thread to be high priority
75
76   for (;;)
77   {
78     if (waitForInterrupt (BUTTON_PIN, -1) > 0)  // Got it
79     {
80 // Bouncing?
81
82       if (millis () < debounceTime)
83       {
84         debounceTime = millis () + DEBOUNCE_TIME ;
85         continue ;
86       }
87
88 // We have a valid one
89
90       state ^= 1 ;
91
92       piLock (COUNT_KEY) ;
93         ++globalCounter ;
94       piUnlock (COUNT_KEY) ;
95
96 // Wait for key to be released
97
98       while (digitalRead (BUTTON_PIN) == LOW)
99         delay (1) ;
100
101       debounceTime = millis () + DEBOUNCE_TIME ;
102     }
103   }
104 }
105
106
107 /*
108  * setup:
109  *      Demo a crude but effective way to initialise the hardware
110  *********************************************************************************
111  */
112
113 void setup (void)
114 {
115
116 // Use the gpio program to initialise the hardware
117 //      (This is the crude, but effective)
118
119   system ("gpio edge 17 falling") ;
120
121 // Setup wiringPi
122
123   wiringPiSetupSys () ;
124
125 // Fire off our interrupt handler
126
127   piThreadCreate (waitForIt) ;
128
129 }
130
131
132 /*
133  * main
134  *********************************************************************************
135  */
136
137 int main (void)
138 {
139   int lastCounter = 0 ;
140   int myCounter   = 0 ;
141
142   setup () ;
143
144   for (;;)
145   {
146     printf ("Waiting ... ") ; fflush (stdout) ;
147
148     while (myCounter == lastCounter)
149     {
150       piLock (COUNT_KEY) ;
151         myCounter = globalCounter ;
152       piUnlock (COUNT_KEY) ;
153       delay (500) ;
154     }
155
156     printf (" Done. myCounter: %5d\n", myCounter) ;
157     lastCounter = myCounter ;
158   }
159
160   return 0 ;
161 }