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