From e5d1b39c4e2d0aa9f1d631585ae85eb4d65fdda9 Mon Sep 17 00:00:00 2001 From: boseji Date: Wed, 18 Sep 2024 20:48:59 +0530 Subject: [PATCH] Format fix for the selected clang-format --- .../SwitchCallbackExample.ino | 44 ++-- examples/SwitchExample/SwitchExample.ino | 119 ++++----- src/avdweb_Switch.cpp | 239 +++++++++--------- src/avdweb_Switch.h | 104 ++++---- 4 files changed, 260 insertions(+), 246 deletions(-) diff --git a/examples/SwitchCallbackExample/SwitchCallbackExample.ino b/examples/SwitchCallbackExample/SwitchCallbackExample.ino index b90c83b..72edccc 100644 --- a/examples/SwitchCallbackExample/SwitchCallbackExample.ino +++ b/examples/SwitchCallbackExample/SwitchCallbackExample.ino @@ -6,31 +6,35 @@ const byte multiresponseButtonpin = 9; Switch toggleSwitch = Switch(toggleSwitchpin); Switch multiresponseButton = Switch(multiresponseButtonpin); -void buttonCallbackFunction(void *s) { - Serial.print("Button: "); - Serial.println((char *)s); +void buttonCallbackFunction(void* s) +{ + Serial.print("Button: "); + Serial.println((char*)s); } -void toggleCallbackFunction(void *s) { - Serial.print("Toggle: "); - Serial.println((char *)s); +void toggleCallbackFunction(void* s) +{ + Serial.print("Toggle: "); + Serial.println((char*)s); } -void setup() { - Serial.begin(9600); - toggleSwitch.setPushedCallback(&toggleCallbackFunction, (void *)"turned on"); - toggleSwitch.setReleasedCallback(&toggleCallbackFunction, - (void *)"turned off"); +void setup() +{ + Serial.begin(9600); + toggleSwitch.setPushedCallback(&toggleCallbackFunction, (void*)"turned on"); + toggleSwitch.setReleasedCallback(&toggleCallbackFunction, + (void*)"turned off"); - multiresponseButton.setLongPressCallback(&buttonCallbackFunction, - (void *)"long press"); - multiresponseButton.setDoubleClickCallback(&buttonCallbackFunction, - (void *)"double click"); - multiresponseButton.setSingleClickCallback(&buttonCallbackFunction, - (void *)"single click"); + multiresponseButton.setLongPressCallback(&buttonCallbackFunction, + (void*)"long press"); + multiresponseButton.setDoubleClickCallback(&buttonCallbackFunction, + (void*)"double click"); + multiresponseButton.setSingleClickCallback(&buttonCallbackFunction, + (void*)"single click"); } -void loop() { - toggleSwitch.poll(); - multiresponseButton.poll(); +void loop() +{ + toggleSwitch.poll(); + multiresponseButton.poll(); } diff --git a/examples/SwitchExample/SwitchExample.ino b/examples/SwitchExample/SwitchExample.ino index e92c4cf..f492e91 100644 --- a/examples/SwitchExample/SwitchExample.ino +++ b/examples/SwitchExample/SwitchExample.ino @@ -1,14 +1,13 @@ #include "avdweb_Switch.h" -const byte pushButtonpin = 10; // downButton -const byte toggleSwitchpin = 9; // upButton +const byte pushButtonpin = 10; // downButton +const byte toggleSwitchpin = 9; // upButton const byte multiresponseButtonpin = 4; // selectButton -const byte alleventsButtonpin = 7; // weldButton -const byte buzzerPin = 3; // for beep when a switch is pressed +const byte alleventsButtonpin = 7; // weldButton +const byte buzzerPin = 3; // for beep when a switch is pressed int i; -Switch pushButton = - Switch(pushButtonpin); // button to GND, use internal 20K pullup resistor +Switch pushButton = Switch(pushButtonpin); // button to GND, use internal 20K pullup resistor Switch toggleSwitch = Switch(toggleSwitchpin); Switch multiresponseButton = Switch(multiresponseButtonpin); Switch alleventsButton = Switch(alleventsButtonpin); @@ -18,65 +17,67 @@ Switch alleventsButton = Switch(alleventsButtonpin); // pushButton1ms = Switch(pushButtonpin, INPUT_PULLUP, LOW, 1); // debounceTime // 1ms -void beepCallbackFunction(void *s) // optional +void beepCallbackFunction(void* s) // optional { - tone(3, 2400, 5); // is non-blocking - // Serial.print("BeepCallback: "); Serial.println((char*)s); - (void)s; // Fix Unused warning + tone(3, 2400, 5); // is non-blocking + // Serial.print("BeepCallback: "); Serial.println((char*)s); + (void)s; // Fix Unused warning } -void setup() { - Serial.begin(115200); - toggleSwitch.setBeepAllCallback( - &beepCallbackFunction /*, "Beep done"*/); // needed only for one switch - // because of static - // multiresponseButton.doubleClickPeriod=0; disable doubleClick() +void setup() +{ + Serial.begin(115200); + toggleSwitch.setBeepAllCallback( + &beepCallbackFunction /*, "Beep done"*/); // needed only for one switch + // because of static + // multiresponseButton.doubleClickPeriod=0; disable doubleClick() } -void loop() { // pushButton simple events - pushButton.poll(); - if (pushButton.switched()) - Serial.print("pushButton switched "); - if (pushButton.pushed()) { - Serial.print("pushButton pushed "); - Serial.print(++i); - Serial.println(" times"); - } - if (pushButton.released()) - Serial.println("pushButton released"); +void loop() +{ // pushButton simple events + pushButton.poll(); + if (pushButton.switched()) + Serial.print("pushButton switched "); + if (pushButton.pushed()) { + Serial.print("pushButton pushed "); + Serial.print(++i); + Serial.println(" times"); + } + if (pushButton.released()) + Serial.println("pushButton released"); - // toggleSwitch report status only when changed - if (toggleSwitch.poll()) { - Serial.print("toggleSwitch status changed to "); - Serial.println(toggleSwitch.on()); - } + // toggleSwitch report status only when changed + if (toggleSwitch.poll()) { + Serial.print("toggleSwitch status changed to "); + Serial.println(toggleSwitch.on()); + } - // multiresponseButton complex events - multiresponseButton.poll(); - if (multiresponseButton.longPress()) - Serial.println("multiresponseButton longPress"); - if (multiresponseButton.doubleClick()) - Serial.println("multiresponseButton doubleClick"); - if (multiresponseButton.singleClick()) - Serial.println("multiresponseButton singleClick"); + // multiresponseButton complex events + multiresponseButton.poll(); + if (multiresponseButton.longPress()) + Serial.println("multiresponseButton longPress"); + if (multiresponseButton.doubleClick()) + Serial.println("multiresponseButton doubleClick"); + if (multiresponseButton.singleClick()) + Serial.println("multiresponseButton singleClick"); - // alleventsButton complex events - alleventsButton.poll(); - if (alleventsButton.switched()) { - Serial.println("all_e_B switched."); - Serial.print(" all_e_B status to "); - Serial.print(alleventsButton.on()); - Serial.println("."); - } - if (alleventsButton.pushed()) { - Serial.println(" all_e_B pushed."); - } - if (alleventsButton.released()) - Serial.println(" all_e_B released."); - if (alleventsButton.longPress()) - Serial.println(" ==> all_e_B longPress."); - if (alleventsButton.doubleClick()) - Serial.println(" ==> all_e_B doubleClick."); - if (alleventsButton.singleClick()) - Serial.println(" ==> all_e_B singleClick."); + // alleventsButton complex events + alleventsButton.poll(); + if (alleventsButton.switched()) { + Serial.println("all_e_B switched."); + Serial.print(" all_e_B status to "); + Serial.print(alleventsButton.on()); + Serial.println("."); + } + if (alleventsButton.pushed()) { + Serial.println(" all_e_B pushed."); + } + if (alleventsButton.released()) + Serial.println(" all_e_B released."); + if (alleventsButton.longPress()) + Serial.println(" ==> all_e_B longPress."); + if (alleventsButton.doubleClick()) + Serial.println(" ==> all_e_B doubleClick."); + if (alleventsButton.singleClick()) + Serial.println(" ==> all_e_B singleClick."); } diff --git a/src/avdweb_Switch.cpp b/src/avdweb_Switch.cpp index 8084309..91781e2 100644 --- a/src/avdweb_Switch.cpp +++ b/src/avdweb_Switch.cpp @@ -149,106 +149,109 @@ #include "avdweb_Switch.h" -switchCallback_t Switch::_beepAllCallback = - nullptr; // definition static function pointer with typedef -void *Switch::_beepAllCallbackParam = nullptr; +switchCallback_t Switch::_beepAllCallback = nullptr; // definition static function pointer with typedef +void* Switch::_beepAllCallbackParam = nullptr; 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); - singleClickDisable = true; - poll(); + 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); + singleClickDisable = true; + poll(); } -bool Switch::poll() { - input = digitalRead(pin); - ms = millis(); - return process(); +bool Switch::poll() +{ + input = digitalRead(pin); + ms = millis(); + return process(); } -bool Switch::process() { - deglitch(); - debounce(); - calcSingleClick(); - calcDoubleClick(); - calcLongPress(); - if (switched()) { - switchedTime = ms; // stores last times for future rounds - if (pushed()) { - if (_beepAllCallback) - _beepAllCallback(_beepAllCallbackParam); - pushedTime = ms; - } else { - releasedTime = ms; +bool Switch::process() +{ + deglitch(); + debounce(); + calcSingleClick(); + calcDoubleClick(); + calcLongPress(); + if (switched()) { + switchedTime = ms; // stores last times for future rounds + if (pushed()) { + if (_beepAllCallback) + _beepAllCallback(_beepAllCallbackParam); + pushedTime = ms; + } else { + releasedTime = ms; + } } - } - triggerCallbacks(); - return _switched; + triggerCallbacks(); + return _switched; } -void inline Switch::deglitch() { - if (input == lastInput) - equal = 1; - else { - equal = 0; - deglitchTime = ms; - } - if (equal && - ((ms - deglitchTime) > deglitchPeriod)) // max 50ms, disable deglitch: 0ms - { - deglitched = input; - deglitchTime = ms; - } - lastInput = input; +void inline Switch::deglitch() +{ + if (input == lastInput) + equal = 1; + else { + equal = 0; + deglitchTime = ms; + } + if (equal && ((ms - deglitchTime) > deglitchPeriod)) // max 50ms, disable deglitch: 0ms + { + deglitched = input; + deglitchTime = ms; + } + lastInput = input; } -void inline Switch::debounce() { - _switched = 0; - if ((deglitched != debounced) && ((ms - switchedTime) > debouncePeriod)) { - debounced = deglitched; - _switched = 1; - } +void inline Switch::debounce() +{ + _switched = 0; + if ((deglitched != debounced) && ((ms - switchedTime) > debouncePeriod)) { + debounced = deglitched; + _switched = 1; + } } -void inline Switch::calcSingleClick() { - _singleClick = false; - if (pushed()) { - if ((ms - pushedTime) >= doubleClickPeriod) { - singleClickDisable = - false; // resets when pushed not in second click of doubleclick - } else { - singleClickDisable = true; // silence single click in second cl. doublecl. +void inline Switch::calcSingleClick() +{ + _singleClick = false; + if (pushed()) { + if ((ms - pushedTime) >= doubleClickPeriod) { + singleClickDisable = false; // resets when pushed not in second click of doubleclick + } else { + singleClickDisable = true; // silence single click in second cl. doublecl. + } + } + if (!singleClickDisable) { + _singleClick = !switched() && !on() && ((releasedTime - pushedTime) <= longPressPeriod) && ((ms - pushedTime) >= doubleClickPeriod); // true just one time between polls + singleClickDisable = _singleClick; // will be reset at next push } - } - if (!singleClickDisable) { - _singleClick = !switched() && !on() && - ((releasedTime - pushedTime) <= longPressPeriod) && - ((ms - pushedTime) >= - doubleClickPeriod); // true just one time between polls - singleClickDisable = _singleClick; // will be reset at next push - } } -void inline Switch::calcDoubleClick() { - _doubleClick = pushed() && ((ms - pushedTime) < doubleClickPeriod); +void inline Switch::calcDoubleClick() +{ + _doubleClick = pushed() && ((ms - pushedTime) < doubleClickPeriod); } -void inline Switch::calcLongPress() { - _longPress = false; - if (released()) - longPressDisable = false; // resets when released - if (!longPressDisable) { - _longPress = !switched() && on() && - ((ms - pushedTime) > - longPressPeriod); // true just one time between polls - longPressDisable = _longPress; // will be reset at next release - } +void inline Switch::calcLongPress() +{ + _longPress = false; + if (released()) + longPressDisable = false; // resets when released + if (!longPressDisable) { + _longPress = !switched() && on() && ((ms - pushedTime) > longPressPeriod); // true just one time between polls + longPressDisable = _longPress; // will be reset at next release + } } bool Switch::switched() { return _switched; } @@ -265,57 +268,63 @@ bool Switch::doubleClick() { return _doubleClick; } bool Switch::singleClick() { return _singleClick; } -void Switch::triggerCallbacks() { - if (_pushedCallback && pushed()) { - _pushedCallback(_pushedCallbackParam); - } else if (_releasedCallback && released()) { - _releasedCallback(_releasedCallbackParam); - } +void Switch::triggerCallbacks() +{ + if (_pushedCallback && pushed()) { + _pushedCallback(_pushedCallbackParam); + } else if (_releasedCallback && released()) { + _releasedCallback(_releasedCallbackParam); + } - if (_longPressCallback && longPress()) { - _longPressCallback(_longPressCallbackParam); - } + if (_longPressCallback && longPress()) { + _longPressCallback(_longPressCallbackParam); + } - if (_doubleClickCallback && doubleClick()) { - _doubleClickCallback(_doubleClickCallbackParam); - } + if (_doubleClickCallback && doubleClick()) { + _doubleClickCallback(_doubleClickCallbackParam); + } - if (_singleClickCallback && singleClick()) { - _singleClickCallback(_singleClickCallbackParam); - } + if (_singleClickCallback && singleClick()) { + _singleClickCallback(_singleClickCallbackParam); + } } -void Switch::setPushedCallback(switchCallback_t cb, void *param) { - _pushedCallback = cb; // Store the "pushed" callback function - _pushedCallbackParam = param; +void Switch::setPushedCallback(switchCallback_t cb, void* param) +{ + _pushedCallback = cb; // Store the "pushed" callback function + _pushedCallbackParam = param; } -void Switch::setReleasedCallback(switchCallback_t cb, void *param) { - _releasedCallback = cb; // Store the "released" callback function - _releasedCallbackParam = param; +void Switch::setReleasedCallback(switchCallback_t cb, void* param) +{ + _releasedCallback = cb; // Store the "released" callback function + _releasedCallbackParam = param; } -void Switch::setLongPressCallback(switchCallback_t cb, void *param) { - _longPressCallback = cb; // Store the "long press" callback function - _longPressCallbackParam = param; +void Switch::setLongPressCallback(switchCallback_t cb, void* param) +{ + _longPressCallback = cb; // Store the "long press" callback function + _longPressCallbackParam = param; } -void Switch::setDoubleClickCallback(switchCallback_t cb, void *param) { - _doubleClickCallback = cb; // Store the "double click" callback function - _doubleClickCallbackParam = param; +void Switch::setDoubleClickCallback(switchCallback_t cb, void* param) +{ + _doubleClickCallback = cb; // Store the "double click" callback function + _doubleClickCallbackParam = param; } -void Switch::setSingleClickCallback(switchCallback_t cb, void *param) { - _singleClickCallback = cb; // Store the "double click" callback function - _singleClickCallbackParam = param; +void Switch::setSingleClickCallback(switchCallback_t cb, void* param) +{ + _singleClickCallback = cb; // Store the "double click" callback function + _singleClickCallbackParam = param; } // void Switch::setBeepAllCallback(void(*cb)(void*), void* param) static // function pointer without typedef void Switch::setBeepAllCallback( switchCallback_t cb, - void *param) // with static function pointer without typedef + void* param) // with static function pointer without typedef { - _beepAllCallback = cb; // Store the "beep" callback function - _beepAllCallbackParam = param; + _beepAllCallback = cb; // Store the "beep" callback function + _beepAllCallbackParam = param; } diff --git a/src/avdweb_Switch.h b/src/avdweb_Switch.h index e4b002c..02e944c 100644 --- a/src/avdweb_Switch.h +++ b/src/avdweb_Switch.h @@ -41,67 +41,67 @@ #include -typedef void (*switchCallback_t)(void *); +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() + 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); + // 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; + 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(); + 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; + 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 + // 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 + 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