would be Simon game, as a demonstrator for buttons debouncing and \"collecting\" ---

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DebounceIn_.h Source File

DebounceIn_.h

00001 /*
00002     Copyright (c) 2010 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022  
00023 #ifndef DEBOUNCEIN_H
00024 #define DEBOUNCEIN_H
00025  
00026 #include "mbed.h"
00027 
00028 /** DebounceIn adds mechanical switch debouncing to DigitialIn.
00029  *
00030  * Example:
00031  * @code
00032  * #include "mbed.h"
00033  * #include "DebounceIn.h"
00034  *
00035  * DebounceIn  d(p5);
00036  * DigitialOut led1(LED1);
00037  * DigitialOut led2(LED2);
00038  *
00039  * int main() {
00040  *     while(1) {
00041  *         led1 = d;
00042  *         led2 = d.read();
00043  *     }
00044  * }
00045  * @endcode
00046  *
00047  * @see set_debounce_us() To change the sampling frequency.
00048  * @see set_samples() To alter the number of samples.
00049  *
00050  * This example shows one input displayed by two outputs. The input
00051  * is debounced by the default 10ms.
00052  */
00053  
00054 class DebounceIn : public DigitalIn {
00055     public:
00056     
00057         /** set_debounce_us
00058          *
00059          * Sets the debounce sample period time in microseconds, default is 1000 (1ms)
00060          *
00061          * @param int i The debounce sample period time to set.
00062          */        
00063         void set_debounce_us(int i) { _ticker.attach_us(this, &DebounceIn::_callback, i); }
00064         
00065         /** set_samples
00066          *
00067          * Defines the number of samples before switching the shadow 
00068          * definition of the pin. 
00069          *
00070          * @param int i The number of samples.
00071          */        
00072         void set_samples(int i) { _samples = i; }
00073         
00074         /** read
00075          *
00076          * Read the value of the debounced pin.
00077          */
00078         int read(void) { return _shadow; }
00079         
00080 #ifdef MBED_OPERATORS
00081         /** operator int()
00082          *
00083          * Read the value of the debounced pin.
00084          */
00085         operator int() { return read(); }
00086 #endif  
00087 
00088         int pressed;
00089         /** Constructor
00090          * 
00091          * @param PinName pin The pin to assign as an input.
00092          */
00093         DebounceIn(PinName pin, const char *name = NULL) : DigitalIn(pin, name) { _counter = 0; _samples = 10; set_debounce_us(1000); pressed = 0;};
00094         
00095     protected:
00096         void _callback(void) { 
00097             if (!DigitalIn::read()) {  // negated as button is pressed when 0 
00098                 if (_counter < _samples) _counter++; 
00099                 if ((_counter == _samples) && (_shadow == 0)) {
00100                   _shadow = 1;
00101                   pressed = 1;
00102                 } 
00103             }
00104             else { 
00105                 if (_counter > 0) _counter--; 
00106                 if (_counter == 0) _shadow = 0; 
00107             }
00108         }
00109         
00110         Ticker _ticker;
00111         int    _shadow;
00112         int    _counter;
00113         int    _samples;
00114 };
00115  
00116 #endif
00117