This is a very tiny library which counts DigitalIn rising , using "Ticker" interrupts.
Fork of Pswitch_Lib by
Revision 0:0bc10e1c0685, committed 2010-12-23
- Comitter:
- MBE13170
- Date:
- Thu Dec 23 07:46:37 2010 +0000
- Child:
- 1:a0eb75ae437f
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Pswitch.cpp Thu Dec 23 07:46:37 2010 +0000 @@ -0,0 +1,52 @@ + + +#include "Pswitch.h" + #include "mbed.h" + + /* + * Constructor + */ + Pswitch::Pswitch(PinName in) + : _in(in) { + + // reset all the flags and counters + _samples = 0; + _output = 0; + _output_last = 0; + _rising_flag = 0; + + // Attach ticker + _ticker.attach(this, &Pswitch::_sample, 0.005); + } + + void Pswitch::_sample() { + + + _output_last = _output; + _output = _in; + if (!_output && _output_last) + { + _rising_flag++; + } + +} + + + + + // return number of rising edges + int Pswitch::count(void) { + int return_value = _rising_flag; + _rising_flag = 0; + return(return_value); + } + + // return the debounced status + int Pswitch::read(void) { + return(_in); + } + + // shorthand for read() + Pswitch::operator int() { + return read(); + }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Pswitch.h Thu Dec 23 07:46:37 2010 +0000 @@ -0,0 +1,26 @@ +#include "mbed.h" + + class Pswitch { + public: + Pswitch(PinName in); + + int read (void); + int count (void); + operator int(); + + private : + // objects + DigitalIn _in; + Ticker _ticker; + + // function to take a sample, and update flags + void _sample(void); + + // counters and flags + int _samples; + int _output; + int _output_last; + int _rising_flag; + + }; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Dec 23 07:46:37 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/20a79241b4a0