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