MHVLib  20111011
An efficiency oriented runtime library for AVR microcontrollers
A:/eclipse/mhvlib/MHV_PinChangeManager.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Make, Hack, Void Inc
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *  * Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  *  * Neither the name of the Make, Hack, Void nor the
00013  *    names of its contributors may be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY
00020  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 #ifndef MHV_PINCHANGE_MANAGER_H_
00029 #define MHV_PINCHANGE_MANAGER_H_
00030 
00031 #include <MHV_io.h>
00032 #include <MHV_Device_RX.h>
00033 
00034 #define MHV_PINCHANGE_MANAGER_ASSIGN_PCINT(___mhvEventManager) \
00035 ISR(PCINT_vect) { \
00036         ___mhvEventManager.pinChange0(); \
00037 }
00038 
00039 #define MHV_PINCHANGE_MANAGER_ASSIGN_PCINT0(___mhvEventManager) \
00040 ISR(PCINT0_vect) { \
00041         ___mhvEventManager.pinChange0(); \
00042 }
00043 
00044 #define MHV_PINCHANGE_MANAGER_ASSIGN_PCINT1(___mhvEventManager) \
00045 ISR(PCINT1_vect) { \
00046         ___mhvEventManager.pinChange1(); \
00047 }
00048 
00049 #define MHV_PINCHANGE_MANAGER_ASSIGN_PCINT2(___mhvEventManager) \
00050 ISR(PCINT2_vect) { \
00051         ___mhvEventManager.pinChange2(); \
00052 }
00053 
00054 #if MHV_PC_INT_COUNT > 15
00055 #define MHV_PINCHANGE_MANAGER_ASSIGN_INTERRUPTS(__mhvEventManager) \
00056                 MHV_PINCHANGE_MANAGER_ASSIGN_PCINT2(__mhvEventManager) \
00057                 MHV_PINCHANGE_MANAGER_ASSIGN_PCINT1(__mhvEventManager) \
00058                 MHV_PINCHANGE_MANAGER_ASSIGN_PCINT0(__mhvEventManager)
00059 #elif MHV_PC_INT_COUNT > 7
00060 #define MHV_PINCHANGE_MANAGER_ASSIGN_INTERRUPTS(__mhvEventManager) \
00061                 MHV_PINCHANGE_MANAGER_ASSIGN_PCINT1(__mhvEventManager) \
00062                 MHV_PINCHANGE_MANAGER_ASSIGN_PCINT0(__mhvEventManager)
00063 #elif defined PCINT0_vect
00064 #define MHV_PINCHANGE_MANAGER_ASSIGN_INTERRUPTS(__mhvEventManager) \
00065                 MHV_PINCHANGE_MANAGER_ASSIGN_PCINT0(__mhvEventManager)
00066 #else
00067 #define MHV_PINCHANGE_MANAGER_ASSIGN_INTERRUPTS(__mhvEventManager) \
00068                 MHV_PINCHANGE_MANAGER_ASSIGN_PCINT(__mhvEventManager)
00069 #endif
00070 
00071 
00072 
00073 
00074 class MHV_PinEventListener {
00075 public:
00076         virtual void pinChanged(uint8_t pcInt, bool newState) =0;
00077 };
00078 
00079 struct mhv_eventPin {
00080         volatile uint8_t                *port;
00081         uint8_t                                 mask;
00082         uint8_t                                 pcInt;
00083         MHV_PinEventListener    *listener;
00084         bool                                    previous;
00085         bool                                    changed;
00086 };
00087 typedef struct mhv_eventPin MHV_EVENT_PIN;
00088 
00089 class MHV_PinChangeManager {
00090 protected:
00091 // Fixme: Allocate externally and pass in
00092         MHV_EVENT_PIN   _pins[MHV_PC_INT_COUNT];
00093 //      uint8_t                 _pinsUsed;
00094 
00095 public:
00096         MHV_PinChangeManager();
00097 
00098         void pinChange0();
00099 #if MHV_PC_INT_COUNT > 7
00100         void pinChange1();
00101 #endif
00102 #if MHV_PC_INT_COUNT > 7
00103         void pinChange2();
00104 #endif
00105         void pinChange(uint8_t offset);
00106         void registerListener(volatile uint8_t *pinDir, volatile uint8_t *pinOut,
00107                         volatile uint8_t *pinIn, uint8_t pinBit, int8_t pinChangeInterrupt,
00108                         MHV_PinEventListener *listener);
00109         void deregisterListener(volatile uint8_t *pinDir, volatile uint8_t *pinOut,
00110                         volatile uint8_t *pinIn, uint8_t pinBit, int8_t pinChangeInterrupt);
00111 
00112         void handleEvents();
00113 
00114 };
00115 
00116 #endif /* MHV_PINCHANGEMANAGER_H_ */