mbed library for slider v2

Dependents:   kl46z_slider_v2

Committer:
mturner5
Date:
Wed Sep 14 07:04:27 2016 +0000
Revision:
0:b7116bd48af6
Tried to use the timer.

Who changed what in which revision?

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