The program converts input morse code to letters and prints them

Fork of PinDetect by Andy K

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