interrupt copy
Dependencies: PinDetect Data_Clock_Pair Seeed_Chainable_LED
Diff: main.cpp
- Revision:
- 4:52eaedac3d65
- Parent:
- 2:b27a4eac471e
- Child:
- 5:54e66ad3c78a
--- a/main.cpp Fri Apr 21 14:03:34 2017 +0000 +++ b/main.cpp Thu Apr 27 16:05:43 2017 -0600 @@ -1,23 +1,113 @@ -#include "mbed.h" - -InterruptIn mybutton(PF_12); -DigitalOut myled(PF_15); - -float delay = 1.0; // 1 sec - -void pressed() -{ - if (delay == 1.0) - delay = 0.2; // 200 ms - else - delay = 1.0; // 1 sec -} - -int main() -{ - mybutton.fall(&pressed); - while (1) { - myled = !myled; - wait(delay); - } -} +/* + Copyright (c) 2010 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/* #ifdef PINDETECT_EXAMPLE_COMPILE */ + +#include "mbed.h" +#include "PinDetect.h" + +PinDetect pin ( PD_14 ); +DigitalOut led1( PF_12 ); +DigitalOut led2( PC_0 ); +DigitalOut led3( PC_3 ); +DigitalOut led4( PF_3 ); + +/* + * Note, the PinDetect can be defined thus:- + * PinDetect pin( p21, PullDown ); + * This allows you to specify the DigitalIn pinmode + * when you create the PinDetect object. This means + * using pin.mode() later is then no longer required. + */ + +// C function callbacks follow. + +void keyPressed( void ) { + led2 = 1; + led3 = 0; + led4 = 0; +} + +void keyReleased( void ) { + led2 = 0; + led3 = 0; + led4 = 0; +} + +void keyPressedHeld( void ) { + led3 = 1; +} + +void keyReleasedHeld( void ) { + led4 = 1; +} + +// The main program. + +int main() { + + pin.mode( PullDown ); + pin.attach_asserted( &keyPressed ); + pin.attach_deasserted( &keyReleased ); + pin.attach_asserted_held( &keyPressedHeld ); + + // This callback will often be of little use as it's + // called after every assertion/deassertion. However, + // it's provided for completeness. You may find a use + // for it. If not, just don't attach a callback and it + // will not activate. + pin.attach_deasserted_held( &keyReleasedHeld ); + + // You can define how many continuous samples must be + // asserted before the attach_asserted() function is called. + // pin.setSamplesTillAssert( 10 ); + // This would mean 10 * 20ms debounce time = 200ms. + + // You can define how many continuous samples must be + // asserted before the attach_asserted_held() function is called. + // pin.setSamplesTillHeld( 200 ); + // This would mean 200 * 20ms debounce time = 2seconds. + + // By default, "asserted" assumes the pin going high from 0volts to 5volts + // and deasserted assumes going from 5volts to 0volts. You can invert this + // logic so that going to 0volts is asserted and going to 5volts is deasserted + // using this setup function:- + // pin.setAssertValue( 0 ); + + // Sampling does NOT begin until you set the frequency. So, until + // you call this function NO callbacks will be made. With no arguments + // passed the default is 20000 microseconds (20ms). Specifiy the sampling + // period in microseconds if you want a different value to 20ms. + // For example, for a sampling period of 10ms do:- + // pin.setSampleFrequency( 10000 ); + // Note, if you change the sampling frequency you will probably also + // want to change the number of samples till assert and held as show + // above. + pin.setSampleFrequency(); // Defaults to 20ms. + + while( 1 ) { + led1 = !led1; + wait( 0.2 ); + } +} + +/* #endif */