Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
inc/InterruptIn.h@1:ff38e3bad33a, 2018-11-15 (annotated)
- Committer:
- martwerl
- Date:
- Thu Nov 15 17:59:08 2018 +0000
- Revision:
- 1:ff38e3bad33a
- Parent:
- 0:afeca64a6543
Diplomarbeit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| martwerl | 0:afeca64a6543 | 1 | /* mbed Microcontroller Library |
| martwerl | 0:afeca64a6543 | 2 | * Copyright (c) 2006-2013 ARM Limited |
| martwerl | 0:afeca64a6543 | 3 | * |
| martwerl | 0:afeca64a6543 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| martwerl | 0:afeca64a6543 | 5 | * of this software and associated documentation files (the "Software"), to deal |
| martwerl | 0:afeca64a6543 | 6 | * in the Software without restriction, including without limitation the rights |
| martwerl | 0:afeca64a6543 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| martwerl | 0:afeca64a6543 | 8 | * copies of the Software, and to permit persons to whom the Software is |
| martwerl | 0:afeca64a6543 | 9 | * furnished to do so, subject to the following conditions: |
| martwerl | 0:afeca64a6543 | 10 | * |
| martwerl | 0:afeca64a6543 | 11 | * The above copyright notice and this permission notice shall be included in |
| martwerl | 0:afeca64a6543 | 12 | * all copies or substantial portions of the Software. |
| martwerl | 0:afeca64a6543 | 13 | * |
| martwerl | 0:afeca64a6543 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| martwerl | 0:afeca64a6543 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| martwerl | 0:afeca64a6543 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| martwerl | 0:afeca64a6543 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| martwerl | 0:afeca64a6543 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| martwerl | 0:afeca64a6543 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| martwerl | 0:afeca64a6543 | 20 | * SOFTWARE. |
| martwerl | 0:afeca64a6543 | 21 | */ |
| martwerl | 0:afeca64a6543 | 22 | #ifndef MBED_INTERRUPTIN_H |
| martwerl | 0:afeca64a6543 | 23 | #define MBED_INTERRUPTIN_H |
| martwerl | 0:afeca64a6543 | 24 | |
| martwerl | 0:afeca64a6543 | 25 | #include "platform.h" |
| martwerl | 0:afeca64a6543 | 26 | |
| martwerl | 0:afeca64a6543 | 27 | #if DEVICE_INTERRUPTIN |
| martwerl | 0:afeca64a6543 | 28 | |
| martwerl | 0:afeca64a6543 | 29 | #include "gpio_api.h" |
| martwerl | 0:afeca64a6543 | 30 | #include "gpio_irq_api.h" |
| martwerl | 0:afeca64a6543 | 31 | |
| martwerl | 0:afeca64a6543 | 32 | #include "FunctionPointer.h" |
| martwerl | 0:afeca64a6543 | 33 | |
| martwerl | 0:afeca64a6543 | 34 | namespace mbed { |
| martwerl | 0:afeca64a6543 | 35 | |
| martwerl | 0:afeca64a6543 | 36 | /** A digital interrupt input, used to call a function on a rising or falling edge |
| martwerl | 0:afeca64a6543 | 37 | * |
| martwerl | 0:afeca64a6543 | 38 | * Example: |
| martwerl | 0:afeca64a6543 | 39 | * @code |
| martwerl | 0:afeca64a6543 | 40 | * // Flash an LED while waiting for events |
| martwerl | 0:afeca64a6543 | 41 | * |
| martwerl | 0:afeca64a6543 | 42 | * #include "mbed.h" |
| martwerl | 0:afeca64a6543 | 43 | * |
| martwerl | 0:afeca64a6543 | 44 | * InterruptIn event(p16); |
| martwerl | 0:afeca64a6543 | 45 | * DigitalOut led(LED1); |
| martwerl | 0:afeca64a6543 | 46 | * |
| martwerl | 0:afeca64a6543 | 47 | * void trigger() { |
| martwerl | 0:afeca64a6543 | 48 | * printf("triggered!\n"); |
| martwerl | 0:afeca64a6543 | 49 | * } |
| martwerl | 0:afeca64a6543 | 50 | * |
| martwerl | 0:afeca64a6543 | 51 | * int main() { |
| martwerl | 0:afeca64a6543 | 52 | * event.rise(&trigger); |
| martwerl | 0:afeca64a6543 | 53 | * while(1) { |
| martwerl | 0:afeca64a6543 | 54 | * led = !led; |
| martwerl | 0:afeca64a6543 | 55 | * wait(0.25); |
| martwerl | 0:afeca64a6543 | 56 | * } |
| martwerl | 0:afeca64a6543 | 57 | * } |
| martwerl | 0:afeca64a6543 | 58 | * @endcode |
| martwerl | 0:afeca64a6543 | 59 | */ |
| martwerl | 0:afeca64a6543 | 60 | class InterruptIn { |
| martwerl | 0:afeca64a6543 | 61 | |
| martwerl | 0:afeca64a6543 | 62 | public: |
| martwerl | 0:afeca64a6543 | 63 | |
| martwerl | 0:afeca64a6543 | 64 | /** Create an InterruptIn connected to the specified pin |
| martwerl | 0:afeca64a6543 | 65 | * |
| martwerl | 0:afeca64a6543 | 66 | * @param pin InterruptIn pin to connect to |
| martwerl | 0:afeca64a6543 | 67 | * @param name (optional) A string to identify the object |
| martwerl | 0:afeca64a6543 | 68 | */ |
| martwerl | 0:afeca64a6543 | 69 | InterruptIn(PinName pin); |
| martwerl | 0:afeca64a6543 | 70 | virtual ~InterruptIn(); |
| martwerl | 0:afeca64a6543 | 71 | |
| martwerl | 0:afeca64a6543 | 72 | int read(); |
| martwerl | 0:afeca64a6543 | 73 | #ifdef MBED_OPERATORS |
| martwerl | 0:afeca64a6543 | 74 | operator int(); |
| martwerl | 0:afeca64a6543 | 75 | |
| martwerl | 0:afeca64a6543 | 76 | #endif |
| martwerl | 0:afeca64a6543 | 77 | |
| martwerl | 0:afeca64a6543 | 78 | /** Attach a function to call when a rising edge occurs on the input |
| martwerl | 0:afeca64a6543 | 79 | * |
| martwerl | 0:afeca64a6543 | 80 | * @param fptr A pointer to a void function, or 0 to set as none |
| martwerl | 0:afeca64a6543 | 81 | */ |
| martwerl | 0:afeca64a6543 | 82 | void rise(void (*fptr)(void)); |
| martwerl | 0:afeca64a6543 | 83 | |
| martwerl | 0:afeca64a6543 | 84 | /** Attach a member function to call when a rising edge occurs on the input |
| martwerl | 0:afeca64a6543 | 85 | * |
| martwerl | 0:afeca64a6543 | 86 | * @param tptr pointer to the object to call the member function on |
| martwerl | 0:afeca64a6543 | 87 | * @param mptr pointer to the member function to be called |
| martwerl | 0:afeca64a6543 | 88 | */ |
| martwerl | 0:afeca64a6543 | 89 | template<typename T> |
| martwerl | 0:afeca64a6543 | 90 | void rise(T* tptr, void (T::*mptr)(void)) { |
| martwerl | 0:afeca64a6543 | 91 | _rise.attach(tptr, mptr); |
| martwerl | 0:afeca64a6543 | 92 | gpio_irq_set(&gpio_irq, IRQ_RISE, 1); |
| martwerl | 0:afeca64a6543 | 93 | } |
| martwerl | 0:afeca64a6543 | 94 | |
| martwerl | 0:afeca64a6543 | 95 | /** Attach a function to call when a falling edge occurs on the input |
| martwerl | 0:afeca64a6543 | 96 | * |
| martwerl | 0:afeca64a6543 | 97 | * @param fptr A pointer to a void function, or 0 to set as none |
| martwerl | 0:afeca64a6543 | 98 | */ |
| martwerl | 0:afeca64a6543 | 99 | void fall(void (*fptr)(void)); |
| martwerl | 0:afeca64a6543 | 100 | |
| martwerl | 0:afeca64a6543 | 101 | /** Attach a member function to call when a falling edge occurs on the input |
| martwerl | 0:afeca64a6543 | 102 | * |
| martwerl | 0:afeca64a6543 | 103 | * @param tptr pointer to the object to call the member function on |
| martwerl | 0:afeca64a6543 | 104 | * @param mptr pointer to the member function to be called |
| martwerl | 0:afeca64a6543 | 105 | */ |
| martwerl | 0:afeca64a6543 | 106 | template<typename T> |
| martwerl | 0:afeca64a6543 | 107 | void fall(T* tptr, void (T::*mptr)(void)) { |
| martwerl | 0:afeca64a6543 | 108 | _fall.attach(tptr, mptr); |
| martwerl | 0:afeca64a6543 | 109 | gpio_irq_set(&gpio_irq, IRQ_FALL, 1); |
| martwerl | 0:afeca64a6543 | 110 | } |
| martwerl | 0:afeca64a6543 | 111 | |
| martwerl | 0:afeca64a6543 | 112 | /** Set the input pin mode |
| martwerl | 0:afeca64a6543 | 113 | * |
| martwerl | 0:afeca64a6543 | 114 | * @param mode PullUp, PullDown, PullNone |
| martwerl | 0:afeca64a6543 | 115 | */ |
| martwerl | 0:afeca64a6543 | 116 | void mode(PinMode pull); |
| martwerl | 0:afeca64a6543 | 117 | |
| martwerl | 0:afeca64a6543 | 118 | static void _irq_handler(uint32_t id, gpio_irq_event event); |
| martwerl | 0:afeca64a6543 | 119 | |
| martwerl | 0:afeca64a6543 | 120 | protected: |
| martwerl | 0:afeca64a6543 | 121 | gpio_t gpio; |
| martwerl | 0:afeca64a6543 | 122 | gpio_irq_t gpio_irq; |
| martwerl | 0:afeca64a6543 | 123 | |
| martwerl | 0:afeca64a6543 | 124 | FunctionPointer _rise; |
| martwerl | 0:afeca64a6543 | 125 | FunctionPointer _fall; |
| martwerl | 0:afeca64a6543 | 126 | }; |
| martwerl | 0:afeca64a6543 | 127 | |
| martwerl | 0:afeca64a6543 | 128 | } // namespace mbed |
| martwerl | 0:afeca64a6543 | 129 | |
| martwerl | 0:afeca64a6543 | 130 | #endif |
| martwerl | 0:afeca64a6543 | 131 | |
| martwerl | 0:afeca64a6543 | 132 | #endif |
| martwerl | 0:afeca64a6543 | 133 |