blob: 6bb68927edd4503bfd5057b57addfc989f81b49a [file] [log] [blame]
Gordon Drogond24a8fc2012-08-17 10:16:31 +01001/*
2 * wfi.c:
3 * Wait for Interrupt test program
4 *
Gordon Henderson3fbc5642013-03-24 20:04:07 +00005 * 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.
Gordon Drogond24a8fc2012-08-17 10:16:31 +010016 ***********************************************************************
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?
Gordon Drogond24a8fc2012-08-17 10:16:31 +010046
Gordon Henderson3fbc5642013-03-24 20:04:07 +000047#define BUTTON_PIN 17
Gordon Drogond24a8fc2012-08-17 10:16:31 +010048
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
58static 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
69PI_THREAD (waitForIt)
70{
71 int state = 0 ;
72 int debounceTime = 0 ;
73
74 (void)piHiPri (10) ; // Set this thread to be high priority
Gordon Drogond24a8fc2012-08-17 10:16:31 +010075
76 for (;;)
77 {
78 if (waitForInterrupt (BUTTON_PIN, -1) > 0) // Got it
79 {
Gordon Drogond24a8fc2012-08-17 10:16:31 +010080// Bouncing?
81
82 if (millis () < debounceTime)
83 {
84 debounceTime = millis () + DEBOUNCE_TIME ;
85 continue ;
86 }
87
88// We have a valid one
89
Gordon Drogond24a8fc2012-08-17 10:16:31 +010090 state ^= 1 ;
91
92 piLock (COUNT_KEY) ;
93 ++globalCounter ;
94 piUnlock (COUNT_KEY) ;
95
96// Wait for key to be released
97
Gordon Henderson3fbc5642013-03-24 20:04:07 +000098 while (digitalRead (BUTTON_PIN) == LOW)
Gordon Drogond24a8fc2012-08-17 10:16:31 +010099 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
113void setup (void)
114{
115
116// Use the gpio program to initialise the hardware
Gordon Henderson3fbc5642013-03-24 20:04:07 +0000117// (This is the crude, but effective)
Gordon Drogond24a8fc2012-08-17 10:16:31 +0100118
Gordon Henderson3fbc5642013-03-24 20:04:07 +0000119 system ("gpio edge 17 falling") ;
Gordon Drogond24a8fc2012-08-17 10:16:31 +0100120
121// Setup wiringPi
122
123 wiringPiSetupSys () ;
124
125// Fire off our interrupt handler
126
Gordon Henderson3fbc5642013-03-24 20:04:07 +0000127 piThreadCreate (waitForIt) ;
Gordon Drogond24a8fc2012-08-17 10:16:31 +0100128
Gordon Drogond24a8fc2012-08-17 10:16:31 +0100129}
130
131
132/*
133 * main
134 *********************************************************************************
135 */
136
137int 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) ;
Gordon Henderson3fbc5642013-03-24 20:04:07 +0000153 delay (500) ;
Gordon Drogond24a8fc2012-08-17 10:16:31 +0100154 }
155
156 printf (" Done. myCounter: %5d\n", myCounter) ;
157 lastCounter = myCounter ;
158 }
159
160 return 0 ;
161}