Basic fading in and out of leds with light sensor.

Dependencies:   mbed

Committer:
mturner5
Date:
Mon Sep 19 03:24:58 2016 +0000
Revision:
0:cf7af2656659
Basic v1

Who changed what in which revision?

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