From 6661d84ace1103a277621a0749146f6e6b57c8b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Thu, 29 Dec 2016 15:25:35 +0100 Subject: [PATCH] Using DebounceEvent library from its own repo --- code/lib/DebounceEvent/DebounceEvent.cpp | 115 ----------------------- code/lib/DebounceEvent/DebounceEvent.h | 62 ------------ code/platformio.ini | 1 + 3 files changed, 1 insertion(+), 177 deletions(-) delete mode 100644 code/lib/DebounceEvent/DebounceEvent.cpp delete mode 100644 code/lib/DebounceEvent/DebounceEvent.h diff --git a/code/lib/DebounceEvent/DebounceEvent.cpp b/code/lib/DebounceEvent/DebounceEvent.cpp deleted file mode 100644 index fccb41e4..00000000 --- a/code/lib/DebounceEvent/DebounceEvent.cpp +++ /dev/null @@ -1,115 +0,0 @@ -/* - - Debounce buttons and trigger events - Copyright (C) 2015 by Xose Pérez - - 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 . - -*/ - -#include -#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; -} diff --git a/code/lib/DebounceEvent/DebounceEvent.h b/code/lib/DebounceEvent/DebounceEvent.h deleted file mode 100644 index b7b30b51..00000000 --- a/code/lib/DebounceEvent/DebounceEvent.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - - Debounce buttons and trigger events - Copyright (C) 2015 by Xose Pérez - - 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 . - -*/ - -#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 diff --git a/code/platformio.ini b/code/platformio.ini index d58217a1..cd09d824 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -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