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 #ifndef MBED_INTERRUPTMANAGER_H
mturner5 0:cf7af2656659 2 #define MBED_INTERRUPTMANAGER_H
mturner5 0:cf7af2656659 3
mturner5 0:cf7af2656659 4 #include "cmsis.h"
mturner5 0:cf7af2656659 5 #include "CallChain.h"
mturner5 0:cf7af2656659 6 #include <string.h>
mturner5 0:cf7af2656659 7
mturner5 0:cf7af2656659 8 namespace mbed {
mturner5 0:cf7af2656659 9
mturner5 0:cf7af2656659 10 /** Use this singleton if you need to chain interrupt handlers.
mturner5 0:cf7af2656659 11 *
mturner5 0:cf7af2656659 12 * Example (for LPC1768):
mturner5 0:cf7af2656659 13 * @code
mturner5 0:cf7af2656659 14 * #include "InterruptManager.h"
mturner5 0:cf7af2656659 15 * #include "mbed.h"
mturner5 0:cf7af2656659 16 *
mturner5 0:cf7af2656659 17 * Ticker flipper;
mturner5 0:cf7af2656659 18 * DigitalOut led1(LED1);
mturner5 0:cf7af2656659 19 * DigitalOut led2(LED2);
mturner5 0:cf7af2656659 20 *
mturner5 0:cf7af2656659 21 * void flip(void) {
mturner5 0:cf7af2656659 22 * led1 = !led1;
mturner5 0:cf7af2656659 23 * }
mturner5 0:cf7af2656659 24 *
mturner5 0:cf7af2656659 25 * void handler(void) {
mturner5 0:cf7af2656659 26 * led2 = !led1;
mturner5 0:cf7af2656659 27 * }
mturner5 0:cf7af2656659 28 *
mturner5 0:cf7af2656659 29 * int main() {
mturner5 0:cf7af2656659 30 * led1 = led2 = 0;
mturner5 0:cf7af2656659 31 * flipper.attach(&flip, 1.0);
mturner5 0:cf7af2656659 32 * InterruptManager::get()->add_handler(handler, TIMER3_IRQn);
mturner5 0:cf7af2656659 33 * }
mturner5 0:cf7af2656659 34 * @endcode
mturner5 0:cf7af2656659 35 */
mturner5 0:cf7af2656659 36 class InterruptManager {
mturner5 0:cf7af2656659 37 public:
mturner5 0:cf7af2656659 38 /** Return the only instance of this class
mturner5 0:cf7af2656659 39 */
mturner5 0:cf7af2656659 40 static InterruptManager* get();
mturner5 0:cf7af2656659 41
mturner5 0:cf7af2656659 42 /** Destroy the current instance of the interrupt manager
mturner5 0:cf7af2656659 43 */
mturner5 0:cf7af2656659 44 static void destroy();
mturner5 0:cf7af2656659 45
mturner5 0:cf7af2656659 46 /** Add a handler for an interrupt at the end of the handler list
mturner5 0:cf7af2656659 47 *
mturner5 0:cf7af2656659 48 * @param function the handler to add
mturner5 0:cf7af2656659 49 * @param irq interrupt number
mturner5 0:cf7af2656659 50 *
mturner5 0:cf7af2656659 51 * @returns
mturner5 0:cf7af2656659 52 * The function object created for 'function'
mturner5 0:cf7af2656659 53 */
mturner5 0:cf7af2656659 54 pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) {
mturner5 0:cf7af2656659 55 return add_common(function, irq);
mturner5 0:cf7af2656659 56 }
mturner5 0:cf7af2656659 57
mturner5 0:cf7af2656659 58 /** Add a handler for an interrupt at the beginning of the handler list
mturner5 0:cf7af2656659 59 *
mturner5 0:cf7af2656659 60 * @param function the handler to add
mturner5 0:cf7af2656659 61 * @param irq interrupt number
mturner5 0:cf7af2656659 62 *
mturner5 0:cf7af2656659 63 * @returns
mturner5 0:cf7af2656659 64 * The function object created for 'function'
mturner5 0:cf7af2656659 65 */
mturner5 0:cf7af2656659 66 pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) {
mturner5 0:cf7af2656659 67 return add_common(function, irq, true);
mturner5 0:cf7af2656659 68 }
mturner5 0:cf7af2656659 69
mturner5 0:cf7af2656659 70 /** Add a handler for an interrupt at the end of the handler list
mturner5 0:cf7af2656659 71 *
mturner5 0:cf7af2656659 72 * @param tptr pointer to the object that has the handler function
mturner5 0:cf7af2656659 73 * @param mptr pointer to the actual handler function
mturner5 0:cf7af2656659 74 * @param irq interrupt number
mturner5 0:cf7af2656659 75 *
mturner5 0:cf7af2656659 76 * @returns
mturner5 0:cf7af2656659 77 * The function object created for 'tptr' and 'mptr'
mturner5 0:cf7af2656659 78 */
mturner5 0:cf7af2656659 79 template<typename T>
mturner5 0:cf7af2656659 80 pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
mturner5 0:cf7af2656659 81 return add_common(tptr, mptr, irq);
mturner5 0:cf7af2656659 82 }
mturner5 0:cf7af2656659 83
mturner5 0:cf7af2656659 84 /** Add a handler for an interrupt at the beginning of the handler list
mturner5 0:cf7af2656659 85 *
mturner5 0:cf7af2656659 86 * @param tptr pointer to the object that has the handler function
mturner5 0:cf7af2656659 87 * @param mptr pointer to the actual handler function
mturner5 0:cf7af2656659 88 * @param irq interrupt number
mturner5 0:cf7af2656659 89 *
mturner5 0:cf7af2656659 90 * @returns
mturner5 0:cf7af2656659 91 * The function object created for 'tptr' and 'mptr'
mturner5 0:cf7af2656659 92 */
mturner5 0:cf7af2656659 93 template<typename T>
mturner5 0:cf7af2656659 94 pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
mturner5 0:cf7af2656659 95 return add_common(tptr, mptr, irq, true);
mturner5 0:cf7af2656659 96 }
mturner5 0:cf7af2656659 97
mturner5 0:cf7af2656659 98 /** Remove a handler from an interrupt
mturner5 0:cf7af2656659 99 *
mturner5 0:cf7af2656659 100 * @param handler the function object for the handler to remove
mturner5 0:cf7af2656659 101 * @param irq the interrupt number
mturner5 0:cf7af2656659 102 *
mturner5 0:cf7af2656659 103 * @returns
mturner5 0:cf7af2656659 104 * true if the handler was found and removed, false otherwise
mturner5 0:cf7af2656659 105 */
mturner5 0:cf7af2656659 106 bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
mturner5 0:cf7af2656659 107
mturner5 0:cf7af2656659 108 private:
mturner5 0:cf7af2656659 109 InterruptManager();
mturner5 0:cf7af2656659 110 ~InterruptManager();
mturner5 0:cf7af2656659 111
mturner5 0:cf7af2656659 112 // We declare the copy contructor and the assignment operator, but we don't
mturner5 0:cf7af2656659 113 // implement them. This way, if someone tries to copy/assign our instance,
mturner5 0:cf7af2656659 114 // he will get an error at compile time.
mturner5 0:cf7af2656659 115 InterruptManager(const InterruptManager&);
mturner5 0:cf7af2656659 116 InterruptManager& operator =(const InterruptManager&);
mturner5 0:cf7af2656659 117
mturner5 0:cf7af2656659 118 template<typename T>
mturner5 0:cf7af2656659 119 pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) {
mturner5 0:cf7af2656659 120 int irq_pos = get_irq_index(irq);
mturner5 0:cf7af2656659 121 bool change = must_replace_vector(irq);
mturner5 0:cf7af2656659 122
mturner5 0:cf7af2656659 123 pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr);
mturner5 0:cf7af2656659 124 if (change)
mturner5 0:cf7af2656659 125 NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
mturner5 0:cf7af2656659 126 return pf;
mturner5 0:cf7af2656659 127 }
mturner5 0:cf7af2656659 128
mturner5 0:cf7af2656659 129 pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false);
mturner5 0:cf7af2656659 130 bool must_replace_vector(IRQn_Type irq);
mturner5 0:cf7af2656659 131 int get_irq_index(IRQn_Type irq);
mturner5 0:cf7af2656659 132 void irq_helper();
mturner5 0:cf7af2656659 133 void add_helper(void (*function)(void), IRQn_Type irq, bool front=false);
mturner5 0:cf7af2656659 134 static void static_irq_helper();
mturner5 0:cf7af2656659 135
mturner5 0:cf7af2656659 136 CallChain* _chains[NVIC_NUM_VECTORS];
mturner5 0:cf7af2656659 137 static InterruptManager* _instance;
mturner5 0:cf7af2656659 138 };
mturner5 0:cf7af2656659 139
mturner5 0:cf7af2656659 140 } // namespace mbed
mturner5 0:cf7af2656659 141
mturner5 0:cf7af2656659 142 #endif
mturner5 0:cf7af2656659 143