Skip to content

Commit

Permalink
Redesign Debouncer to avoid conflichts.
Browse files Browse the repository at this point in the history
  • Loading branch information
prampec committed Oct 8, 2015
1 parent a1bedfa commit 444ca37
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
31 changes: 20 additions & 11 deletions Debouncer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,25 @@

#include "Arduino.h"
#include "Debouncer.h"
#include <DelayRun.h>
#include <Task.h>

Debouncer::Debouncer(int pin, int pushMode, void (*onPressed)(), void (*onReleased)(unsigned long pressTimespan))
: DelayRun(DEFAULT_DEBOUNCE_DELAY_MILLIS, &(Debouncer::step)) {
#define IDDLE_TIME_MICROS -1L

Debouncer::Debouncer(int pin, int pushMode, void (*onPressed)(), void (*onReleased)(unsigned long pressTimespan), bool pullUp)
: Task(IDDLE_TIME_MICROS, &(Debouncer::step)) {
this->_pin = pin;
this->_state = STATE_OFF;
this->_onLevel = pushMode;
this->_onPressed = onPressed;
this->_onReleased = onReleased;

pinMode(pin, INPUT);
if(pullUp) {
pinMode(pin, INPUT_PULLUP);
} else {
pinMode(pin, INPUT);
}

SoftTimer.add(this);
}

void Debouncer::pciHandleInterrupt(byte vect) {
Expand All @@ -50,14 +58,19 @@ void Debouncer::pciHandleInterrupt(byte vect) {
}
// -- After pin change we have the opposite level, lets start the bouncing timespan.
this->_state += 1;
this->startDelayed();
this->lastCallTimeMicros = micros();
this->setPeriodMs(this->debounceDelayMs);
}
}
}

boolean Debouncer::step(Task* task) {
void Debouncer::step(Task* task) {
Debouncer* debouncer = (Debouncer*)task;
if((debouncer->_state == STATE_OFF) || (debouncer->_state == STATE_ON)) {
return;
}
int val = digitalRead(debouncer->_pin);
debouncer->setPeriodMs(IDDLE_TIME_MICROS);
if(debouncer->_state == STATE_OFFON_BOUNCING) {
if(val == debouncer->_onLevel) {

Expand Down Expand Up @@ -91,9 +104,5 @@ boolean Debouncer::step(Task* task) {
debouncer->_state = STATE_ON;
}
}
return false;
return;
}

boolean Debouncer::setDebounceDelayMs(unsigned long debounceDelayMs) {
this->setPeriodMs(debounceDelayMs);
}
9 changes: 5 additions & 4 deletions Debouncer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
/** We say that 50 milliseconds are enough for the signal to stabilize. */
#define DEFAULT_DEBOUNCE_DELAY_MILLIS 50

class Debouncer : public PciListener, public DelayRun
class Debouncer : public PciListener, public Task
{
public:
/**
Expand All @@ -59,7 +59,7 @@ class Debouncer : public PciListener, public DelayRun
* The callback receives the pressTimespan parameter that is the time in milliseconds the button was hold down before
* it was released.
*/
Debouncer(int pin, int pushMode, void (*onPressed)(), void (*onReleased)(unsigned long pressTimespan));
Debouncer(int pin, int pushMode, void (*onPressed)(), void (*onReleased)(unsigned long pressTimespan), bool pullUp=false);

/**
* Please call this function on interrupt.
Expand All @@ -69,15 +69,16 @@ class Debouncer : public PciListener, public DelayRun
/**
* Change the delay of the bouncing time-span. The default value is DEFAULT_DEBOUNCE_DELAY_MILLIS;
*/
boolean setDebounceDelayMs(unsigned long debounceDelayMs);
unsigned long debounceDelayMs = DEFAULT_DEBOUNCE_DELAY_MILLIS;
boolean setDebounceDelayMs(unsigned long debounceDelayMs) { this->debounceDelayMs = debounceDelayMs; return true; };
private:
int _pin;
int _onLevel;
volatile int _state; // 0=off, 1=bouncing, 2=pressing
unsigned long _pressStart;
void (*_onPressed)();
void (*_onReleased)(unsigned long pressTimespan);
static boolean step(Task* me);
static void step(Task* me);
};

#endif
Expand Down

0 comments on commit 444ca37

Please sign in to comment.