InterruptIn style DigitalIn debounced with callbacks for pin state change and pin state hold.

Dependents:   Programa_juego_info

Fork of PinDetect by Andy K

Committer:
rodomnz
Date:
Sun May 11 04:30:51 2014 +0000
Revision:
3:1e646b45d581
Parent:
2:cb3afc45028b
C?digo Para Juego de Aros Ultras?nicos utilizando la tarjeta freedom KL25Z

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:4f4ccb203a70 1 /*
AjK 0:4f4ccb203a70 2 Copyright (c) 2010 Andy Kirkham
AjK 0:4f4ccb203a70 3
rodomnz 3:1e646b45d581 4
rodomnz 3:1e646b45d581 5 Copyright (c) 2010 Andy Kirkham
rodomnz 3:1e646b45d581 6
AjK 0:4f4ccb203a70 7 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 0:4f4ccb203a70 8 of this software and associated documentation files (the "Software"), to deal
AjK 0:4f4ccb203a70 9 in the Software without restriction, including without limitation the rights
AjK 0:4f4ccb203a70 10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 0:4f4ccb203a70 11 copies of the Software, and to permit persons to whom the Software is
AjK 0:4f4ccb203a70 12 furnished to do so, subject to the following conditions:
AjK 0:4f4ccb203a70 13
AjK 0:4f4ccb203a70 14 The above copyright notice and this permission notice shall be included in
AjK 0:4f4ccb203a70 15 all copies or substantial portions of the Software.
AjK 0:4f4ccb203a70 16
AjK 0:4f4ccb203a70 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 0:4f4ccb203a70 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 0:4f4ccb203a70 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 0:4f4ccb203a70 20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 0:4f4ccb203a70 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 0:4f4ccb203a70 22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 0:4f4ccb203a70 23 THE SOFTWARE.
AjK 0:4f4ccb203a70 24 */
AjK 0:4f4ccb203a70 25
AjK 0:4f4ccb203a70 26 #ifndef AJK_PIN_DETECT_H
AjK 0:4f4ccb203a70 27 #define AJK_PIN_DETECT_H
AjK 0:4f4ccb203a70 28
AjK 0:4f4ccb203a70 29 #ifndef MBED_H
AjK 0:4f4ccb203a70 30 #include "mbed.h"
AjK 0:4f4ccb203a70 31 #endif
AjK 0:4f4ccb203a70 32
AjK 0:4f4ccb203a70 33 #ifndef PINDETECT_PIN_ASSTERED
AjK 0:4f4ccb203a70 34 #define PINDETECT_PIN_ASSTERED 1
AjK 0:4f4ccb203a70 35 #endif
AjK 0:4f4ccb203a70 36
AjK 0:4f4ccb203a70 37 #ifndef PINDETECT_SAMPLE_PERIOD
AjK 0:4f4ccb203a70 38 #define PINDETECT_SAMPLE_PERIOD 20000
AjK 0:4f4ccb203a70 39 #endif
AjK 0:4f4ccb203a70 40
AjK 0:4f4ccb203a70 41 #ifndef PINDETECT_ASSERT_COUNT
AjK 0:4f4ccb203a70 42 #define PINDETECT_ASSERT_COUNT 1
AjK 0:4f4ccb203a70 43 #endif
AjK 0:4f4ccb203a70 44
AjK 0:4f4ccb203a70 45 #ifndef PINDETECT_HOLD_COUNT
AjK 0:4f4ccb203a70 46 #define PINDETECT_HOLD_COUNT 50
AjK 0:4f4ccb203a70 47 #endif
AjK 0:4f4ccb203a70 48
AjK 0:4f4ccb203a70 49 namespace AjK {
AjK 0:4f4ccb203a70 50
AjK 0:4f4ccb203a70 51 /** PinDetect adds mechanical switch debouncing to DigitialIn and interrupt callbacks.
AjK 0:4f4ccb203a70 52 *
AjK 0:4f4ccb203a70 53 * This is done by sampling the specified pin at regular intervals and detecting any
AjK 0:4f4ccb203a70 54 * change of state ( 0 -> 1 or 1 -> 0 ). When a state change is detected the attached
AjK 0:4f4ccb203a70 55 * callback handler is called. Additionally, if the pin stays in the same state after
AjK 0:4f4ccb203a70 56 * a state change for a defined period of time, an extra callback is made allowing a
AjK 0:4f4ccb203a70 57 * program to detect when a "key is pressed and held down" rather than a momentary
AjK 0:4f4ccb203a70 58 * key/switch press.
AjK 0:4f4ccb203a70 59 *
AjK 0:4f4ccb203a70 60 * All parameters are customisable which include:-
AjK 0:4f4ccb203a70 61 * <ul>
AjK 0:4f4ccb203a70 62 * <li> The sampling frequency. </li>
AjK 0:4f4ccb203a70 63 * <li> The number of continuous samples until a state change is detected. </li>
AjK 0:4f4ccb203a70 64 * <li> The number of continuous samples until a key is assumed held after a state change. </li>
AjK 0:4f4ccb203a70 65 * <li> The logic level which is assumed to be asserted (0volts or +volts). </li>
AjK 0:4f4ccb203a70 66 * </ul>
AjK 0:4f4ccb203a70 67 *
AjK 0:4f4ccb203a70 68 * Only callbacks that have been attached will be called by the library.
AjK 0:4f4ccb203a70 69 *
AjK 0:4f4ccb203a70 70 * Example:
AjK 0:4f4ccb203a70 71 * @code
AjK 0:4f4ccb203a70 72 * #include "mbed.h"
AjK 0:4f4ccb203a70 73 * #include "PinDetect.h"
AjK 0:4f4ccb203a70 74 *
AjK 0:4f4ccb203a70 75 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 76 * DigitialOut led1( LED1 );
AjK 0:4f4ccb203a70 77 * DigitialOut led2( LED2 );
AjK 0:4f4ccb203a70 78 * DigitialOut led3( LED3 );
AjK 0:4f4ccb203a70 79 * DigitialOut led4( LED4 );
AjK 0:4f4ccb203a70 80 *
AjK 0:4f4ccb203a70 81 * void keyPressed( void ) {
AjK 0:4f4ccb203a70 82 * led2 = 1;
AjK 0:4f4ccb203a70 83 * led3 = 0;
AjK 0:4f4ccb203a70 84 * led4 = 0;
AjK 0:4f4ccb203a70 85 * }
AjK 0:4f4ccb203a70 86 *
AjK 0:4f4ccb203a70 87 * void keyReleased( void ) {
AjK 0:4f4ccb203a70 88 * led2 = 0;
AjK 0:4f4ccb203a70 89 * led3 = 0;
AjK 0:4f4ccb203a70 90 * led4 = 0;
AjK 0:4f4ccb203a70 91 * }
AjK 0:4f4ccb203a70 92 *
AjK 0:4f4ccb203a70 93 * void keyPressedHeld( void ) {
AjK 0:4f4ccb203a70 94 * led3 = 1;
AjK 0:4f4ccb203a70 95 * }
AjK 0:4f4ccb203a70 96 *
AjK 0:4f4ccb203a70 97 * void keyReleasedHeld( void ) {
AjK 0:4f4ccb203a70 98 * led4 = 1;
AjK 0:4f4ccb203a70 99 * }
AjK 0:4f4ccb203a70 100 *
AjK 0:4f4ccb203a70 101 * int main() {
AjK 0:4f4ccb203a70 102 *
AjK 0:4f4ccb203a70 103 * pin.mode( PullDown );
AjK 0:4f4ccb203a70 104 * pin.attach_asserted( &keyPressed );
AjK 0:4f4ccb203a70 105 * pin.attach_deasserted( &keyReleased );
AjK 0:4f4ccb203a70 106 * pin.attach_asserted_held( &keyPressedHeld );
AjK 0:4f4ccb203a70 107 * pin.attach_deasserted_held( &keyReleasedHeld );
AjK 0:4f4ccb203a70 108 *
AjK 0:4f4ccb203a70 109 * // Sampling does not begin until you set a frequency.
AjK 0:4f4ccb203a70 110 * // The default is 20ms. If you want a different frequency
AjK 0:4f4ccb203a70 111 * // then pass the period in microseconds for example, for 10ms :-
AjK 0:4f4ccb203a70 112 * // pin.setSampleFrequency( 10000 );
AjK 0:4f4ccb203a70 113 * //
AjK 0:4f4ccb203a70 114 * pin.setSampleFrequency(); // Defaults to 20ms.
AjK 0:4f4ccb203a70 115 *
AjK 0:4f4ccb203a70 116 * while( 1 ) {
AjK 0:4f4ccb203a70 117 * led1 = !led1;
AjK 0:4f4ccb203a70 118 * wait( 0.2 );
AjK 0:4f4ccb203a70 119 * }
AjK 0:4f4ccb203a70 120 * }
AjK 0:4f4ccb203a70 121 * @endcode
AjK 0:4f4ccb203a70 122 *
AjK 0:4f4ccb203a70 123 * This example will flash led1 in a similar to a standard starting program.
AjK 0:4f4ccb203a70 124 *
AjK 0:4f4ccb203a70 125 * Applying a "1" (switch on) to pin 30 will switch on led2, removing the "1" to "0"
AjK 0:4f4ccb203a70 126 * (switch off) led2 goes out. Holding the "switch" at one for one second will switch
AjK 0:4f4ccb203a70 127 * on led3. An unasserted P30 (switched off) will, after one second illuminate led4
AjK 0:4f4ccb203a70 128 * when the deasserted calledback is called.
AjK 0:4f4ccb203a70 129 *
AjK 0:4f4ccb203a70 130 * The above is a very basic introduction. For more details:-
AjK 0:4f4ccb203a70 131 * @see example.h
AjK 0:4f4ccb203a70 132 */
AjK 0:4f4ccb203a70 133 class PinDetect {
AjK 1:611a8f5ac65c 134
AjK 1:611a8f5ac65c 135 protected:
AjK 1:611a8f5ac65c 136 DigitalIn *_in;
AjK 1:611a8f5ac65c 137 Ticker *_ticker;
AjK 1:611a8f5ac65c 138 int _prevState;
AjK 1:611a8f5ac65c 139 int _currentStateCounter;
AjK 1:611a8f5ac65c 140 int _sampleTime;
AjK 1:611a8f5ac65c 141 int _assertValue;
AjK 1:611a8f5ac65c 142 int _samplesTillAssertReload;
AjK 1:611a8f5ac65c 143 int _samplesTillAssert;
AjK 1:611a8f5ac65c 144 int _samplesTillHeldReload;
AjK 1:611a8f5ac65c 145 int _samplesTillHeld;
AjK 1:611a8f5ac65c 146 FunctionPointer _callbackAsserted;
AjK 1:611a8f5ac65c 147 FunctionPointer _callbackDeasserted;
AjK 1:611a8f5ac65c 148 FunctionPointer _callbackAssertedHeld;
AjK 1:611a8f5ac65c 149 FunctionPointer _callbackDeassertedHeld;
AjK 1:611a8f5ac65c 150
AjK 1:611a8f5ac65c 151 /** initialise class
AjK 1:611a8f5ac65c 152 *
AjK 1:611a8f5ac65c 153 * @param PinName p is a valid pin that supports DigitalIn
AjK 1:611a8f5ac65c 154 * @param PinMode m The mode the DigitalIn should use.
AjK 1:611a8f5ac65c 155 */
AjK 1:611a8f5ac65c 156 void init(PinName p, PinMode m) {
AjK 1:611a8f5ac65c 157 _sampleTime = PINDETECT_SAMPLE_PERIOD;
AjK 1:611a8f5ac65c 158 _samplesTillAssert = PINDETECT_ASSERT_COUNT;
AjK 1:611a8f5ac65c 159 _samplesTillHeld = 0;
AjK 1:611a8f5ac65c 160 _samplesTillAssertReload = PINDETECT_ASSERT_COUNT;
AjK 1:611a8f5ac65c 161 _samplesTillHeldReload = PINDETECT_HOLD_COUNT;
AjK 1:611a8f5ac65c 162 _assertValue = PINDETECT_PIN_ASSTERED;
AjK 1:611a8f5ac65c 163
AjK 1:611a8f5ac65c 164 _in = new DigitalIn( p );
AjK 1:611a8f5ac65c 165 _in->mode( m );
AjK 1:611a8f5ac65c 166 _prevState = _in->read();
AjK 1:611a8f5ac65c 167 _ticker = new Ticker;
AjK 1:611a8f5ac65c 168 }
AjK 1:611a8f5ac65c 169
AjK 0:4f4ccb203a70 170 public:
AjK 0:4f4ccb203a70 171
AjK 2:cb3afc45028b 172 friend class Ticker;
AjK 2:cb3afc45028b 173
AjK 0:4f4ccb203a70 174 PinDetect() { error("You must supply a PinName"); }
AjK 0:4f4ccb203a70 175
AjK 0:4f4ccb203a70 176 /** PinDetect constructor
AjK 0:4f4ccb203a70 177 *
AjK 1:611a8f5ac65c 178 * By default the PinMode is set to PullDown.
AjK 1:611a8f5ac65c 179 *
AjK 0:4f4ccb203a70 180 * @see http://mbed.org/handbook/DigitalIn
AjK 0:4f4ccb203a70 181 * @param p PinName is a valid pin that supports DigitalIn
AjK 0:4f4ccb203a70 182 */
rodomnz 3:1e646b45d581 183 PinDetect(PinName p, PinMode m) {
rodomnz 3:1e646b45d581 184 init( p, PullUp );
AjK 1:611a8f5ac65c 185 }
AjK 1:611a8f5ac65c 186
AjK 1:611a8f5ac65c 187 /** PinDetect constructor
AjK 1:611a8f5ac65c 188 *
AjK 1:611a8f5ac65c 189 * @see http://mbed.org/handbook/DigitalIn
AjK 1:611a8f5ac65c 190 * @param PinName p is a valid pin that supports DigitalIn
AjK 1:611a8f5ac65c 191 * @param PinMode m The mode the DigitalIn should use.
AjK 1:611a8f5ac65c 192 */
rodomnz 3:1e646b45d581 193 PinDetect(PinName p) {
rodomnz 3:1e646b45d581 194 init( p, PullUp );
AjK 0:4f4ccb203a70 195 }
AjK 0:4f4ccb203a70 196
AjK 0:4f4ccb203a70 197 /** PinDetect destructor
AjK 0:4f4ccb203a70 198 */
AjK 0:4f4ccb203a70 199 ~PinDetect() {
AjK 1:611a8f5ac65c 200 if ( _ticker ) delete( _ticker );
AjK 1:611a8f5ac65c 201 if ( _in ) delete( _in );
AjK 0:4f4ccb203a70 202 }
AjK 0:4f4ccb203a70 203
AjK 0:4f4ccb203a70 204 /** Set the sampling time in microseconds.
AjK 0:4f4ccb203a70 205 *
AjK 0:4f4ccb203a70 206 * @param int The time between pin samples in microseconds.
AjK 0:4f4ccb203a70 207 */
AjK 0:4f4ccb203a70 208 void setSampleFrequency(int i = PINDETECT_SAMPLE_PERIOD) {
AjK 0:4f4ccb203a70 209 _sampleTime = i;
AjK 2:cb3afc45028b 210 _prevState = _in->read();
AjK 1:611a8f5ac65c 211 _ticker->attach_us( this, &PinDetect::isr, _sampleTime );
AjK 0:4f4ccb203a70 212 }
AjK 0:4f4ccb203a70 213
AjK 0:4f4ccb203a70 214 /** Set the value used as assert.
AjK 0:4f4ccb203a70 215 *
AjK 0:4f4ccb203a70 216 * Defaults to 1 (ie if pin == 1 then pin asserted).
AjK 0:4f4ccb203a70 217 *
AjK 0:4f4ccb203a70 218 * @param int New assert value (1 or 0)
AjK 0:4f4ccb203a70 219 */
AjK 0:4f4ccb203a70 220 void setAssertValue (int i = PINDETECT_PIN_ASSTERED) { _assertValue = i & 1; }
AjK 0:4f4ccb203a70 221
AjK 0:4f4ccb203a70 222 /** Set the number of continuous samples until assert assumed.
AjK 0:4f4ccb203a70 223 *
AjK 0:4f4ccb203a70 224 * Defaults to 1 (1 * sample frequency).
AjK 0:4f4ccb203a70 225 *
AjK 0:4f4ccb203a70 226 * @param int The number of continuous samples until assert assumed.
AjK 0:4f4ccb203a70 227 */
AjK 0:4f4ccb203a70 228 void setSamplesTillAssert(int i) { _samplesTillAssertReload = i; }
AjK 0:4f4ccb203a70 229
AjK 0:4f4ccb203a70 230 /** Set the number of continuous samples until held assumed.
AjK 0:4f4ccb203a70 231 *
AjK 0:4f4ccb203a70 232 * Defaults to 50 * sample frequency.
AjK 0:4f4ccb203a70 233 *
AjK 0:4f4ccb203a70 234 * @param int The number of continuous samples until held assumed.
AjK 0:4f4ccb203a70 235 */
AjK 0:4f4ccb203a70 236 void setSamplesTillHeld(int i) { _samplesTillHeldReload = i; }
AjK 0:4f4ccb203a70 237
AjK 0:4f4ccb203a70 238 /** Set the pin mode.
AjK 0:4f4ccb203a70 239 *
AjK 0:4f4ccb203a70 240 * @see http://mbed.org/projects/libraries/api/mbed/trunk/DigitalInOut#DigitalInOut.mode
AjK 0:4f4ccb203a70 241 * @param PinMode m The mode to pass on to the DigitalIn
AjK 0:4f4ccb203a70 242 */
AjK 1:611a8f5ac65c 243 void mode(PinMode m) { _in->mode( m ); }
AjK 0:4f4ccb203a70 244
AjK 0:4f4ccb203a70 245 /** Attach a callback function
AjK 0:4f4ccb203a70 246 *
AjK 0:4f4ccb203a70 247 * @code
AjK 0:4f4ccb203a70 248 *
AjK 0:4f4ccb203a70 249 * DigitalOut led1( LED1 );
AjK 0:4f4ccb203a70 250 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 251 *
AjK 0:4f4ccb203a70 252 * void myCallback( void ) {
AjK 0:4f4ccb203a70 253 * led1 = 1;
AjK 0:4f4ccb203a70 254 * };
AjK 0:4f4ccb203a70 255 *
AjK 0:4f4ccb203a70 256 * main() {
AjK 0:4f4ccb203a70 257 * pin.attach_asserted( &myCallback );
AjK 0:4f4ccb203a70 258 * }
AjK 0:4f4ccb203a70 259 *
AjK 0:4f4ccb203a70 260 * @endcode
AjK 0:4f4ccb203a70 261 *
AjK 0:4f4ccb203a70 262 * Call this function when a pin is asserted.
AjK 0:4f4ccb203a70 263 * @param function A C function pointer
AjK 0:4f4ccb203a70 264 */
AjK 0:4f4ccb203a70 265 void attach_asserted(void (*function)(void)) {
AjK 1:611a8f5ac65c 266 _callbackAsserted.attach( function );
AjK 0:4f4ccb203a70 267 }
AjK 0:4f4ccb203a70 268
AjK 0:4f4ccb203a70 269 /** Attach a callback object/method
AjK 0:4f4ccb203a70 270 *
AjK 0:4f4ccb203a70 271 * @code
AjK 0:4f4ccb203a70 272 *
AjK 0:4f4ccb203a70 273 * class Bar {
AjK 0:4f4ccb203a70 274 * public:
AjK 0:4f4ccb203a70 275 * void myCallback( void ) { led1 = 1; }
AjK 0:4f4ccb203a70 276 * };
AjK 0:4f4ccb203a70 277 *
AjK 0:4f4ccb203a70 278 * DigitalOut led1( LED1 );
AjK 0:4f4ccb203a70 279 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 280 * Bar bar;
AjK 0:4f4ccb203a70 281 *
AjK 0:4f4ccb203a70 282 * main() {
AjK 0:4f4ccb203a70 283 * pin.attach_asserted( &bar, &Bar::myCallback );
AjK 0:4f4ccb203a70 284 * }
AjK 0:4f4ccb203a70 285 *
AjK 0:4f4ccb203a70 286 * @endcode
AjK 0:4f4ccb203a70 287 *
AjK 0:4f4ccb203a70 288 * Call this function when a pin is asserted.
AjK 0:4f4ccb203a70 289 * @param object An object that conatins the callback method.
AjK 0:4f4ccb203a70 290 * @param method The method within the object to call.
AjK 0:4f4ccb203a70 291 */
AjK 0:4f4ccb203a70 292 template<typename T>
AjK 0:4f4ccb203a70 293 void attach_asserted(T *object, void (T::*member)(void)) {
AjK 1:611a8f5ac65c 294 _callbackAsserted.attach( object, member );
AjK 0:4f4ccb203a70 295 }
AjK 0:4f4ccb203a70 296
AjK 0:4f4ccb203a70 297 /** Attach a callback function
AjK 0:4f4ccb203a70 298 *
AjK 0:4f4ccb203a70 299 * @code
AjK 0:4f4ccb203a70 300 *
AjK 0:4f4ccb203a70 301 * DigitalOut led1( LED1 );
AjK 0:4f4ccb203a70 302 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 303 *
AjK 0:4f4ccb203a70 304 * void myCallback( void ) {
AjK 0:4f4ccb203a70 305 * led1 = 0;
AjK 0:4f4ccb203a70 306 * };
AjK 0:4f4ccb203a70 307 *
AjK 0:4f4ccb203a70 308 * main() {
AjK 0:4f4ccb203a70 309 * pin.attach_deasserted( &myCallback );
AjK 0:4f4ccb203a70 310 * }
AjK 0:4f4ccb203a70 311 *
AjK 0:4f4ccb203a70 312 * @endcode
AjK 0:4f4ccb203a70 313 *
AjK 0:4f4ccb203a70 314 * Call this function when a pin is deasserted.
AjK 0:4f4ccb203a70 315 * @param function A C function pointer
AjK 0:4f4ccb203a70 316 */
AjK 0:4f4ccb203a70 317 void attach_deasserted(void (*function)(void)) {
AjK 1:611a8f5ac65c 318 _callbackDeasserted.attach( function );
AjK 0:4f4ccb203a70 319 }
AjK 0:4f4ccb203a70 320
AjK 0:4f4ccb203a70 321 /** Attach a callback object/method
AjK 0:4f4ccb203a70 322 *
AjK 0:4f4ccb203a70 323 * @code
AjK 0:4f4ccb203a70 324 *
AjK 0:4f4ccb203a70 325 * class Bar {
AjK 0:4f4ccb203a70 326 * public:
AjK 0:4f4ccb203a70 327 * void myCallback( void ) { led1 = 0; }
AjK 0:4f4ccb203a70 328 * };
AjK 0:4f4ccb203a70 329 *
AjK 0:4f4ccb203a70 330 * DigitalOut led1( LED1 );
AjK 0:4f4ccb203a70 331 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 332 * Bar bar;
AjK 0:4f4ccb203a70 333 *
AjK 0:4f4ccb203a70 334 * main() {
AjK 0:4f4ccb203a70 335 * pin.attach_deasserted( &bar, &Bar::myCallback );
AjK 0:4f4ccb203a70 336 * }
AjK 0:4f4ccb203a70 337 *
AjK 0:4f4ccb203a70 338 * @endcode
AjK 0:4f4ccb203a70 339 *
AjK 0:4f4ccb203a70 340 * Call this function when a pin is deasserted.
AjK 0:4f4ccb203a70 341 * @param object An object that conatins the callback method.
AjK 0:4f4ccb203a70 342 * @param method The method within the object to call.
AjK 0:4f4ccb203a70 343 */
AjK 0:4f4ccb203a70 344 template<typename T>
AjK 0:4f4ccb203a70 345 void attach_deasserted(T *object, void (T::*member)(void)) {
AjK 1:611a8f5ac65c 346 _callbackDeasserted.attach( object, member );
AjK 0:4f4ccb203a70 347 }
AjK 0:4f4ccb203a70 348
AjK 0:4f4ccb203a70 349 /** Attach a callback function
AjK 0:4f4ccb203a70 350 *
AjK 0:4f4ccb203a70 351 * @code
AjK 0:4f4ccb203a70 352 *
AjK 0:4f4ccb203a70 353 * DigitalOut led2( LED2 );
AjK 0:4f4ccb203a70 354 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 355 *
AjK 0:4f4ccb203a70 356 * void myCallback( void ) {
AjK 0:4f4ccb203a70 357 * led2 = 1;
AjK 0:4f4ccb203a70 358 * };
AjK 0:4f4ccb203a70 359 *
AjK 0:4f4ccb203a70 360 * main() {
AjK 0:4f4ccb203a70 361 * pin.attach_asserted_held( &myCallback );
AjK 0:4f4ccb203a70 362 * }
AjK 0:4f4ccb203a70 363 *
AjK 0:4f4ccb203a70 364 * @endcode
AjK 0:4f4ccb203a70 365 *
AjK 0:4f4ccb203a70 366 * Call this function when a pin is asserted and held.
AjK 0:4f4ccb203a70 367 * @param function A C function pointer
AjK 0:4f4ccb203a70 368 */
AjK 0:4f4ccb203a70 369 void attach_asserted_held(void (*function)(void)) {
AjK 1:611a8f5ac65c 370 _callbackAssertedHeld.attach( function );
AjK 0:4f4ccb203a70 371 }
AjK 0:4f4ccb203a70 372
AjK 0:4f4ccb203a70 373 /** Attach a callback object/method
AjK 0:4f4ccb203a70 374 *
AjK 0:4f4ccb203a70 375 * @code
AjK 0:4f4ccb203a70 376 *
AjK 0:4f4ccb203a70 377 * class Bar {
AjK 0:4f4ccb203a70 378 * public:
AjK 0:4f4ccb203a70 379 * void myCallback( void ) { led2 = 0; }
AjK 0:4f4ccb203a70 380 * };
AjK 0:4f4ccb203a70 381 *
AjK 0:4f4ccb203a70 382 * DigitalOut led2( LED2 );
AjK 0:4f4ccb203a70 383 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 384 * Bar bar;
AjK 0:4f4ccb203a70 385 *
AjK 0:4f4ccb203a70 386 * main() {
AjK 0:4f4ccb203a70 387 * pin.attach_asserted_held( &bar, &Bar::myCallback );
AjK 0:4f4ccb203a70 388 * }
AjK 0:4f4ccb203a70 389 *
AjK 0:4f4ccb203a70 390 * @endcode
AjK 0:4f4ccb203a70 391 *
AjK 0:4f4ccb203a70 392 * Call this function when a pin is asserted and held.
AjK 0:4f4ccb203a70 393 * @param object An object that conatins the callback method.
AjK 0:4f4ccb203a70 394 * @param method The method within the object to call.
AjK 0:4f4ccb203a70 395 */
AjK 0:4f4ccb203a70 396 template<typename T>
AjK 0:4f4ccb203a70 397 void attach_asserted_held(T *object, void (T::*member)(void)) {
AjK 1:611a8f5ac65c 398 _callbackAssertedHeld.attach( object, member );
AjK 0:4f4ccb203a70 399 }
AjK 0:4f4ccb203a70 400
AjK 0:4f4ccb203a70 401 /** Attach a callback function
AjK 0:4f4ccb203a70 402 *
AjK 0:4f4ccb203a70 403 * @code
AjK 0:4f4ccb203a70 404 *
AjK 0:4f4ccb203a70 405 * DigitalOut led3( LED3 );
AjK 0:4f4ccb203a70 406 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 407 *
AjK 0:4f4ccb203a70 408 * void myCallback( void ) {
AjK 0:4f4ccb203a70 409 * led3 = 1;
AjK 0:4f4ccb203a70 410 * };
AjK 0:4f4ccb203a70 411 *
AjK 0:4f4ccb203a70 412 * main() {
AjK 0:4f4ccb203a70 413 * pin.attach_deasserted_held( &myCallback );
AjK 0:4f4ccb203a70 414 * }
AjK 0:4f4ccb203a70 415 *
AjK 0:4f4ccb203a70 416 * @endcode
AjK 0:4f4ccb203a70 417 *
AjK 0:4f4ccb203a70 418 * Call this function when a pin is deasserted and held.
AjK 0:4f4ccb203a70 419 * @param function A C function pointer
AjK 0:4f4ccb203a70 420 */
AjK 0:4f4ccb203a70 421 void attach_deasserted_held(void (*function)(void)) {
AjK 1:611a8f5ac65c 422 _callbackDeassertedHeld.attach( function );
AjK 0:4f4ccb203a70 423 }
AjK 0:4f4ccb203a70 424
AjK 0:4f4ccb203a70 425 /** Attach a callback object/method
AjK 0:4f4ccb203a70 426 *
AjK 0:4f4ccb203a70 427 * @code
AjK 0:4f4ccb203a70 428 *
AjK 0:4f4ccb203a70 429 * class Bar {
AjK 0:4f4ccb203a70 430 * public:
AjK 0:4f4ccb203a70 431 * void myCallback( void ) { led3 = 0; }
AjK 0:4f4ccb203a70 432 * };
AjK 0:4f4ccb203a70 433 *
AjK 0:4f4ccb203a70 434 * DigitalOut led3( LED3 );
AjK 0:4f4ccb203a70 435 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 436 * Bar bar;
AjK 0:4f4ccb203a70 437 *
AjK 0:4f4ccb203a70 438 * main() {
AjK 0:4f4ccb203a70 439 * pin.attach_deasserted_held( &bar, &Bar::myCallback );
AjK 0:4f4ccb203a70 440 * }
AjK 0:4f4ccb203a70 441 *
AjK 0:4f4ccb203a70 442 * @endcode
AjK 0:4f4ccb203a70 443 *
AjK 0:4f4ccb203a70 444 * Call this function when a pin is deasserted and held.
AjK 0:4f4ccb203a70 445 * @param object An object that conatins the callback method.
AjK 0:4f4ccb203a70 446 * @param method The method within the object to call.
AjK 0:4f4ccb203a70 447 */
AjK 0:4f4ccb203a70 448 template<typename T>
AjK 0:4f4ccb203a70 449 void attach_deasserted_held(T *object, void (T::*member)(void)) {
AjK 1:611a8f5ac65c 450 _callbackDeassertedHeld.attach( object, member );
AjK 0:4f4ccb203a70 451 }
AjK 0:4f4ccb203a70 452
AjK 0:4f4ccb203a70 453 /** operator int()
AjK 0:4f4ccb203a70 454 *
AjK 0:4f4ccb203a70 455 * Read the value of the pin being sampled.
AjK 0:4f4ccb203a70 456 */
AjK 0:4f4ccb203a70 457 operator int() { return _in->read(); }
AjK 2:cb3afc45028b 458
AjK 2:cb3afc45028b 459 protected:
AjK 0:4f4ccb203a70 460 /** The Ticker periodic callback function
AjK 0:4f4ccb203a70 461 */
AjK 0:4f4ccb203a70 462 void isr(void) {
AjK 0:4f4ccb203a70 463 int currentState = _in->read();
AjK 0:4f4ccb203a70 464
AjK 1:611a8f5ac65c 465 if ( currentState != _prevState ) {
AjK 1:611a8f5ac65c 466 if ( _samplesTillAssert == 0 ) {
AjK 0:4f4ccb203a70 467 _prevState = currentState;
AjK 0:4f4ccb203a70 468 _samplesTillHeld = _samplesTillHeldReload;
AjK 1:611a8f5ac65c 469 if ( currentState == _assertValue )
AjK 0:4f4ccb203a70 470 _callbackAsserted.call();
AjK 0:4f4ccb203a70 471 else
AjK 0:4f4ccb203a70 472 _callbackDeasserted.call();
AjK 0:4f4ccb203a70 473 }
AjK 0:4f4ccb203a70 474 else {
AjK 0:4f4ccb203a70 475 _samplesTillAssert--;
AjK 0:4f4ccb203a70 476 }
AjK 0:4f4ccb203a70 477 }
AjK 0:4f4ccb203a70 478 else {
AjK 0:4f4ccb203a70 479 _samplesTillAssert = _samplesTillAssertReload;
AjK 0:4f4ccb203a70 480 }
AjK 0:4f4ccb203a70 481
AjK 1:611a8f5ac65c 482 if ( _samplesTillHeld ) {
AjK 1:611a8f5ac65c 483 if ( _prevState == currentState ) {
AjK 0:4f4ccb203a70 484 _samplesTillHeld--;
AjK 1:611a8f5ac65c 485 if ( _samplesTillHeld == 0 ) {
AjK 1:611a8f5ac65c 486 if ( currentState == _assertValue )
AjK 0:4f4ccb203a70 487 _callbackAssertedHeld.call();
AjK 0:4f4ccb203a70 488 else
AjK 0:4f4ccb203a70 489 _callbackDeassertedHeld.call();
AjK 0:4f4ccb203a70 490 }
AjK 0:4f4ccb203a70 491 }
AjK 0:4f4ccb203a70 492 else {
AjK 0:4f4ccb203a70 493 _samplesTillHeld = 0;
AjK 0:4f4ccb203a70 494 }
AjK 0:4f4ccb203a70 495 }
AjK 0:4f4ccb203a70 496 }
AjK 0:4f4ccb203a70 497
AjK 0:4f4ccb203a70 498 };
AjK 0:4f4ccb203a70 499
AjK 0:4f4ccb203a70 500 }; // namespace AjK ends.
AjK 0:4f4ccb203a70 501
AjK 0:4f4ccb203a70 502 using namespace AjK;
AjK 0:4f4ccb203a70 503
AjK 0:4f4ccb203a70 504 #endif