d

Fork of PinDetect by Andy K

Committer:
taylornichols
Date:
Sun Oct 30 01:20:37 2016 +0000
Revision:
3:a4334fba854f
Parent:
1:611a8f5ac65c
l4 all libs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:4f4ccb203a70 1 /*
AjK 0:4f4ccb203a70 2 Copyright (c) 2010 Andy Kirkham
AjK 0:4f4ccb203a70 3
AjK 0:4f4ccb203a70 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 0:4f4ccb203a70 5 of this software and associated documentation files (the "Software"), to deal
AjK 0:4f4ccb203a70 6 in the Software without restriction, including without limitation the rights
AjK 0:4f4ccb203a70 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 0:4f4ccb203a70 8 copies of the Software, and to permit persons to whom the Software is
AjK 0:4f4ccb203a70 9 furnished to do so, subject to the following conditions:
AjK 0:4f4ccb203a70 10
AjK 0:4f4ccb203a70 11 The above copyright notice and this permission notice shall be included in
AjK 0:4f4ccb203a70 12 all copies or substantial portions of the Software.
AjK 0:4f4ccb203a70 13
AjK 0:4f4ccb203a70 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 0:4f4ccb203a70 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 0:4f4ccb203a70 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 0:4f4ccb203a70 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 0:4f4ccb203a70 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 0:4f4ccb203a70 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 0:4f4ccb203a70 20 THE SOFTWARE.
AjK 0:4f4ccb203a70 21 */
AjK 0:4f4ccb203a70 22
taylornichols 3:a4334fba854f 23 #ifdef 0
AjK 0:4f4ccb203a70 24
AjK 0:4f4ccb203a70 25 #include "mbed.h"
AjK 0:4f4ccb203a70 26 #include "PinDetect.h"
AjK 0:4f4ccb203a70 27
AjK 1:611a8f5ac65c 28 PinDetect pin ( p21 );
AjK 0:4f4ccb203a70 29 DigitalOut led1( LED1 );
AjK 0:4f4ccb203a70 30 DigitalOut led2( LED2 );
AjK 0:4f4ccb203a70 31 DigitalOut led3( LED3 );
AjK 0:4f4ccb203a70 32 DigitalOut led4( LED4 );
AjK 0:4f4ccb203a70 33
AjK 1:611a8f5ac65c 34 /*
AjK 1:611a8f5ac65c 35 * Note, the PinDetect can be defined thus:-
AjK 1:611a8f5ac65c 36 * PinDetect pin( p21, PullDown );
AjK 1:611a8f5ac65c 37 * This allows you to specify the DigitalIn pinmode
AjK 1:611a8f5ac65c 38 * when you create the PinDetect object. This means
AjK 1:611a8f5ac65c 39 * using pin.mode() later is then no longer required.
AjK 1:611a8f5ac65c 40 */
AjK 1:611a8f5ac65c 41
AjK 1:611a8f5ac65c 42 // C function callbacks follow.
AjK 1:611a8f5ac65c 43
AjK 0:4f4ccb203a70 44 void keyPressed( void ) {
AjK 0:4f4ccb203a70 45 led2 = 1;
AjK 0:4f4ccb203a70 46 led3 = 0;
AjK 0:4f4ccb203a70 47 led4 = 0;
AjK 0:4f4ccb203a70 48 }
AjK 0:4f4ccb203a70 49
AjK 0:4f4ccb203a70 50 void keyReleased( void ) {
AjK 0:4f4ccb203a70 51 led2 = 0;
AjK 0:4f4ccb203a70 52 led3 = 0;
AjK 0:4f4ccb203a70 53 led4 = 0;
AjK 0:4f4ccb203a70 54 }
AjK 0:4f4ccb203a70 55
AjK 0:4f4ccb203a70 56 void keyPressedHeld( void ) {
AjK 0:4f4ccb203a70 57 led3 = 1;
AjK 0:4f4ccb203a70 58 }
AjK 0:4f4ccb203a70 59
AjK 0:4f4ccb203a70 60 void keyReleasedHeld( void ) {
AjK 0:4f4ccb203a70 61 led4 = 1;
AjK 0:4f4ccb203a70 62 }
AjK 0:4f4ccb203a70 63
AjK 1:611a8f5ac65c 64 // The main program.
AjK 1:611a8f5ac65c 65
AjK 0:4f4ccb203a70 66 int main() {
AjK 0:4f4ccb203a70 67
AjK 0:4f4ccb203a70 68 pin.mode( PullDown );
AjK 0:4f4ccb203a70 69 pin.attach_asserted( &keyPressed );
AjK 0:4f4ccb203a70 70 pin.attach_deasserted( &keyReleased );
AjK 0:4f4ccb203a70 71 pin.attach_asserted_held( &keyPressedHeld );
AjK 0:4f4ccb203a70 72
AjK 0:4f4ccb203a70 73 // This callback will often be of little use as it's
AjK 0:4f4ccb203a70 74 // called after every assertion/deassertion. However,
AjK 0:4f4ccb203a70 75 // it's provided for completeness. You may find a use
AjK 0:4f4ccb203a70 76 // for it. If not, just don't attach a callback and it
AjK 0:4f4ccb203a70 77 // will not activate.
AjK 0:4f4ccb203a70 78 pin.attach_deasserted_held( &keyReleasedHeld );
AjK 0:4f4ccb203a70 79
AjK 0:4f4ccb203a70 80 // You can define how many continuous samples must be
AjK 0:4f4ccb203a70 81 // asserted before the attach_asserted() function is called.
AjK 0:4f4ccb203a70 82 // pin.setSamplesTillAssert( 10 );
AjK 0:4f4ccb203a70 83 // This would mean 10 * 20ms debounce time = 200ms.
AjK 0:4f4ccb203a70 84
AjK 0:4f4ccb203a70 85 // You can define how many continuous samples must be
AjK 0:4f4ccb203a70 86 // asserted before the attach_asserted_held() function is called.
AjK 0:4f4ccb203a70 87 // pin.setSamplesTillHeld( 200 );
AjK 0:4f4ccb203a70 88 // This would mean 200 * 20ms debounce time = 2seconds.
AjK 0:4f4ccb203a70 89
AjK 0:4f4ccb203a70 90 // By default, "asserted" assumes the pin going high from 0volts to 5volts
AjK 0:4f4ccb203a70 91 // and deasserted assumes going from 5volts to 0volts. You can invert this
AjK 0:4f4ccb203a70 92 // logic so that going to 0volts is asserted and going to 5volts is deasserted
AjK 0:4f4ccb203a70 93 // using this setup function:-
AjK 0:4f4ccb203a70 94 // pin.setAssertValue( 0 );
AjK 0:4f4ccb203a70 95
AjK 0:4f4ccb203a70 96 // Sampling does NOT begin until you set the frequency. So, until
AjK 0:4f4ccb203a70 97 // you call this function NO callbacks will be made. With no arguments
AjK 0:4f4ccb203a70 98 // passed the default is 20000 microseconds (20ms). Specifiy the sampling
AjK 0:4f4ccb203a70 99 // period in microseconds if you want a different value to 20ms.
AjK 0:4f4ccb203a70 100 // For example, for a sampling period of 10ms do:-
AjK 0:4f4ccb203a70 101 // pin.setSampleFrequency( 10000 );
AjK 0:4f4ccb203a70 102 // Note, if you change the sampling frequency you will probably also
AjK 0:4f4ccb203a70 103 // want to change the number of samples till assert and held as show
AjK 0:4f4ccb203a70 104 // above.
AjK 0:4f4ccb203a70 105 pin.setSampleFrequency(); // Defaults to 20ms.
AjK 0:4f4ccb203a70 106
AjK 0:4f4ccb203a70 107 while( 1 ) {
AjK 0:4f4ccb203a70 108 led1 = !led1;
AjK 0:4f4ccb203a70 109 wait( 0.2 );
AjK 0:4f4ccb203a70 110 }
AjK 0:4f4ccb203a70 111 }
AjK 0:4f4ccb203a70 112
AjK 0:4f4ccb203a70 113 #endif