Changed default PinMode from PullDown to PullDefault for compatibility with KL25Z

Dependents:   ChordJoy idd_hw2_andrewhead_chordjoy idd_hw2_kevinlee_typinglove idd_hw2_kevinlee_typinglove ... more

Fork of PinDetect by Andy K

Committer:
AjK
Date:
Thu Jan 13 09:53:26 2011 +0000
Revision:
1:611a8f5ac65c
Parent:
0:4f4ccb203a70
Child:
2:cb3afc45028b
1.4 See ChangeLog.h

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