Black Jack game using speaker sound, sd images, and joystick

Dependencies:   4DGL-uLCD-SE mbed

Committer:
najeekitchens
Date:
Wed Mar 09 06:34:54 2016 +0000
Revision:
0:5b2b64f676b6
Only need to create black rectangles over printfs and add all card addresses

Who changed what in which revision?

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