Browse Source

Using DebounceEvent library from its own repo

fastled
Xose Pérez 7 years ago
parent
commit
6661d84ace
3 changed files with 1 additions and 177 deletions
  1. +0
    -115
      code/lib/DebounceEvent/DebounceEvent.cpp
  2. +0
    -62
      code/lib/DebounceEvent/DebounceEvent.h
  3. +1
    -0
      code/platformio.ini

+ 0
- 115
code/lib/DebounceEvent/DebounceEvent.cpp View File

@ -1,115 +0,0 @@
/*
Debounce buttons and trigger events
Copyright (C) 2015 by Xose Pérez <xose dot perez at gmail dot com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "DebounceEvent.h"
DebounceEvent::DebounceEvent(uint8_t pin, callback_t callback, uint8_t defaultStatus, unsigned long delay) {
_callback = callback;
DebounceEvent(pin, defaultStatus, delay);
}
DebounceEvent::DebounceEvent(uint8_t pin, uint8_t defaultStatus, unsigned long delay) {
// store configuration
_pin = pin;
_status = _defaultStatus = defaultStatus;
_delay = delay;
// set up button
if (_defaultStatus == LOW) {
pinMode(_pin, INPUT);
} else {
pinMode(_pin, INPUT_PULLUP);
}
}
bool DebounceEvent::loop() {
// holds whether status has changed or not
bool changed = false;
_event = EVENT_NONE;
if (digitalRead(_pin) != _status) {
// Debounce
delay(_delay);
uint8_t newStatus = digitalRead(_pin);
if (newStatus != _status) {
changed = true;
_clicked = false;
_status = newStatus;
// released
if (_status == _defaultStatus) {
// get event
if (millis() - _this_start > LONG_CLICK_DELAY) {
_event = EVENT_LONG_CLICK;
} else if (millis() - _last_start < DOUBLE_CLICK_DELAY ) {
_event = EVENT_DOUBLE_CLICK;
} else {
// We are not setting the event type here because we still don't
// know what kind of event it will be (it might be a double click).
// Instead we are setting the _clicked variable to check later
_clicked = true;
changed = false;
}
// pressed
} else {
_last_start = _this_start;
_this_start = millis();
_event = EVENT_PRESSED;
}
}
}
if (_clicked && (millis() - _this_start > DOUBLE_CLICK_DELAY) && (!changed) && (_status == _defaultStatus)) {
_clicked = false;
changed = true;
_event = EVENT_SINGLE_CLICK;
}
if (changed) {
if (_callback) {
_callback(_pin, EVENT_CHANGED);
_callback(_pin, _event);
}
}
return changed;
}
bool DebounceEvent::pressed() {
return (_status != _defaultStatus);
}
uint8_t DebounceEvent::getEvent() {
return _event;
}

+ 0
- 62
code/lib/DebounceEvent/DebounceEvent.h View File

@ -1,62 +0,0 @@
/*
Debounce buttons and trigger events
Copyright (C) 2015 by Xose Pérez <xose dot perez at gmail dot com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _DEBOUNCE_EVENT_h
#define _DEBOUNCE_EVENT_h
#define DEBOUNCE_DELAY 50
#define LONG_CLICK_DELAY 1000
#define DOUBLE_CLICK_DELAY 500
#define EVENT_NONE 0
#define EVENT_CHANGED 1
#define EVENT_PRESSED 2
#define EVENT_RELEASED 3
#define EVENT_SINGLE_CLICK 3
#define EVENT_DOUBLE_CLICK 4
#define EVENT_LONG_CLICK 5
typedef void(*callback_t)(uint8_t pin, uint8_t event);
class DebounceEvent {
private:
uint8_t _pin;
uint8_t _status;
uint8_t _event;
bool _clicked = false;
unsigned long _this_start;
unsigned long _last_start;
uint8_t _defaultStatus;
unsigned long _delay;
callback_t _callback = false;
public:
DebounceEvent(uint8_t pin, callback_t callback, uint8_t defaultStatus = HIGH, unsigned long delay = DEBOUNCE_DELAY);
DebounceEvent(uint8_t pin, uint8_t defaultStatus = HIGH, unsigned long delay = DEBOUNCE_DELAY);
bool pressed();
bool loop();
uint8_t getEvent();
};
#endif

+ 1
- 0
code/platformio.ini View File

@ -21,6 +21,7 @@ lib_deps =
https://bitbucket.org/xoseperez/hlw8012.git
https://bitbucket.org/xoseperez/emonliteesp.git
https://bitbucket.org/xoseperez/fauxmoESP.git
https://bitbucket.org/xoseperez/debounceevent.git
https://github.com/xoseperez/RemoteSwitch-arduino-library.git
lib_ignore = FauxmoESP, ESPAsyncUDP


Loading…
Cancel
Save