MHVLib
20111011
An efficiency oriented runtime library for AVR microcontrollers
|
00001 /* 00002 * MHV_Debounce.h 00003 * 00004 * Created on: 12/10/2010 00005 * Author: Deece 00006 */ 00007 00008 #ifndef MHV_DEBOUNCE_H_ 00009 #define MHV_DEBOUNCE_H_ 00010 00011 #include <string.h> 00012 00013 #include <MHV_RTC.h> 00014 #include <MHV_io.h> 00015 #include <MHV_PinChangeManager.h> 00016 00017 00018 #define MHV_DEBOUNCE_ASSIGN_PCINT(__mhvDebounce) \ 00019 ISR(PCINT_vect) { \ 00020 __mhvDebounce.pinChange0(); \ 00021 } 00022 00023 #define MHV_DEBOUNCE_ASSIGN_PCINT0(__mhvDebounce) \ 00024 ISR(PCINT0_vect) { \ 00025 __mhvDebounce.pinChange0(); \ 00026 } 00027 00028 #define MHV_DEBOUNCE_ASSIGN_PCINT1(__mhvDebounce) \ 00029 ISR(PCINT1_vect) { \ 00030 __mhvDebounce.pinChange1(); \ 00031 } 00032 00033 #define MHV_DEBOUNCE_ASSIGN_PCINT2(__mhvDebounce) \ 00034 ISR(PCINT2_vect) { \ 00035 __mhvDebounce.pinChange2(); \ 00036 } 00037 00038 00039 #if MHV_PC_INT_COUNT > 15 00040 #define MHV_DEBOUNCE_ASSIGN_INTERRUPTS(_mhvDebounce) \ 00041 MHV_DEBOUNCE_ASSIGN_PCINT2(_mhvDebounce) \ 00042 MHV_DEBOUNCE_ASSIGN_PCINT1(_mhvDebounce) \ 00043 MHV_DEBOUNCE_ASSIGN_PCINT0(_mhvDebounce) 00044 #elif MHV_PC_INT_COUNT > 7 00045 #define MHV_DEBOUNCE_ASSIGN_INTERRUPTS(_mhvDebounce) \ 00046 MHV_DEBOUNCE_ASSIGN_PCINT1(_mhvDebounce) \ 00047 MHV_DEBOUNCE_ASSIGN_PCINT0(_mhvDebounce) 00048 #elif defined PCINT0_vect 00049 #define MHV_DEBOUNCE_ASSIGN_INTERRUPTS(mhvDebounce) \ 00050 MHV_DEBOUNCE_ASSIGN_PCINT0(_mhvDebounce) 00051 #else 00052 #define MHV_DEBOUNCE_ASSIGN_INTERRUPTS(mhvDebounce) \ 00053 MHV_DEBOUNCE_ASSIGN_PCINT(_mhvDebounce) 00054 #endif 00055 00056 class MHV_DebounceListener { 00057 public: 00058 virtual void singlePress(uint8_t pcInt, MHV_TIMESTAMP *heldFor) =0; 00059 virtual void heldDown(uint8_t pcInt, MHV_TIMESTAMP *heldFor) =0; 00060 }; 00061 00062 struct mhv_debouncePin { 00063 uint8_t previous; 00064 MHV_TIMESTAMP timestamp; 00065 MHV_DebounceListener *listener; 00066 bool held; 00067 }; 00068 typedef struct mhv_debouncePin MHV_DEBOUNCE_PIN; 00069 00070 class MHV_Debounce : MHV_PinEventListener { 00071 protected: 00072 MHV_RTC *_rtc; 00073 MHV_DEBOUNCE_PIN _pins[MHV_PC_INT_COUNT]; 00074 MHV_TIMESTAMP _debounceTime; 00075 MHV_TIMESTAMP _heldTime; 00076 MHV_TIMESTAMP _repeatTime; 00077 MHV_PinChangeManager *_pinChangeManager; 00078 00079 void pinChanged(uint8_t pcInt, bool newState); 00080 void initPin(uint8_t pinchangeInterrupt); 00081 00082 public: 00083 MHV_Debounce(MHV_PinChangeManager *pinChangeManager, MHV_RTC *rtc, uint16_t debounceTime, uint16_t heldTime, uint16_t repeatTime); 00084 void assignKey(volatile uint8_t *dir, volatile uint8_t *out, volatile uint8_t *in, 00085 uint8_t pin, int8_t pinchangeInterrupt, MHV_DebounceListener *listener); 00086 void deassignKey(volatile uint8_t *dir, volatile uint8_t *out, volatile uint8_t *in, 00087 uint8_t pin, int8_t pinchangeInterrupt); 00088 void checkHeld(); 00089 }; 00090 00091 #endif /* MHV_DEBOUNCE_H_ */