PinDetect rework, removed the continuous sampling, using just irq, timers and pin state sequence.

Fork of PinDetect by Andy K

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