Debaunced Input like InterruptIn.

Dependents:   LcdClock idd_hw2_martincowell_bicyclehid IDD_FALL15_HW2_JMARQUIS_Keypadentry

Committer:
togayan
Date:
Sat Feb 22 23:55:10 2014 +0000
Revision:
0:471239ea932b
1st release.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
togayan 0:471239ea932b 1 #include "DebouncedEdgeIn.h"
togayan 0:471239ea932b 2 #include "mbed.h"
togayan 0:471239ea932b 3
togayan 0:471239ea932b 4 /*
togayan 0:471239ea932b 5 * Constructor
togayan 0:471239ea932b 6 */
togayan 0:471239ea932b 7 DebouncedEdgeIn::DebouncedEdgeIn(PinName in)
togayan 0:471239ea932b 8 : _in(in),
togayan 0:471239ea932b 9 _ticker(),
togayan 0:471239ea932b 10 _rise(),
togayan 0:471239ea932b 11 _fall(),
togayan 0:471239ea932b 12 // reset all the flags and counters
togayan 0:471239ea932b 13 _samples(0),
togayan 0:471239ea932b 14 _output(0),
togayan 0:471239ea932b 15 _output_last(0)
togayan 0:471239ea932b 16 {
togayan 0:471239ea932b 17 // Attach ticker
togayan 0:471239ea932b 18 _ticker.attach(this, &DebouncedEdgeIn::_sample, 0.005);
togayan 0:471239ea932b 19 }
togayan 0:471239ea932b 20
togayan 0:471239ea932b 21 DebouncedEdgeIn::~DebouncedEdgeIn() {
togayan 0:471239ea932b 22 _ticker.detach();
togayan 0:471239ea932b 23 }
togayan 0:471239ea932b 24
togayan 0:471239ea932b 25 void DebouncedEdgeIn::rise(void (*fptr)(void))
togayan 0:471239ea932b 26 {
togayan 0:471239ea932b 27 _rise.attach(fptr);
togayan 0:471239ea932b 28 }
togayan 0:471239ea932b 29
togayan 0:471239ea932b 30 void DebouncedEdgeIn::fall(void (*fptr)(void))
togayan 0:471239ea932b 31 {
togayan 0:471239ea932b 32 _fall.attach(fptr);
togayan 0:471239ea932b 33 }
togayan 0:471239ea932b 34
togayan 0:471239ea932b 35 void DebouncedEdgeIn::mode(PinMode pull) {
togayan 0:471239ea932b 36 _in.mode(pull);
togayan 0:471239ea932b 37 }
togayan 0:471239ea932b 38
togayan 0:471239ea932b 39 // return the debounced status
togayan 0:471239ea932b 40 int DebouncedEdgeIn::read(void)
togayan 0:471239ea932b 41 {
togayan 0:471239ea932b 42 return(_output);
togayan 0:471239ea932b 43 }
togayan 0:471239ea932b 44
togayan 0:471239ea932b 45 // shorthand for read()
togayan 0:471239ea932b 46 DebouncedEdgeIn::operator int()
togayan 0:471239ea932b 47 {
togayan 0:471239ea932b 48 return read();
togayan 0:471239ea932b 49 }
togayan 0:471239ea932b 50
togayan 0:471239ea932b 51 void DebouncedEdgeIn::_sample(void)
togayan 0:471239ea932b 52 {
togayan 0:471239ea932b 53 // take a sample
togayan 0:471239ea932b 54 _samples = _samples >> 1; // shift left
togayan 0:471239ea932b 55
togayan 0:471239ea932b 56 if (_in) {
togayan 0:471239ea932b 57 _samples |= 0x80;
togayan 0:471239ea932b 58 }
togayan 0:471239ea932b 59
togayan 0:471239ea932b 60 // examine the sample window, look for steady state
togayan 0:471239ea932b 61 if (_samples == 0x00) {
togayan 0:471239ea932b 62 _output = 0;
togayan 0:471239ea932b 63 } else if (_samples == 0xFF) {
togayan 0:471239ea932b 64 _output = 1;
togayan 0:471239ea932b 65 }
togayan 0:471239ea932b 66
togayan 0:471239ea932b 67 // Rising edge detection
togayan 0:471239ea932b 68 if ((_output == 1) && (_output_last == 0)) {
togayan 0:471239ea932b 69 _rise.call();
togayan 0:471239ea932b 70 }
togayan 0:471239ea932b 71 // Falling edge detection
togayan 0:471239ea932b 72 else if ((_output == 0) && (_output_last == 1)) {
togayan 0:471239ea932b 73 _fall.call();
togayan 0:471239ea932b 74 }
togayan 0:471239ea932b 75
togayan 0:471239ea932b 76 // update the output
togayan 0:471239ea932b 77 _output_last = _output;
togayan 0:471239ea932b 78 }