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