Kero Sunutte
/
Pswitch
This is a very simple way to count DigitalIn rising by using Ticker Interrupts.
Pswitch.cpp@0:d8173cfb7a78, 2010-12-23 (annotated)
- Committer:
- MBE13170
- Date:
- Thu Dec 23 07:48:27 2010 +0000
- Revision:
- 0:d8173cfb7a78
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MBE13170 | 0:d8173cfb7a78 | 1 | |
MBE13170 | 0:d8173cfb7a78 | 2 | |
MBE13170 | 0:d8173cfb7a78 | 3 | #include "Pswitch.h" |
MBE13170 | 0:d8173cfb7a78 | 4 | #include "mbed.h" |
MBE13170 | 0:d8173cfb7a78 | 5 | |
MBE13170 | 0:d8173cfb7a78 | 6 | /* |
MBE13170 | 0:d8173cfb7a78 | 7 | * Constructor |
MBE13170 | 0:d8173cfb7a78 | 8 | */ |
MBE13170 | 0:d8173cfb7a78 | 9 | Pswitch::Pswitch(PinName in) |
MBE13170 | 0:d8173cfb7a78 | 10 | : _in(in) { |
MBE13170 | 0:d8173cfb7a78 | 11 | |
MBE13170 | 0:d8173cfb7a78 | 12 | // reset all the flags and counters |
MBE13170 | 0:d8173cfb7a78 | 13 | _samples = 0; |
MBE13170 | 0:d8173cfb7a78 | 14 | _output = 0; |
MBE13170 | 0:d8173cfb7a78 | 15 | _output_last = 0; |
MBE13170 | 0:d8173cfb7a78 | 16 | _rising_flag = 0; |
MBE13170 | 0:d8173cfb7a78 | 17 | |
MBE13170 | 0:d8173cfb7a78 | 18 | // Attach ticker |
MBE13170 | 0:d8173cfb7a78 | 19 | _ticker.attach(this, &Pswitch::_sample, 0.005); |
MBE13170 | 0:d8173cfb7a78 | 20 | } |
MBE13170 | 0:d8173cfb7a78 | 21 | |
MBE13170 | 0:d8173cfb7a78 | 22 | void Pswitch::_sample() { |
MBE13170 | 0:d8173cfb7a78 | 23 | |
MBE13170 | 0:d8173cfb7a78 | 24 | |
MBE13170 | 0:d8173cfb7a78 | 25 | _output_last = _output; |
MBE13170 | 0:d8173cfb7a78 | 26 | _output = _in; |
MBE13170 | 0:d8173cfb7a78 | 27 | if (!_output && _output_last) |
MBE13170 | 0:d8173cfb7a78 | 28 | { |
MBE13170 | 0:d8173cfb7a78 | 29 | _rising_flag++; |
MBE13170 | 0:d8173cfb7a78 | 30 | } |
MBE13170 | 0:d8173cfb7a78 | 31 | |
MBE13170 | 0:d8173cfb7a78 | 32 | } |
MBE13170 | 0:d8173cfb7a78 | 33 | |
MBE13170 | 0:d8173cfb7a78 | 34 | |
MBE13170 | 0:d8173cfb7a78 | 35 | |
MBE13170 | 0:d8173cfb7a78 | 36 | |
MBE13170 | 0:d8173cfb7a78 | 37 | // return number of rising edges |
MBE13170 | 0:d8173cfb7a78 | 38 | int Pswitch::count(void) { |
MBE13170 | 0:d8173cfb7a78 | 39 | int return_value = _rising_flag; |
MBE13170 | 0:d8173cfb7a78 | 40 | _rising_flag = 0; |
MBE13170 | 0:d8173cfb7a78 | 41 | return(return_value); |
MBE13170 | 0:d8173cfb7a78 | 42 | } |
MBE13170 | 0:d8173cfb7a78 | 43 | |
MBE13170 | 0:d8173cfb7a78 | 44 | // return the debounced status |
MBE13170 | 0:d8173cfb7a78 | 45 | int Pswitch::read(void) { |
MBE13170 | 0:d8173cfb7a78 | 46 | return(_in); |
MBE13170 | 0:d8173cfb7a78 | 47 | } |
MBE13170 | 0:d8173cfb7a78 | 48 | |
MBE13170 | 0:d8173cfb7a78 | 49 | // shorthand for read() |
MBE13170 | 0:d8173cfb7a78 | 50 | Pswitch::operator int() { |
MBE13170 | 0:d8173cfb7a78 | 51 | return read(); |
MBE13170 | 0:d8173cfb7a78 | 52 | } |