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