diff --git a/avdweb_Switch.cpp b/avdweb_Switch.cpp index 31c7a7b..36a5acc 100644 --- a/avdweb_Switch.cpp +++ b/avdweb_Switch.cpp @@ -113,12 +113,11 @@ WEBSITE: http://www.avdweb.nl/arduino/hardware-interfacing/simple-switch-debounc #include "Arduino.h" #include "avdweb_Switch.h" -switchCallback_t Switch::_beepAllCallback; // definition static function pointer with typedef -//void (*Switch::_beepAllCallback)(void*); // definition static function pointer without typedef -void* Switch::_beepAllCallbackParam; // without = +switchCallback_t Switch::_beepAllCallback = nullptr; // definition static function pointer with typedef +void* Switch::_beepAllCallbackParam = nullptr; -Switch::Switch(byte _pin, byte PinMode, bool polarity, int debouncePeriod, int longPressPeriod, int doubleClickPeriod, int deglitchPeriod): -pin(_pin), polarity(polarity), deglitchPeriod(deglitchPeriod), debouncePeriod(debouncePeriod), longPressPeriod(longPressPeriod), doubleClickPeriod(doubleClickPeriod) +Switch::Switch(byte _pin, byte PinMode, bool polarity, unsigned long debouncePeriod, unsigned long longPressPeriod, unsigned long doubleClickPeriod, unsigned long deglitchPeriod): +deglitchPeriod(deglitchPeriod), debouncePeriod(debouncePeriod), longPressPeriod(longPressPeriod), doubleClickPeriod(doubleClickPeriod), pin(_pin), polarity(polarity) { pinMode(pin, PinMode); switchedTime = millis(); debounced = digitalRead(pin); diff --git a/avdweb_Switch.h b/avdweb_Switch.h index e2a996f..ce51ac9 100644 --- a/avdweb_Switch.h +++ b/avdweb_Switch.h @@ -1,73 +1,73 @@ -/* -Switch.cpp -Copyright (C) 2012 Albert van Dalen http://www.avdweb.nl -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 at http://www.gnu.org/licenses . - -AUTHOR: Albert van Dalen -WEBSITE: http://www.avdweb.nl/arduino/hardware-interfacing/simple-switch-debouncer.html -*/ - -#ifndef AVDWEB_SWITCH_H -#define AVDWEB_SWITCH_H - -typedef void (*switchCallback_t)(void*); - -class Switch -{ -public: - Switch(byte _pin, byte PinMode=INPUT_PULLUP, bool polarity=LOW, int debouncePeriod=50, int longPressPeriod=300, int doubleClickPeriod=250, int deglitchPeriod=10); - bool poll(); // Returns 1 if switched - bool switched(); // will be refreshed by poll() - bool on(); - bool pushed(); // will be refreshed by poll() - bool released(); // will be refreshed by poll() - bool longPress(); // will be refreshed by poll() - bool doubleClick(); // will be refreshed by poll() - bool singleClick(); // will be refreshed by poll() - - // Set methods for event callbacks - void setPushedCallback(switchCallback_t cb, void* param = nullptr); - void setReleasedCallback(switchCallback_t cb, void* param = nullptr); - void setLongPressCallback(switchCallback_t cb, void* param = nullptr); - void setDoubleClickCallback(switchCallback_t cb, void* param = nullptr); - void setSingleClickCallback(switchCallback_t cb, void* param = nullptr); - void setBeepAllCallback(switchCallback_t cb, void* param = nullptr); - - int deglitchPeriod, debouncePeriod, longPressPeriod, doubleClickPeriod; - - protected: - bool process(); // not inline, used in child class - void inline deglitch(); - void inline debounce(); - void inline calcLongPress(); - void inline calcDoubleClick(); - void inline calcSingleClick(); - void triggerCallbacks(); - - unsigned long deglitchTime, switchedTime, pushedTime, releasedTime, ms; - const byte pin; - const bool polarity; - bool input, lastInput, equal, deglitched, debounced, _switched, _longPress, longPressDisable, _doubleClick, _singleClick, singleClickDisable; - - // Event callbacks - switchCallback_t _pushedCallback = nullptr; - switchCallback_t _releasedCallback = nullptr; - switchCallback_t _longPressCallback = nullptr; - switchCallback_t _doubleClickCallback = nullptr; - switchCallback_t _singleClickCallback = nullptr; - static switchCallback_t _beepAllCallback; // static function pointer, can be used by all objects - //static switchCallback_t _beepAllCallback = nullptr; // gives error with SAMD21 - //static void(*_beepAllCallback)(void*) = nullptr; // static function pointer without typedef - - void* _pushedCallbackParam = nullptr; - void* _releasedCallbackParam = nullptr; - void* _longPressCallbackParam = nullptr; - void* _doubleClickCallbackParam = nullptr; - void* _singleClickCallbackParam = nullptr; - static void* _beepAllCallbackParam; // can be used by all objects - //static void* _beepAllCallbackParam = nullptr; // gives error with SAMD21 -}; -#endif +/* +Switch.cpp +Copyright (C) 2012 Albert van Dalen http://www.avdweb.nl +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 at http://www.gnu.org/licenses . + +AUTHOR: Albert van Dalen +WEBSITE: http://www.avdweb.nl/arduino/hardware-interfacing/simple-switch-debouncer.html +*/ + +#ifndef AVDWEB_SWITCH_H +#define AVDWEB_SWITCH_H + +typedef void (*switchCallback_t)(void*); + +class Switch +{ +public: + Switch(byte _pin, byte PinMode=INPUT_PULLUP, bool polarity=LOW, unsigned long debouncePeriod=50, unsigned long longPressPeriod=300, unsigned long doubleClickPeriod=250, unsigned long deglitchPeriod=10); + bool poll(); // Returns 1 if switched + bool switched(); // will be refreshed by poll() + bool on(); + bool pushed(); // will be refreshed by poll() + bool released(); // will be refreshed by poll() + bool longPress(); // will be refreshed by poll() + bool doubleClick(); // will be refreshed by poll() + bool singleClick(); // will be refreshed by poll() + + // Set methods for event callbacks + void setPushedCallback(switchCallback_t cb, void* param = nullptr); + void setReleasedCallback(switchCallback_t cb, void* param = nullptr); + void setLongPressCallback(switchCallback_t cb, void* param = nullptr); + void setDoubleClickCallback(switchCallback_t cb, void* param = nullptr); + void setSingleClickCallback(switchCallback_t cb, void* param = nullptr); + void setBeepAllCallback(switchCallback_t cb, void* param = nullptr); + + unsigned long deglitchPeriod, debouncePeriod, longPressPeriod, doubleClickPeriod; + + protected: + bool process(); // not inline, used in child class + void inline deglitch(); + void inline debounce(); + void inline calcLongPress(); + void inline calcDoubleClick(); + void inline calcSingleClick(); + void triggerCallbacks(); + + unsigned long deglitchTime, switchedTime, pushedTime, releasedTime, ms; + const byte pin; + const bool polarity; + bool input, lastInput, equal, deglitched, debounced, _switched, _longPress, longPressDisable, _doubleClick, _singleClick, singleClickDisable; + + // Event callbacks + switchCallback_t _pushedCallback = nullptr; + switchCallback_t _releasedCallback = nullptr; + switchCallback_t _longPressCallback = nullptr; + switchCallback_t _doubleClickCallback = nullptr; + switchCallback_t _singleClickCallback = nullptr; + static switchCallback_t _beepAllCallback; // static function pointer, can be used by all objects + //static switchCallback_t _beepAllCallback = nullptr; // gives error with SAMD21 + //static void(*_beepAllCallback)(void*) = nullptr; // static function pointer without typedef + + void* _pushedCallbackParam = nullptr; + void* _releasedCallbackParam = nullptr; + void* _longPressCallbackParam = nullptr; + void* _doubleClickCallbackParam = nullptr; + void* _singleClickCallbackParam = nullptr; + static void* _beepAllCallbackParam; // can be used by all objects + //static void* _beepAllCallbackParam = nullptr; // gives error with SAMD21 +}; +#endif