external UI solar on foils

Dependencies:   mbed

Fork of CAN_module_ext_UI by Dannis Brugman

Committer:
Dannis_mbed
Date:
Tue Aug 11 08:46:16 2015 +0000
Revision:
0:877c5ece70ac
presentation version

Who changed what in which revision?

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