class to debouce interrupt pin

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Debounce.cpp Source File

Debounce.cpp

00001 #include "mbed.h"
00002 #include "Debounce.h"
00003 
00004 Debounce::Debounce(PinName Pin, int DebounceMS, void (*RiseFunction)(), void (*FallFunction)()) : Input(Pin)
00005 {
00006     Input.mode(PullUp); //Set the input to pull up or pull down.
00007     DebounceTime = (float)DebounceMS / 1000;    //Calculate the debouncetime in seconds.
00008     Input.rise(callback(this,&Debounce::InputRise));
00009     Input.fall(callback(this,&Debounce::InputFall));  
00010     RiseFunctionPointer = RiseFunction;
00011     FallFunctionPointer = FallFunction;
00012 }
00013 
00014 void Debounce::InputRise()
00015 {
00016     //This function will get called when the input rises.
00017     InputRiseTick.attach(callback(this, &Debounce::RiseTick), DebounceTime); //Attach a ticker with the desired check time and call the function risetick when time's up.
00018 }
00019 
00020 void Debounce::InputFall()
00021 {
00022     //This function will get called when the input falls.
00023     InputFallTick.attach(callback(this, &Debounce::FallTick), DebounceTime); //Attach a ticker with the desired check time and call the function risetick when time's up.
00024 }
00025 
00026 void Debounce::RiseTick()
00027 {
00028     //This function will get called after a set time from the last rise event.
00029     InputRiseTick.detach(); //detach the ticker, as we only use it once.
00030     if (Input) //Check wheter or not the input is still high.
00031     {
00032         //Input is still high after the last rise event.
00033         (*RiseFunctionPointer)();  //Call the rise function. 
00034           
00035     }
00036 }
00037 
00038 void Debounce::FallTick()
00039 {
00040     //This function will get called after a set time from the last fall event.
00041     InputFallTick.detach(); //detach the ticker, as we only use it once.
00042     if (!Input) //Check wheter or not the input is still low.
00043     {
00044         //Input is still low after the last fall event.
00045         (*FallFunctionPointer)();   
00046     }
00047 }