InterruptIn style DigitalIn debounced with callbacks for pin state change and pin state hold.
Dependents: AVC_20110423 SimplePIDBot Pushbutton_Debounce_Interrupt FinalProgram ... more
example.h
00001 /* 00002 Copyright (c) 2010 Andy Kirkham 00003 00004 Permission is hereby granted, free of charge, to any person obtaining a copy 00005 of this software and associated documentation files (the "Software"), to deal 00006 in the Software without restriction, including without limitation the rights 00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 copies of the Software, and to permit persons to whom the Software is 00009 furnished to do so, subject to the following conditions: 00010 00011 The above copyright notice and this permission notice shall be included in 00012 all copies or substantial portions of the Software. 00013 00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 THE SOFTWARE. 00021 */ 00022 00023 #ifdef PINDETECT_EXAMPLE_COMPILE 00024 00025 #include "mbed.h" 00026 #include "PinDetect.h" 00027 00028 PinDetect pin ( p21 ); 00029 DigitalOut led1( LED1 ); 00030 DigitalOut led2( LED2 ); 00031 DigitalOut led3( LED3 ); 00032 DigitalOut led4( LED4 ); 00033 00034 /* 00035 * Note, the PinDetect can be defined thus:- 00036 * PinDetect pin( p21, PullDown ); 00037 * This allows you to specify the DigitalIn pinmode 00038 * when you create the PinDetect object. This means 00039 * using pin.mode() later is then no longer required. 00040 */ 00041 00042 // C function callbacks follow. 00043 00044 void keyPressed( void ) { 00045 led2 = 1; 00046 led3 = 0; 00047 led4 = 0; 00048 } 00049 00050 void keyReleased( void ) { 00051 led2 = 0; 00052 led3 = 0; 00053 led4 = 0; 00054 } 00055 00056 void keyPressedHeld( void ) { 00057 led3 = 1; 00058 } 00059 00060 void keyReleasedHeld( void ) { 00061 led4 = 1; 00062 } 00063 00064 // The main program. 00065 00066 int main() { 00067 00068 pin.mode( PullDown ); 00069 pin.attach_asserted( &keyPressed ); 00070 pin.attach_deasserted( &keyReleased ); 00071 pin.attach_asserted_held( &keyPressedHeld ); 00072 00073 // This callback will often be of little use as it's 00074 // called after every assertion/deassertion. However, 00075 // it's provided for completeness. You may find a use 00076 // for it. If not, just don't attach a callback and it 00077 // will not activate. 00078 pin.attach_deasserted_held( &keyReleasedHeld ); 00079 00080 // You can define how many continuous samples must be 00081 // asserted before the attach_asserted() function is called. 00082 // pin.setSamplesTillAssert( 10 ); 00083 // This would mean 10 * 20ms debounce time = 200ms. 00084 00085 // You can define how many continuous samples must be 00086 // asserted before the attach_asserted_held() function is called. 00087 // pin.setSamplesTillHeld( 200 ); 00088 // This would mean 200 * 20ms debounce time = 2seconds. 00089 00090 // By default, "asserted" assumes the pin going high from 0volts to 5volts 00091 // and deasserted assumes going from 5volts to 0volts. You can invert this 00092 // logic so that going to 0volts is asserted and going to 5volts is deasserted 00093 // using this setup function:- 00094 // pin.setAssertValue( 0 ); 00095 00096 // Sampling does NOT begin until you set the frequency. So, until 00097 // you call this function NO callbacks will be made. With no arguments 00098 // passed the default is 20000 microseconds (20ms). Specifiy the sampling 00099 // period in microseconds if you want a different value to 20ms. 00100 // For example, for a sampling period of 10ms do:- 00101 // pin.setSampleFrequency( 10000 ); 00102 // Note, if you change the sampling frequency you will probably also 00103 // want to change the number of samples till assert and held as show 00104 // above. 00105 pin.setSampleFrequency(); // Defaults to 20ms. 00106 00107 while( 1 ) { 00108 led1 = !led1; 00109 wait( 0.2 ); 00110 } 00111 } 00112 00113 #endif
Generated on Tue Jul 12 2022 16:27:43 by 1.7.2