InterruptIn style DigitalIn debounced with callbacks for pin state change and pin state hold. Fork of original https://os.mbed.com/users/AjK/code/PinDetect/ and modification for compatibility with MbedOS6+

Committer:
JohnnyK
Date:
Thu Jun 09 18:25:42 2022 +0000
Revision:
4:947c59b48cb8
Parent:
1:611a8f5ac65c
example correction

Who changed what in which revision?

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