This is a very tiny library which counts DigitalIn rising , using "Ticker" interrupts.

Dependents:   mgnetswitch2-NaoKondo

Committer:
MBE13170
Date:
Thu Dec 23 14:56:50 2010 +0000
Revision:
1:a0eb75ae437f
Parent:
0:0bc10e1c0685

        

Who changed what in which revision?

UserRevisionLine numberNew 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) {
MBE13170 0:0bc10e1c0685 11
MBE13170 0:0bc10e1c0685 12 // reset all the flags and counters
MBE13170 0:0bc10e1c0685 13 _samples = 0;
MBE13170 0:0bc10e1c0685 14 _output = 0;
MBE13170 0:0bc10e1c0685 15 _output_last = 0;
MBE13170 0:0bc10e1c0685 16 _rising_flag = 0;
MBE13170 0:0bc10e1c0685 17
MBE13170 0:0bc10e1c0685 18 // Attach ticker
MBE13170 0:0bc10e1c0685 19 _ticker.attach(this, &Pswitch::_sample, 0.005);
MBE13170 0:0bc10e1c0685 20 }
MBE13170 0:0bc10e1c0685 21
MBE13170 0:0bc10e1c0685 22 void Pswitch::_sample() {
MBE13170 0:0bc10e1c0685 23
MBE13170 0:0bc10e1c0685 24
MBE13170 0:0bc10e1c0685 25 _output_last = _output;
MBE13170 0:0bc10e1c0685 26 _output = _in;
MBE13170 0:0bc10e1c0685 27 if (!_output && _output_last)
MBE13170 0:0bc10e1c0685 28 {
MBE13170 0:0bc10e1c0685 29 _rising_flag++;
MBE13170 0:0bc10e1c0685 30 }
MBE13170 0:0bc10e1c0685 31
MBE13170 0:0bc10e1c0685 32 }
MBE13170 0:0bc10e1c0685 33
MBE13170 0:0bc10e1c0685 34
MBE13170 0:0bc10e1c0685 35
MBE13170 0:0bc10e1c0685 36
MBE13170 0:0bc10e1c0685 37 // return number of rising edges
MBE13170 0:0bc10e1c0685 38 int Pswitch::count(void) {
MBE13170 0:0bc10e1c0685 39 int return_value = _rising_flag;
MBE13170 0:0bc10e1c0685 40 _rising_flag = 0;
MBE13170 0:0bc10e1c0685 41 return(return_value);
MBE13170 0:0bc10e1c0685 42 }
MBE13170 0:0bc10e1c0685 43
MBE13170 0:0bc10e1c0685 44 // return the debounced status
MBE13170 0:0bc10e1c0685 45 int Pswitch::read(void) {
MBE13170 0:0bc10e1c0685 46 return(_in);
MBE13170 0:0bc10e1c0685 47 }
MBE13170 0:0bc10e1c0685 48
MBE13170 0:0bc10e1c0685 49 // shorthand for read()
MBE13170 0:0bc10e1c0685 50 Pswitch::operator int() {
MBE13170 0:0bc10e1c0685 51 return read();
MBE13170 0:0bc10e1c0685 52 }