chiark / gitweb /
Debian build: Do not install COPYING.LESSER
[wiringPi.git] / examples / isr.c
1 /*
2  * isr.c:
3  *      Wait for Interrupt test program - ISR method
4  *
5  *      How to test:
6  *        Use the SoC's pull-up and pull down resistors that are avalable
7  *      on input pins. So compile & run this program (via sudo), then
8  *      in another terminal:
9  *              gpio mode 0 up
10  *              gpio mode 0 down
11  *      at which point it should trigger an interrupt. Toggle the pin
12  *      up/down to generate more interrupts to test.
13  *
14  * Copyright (c) 2013 Gordon Henderson.
15  ***********************************************************************
16  * This file is part of wiringPi:
17  *      https://projects.drogon.net/raspberry-pi/wiringpi/
18  *
19  *    wiringPi is free software: you can redistribute it and/or modify
20  *    it under the terms of the GNU Lesser General Public License as published by
21  *    the Free Software Foundation, either version 3 of the License, or
22  *    (at your option) any later version.
23  *
24  *    wiringPi is distributed in the hope that it will be useful,
25  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *    GNU Lesser General Public License for more details.
28  *
29  *    You should have received a copy of the GNU Lesser General Public License
30  *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
31  ***********************************************************************
32  */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <wiringPi.h>
39
40
41 // globalCounter:
42 //      Global variable to count interrupts
43 //      Should be declared volatile to make sure the compiler doesn't cache it.
44
45 static volatile int globalCounter [8] ;
46
47
48 /*
49  * myInterrupt:
50  *********************************************************************************
51  */
52
53 void myInterrupt0 (void) { ++globalCounter [0] ; }
54 void myInterrupt1 (void) { ++globalCounter [1] ; }
55 void myInterrupt2 (void) { ++globalCounter [2] ; }
56 void myInterrupt3 (void) { ++globalCounter [3] ; }
57 void myInterrupt4 (void) { ++globalCounter [4] ; }
58 void myInterrupt5 (void) { ++globalCounter [5] ; }
59 void myInterrupt6 (void) { ++globalCounter [6] ; }
60 void myInterrupt7 (void) { ++globalCounter [7] ; }
61
62
63 /*
64  *********************************************************************************
65  * main
66  *********************************************************************************
67  */
68
69 int main (void)
70 {
71   int gotOne, pin ;
72   int myCounter [8] ;
73
74   for (pin = 0 ; pin < 8 ; ++pin) 
75     globalCounter [pin] = myCounter [pin] = 0 ;
76
77   wiringPiSetup () ;
78
79   wiringPiISR (0, INT_EDGE_FALLING, &myInterrupt0) ;
80   wiringPiISR (1, INT_EDGE_FALLING, &myInterrupt1) ;
81   wiringPiISR (2, INT_EDGE_FALLING, &myInterrupt2) ;
82   wiringPiISR (3, INT_EDGE_FALLING, &myInterrupt3) ;
83   wiringPiISR (4, INT_EDGE_FALLING, &myInterrupt4) ;
84   wiringPiISR (5, INT_EDGE_FALLING, &myInterrupt5) ;
85   wiringPiISR (6, INT_EDGE_FALLING, &myInterrupt6) ;
86   wiringPiISR (7, INT_EDGE_FALLING, &myInterrupt7) ;
87
88   for (;;)
89   {
90     gotOne = 0 ;
91     printf ("Waiting ... ") ; fflush (stdout) ;
92
93     for (;;)
94     {
95       for (pin = 0 ; pin < 8 ; ++pin)
96       {
97         if (globalCounter [pin] != myCounter [pin])
98         {
99           printf (" Int on pin %d: Counter: %5d\n", pin, globalCounter [pin]) ;
100           myCounter [pin] = globalCounter [pin] ;
101           ++gotOne ;
102         }
103       }
104       if (gotOne != 0)
105         break ;
106     }
107   }
108
109   return 0 ;
110 }