abc
Dependents: Microwave_MBED MicrowaveSimulation_LPC1768 RTOS_Alarm_Clock USB_Project_Host ... more
DebouncedInterrupt.cpp@17:96a51b236ba0, 2014-02-18 (annotated)
- Committer:
- kandangath
- Date:
- Tue Feb 18 20:38:50 2014 +0000
- Revision:
- 17:96a51b236ba0
- Parent:
- 14:da09706b92f5
Monitor both rising and falling edges
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kandangath | 10:cb16d2957f8d | 1 | /** |
kandangath | 10:cb16d2957f8d | 2 | * DebouncedInterrupt.cpp |
kandangath | 10:cb16d2957f8d | 3 | **/ |
kandangath | 10:cb16d2957f8d | 4 | #include "DebouncedInterrupt.h" |
kandangath | 0:ca5a0fee9f52 | 5 | |
kandangath | 0:ca5a0fee9f52 | 6 | Timeout timeout; |
kandangath | 0:ca5a0fee9f52 | 7 | |
kandangath | 10:cb16d2957f8d | 8 | DebouncedInterrupt::DebouncedInterrupt(PinName pin) |
kandangath | 10:cb16d2957f8d | 9 | { |
kandangath | 10:cb16d2957f8d | 10 | _in = new InterruptIn(pin); |
kandangath | 14:da09706b92f5 | 11 | _din = new DigitalIn(pin); |
kandangath | 10:cb16d2957f8d | 12 | } |
kandangath | 10:cb16d2957f8d | 13 | |
kandangath | 10:cb16d2957f8d | 14 | DebouncedInterrupt::~DebouncedInterrupt() |
kandangath | 10:cb16d2957f8d | 15 | { |
kandangath | 14:da09706b92f5 | 16 | delete _in; |
kandangath | 14:da09706b92f5 | 17 | delete _din; |
kandangath | 10:cb16d2957f8d | 18 | } |
kandangath | 10:cb16d2957f8d | 19 | |
kandangath | 17:96a51b236ba0 | 20 | void DebouncedInterrupt::attach(void (*fptr)(void), const unsigned int& debounce_ms) |
kandangath | 0:ca5a0fee9f52 | 21 | { |
kandangath | 10:cb16d2957f8d | 22 | if(fptr) { |
kandangath | 10:cb16d2957f8d | 23 | fCallback = fptr; |
kandangath | 13:09b53a088a9c | 24 | _last_bounce_count = _bounce_count = 0; |
kandangath | 17:96a51b236ba0 | 25 | _debounce_us = 1000*debounce_ms; |
kandangath | 10:cb16d2957f8d | 26 | |
kandangath | 17:96a51b236ba0 | 27 | _in->rise(this, &DebouncedInterrupt::_onInterrupt); |
kandangath | 17:96a51b236ba0 | 28 | _in->fall(this, &DebouncedInterrupt::_onInterrupt); |
kandangath | 0:ca5a0fee9f52 | 29 | } |
kandangath | 0:ca5a0fee9f52 | 30 | } |
kandangath | 0:ca5a0fee9f52 | 31 | |
kandangath | 10:cb16d2957f8d | 32 | void DebouncedInterrupt::reset() |
kandangath | 0:ca5a0fee9f52 | 33 | { |
kandangath | 10:cb16d2957f8d | 34 | timeout.detach(); |
kandangath | 0:ca5a0fee9f52 | 35 | } |
kandangath | 0:ca5a0fee9f52 | 36 | |
kandangath | 13:09b53a088a9c | 37 | unsigned int DebouncedInterrupt::get_bounce() |
kandangath | 10:cb16d2957f8d | 38 | { |
kandangath | 13:09b53a088a9c | 39 | return _last_bounce_count; |
kandangath | 10:cb16d2957f8d | 40 | } |
kandangath | 10:cb16d2957f8d | 41 | |
kandangath | 10:cb16d2957f8d | 42 | void DebouncedInterrupt::_callback() |
kandangath | 7:2d73e219dadf | 43 | { |
kandangath | 13:09b53a088a9c | 44 | _last_bounce_count = _bounce_count; |
kandangath | 13:09b53a088a9c | 45 | _bounce_count = 0; |
kandangath | 17:96a51b236ba0 | 46 | if(_din->read()) { |
kandangath | 14:da09706b92f5 | 47 | fCallback(); |
kandangath | 14:da09706b92f5 | 48 | } |
kandangath | 7:2d73e219dadf | 49 | } |
kandangath | 7:2d73e219dadf | 50 | |
kandangath | 10:cb16d2957f8d | 51 | void DebouncedInterrupt::_onInterrupt() |
kandangath | 0:ca5a0fee9f52 | 52 | { |
kandangath | 13:09b53a088a9c | 53 | _bounce_count++; |
kandangath | 10:cb16d2957f8d | 54 | timeout.attach_us(this, &DebouncedInterrupt::_callback, _debounce_us); |
kandangath | 7:2d73e219dadf | 55 | } |