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