Code to run drink mixer using Windows GUI.

Dependencies:   mbed

Committer:
mabdella3
Date:
Mon Dec 03 16:09:04 2018 +0000
Revision:
0:2f094535a070
Manual only with timing;

Who changed what in which revision?

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