Debounce InterruptIn
Dependents: led_sigfox Allumag_lampe_sigfox Case_study_02_Turnstile B18_MP3_PLAYER ... more
DebouncedInterrupt.cpp@19:d12a5b89ac3a, 2014-02-19 (annotated)
- Committer:
- kandangath
- Date:
- Wed Feb 19 03:30:52 2014 +0000
- Revision:
- 19:d12a5b89ac3a
- Parent:
- 18:e6e9bc6402b7
- Child:
- 21:34b95e1b2bf3
A bit more code cleanup
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 | 18:e6e9bc6402b7 | 20 | void DebouncedInterrupt::attach(void (*fptr)(void), const gpio_irq_event trigger, 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 | 18:e6e9bc6402b7 | 26 | _trigger = trigger; |
kandangath | 10:cb16d2957f8d | 27 | |
kandangath | 18:e6e9bc6402b7 | 28 | switch(trigger) |
kandangath | 18:e6e9bc6402b7 | 29 | { |
kandangath | 18:e6e9bc6402b7 | 30 | case IRQ_RISE: |
kandangath | 18:e6e9bc6402b7 | 31 | _in->rise(this, &DebouncedInterrupt::_onInterrupt); |
kandangath | 18:e6e9bc6402b7 | 32 | break; |
kandangath | 18:e6e9bc6402b7 | 33 | case IRQ_FALL: |
kandangath | 18:e6e9bc6402b7 | 34 | _in->fall(this, &DebouncedInterrupt::_onInterrupt); |
kandangath | 18:e6e9bc6402b7 | 35 | break; |
kandangath | 18:e6e9bc6402b7 | 36 | case IRQ_NONE: |
kandangath | 18:e6e9bc6402b7 | 37 | reset(); // Unexpected. Clear callbacks. |
kandangath | 18:e6e9bc6402b7 | 38 | break; |
kandangath | 18:e6e9bc6402b7 | 39 | } |
kandangath | 18:e6e9bc6402b7 | 40 | } else { |
kandangath | 18:e6e9bc6402b7 | 41 | reset(); |
kandangath | 0:ca5a0fee9f52 | 42 | } |
kandangath | 0:ca5a0fee9f52 | 43 | } |
kandangath | 0:ca5a0fee9f52 | 44 | |
kandangath | 10:cb16d2957f8d | 45 | void DebouncedInterrupt::reset() |
kandangath | 0:ca5a0fee9f52 | 46 | { |
kandangath | 10:cb16d2957f8d | 47 | timeout.detach(); |
kandangath | 0:ca5a0fee9f52 | 48 | } |
kandangath | 0:ca5a0fee9f52 | 49 | |
kandangath | 13:09b53a088a9c | 50 | unsigned int DebouncedInterrupt::get_bounce() |
kandangath | 10:cb16d2957f8d | 51 | { |
kandangath | 13:09b53a088a9c | 52 | return _last_bounce_count; |
kandangath | 10:cb16d2957f8d | 53 | } |
kandangath | 10:cb16d2957f8d | 54 | |
kandangath | 10:cb16d2957f8d | 55 | void DebouncedInterrupt::_callback() |
kandangath | 7:2d73e219dadf | 56 | { |
kandangath | 13:09b53a088a9c | 57 | _last_bounce_count = _bounce_count; |
kandangath | 13:09b53a088a9c | 58 | _bounce_count = 0; |
kandangath | 19:d12a5b89ac3a | 59 | if(_din->read() == (_trigger==IRQ_RISE)) { |
kandangath | 14:da09706b92f5 | 60 | fCallback(); |
kandangath | 14:da09706b92f5 | 61 | } |
kandangath | 7:2d73e219dadf | 62 | } |
kandangath | 7:2d73e219dadf | 63 | |
kandangath | 10:cb16d2957f8d | 64 | void DebouncedInterrupt::_onInterrupt() |
kandangath | 0:ca5a0fee9f52 | 65 | { |
kandangath | 13:09b53a088a9c | 66 | _bounce_count++; |
kandangath | 10:cb16d2957f8d | 67 | timeout.attach_us(this, &DebouncedInterrupt::_callback, _debounce_us); |
kandangath | 7:2d73e219dadf | 68 | } |