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