Ryo Od / ExioController
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ExioMcp23s17DebounceIn.h Source File

ExioMcp23s17DebounceIn.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 /*
00024  * 2016.11.04 Customized for MCP23S17 by ryood
00025  */
00026  
00027 #ifndef EXIOMCP23S17DEBOUNCEIN_H
00028 #define EXIOMCP23S17DEBOUNCEIN_H
00029  
00030 #include "mbed.h"
00031 #include "ExioMcp23s17DigitalIn.h"
00032  
00033 /** DebounceIn adds mechanical switch debouncing to DigitialIn.
00034  *
00035  * Example:
00036  * @code
00037  * #include "mbed.h"
00038  * #include "DebounceIn.h"
00039  *
00040  * DebounceIn  d(p5);
00041  * DigitialOut led1(LED1);
00042  * DigitialOut led2(LED2);
00043  *
00044  * int main() {
00045  *     while(1) {
00046  *         led1 = d;
00047  *         led2 = d.read();
00048  *     }
00049  * }
00050  * @endcode
00051  *
00052  * @see set_debounce_us() To change the sampling frequency.
00053  * @see set_samples() To alter the number of samples.
00054  *
00055  * Users of this library may also be interested in PinDetect library:-
00056  * @see http://mbed.org/users/AjK/libraries/PinDetect/latest
00057  *
00058  * This example shows one input displayed by two outputs. The input
00059  * is debounced by the default 10ms.
00060  */
00061  
00062 class ExioMcp23s17DebounceIn : public ExioMcp23s17DigitalIn {
00063     public:
00064     
00065         /** set_debounce_us
00066          *
00067          * Sets the debounce sample period time in microseconds, default is 1000 (1ms)
00068          *
00069          * @param int i The debounce sample period time to set.
00070          */        
00071         void set_debounce_us(int i) { _ticker.attach_us(this, &ExioMcp23s17DebounceIn::_callback, i); }
00072         
00073         /** set_samples
00074          *
00075          * Defines the number of samples before switching the shadow 
00076          * definition of the pin. 
00077          *
00078          * @param int i The number of samples.
00079          */        
00080         void set_samples(int i) { _samples = i; }
00081         
00082         /** read
00083          *
00084          * Read the value of the debounced pin.
00085          */
00086         int read(void) { return _shadow; }
00087         
00088 #ifdef MBED_OPERATORS
00089         /** operator int()
00090          *
00091          * Read the value of the debounced pin.
00092          */
00093         operator int() { return read(); }
00094 #endif  
00095  
00096         /** Constructor
00097          * 
00098          * @param _pDevice The ExioMcp23s17 object
00099          * @param _port The port
00100          * @param _pin The pin number
00101          */
00102         ExioMcp23s17DebounceIn(ExioMcp23s17* _pDevice, ExioPort _port, int _pin) : 
00103             ExioMcp23s17DigitalIn(_pDevice, _port, _pin)
00104         {
00105              _counter = 0;
00106              _samples = 10;
00107              // set_debounce_us(1000);
00108          };
00109         
00110     protected:
00111         void _callback(void) { 
00112             if (ExioMcp23s17DigitalIn::read()) { 
00113                 if (_counter < _samples) _counter++; 
00114                 if (_counter == _samples) _shadow = 1; 
00115             }
00116             else { 
00117                 if (_counter > 0) _counter--; 
00118                 if (_counter == 0) _shadow = 0; 
00119             }
00120         }
00121         
00122         Ticker _ticker;
00123         int    _shadow;
00124         int    _counter;
00125         int    _samples;
00126 };
00127  
00128 #endif
00129