Luis Bernal / Mbed 2 deprecated xbeat-hoppel-code

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers button.cpp Source File

button.cpp

00001 /*
00002  *
00003  * This program is free software; you can redistribute it and/or modify
00004  * it under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation; either version 3 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * @file button.cpp
00017  * @author Andre Moehl
00018  * @date 01/2011
00019  * @brief Button Class definition
00020  */
00021  
00022 /*--- Includes ------------------------*/
00023 #include "button.h"
00024 
00025 /*--- Functions -----------------------*/
00026 //Contructor 
00027 Button::Button(PinName pin, const char *name): DigitalIn(pin,name)
00028 {
00029     _counter = 0;
00030     _samples = 10;
00031     set_debounce_us(1000);
00032 }
00033 
00034 // sets Sample for debounce
00035 void Button::set_samples(int i)
00036 {
00037         _samples = i;
00038 }
00039 
00040 // set debounce time
00041 void Button::set_debounce_us(int i)
00042 {
00043     _ticker.attach_us(this, &Button::_callback, i);
00044 }
00045 
00046 // return the final state   
00047 int Button::read()
00048 {
00049     return _shadow;
00050 }
00051 
00052 //overwrite fuction from derived "DigitalIn"
00053 Button::operator int()
00054 {
00055     return read();
00056 }
00057 
00058 
00059 // counts the oscillations of the button
00060 void Button::_callback(void) 
00061 { 
00062     if (DigitalIn::read()) 
00063     { 
00064         if (_counter < _samples) _counter++; 
00065         if (_counter == _samples) _shadow = 1; 
00066     }
00067     else { 
00068         if (_counter > 0) _counter--; 
00069         if (_counter == 0) _shadow = 0; 
00070     }
00071 }
00072 
00073