Nathan Yonkee / Mbed OS Nucleo_read_button_interrupt_copy

Dependencies:   PinDetect Data_Clock_Pair Seeed_Chainable_LED

Committer:
tulanthoar
Date:
Thu Apr 27 16:05:43 2017 -0600
Revision:
4:52eaedac3d65
Parent:
2:b27a4eac471e
Child:
5:54e66ad3c78a
use the pin detect library for interrupts

Who changed what in which revision?

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