Gordon Drogon | d24a8fc | 2012-08-17 10:16:31 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * wfi.c: |
| 3 | * Wait for Interrupt test program |
| 4 | * |
| 5 | * Copyright (c) 2012 Gordon Henderson. |
| 6 | *********************************************************************** |
| 7 | * This file is part of wiringPi: |
| 8 | * https://projects.drogon.net/raspberry-pi/wiringpi/ |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 19 | * |
| 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 | *********************************************************************** |
| 23 | */ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <wiringPi.h> |
| 28 | |
| 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. |
| 32 | |
| 33 | #define COUNT_KEY 0 |
| 34 | |
| 35 | // What BCM_GPIO input are we using? |
| 36 | // GPIO 0 is one of the I2C pins with an on-board pull-up |
| 37 | |
| 38 | #define BUTTON_PIN 0 |
| 39 | |
| 40 | // Debounce time in mS |
| 41 | |
| 42 | #define DEBOUNCE_TIME 100 |
| 43 | |
| 44 | |
| 45 | // globalCounter: |
| 46 | // Global variable to count interrupts |
| 47 | // Should be declared volatile to make sure the compiler doesn't cache it. |
| 48 | |
| 49 | static volatile int globalCounter = 0 ; |
| 50 | |
| 51 | |
| 52 | /* |
| 53 | * waitForIt: |
| 54 | * This is a thread created using the wiringPi simplified threading |
| 55 | * mechanism. It will wait on an interrupt on the button and increment |
| 56 | * a counter. |
| 57 | ********************************************************************************* |
| 58 | */ |
| 59 | |
| 60 | PI_THREAD (waitForIt) |
| 61 | { |
| 62 | int state = 0 ; |
| 63 | int debounceTime = 0 ; |
| 64 | |
| 65 | (void)piHiPri (10) ; // Set this thread to be high priority |
| 66 | digitalWrite (18, 1) ; |
| 67 | |
| 68 | for (;;) |
| 69 | { |
| 70 | if (waitForInterrupt (BUTTON_PIN, -1) > 0) // Got it |
| 71 | { |
| 72 | |
| 73 | // Bouncing? |
| 74 | |
| 75 | if (millis () < debounceTime) |
| 76 | { |
| 77 | debounceTime = millis () + DEBOUNCE_TIME ; |
| 78 | continue ; |
| 79 | } |
| 80 | |
| 81 | // We have a valid one |
| 82 | |
| 83 | digitalWrite (17, state) ; |
| 84 | state ^= 1 ; |
| 85 | |
| 86 | piLock (COUNT_KEY) ; |
| 87 | ++globalCounter ; |
| 88 | piUnlock (COUNT_KEY) ; |
| 89 | |
| 90 | // Wait for key to be released |
| 91 | |
| 92 | while (digitalRead (0) == LOW) |
| 93 | delay (1) ; |
| 94 | |
| 95 | debounceTime = millis () + DEBOUNCE_TIME ; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /* |
| 102 | * setup: |
| 103 | * Demo a crude but effective way to initialise the hardware |
| 104 | ********************************************************************************* |
| 105 | */ |
| 106 | |
| 107 | void setup (void) |
| 108 | { |
| 109 | |
| 110 | // Use the gpio program to initialise the hardware |
| 111 | // (This is the crude, but effective bit) |
| 112 | |
| 113 | system ("gpio edge 0 falling") ; |
| 114 | system ("gpio export 17 out") ; |
| 115 | system ("gpio export 18 out") ; |
| 116 | |
| 117 | // Setup wiringPi |
| 118 | |
| 119 | wiringPiSetupSys () ; |
| 120 | |
| 121 | // Fire off our interrupt handler |
| 122 | |
| 123 | piThreadCreate (waitForIt) ; |
| 124 | |
| 125 | digitalWrite (17, 0) ; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /* |
| 130 | * main |
| 131 | ********************************************************************************* |
| 132 | */ |
| 133 | |
| 134 | int main (void) |
| 135 | { |
| 136 | int lastCounter = 0 ; |
| 137 | int myCounter = 0 ; |
| 138 | |
| 139 | setup () ; |
| 140 | |
| 141 | for (;;) |
| 142 | { |
| 143 | printf ("Waiting ... ") ; fflush (stdout) ; |
| 144 | |
| 145 | while (myCounter == lastCounter) |
| 146 | { |
| 147 | piLock (COUNT_KEY) ; |
| 148 | myCounter = globalCounter ; |
| 149 | piUnlock (COUNT_KEY) ; |
| 150 | delay (5000) ; |
| 151 | } |
| 152 | |
| 153 | printf (" Done. myCounter: %5d\n", myCounter) ; |
| 154 | lastCounter = myCounter ; |
| 155 | } |
| 156 | |
| 157 | return 0 ; |
| 158 | } |