Types a key based on location in array on board.

Fork of PinDetect_KL25Z by Bjoern Hartmann

Committer:
kcparashar
Date:
Sun Sep 13 00:57:13 2015 +0000
Revision:
5:330e56c45ad5
Parent:
4:4f11ae3737c7
Done;

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;
bjo3rn 4:4f11ae3737c7 143 bool _holdRepeat;
AjK 1:611a8f5ac65c 144 FunctionPointer _callbackAsserted;
AjK 1:611a8f5ac65c 145 FunctionPointer _callbackDeasserted;
AjK 1:611a8f5ac65c 146 FunctionPointer _callbackAssertedHeld;
AjK 1:611a8f5ac65c 147 FunctionPointer _callbackDeassertedHeld;
AjK 1:611a8f5ac65c 148
AjK 1:611a8f5ac65c 149 /** initialise class
AjK 1:611a8f5ac65c 150 *
AjK 1:611a8f5ac65c 151 * @param PinName p is a valid pin that supports DigitalIn
AjK 1:611a8f5ac65c 152 * @param PinMode m The mode the DigitalIn should use.
AjK 1:611a8f5ac65c 153 */
AjK 1:611a8f5ac65c 154 void init(PinName p, PinMode m) {
AjK 1:611a8f5ac65c 155 _sampleTime = PINDETECT_SAMPLE_PERIOD;
AjK 1:611a8f5ac65c 156 _samplesTillAssert = PINDETECT_ASSERT_COUNT;
AjK 1:611a8f5ac65c 157 _samplesTillHeld = 0;
AjK 1:611a8f5ac65c 158 _samplesTillAssertReload = PINDETECT_ASSERT_COUNT;
AjK 1:611a8f5ac65c 159 _samplesTillHeldReload = PINDETECT_HOLD_COUNT;
AjK 1:611a8f5ac65c 160 _assertValue = PINDETECT_PIN_ASSTERED;
bjo3rn 4:4f11ae3737c7 161 _holdRepeat = false;
AjK 1:611a8f5ac65c 162 _in = new DigitalIn( p );
AjK 1:611a8f5ac65c 163 _in->mode( m );
AjK 1:611a8f5ac65c 164 _prevState = _in->read();
AjK 1:611a8f5ac65c 165 _ticker = new Ticker;
AjK 1:611a8f5ac65c 166 }
AjK 1:611a8f5ac65c 167
AjK 0:4f4ccb203a70 168 public:
AjK 0:4f4ccb203a70 169
AjK 2:cb3afc45028b 170 friend class Ticker;
AjK 2:cb3afc45028b 171
AjK 0:4f4ccb203a70 172 PinDetect() { error("You must supply a PinName"); }
AjK 0:4f4ccb203a70 173
AjK 0:4f4ccb203a70 174 /** PinDetect constructor
AjK 0:4f4ccb203a70 175 *
bjo3rn 3:2cc013fd659d 176 * By default the PinMode is set to PullDefault.
AjK 1:611a8f5ac65c 177 *
AjK 0:4f4ccb203a70 178 * @see http://mbed.org/handbook/DigitalIn
AjK 0:4f4ccb203a70 179 * @param p PinName is a valid pin that supports DigitalIn
AjK 0:4f4ccb203a70 180 */
AjK 0:4f4ccb203a70 181 PinDetect(PinName p) {
kcparashar 5:330e56c45ad5 182 init( p, PullUp );
AjK 1:611a8f5ac65c 183 }
AjK 1:611a8f5ac65c 184
AjK 1:611a8f5ac65c 185 /** PinDetect constructor
AjK 1:611a8f5ac65c 186 *
AjK 1:611a8f5ac65c 187 * @see http://mbed.org/handbook/DigitalIn
AjK 1:611a8f5ac65c 188 * @param PinName p is a valid pin that supports DigitalIn
AjK 1:611a8f5ac65c 189 * @param PinMode m The mode the DigitalIn should use.
AjK 1:611a8f5ac65c 190 */
AjK 1:611a8f5ac65c 191 PinDetect(PinName p, PinMode m) {
AjK 1:611a8f5ac65c 192 init( p, m );
AjK 0:4f4ccb203a70 193 }
AjK 0:4f4ccb203a70 194
AjK 0:4f4ccb203a70 195 /** PinDetect destructor
AjK 0:4f4ccb203a70 196 */
AjK 0:4f4ccb203a70 197 ~PinDetect() {
AjK 1:611a8f5ac65c 198 if ( _ticker ) delete( _ticker );
AjK 1:611a8f5ac65c 199 if ( _in ) delete( _in );
AjK 0:4f4ccb203a70 200 }
AjK 0:4f4ccb203a70 201
bjo3rn 4:4f11ae3737c7 202 /** Set whether hold callback is repeatedly invoked while held.
bjo3rn 4:4f11ae3737c7 203 *
bjo3rn 4:4f11ae3737c7 204 * @param bool true to repeat callback, false to call back only once
bjo3rn 4:4f11ae3737c7 205 */
bjo3rn 4:4f11ae3737c7 206 void setHoldRepeat(bool repeat) {
bjo3rn 4:4f11ae3737c7 207 _holdRepeat = repeat;
bjo3rn 4:4f11ae3737c7 208 }
AjK 0:4f4ccb203a70 209 /** Set the sampling time in microseconds.
AjK 0:4f4ccb203a70 210 *
AjK 0:4f4ccb203a70 211 * @param int The time between pin samples in microseconds.
AjK 0:4f4ccb203a70 212 */
AjK 0:4f4ccb203a70 213 void setSampleFrequency(int i = PINDETECT_SAMPLE_PERIOD) {
AjK 0:4f4ccb203a70 214 _sampleTime = i;
AjK 2:cb3afc45028b 215 _prevState = _in->read();
AjK 1:611a8f5ac65c 216 _ticker->attach_us( this, &PinDetect::isr, _sampleTime );
AjK 0:4f4ccb203a70 217 }
AjK 0:4f4ccb203a70 218
AjK 0:4f4ccb203a70 219 /** Set the value used as assert.
AjK 0:4f4ccb203a70 220 *
AjK 0:4f4ccb203a70 221 * Defaults to 1 (ie if pin == 1 then pin asserted).
AjK 0:4f4ccb203a70 222 *
AjK 0:4f4ccb203a70 223 * @param int New assert value (1 or 0)
AjK 0:4f4ccb203a70 224 */
AjK 0:4f4ccb203a70 225 void setAssertValue (int i = PINDETECT_PIN_ASSTERED) { _assertValue = i & 1; }
AjK 0:4f4ccb203a70 226
AjK 0:4f4ccb203a70 227 /** Set the number of continuous samples until assert assumed.
AjK 0:4f4ccb203a70 228 *
AjK 0:4f4ccb203a70 229 * Defaults to 1 (1 * sample frequency).
AjK 0:4f4ccb203a70 230 *
AjK 0:4f4ccb203a70 231 * @param int The number of continuous samples until assert assumed.
AjK 0:4f4ccb203a70 232 */
AjK 0:4f4ccb203a70 233 void setSamplesTillAssert(int i) { _samplesTillAssertReload = i; }
AjK 0:4f4ccb203a70 234
AjK 0:4f4ccb203a70 235 /** Set the number of continuous samples until held assumed.
AjK 0:4f4ccb203a70 236 *
AjK 0:4f4ccb203a70 237 * Defaults to 50 * sample frequency.
AjK 0:4f4ccb203a70 238 *
AjK 0:4f4ccb203a70 239 * @param int The number of continuous samples until held assumed.
AjK 0:4f4ccb203a70 240 */
AjK 0:4f4ccb203a70 241 void setSamplesTillHeld(int i) { _samplesTillHeldReload = i; }
AjK 0:4f4ccb203a70 242
AjK 0:4f4ccb203a70 243 /** Set the pin mode.
AjK 0:4f4ccb203a70 244 *
AjK 0:4f4ccb203a70 245 * @see http://mbed.org/projects/libraries/api/mbed/trunk/DigitalInOut#DigitalInOut.mode
AjK 0:4f4ccb203a70 246 * @param PinMode m The mode to pass on to the DigitalIn
AjK 0:4f4ccb203a70 247 */
AjK 1:611a8f5ac65c 248 void mode(PinMode m) { _in->mode( m ); }
AjK 0:4f4ccb203a70 249
AjK 0:4f4ccb203a70 250 /** Attach a callback function
AjK 0:4f4ccb203a70 251 *
AjK 0:4f4ccb203a70 252 * @code
AjK 0:4f4ccb203a70 253 *
AjK 0:4f4ccb203a70 254 * DigitalOut led1( LED1 );
AjK 0:4f4ccb203a70 255 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 256 *
AjK 0:4f4ccb203a70 257 * void myCallback( void ) {
AjK 0:4f4ccb203a70 258 * led1 = 1;
AjK 0:4f4ccb203a70 259 * };
AjK 0:4f4ccb203a70 260 *
AjK 0:4f4ccb203a70 261 * main() {
AjK 0:4f4ccb203a70 262 * pin.attach_asserted( &myCallback );
AjK 0:4f4ccb203a70 263 * }
AjK 0:4f4ccb203a70 264 *
AjK 0:4f4ccb203a70 265 * @endcode
AjK 0:4f4ccb203a70 266 *
AjK 0:4f4ccb203a70 267 * Call this function when a pin is asserted.
AjK 0:4f4ccb203a70 268 * @param function A C function pointer
AjK 0:4f4ccb203a70 269 */
AjK 0:4f4ccb203a70 270 void attach_asserted(void (*function)(void)) {
AjK 1:611a8f5ac65c 271 _callbackAsserted.attach( function );
AjK 0:4f4ccb203a70 272 }
AjK 0:4f4ccb203a70 273
AjK 0:4f4ccb203a70 274 /** Attach a callback object/method
AjK 0:4f4ccb203a70 275 *
AjK 0:4f4ccb203a70 276 * @code
AjK 0:4f4ccb203a70 277 *
AjK 0:4f4ccb203a70 278 * class Bar {
AjK 0:4f4ccb203a70 279 * public:
AjK 0:4f4ccb203a70 280 * void myCallback( void ) { led1 = 1; }
AjK 0:4f4ccb203a70 281 * };
AjK 0:4f4ccb203a70 282 *
AjK 0:4f4ccb203a70 283 * DigitalOut led1( LED1 );
AjK 0:4f4ccb203a70 284 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 285 * Bar bar;
AjK 0:4f4ccb203a70 286 *
AjK 0:4f4ccb203a70 287 * main() {
AjK 0:4f4ccb203a70 288 * pin.attach_asserted( &bar, &Bar::myCallback );
AjK 0:4f4ccb203a70 289 * }
AjK 0:4f4ccb203a70 290 *
AjK 0:4f4ccb203a70 291 * @endcode
AjK 0:4f4ccb203a70 292 *
AjK 0:4f4ccb203a70 293 * Call this function when a pin is asserted.
AjK 0:4f4ccb203a70 294 * @param object An object that conatins the callback method.
AjK 0:4f4ccb203a70 295 * @param method The method within the object to call.
AjK 0:4f4ccb203a70 296 */
AjK 0:4f4ccb203a70 297 template<typename T>
AjK 0:4f4ccb203a70 298 void attach_asserted(T *object, void (T::*member)(void)) {
AjK 1:611a8f5ac65c 299 _callbackAsserted.attach( object, member );
AjK 0:4f4ccb203a70 300 }
AjK 0:4f4ccb203a70 301
AjK 0:4f4ccb203a70 302 /** Attach a callback function
AjK 0:4f4ccb203a70 303 *
AjK 0:4f4ccb203a70 304 * @code
AjK 0:4f4ccb203a70 305 *
AjK 0:4f4ccb203a70 306 * DigitalOut led1( LED1 );
AjK 0:4f4ccb203a70 307 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 308 *
AjK 0:4f4ccb203a70 309 * void myCallback( void ) {
AjK 0:4f4ccb203a70 310 * led1 = 0;
AjK 0:4f4ccb203a70 311 * };
AjK 0:4f4ccb203a70 312 *
AjK 0:4f4ccb203a70 313 * main() {
AjK 0:4f4ccb203a70 314 * pin.attach_deasserted( &myCallback );
AjK 0:4f4ccb203a70 315 * }
AjK 0:4f4ccb203a70 316 *
AjK 0:4f4ccb203a70 317 * @endcode
AjK 0:4f4ccb203a70 318 *
AjK 0:4f4ccb203a70 319 * Call this function when a pin is deasserted.
AjK 0:4f4ccb203a70 320 * @param function A C function pointer
AjK 0:4f4ccb203a70 321 */
AjK 0:4f4ccb203a70 322 void attach_deasserted(void (*function)(void)) {
AjK 1:611a8f5ac65c 323 _callbackDeasserted.attach( function );
AjK 0:4f4ccb203a70 324 }
AjK 0:4f4ccb203a70 325
AjK 0:4f4ccb203a70 326 /** Attach a callback object/method
AjK 0:4f4ccb203a70 327 *
AjK 0:4f4ccb203a70 328 * @code
AjK 0:4f4ccb203a70 329 *
AjK 0:4f4ccb203a70 330 * class Bar {
AjK 0:4f4ccb203a70 331 * public:
AjK 0:4f4ccb203a70 332 * void myCallback( void ) { led1 = 0; }
AjK 0:4f4ccb203a70 333 * };
AjK 0:4f4ccb203a70 334 *
AjK 0:4f4ccb203a70 335 * DigitalOut led1( LED1 );
AjK 0:4f4ccb203a70 336 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 337 * Bar bar;
AjK 0:4f4ccb203a70 338 *
AjK 0:4f4ccb203a70 339 * main() {
AjK 0:4f4ccb203a70 340 * pin.attach_deasserted( &bar, &Bar::myCallback );
AjK 0:4f4ccb203a70 341 * }
AjK 0:4f4ccb203a70 342 *
AjK 0:4f4ccb203a70 343 * @endcode
AjK 0:4f4ccb203a70 344 *
AjK 0:4f4ccb203a70 345 * Call this function when a pin is deasserted.
AjK 0:4f4ccb203a70 346 * @param object An object that conatins the callback method.
AjK 0:4f4ccb203a70 347 * @param method The method within the object to call.
AjK 0:4f4ccb203a70 348 */
AjK 0:4f4ccb203a70 349 template<typename T>
AjK 0:4f4ccb203a70 350 void attach_deasserted(T *object, void (T::*member)(void)) {
AjK 1:611a8f5ac65c 351 _callbackDeasserted.attach( object, member );
AjK 0:4f4ccb203a70 352 }
AjK 0:4f4ccb203a70 353
AjK 0:4f4ccb203a70 354 /** Attach a callback function
AjK 0:4f4ccb203a70 355 *
AjK 0:4f4ccb203a70 356 * @code
AjK 0:4f4ccb203a70 357 *
AjK 0:4f4ccb203a70 358 * DigitalOut led2( LED2 );
AjK 0:4f4ccb203a70 359 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 360 *
AjK 0:4f4ccb203a70 361 * void myCallback( void ) {
AjK 0:4f4ccb203a70 362 * led2 = 1;
AjK 0:4f4ccb203a70 363 * };
AjK 0:4f4ccb203a70 364 *
AjK 0:4f4ccb203a70 365 * main() {
AjK 0:4f4ccb203a70 366 * pin.attach_asserted_held( &myCallback );
AjK 0:4f4ccb203a70 367 * }
AjK 0:4f4ccb203a70 368 *
AjK 0:4f4ccb203a70 369 * @endcode
AjK 0:4f4ccb203a70 370 *
AjK 0:4f4ccb203a70 371 * Call this function when a pin is asserted and held.
AjK 0:4f4ccb203a70 372 * @param function A C function pointer
AjK 0:4f4ccb203a70 373 */
AjK 0:4f4ccb203a70 374 void attach_asserted_held(void (*function)(void)) {
AjK 1:611a8f5ac65c 375 _callbackAssertedHeld.attach( function );
AjK 0:4f4ccb203a70 376 }
AjK 0:4f4ccb203a70 377
AjK 0:4f4ccb203a70 378 /** Attach a callback object/method
AjK 0:4f4ccb203a70 379 *
AjK 0:4f4ccb203a70 380 * @code
AjK 0:4f4ccb203a70 381 *
AjK 0:4f4ccb203a70 382 * class Bar {
AjK 0:4f4ccb203a70 383 * public:
AjK 0:4f4ccb203a70 384 * void myCallback( void ) { led2 = 0; }
AjK 0:4f4ccb203a70 385 * };
AjK 0:4f4ccb203a70 386 *
AjK 0:4f4ccb203a70 387 * DigitalOut led2( LED2 );
AjK 0:4f4ccb203a70 388 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 389 * Bar bar;
AjK 0:4f4ccb203a70 390 *
AjK 0:4f4ccb203a70 391 * main() {
AjK 0:4f4ccb203a70 392 * pin.attach_asserted_held( &bar, &Bar::myCallback );
AjK 0:4f4ccb203a70 393 * }
AjK 0:4f4ccb203a70 394 *
AjK 0:4f4ccb203a70 395 * @endcode
AjK 0:4f4ccb203a70 396 *
AjK 0:4f4ccb203a70 397 * Call this function when a pin is asserted and held.
AjK 0:4f4ccb203a70 398 * @param object An object that conatins the callback method.
AjK 0:4f4ccb203a70 399 * @param method The method within the object to call.
AjK 0:4f4ccb203a70 400 */
AjK 0:4f4ccb203a70 401 template<typename T>
AjK 0:4f4ccb203a70 402 void attach_asserted_held(T *object, void (T::*member)(void)) {
AjK 1:611a8f5ac65c 403 _callbackAssertedHeld.attach( object, member );
AjK 0:4f4ccb203a70 404 }
AjK 0:4f4ccb203a70 405
AjK 0:4f4ccb203a70 406 /** Attach a callback function
AjK 0:4f4ccb203a70 407 *
AjK 0:4f4ccb203a70 408 * @code
AjK 0:4f4ccb203a70 409 *
AjK 0:4f4ccb203a70 410 * DigitalOut led3( LED3 );
AjK 0:4f4ccb203a70 411 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 412 *
AjK 0:4f4ccb203a70 413 * void myCallback( void ) {
AjK 0:4f4ccb203a70 414 * led3 = 1;
AjK 0:4f4ccb203a70 415 * };
AjK 0:4f4ccb203a70 416 *
AjK 0:4f4ccb203a70 417 * main() {
AjK 0:4f4ccb203a70 418 * pin.attach_deasserted_held( &myCallback );
AjK 0:4f4ccb203a70 419 * }
AjK 0:4f4ccb203a70 420 *
AjK 0:4f4ccb203a70 421 * @endcode
AjK 0:4f4ccb203a70 422 *
AjK 0:4f4ccb203a70 423 * Call this function when a pin is deasserted and held.
AjK 0:4f4ccb203a70 424 * @param function A C function pointer
AjK 0:4f4ccb203a70 425 */
AjK 0:4f4ccb203a70 426 void attach_deasserted_held(void (*function)(void)) {
AjK 1:611a8f5ac65c 427 _callbackDeassertedHeld.attach( function );
AjK 0:4f4ccb203a70 428 }
AjK 0:4f4ccb203a70 429
AjK 0:4f4ccb203a70 430 /** Attach a callback object/method
AjK 0:4f4ccb203a70 431 *
AjK 0:4f4ccb203a70 432 * @code
AjK 0:4f4ccb203a70 433 *
AjK 0:4f4ccb203a70 434 * class Bar {
AjK 0:4f4ccb203a70 435 * public:
AjK 0:4f4ccb203a70 436 * void myCallback( void ) { led3 = 0; }
AjK 0:4f4ccb203a70 437 * };
AjK 0:4f4ccb203a70 438 *
AjK 0:4f4ccb203a70 439 * DigitalOut led3( LED3 );
AjK 0:4f4ccb203a70 440 * PinDetect pin( p30 );
AjK 0:4f4ccb203a70 441 * Bar bar;
AjK 0:4f4ccb203a70 442 *
AjK 0:4f4ccb203a70 443 * main() {
AjK 0:4f4ccb203a70 444 * pin.attach_deasserted_held( &bar, &Bar::myCallback );
AjK 0:4f4ccb203a70 445 * }
AjK 0:4f4ccb203a70 446 *
AjK 0:4f4ccb203a70 447 * @endcode
AjK 0:4f4ccb203a70 448 *
AjK 0:4f4ccb203a70 449 * Call this function when a pin is deasserted and held.
AjK 0:4f4ccb203a70 450 * @param object An object that conatins the callback method.
AjK 0:4f4ccb203a70 451 * @param method The method within the object to call.
AjK 0:4f4ccb203a70 452 */
AjK 0:4f4ccb203a70 453 template<typename T>
AjK 0:4f4ccb203a70 454 void attach_deasserted_held(T *object, void (T::*member)(void)) {
AjK 1:611a8f5ac65c 455 _callbackDeassertedHeld.attach( object, member );
AjK 0:4f4ccb203a70 456 }
AjK 0:4f4ccb203a70 457
AjK 0:4f4ccb203a70 458 /** operator int()
AjK 0:4f4ccb203a70 459 *
AjK 0:4f4ccb203a70 460 * Read the value of the pin being sampled.
AjK 0:4f4ccb203a70 461 */
AjK 0:4f4ccb203a70 462 operator int() { return _in->read(); }
AjK 2:cb3afc45028b 463
AjK 2:cb3afc45028b 464 protected:
AjK 0:4f4ccb203a70 465 /** The Ticker periodic callback function
AjK 0:4f4ccb203a70 466 */
AjK 0:4f4ccb203a70 467 void isr(void) {
AjK 0:4f4ccb203a70 468 int currentState = _in->read();
AjK 0:4f4ccb203a70 469
AjK 1:611a8f5ac65c 470 if ( currentState != _prevState ) {
AjK 1:611a8f5ac65c 471 if ( _samplesTillAssert == 0 ) {
AjK 0:4f4ccb203a70 472 _prevState = currentState;
AjK 0:4f4ccb203a70 473 _samplesTillHeld = _samplesTillHeldReload;
AjK 1:611a8f5ac65c 474 if ( currentState == _assertValue )
AjK 0:4f4ccb203a70 475 _callbackAsserted.call();
AjK 0:4f4ccb203a70 476 else
AjK 0:4f4ccb203a70 477 _callbackDeasserted.call();
AjK 0:4f4ccb203a70 478 }
AjK 0:4f4ccb203a70 479 else {
AjK 0:4f4ccb203a70 480 _samplesTillAssert--;
AjK 0:4f4ccb203a70 481 }
AjK 0:4f4ccb203a70 482 }
AjK 0:4f4ccb203a70 483 else {
AjK 0:4f4ccb203a70 484 _samplesTillAssert = _samplesTillAssertReload;
AjK 0:4f4ccb203a70 485 }
AjK 0:4f4ccb203a70 486
AjK 1:611a8f5ac65c 487 if ( _samplesTillHeld ) {
AjK 1:611a8f5ac65c 488 if ( _prevState == currentState ) {
AjK 0:4f4ccb203a70 489 _samplesTillHeld--;
AjK 1:611a8f5ac65c 490 if ( _samplesTillHeld == 0 ) {
AjK 1:611a8f5ac65c 491 if ( currentState == _assertValue )
AjK 0:4f4ccb203a70 492 _callbackAssertedHeld.call();
AjK 0:4f4ccb203a70 493 else
AjK 0:4f4ccb203a70 494 _callbackDeassertedHeld.call();
bjo3rn 4:4f11ae3737c7 495 if(_holdRepeat) {
bjo3rn 4:4f11ae3737c7 496 _samplesTillHeld = _samplesTillHeldReload;
bjo3rn 4:4f11ae3737c7 497 }
AjK 0:4f4ccb203a70 498 }
AjK 0:4f4ccb203a70 499 }
AjK 0:4f4ccb203a70 500 else {
AjK 0:4f4ccb203a70 501 _samplesTillHeld = 0;
AjK 0:4f4ccb203a70 502 }
AjK 0:4f4ccb203a70 503 }
AjK 0:4f4ccb203a70 504 }
AjK 0:4f4ccb203a70 505
AjK 0:4f4ccb203a70 506 };
AjK 0:4f4ccb203a70 507
AjK 0:4f4ccb203a70 508 }; // namespace AjK ends.
AjK 0:4f4ccb203a70 509
AjK 0:4f4ccb203a70 510 using namespace AjK;
AjK 0:4f4ccb203a70 511
AjK 0:4f4ccb203a70 512 #endif