Menu system broken

Dependencies:   ANSITermMenuSystem

Fork of menuSystemMbed by Ryan Scott

Committer:
Rybowonder
Date:
Fri Apr 26 16:17:18 2013 +0000
Revision:
7:df9f5b6dd4ed
April 26th Need To handle Saved Tracks

Who changed what in which revision?

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