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