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