Test code for Grove Node BLE
Dependencies: BLE_API nRF51822
Fork of BLE_LoopbackUART by
mbed-src/api/InterruptIn.h@9:05f0b5a3a70a, 2014-10-29 (annotated)
- Committer:
- yihui
- Date:
- Wed Oct 29 06:23:47 2014 +0000
- Revision:
- 9:05f0b5a3a70a
initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 9:05f0b5a3a70a | 1 | /* mbed Microcontroller Library |
yihui | 9:05f0b5a3a70a | 2 | * Copyright (c) 2006-2013 ARM Limited |
yihui | 9:05f0b5a3a70a | 3 | * |
yihui | 9:05f0b5a3a70a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 9:05f0b5a3a70a | 5 | * you may not use this file except in compliance with the License. |
yihui | 9:05f0b5a3a70a | 6 | * You may obtain a copy of the License at |
yihui | 9:05f0b5a3a70a | 7 | * |
yihui | 9:05f0b5a3a70a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 9:05f0b5a3a70a | 9 | * |
yihui | 9:05f0b5a3a70a | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 9:05f0b5a3a70a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 9:05f0b5a3a70a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 9:05f0b5a3a70a | 13 | * See the License for the specific language governing permissions and |
yihui | 9:05f0b5a3a70a | 14 | * limitations under the License. |
yihui | 9:05f0b5a3a70a | 15 | */ |
yihui | 9:05f0b5a3a70a | 16 | #ifndef MBED_INTERRUPTIN_H |
yihui | 9:05f0b5a3a70a | 17 | #define MBED_INTERRUPTIN_H |
yihui | 9:05f0b5a3a70a | 18 | |
yihui | 9:05f0b5a3a70a | 19 | #include "platform.h" |
yihui | 9:05f0b5a3a70a | 20 | |
yihui | 9:05f0b5a3a70a | 21 | #if DEVICE_INTERRUPTIN |
yihui | 9:05f0b5a3a70a | 22 | |
yihui | 9:05f0b5a3a70a | 23 | #include "gpio_api.h" |
yihui | 9:05f0b5a3a70a | 24 | #include "gpio_irq_api.h" |
yihui | 9:05f0b5a3a70a | 25 | #include "FunctionPointer.h" |
yihui | 9:05f0b5a3a70a | 26 | |
yihui | 9:05f0b5a3a70a | 27 | namespace mbed { |
yihui | 9:05f0b5a3a70a | 28 | |
yihui | 9:05f0b5a3a70a | 29 | /** A digital interrupt input, used to call a function on a rising or falling edge |
yihui | 9:05f0b5a3a70a | 30 | * |
yihui | 9:05f0b5a3a70a | 31 | * Example: |
yihui | 9:05f0b5a3a70a | 32 | * @code |
yihui | 9:05f0b5a3a70a | 33 | * // Flash an LED while waiting for events |
yihui | 9:05f0b5a3a70a | 34 | * |
yihui | 9:05f0b5a3a70a | 35 | * #include "mbed.h" |
yihui | 9:05f0b5a3a70a | 36 | * |
yihui | 9:05f0b5a3a70a | 37 | * InterruptIn event(p16); |
yihui | 9:05f0b5a3a70a | 38 | * DigitalOut led(LED1); |
yihui | 9:05f0b5a3a70a | 39 | * |
yihui | 9:05f0b5a3a70a | 40 | * void trigger() { |
yihui | 9:05f0b5a3a70a | 41 | * printf("triggered!\n"); |
yihui | 9:05f0b5a3a70a | 42 | * } |
yihui | 9:05f0b5a3a70a | 43 | * |
yihui | 9:05f0b5a3a70a | 44 | * int main() { |
yihui | 9:05f0b5a3a70a | 45 | * event.rise(&trigger); |
yihui | 9:05f0b5a3a70a | 46 | * while(1) { |
yihui | 9:05f0b5a3a70a | 47 | * led = !led; |
yihui | 9:05f0b5a3a70a | 48 | * wait(0.25); |
yihui | 9:05f0b5a3a70a | 49 | * } |
yihui | 9:05f0b5a3a70a | 50 | * } |
yihui | 9:05f0b5a3a70a | 51 | * @endcode |
yihui | 9:05f0b5a3a70a | 52 | */ |
yihui | 9:05f0b5a3a70a | 53 | class InterruptIn { |
yihui | 9:05f0b5a3a70a | 54 | |
yihui | 9:05f0b5a3a70a | 55 | public: |
yihui | 9:05f0b5a3a70a | 56 | |
yihui | 9:05f0b5a3a70a | 57 | /** Create an InterruptIn connected to the specified pin |
yihui | 9:05f0b5a3a70a | 58 | * |
yihui | 9:05f0b5a3a70a | 59 | * @param pin InterruptIn pin to connect to |
yihui | 9:05f0b5a3a70a | 60 | * @param name (optional) A string to identify the object |
yihui | 9:05f0b5a3a70a | 61 | */ |
yihui | 9:05f0b5a3a70a | 62 | InterruptIn(PinName pin); |
yihui | 9:05f0b5a3a70a | 63 | virtual ~InterruptIn(); |
yihui | 9:05f0b5a3a70a | 64 | |
yihui | 9:05f0b5a3a70a | 65 | int read(); |
yihui | 9:05f0b5a3a70a | 66 | #ifdef MBED_OPERATORS |
yihui | 9:05f0b5a3a70a | 67 | operator int(); |
yihui | 9:05f0b5a3a70a | 68 | |
yihui | 9:05f0b5a3a70a | 69 | #endif |
yihui | 9:05f0b5a3a70a | 70 | |
yihui | 9:05f0b5a3a70a | 71 | /** Attach a function to call when a rising edge occurs on the input |
yihui | 9:05f0b5a3a70a | 72 | * |
yihui | 9:05f0b5a3a70a | 73 | * @param fptr A pointer to a void function, or 0 to set as none |
yihui | 9:05f0b5a3a70a | 74 | */ |
yihui | 9:05f0b5a3a70a | 75 | void rise(void (*fptr)(void)); |
yihui | 9:05f0b5a3a70a | 76 | |
yihui | 9:05f0b5a3a70a | 77 | /** Attach a member function to call when a rising edge occurs on the input |
yihui | 9:05f0b5a3a70a | 78 | * |
yihui | 9:05f0b5a3a70a | 79 | * @param tptr pointer to the object to call the member function on |
yihui | 9:05f0b5a3a70a | 80 | * @param mptr pointer to the member function to be called |
yihui | 9:05f0b5a3a70a | 81 | */ |
yihui | 9:05f0b5a3a70a | 82 | template<typename T> |
yihui | 9:05f0b5a3a70a | 83 | void rise(T* tptr, void (T::*mptr)(void)) { |
yihui | 9:05f0b5a3a70a | 84 | _rise.attach(tptr, mptr); |
yihui | 9:05f0b5a3a70a | 85 | gpio_irq_set(&gpio_irq, IRQ_RISE, 1); |
yihui | 9:05f0b5a3a70a | 86 | } |
yihui | 9:05f0b5a3a70a | 87 | |
yihui | 9:05f0b5a3a70a | 88 | /** Attach a function to call when a falling edge occurs on the input |
yihui | 9:05f0b5a3a70a | 89 | * |
yihui | 9:05f0b5a3a70a | 90 | * @param fptr A pointer to a void function, or 0 to set as none |
yihui | 9:05f0b5a3a70a | 91 | */ |
yihui | 9:05f0b5a3a70a | 92 | void fall(void (*fptr)(void)); |
yihui | 9:05f0b5a3a70a | 93 | |
yihui | 9:05f0b5a3a70a | 94 | /** Attach a member function to call when a falling edge occurs on the input |
yihui | 9:05f0b5a3a70a | 95 | * |
yihui | 9:05f0b5a3a70a | 96 | * @param tptr pointer to the object to call the member function on |
yihui | 9:05f0b5a3a70a | 97 | * @param mptr pointer to the member function to be called |
yihui | 9:05f0b5a3a70a | 98 | */ |
yihui | 9:05f0b5a3a70a | 99 | template<typename T> |
yihui | 9:05f0b5a3a70a | 100 | void fall(T* tptr, void (T::*mptr)(void)) { |
yihui | 9:05f0b5a3a70a | 101 | _fall.attach(tptr, mptr); |
yihui | 9:05f0b5a3a70a | 102 | gpio_irq_set(&gpio_irq, IRQ_FALL, 1); |
yihui | 9:05f0b5a3a70a | 103 | } |
yihui | 9:05f0b5a3a70a | 104 | |
yihui | 9:05f0b5a3a70a | 105 | /** Set the input pin mode |
yihui | 9:05f0b5a3a70a | 106 | * |
yihui | 9:05f0b5a3a70a | 107 | * @param mode PullUp, PullDown, PullNone |
yihui | 9:05f0b5a3a70a | 108 | */ |
yihui | 9:05f0b5a3a70a | 109 | void mode(PinMode pull); |
yihui | 9:05f0b5a3a70a | 110 | |
yihui | 9:05f0b5a3a70a | 111 | /** Enable IRQ. This method depends on hw implementation, might enable one |
yihui | 9:05f0b5a3a70a | 112 | * port interrupts. For further information, check gpio_irq_enable(). |
yihui | 9:05f0b5a3a70a | 113 | */ |
yihui | 9:05f0b5a3a70a | 114 | void enable_irq(); |
yihui | 9:05f0b5a3a70a | 115 | |
yihui | 9:05f0b5a3a70a | 116 | /** Disable IRQ. This method depends on hw implementation, might disable one |
yihui | 9:05f0b5a3a70a | 117 | * port interrupts. For further information, check gpio_irq_disable(). |
yihui | 9:05f0b5a3a70a | 118 | */ |
yihui | 9:05f0b5a3a70a | 119 | void disable_irq(); |
yihui | 9:05f0b5a3a70a | 120 | |
yihui | 9:05f0b5a3a70a | 121 | static void _irq_handler(uint32_t id, gpio_irq_event event); |
yihui | 9:05f0b5a3a70a | 122 | |
yihui | 9:05f0b5a3a70a | 123 | protected: |
yihui | 9:05f0b5a3a70a | 124 | gpio_t gpio; |
yihui | 9:05f0b5a3a70a | 125 | gpio_irq_t gpio_irq; |
yihui | 9:05f0b5a3a70a | 126 | |
yihui | 9:05f0b5a3a70a | 127 | FunctionPointer _rise; |
yihui | 9:05f0b5a3a70a | 128 | FunctionPointer _fall; |
yihui | 9:05f0b5a3a70a | 129 | }; |
yihui | 9:05f0b5a3a70a | 130 | |
yihui | 9:05f0b5a3a70a | 131 | } // namespace mbed |
yihui | 9:05f0b5a3a70a | 132 | |
yihui | 9:05f0b5a3a70a | 133 | #endif |
yihui | 9:05f0b5a3a70a | 134 | |
yihui | 9:05f0b5a3a70a | 135 | #endif |