Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Button.h Source File

Button.h

00001 #ifndef BUTTON_H
00002 #define BUTTON_H
00003 
00004 #include "libs/Kernel.h"
00005 #include "libs/utils.h"
00006 #include "libs/Pin.h"
00007 #include "libs/Hook.h"
00008 
00009 
00010 class Button
00011 {
00012 public:
00013     Button()
00014     {
00015         this->counter = 0;
00016         this->value = false;
00017         this->up_hook = NULL;
00018         this->down_hook = NULL;
00019         this->button_pin = NULL;
00020         this->repeat=false;
00021         this->first_timer=0;
00022         this->second_timer=0;
00023         this->longpress_delay=0;
00024     }
00025 
00026     Button *pin(Pin *passed_pin)
00027     {
00028         this->button_pin = passed_pin;
00029         return this;
00030     }
00031 
00032     void check_signal()
00033     {
00034         check_signal(this->button_pin->get() ? 1 : 0);
00035     }
00036 
00037     void check_signal(int val)
00038     {
00039         bool start_value = this->value;
00040         if ( val ) {
00041             if ( this->counter < 5  ) {
00042                 this->counter++;
00043             }
00044             if ( this->counter == 5 ) {
00045                 this->value = true;
00046             }
00047         } else {
00048             if ( this->counter > 0  ) {
00049                 this->counter--;
00050             }
00051             if ( this->counter == 0 ) {
00052                 this->value = false;
00053             }
00054         }
00055 
00056         if ( start_value != this->value ) {
00057             if ( this->value ) {
00058                 if ( this->up_hook != NULL ) {
00059                     this->up_hook->call();
00060                     this->first_timer=0;
00061                     this->second_timer=0;
00062                     this->repeat=false;
00063                 }
00064             } else {
00065                 if ( this->down_hook != NULL ) {
00066                     this->down_hook->call();
00067                 }
00068             }
00069         }
00070     //auto repeat button presses
00071         if(this->longpress_delay>0){
00072             if(this->value){
00073                  if(this->repeat){
00074                     this->second_timer++;
00075                     if(this->second_timer==10){
00076                         this->up_hook->call();
00077                         this->second_timer=0;
00078                     }
00079                  }
00080                  else{
00081                     this->first_timer++;
00082                     if(this->first_timer==longpress_delay){
00083                         this->repeat=true;
00084                         this->first_timer=0;
00085                     }
00086                  }
00087             }
00088         }
00089     }
00090     
00091     void set_longpress_delay(int delay){
00092         this->longpress_delay=delay;
00093     }
00094     
00095     bool get()
00096     {
00097         return this->value;
00098     }
00099 
00100 
00101     template<typename T> Button *up_attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) )
00102     {
00103         this->up_hook = new Hook();
00104         this->up_hook->attach(optr, fptr);
00105         return this;
00106     }
00107 
00108     template<typename T> Button *down_attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) )
00109     {
00110         this->down_hook = new Hook();
00111         this->down_hook->attach(optr, fptr);
00112         return this;
00113     }
00114 
00115 private:
00116     Hook *up_hook;
00117     Hook *down_hook;
00118     bool value;
00119     char counter;
00120     Pin *button_pin;
00121     
00122     int longpress_delay;
00123     int first_timer;    //delay before starting to repeat
00124     int second_timer;   //time beetwen repeats
00125     bool repeat;
00126 };
00127 
00128 
00129 
00130 
00131 
00132 #endif