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