PokittoLib with changes to lcd refresh etc.

Dependents:   Pokittris

Fork of Pokitto by Pokitto Community Team

This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.

Committer:
spinal
Date:
Sun Oct 15 18:03:02 2017 +0000
Revision:
11:02ad9c807a21
Parent:
5:7e5c566b1760
fixed 4color refreshRegion code

Who changed what in which revision?

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