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