From 006195fcb609e0bfd8fc5b0797962e2e7a004512 Mon Sep 17 00:00:00 2001 From: Dlloydev Date: Tue, 31 May 2022 00:52:08 -0400 Subject: [PATCH] Update Some fixes and a new blink on change example --- examples/Blink_On_Change/Blink_On_Change.ino | 25 ++++++++++++++ examples/Eight_buttons/Eight_buttons.ino | 3 +- keywords.txt | 1 + library.json | 2 +- library.properties | 2 +- src/Toggle.cpp | 35 ++++++-------------- src/Toggle.h | 12 +++---- 7 files changed, 44 insertions(+), 36 deletions(-) create mode 100644 examples/Blink_On_Change/Blink_On_Change.ino diff --git a/examples/Blink_On_Change/Blink_On_Change.ino b/examples/Blink_On_Change/Blink_On_Change.ino new file mode 100644 index 0000000..9be4417 --- /dev/null +++ b/examples/Blink_On_Change/Blink_On_Change.ino @@ -0,0 +1,25 @@ +/*********************************************************** + Toggle Blink On Change Example: + =============================== + A simple example that blinks an LED each time a button is + pressed or released. + **********************************************************/ + +#include + +const byte buttonPin = 2; +const byte ledPin = LED_BUILTIN; + +Toggle sw1(buttonPin); + +void setup() { + sw1.begin(buttonPin); + pinMode(ledPin, OUTPUT); +} + +void loop() { + sw1.poll(); + digitalWrite(ledPin, sw1.blink(100, 2)); + sw1.onPress(); + sw1.onRelease(); +} diff --git a/examples/Eight_buttons/Eight_buttons.ino b/examples/Eight_buttons/Eight_buttons.ino index f1f83eb..c132248 100644 --- a/examples/Eight_buttons/Eight_buttons.ino +++ b/examples/Eight_buttons/Eight_buttons.ino @@ -10,7 +10,7 @@ const byte num = 8; // number of buttons const byte pin[num] = {2, 3, 4, 5, 6, 7, 8, 9}; //button pins -const byte ledPin = 13; +const byte ledPin = LED_BUILTIN; const unsigned int blinkMs = 100; // led blink duration (ms) Toggle *sw = new Toggle[num]; @@ -31,6 +31,7 @@ void loop() { } else { ledState--; } + sw[i].onPress(); } digitalWrite(ledPin, ledState); } diff --git a/keywords.txt b/keywords.txt index 46328e8..cf6ef26 100644 --- a/keywords.txt +++ b/keywords.txt @@ -44,6 +44,7 @@ releasedFor KEYWORD2 retrigger KEYWORD2 retrigger KEYWORD2 pressCode KEYWORD2 +debounceInput KEYWORD2 isUP KEYWORD2 isMID KEYWORD2 isDN KEYWORD2 diff --git a/library.json b/library.json index a689f33..dc46bac 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "Toggle", - "version": "3.1.1", + "version": "3.1.2", "description": "Arduino bounce library for deglitching and debouncing hardware, signals and data. Works with all switch types, port expander and other 8-bit data sources. Flexible algorithm with Robust, Normal and Quick response modes.", "keywords": "debounce, toggle, button, switch, data, deglitch", "repository": diff --git a/library.properties b/library.properties index 63f22f1..fdf3371 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Toggle -version=3.1.1 +version=3.1.2 author=David Lloyd maintainer=David Lloyd sentence=Arduino bounce library for deglitching and debouncing hardware, signals and data. Works with all switch types, port expander and other 8-bit data sources. diff --git a/src/Toggle.cpp b/src/Toggle.cpp index 73fb159..9fc1e6d 100644 --- a/src/Toggle.cpp +++ b/src/Toggle.cpp @@ -1,5 +1,5 @@ /************************************************ - Toggle Library for Arduino - Version 3.1.1 + Toggle Library for Arduino - Version 3.1.2 by dlloydev https://github.com/Dlloydev/Toggle Licensed under the MIT License. ************************************************/ @@ -121,25 +121,15 @@ bool Toggle::toggle(bool invert, uint8_t bit) { /************* button timer functions ****************/ -void Toggle::setTimerMode(uint8_t mode) { - lsr &= ~0b11000000; // onPress (default) - if (mode == 1) lsr |= 0b01000000; // onRelease - else if (mode == 2) { // onChange - lsr &= ~0b01000000; - lsr |= 0b10000000; - } -} - -uint8_t Toggle::getTimerMode() { - return (lsr & 0b11000000) >> 6; -} - -bool Toggle::blink(uint16_t ms) { +bool Toggle::blink(uint16_t ms, uint8_t mode) { + if (mode == 2 && onChange()) startUs = micros(); + else if (mode == 1 && onChange() == 2) startUs = micros(); + else if (onChange() == 1) startUs = micros(); return (bool)(ms > (getElapsedMs())); } bool Toggle::pressedFor(uint16_t ms) { - if (getTimerMode()) setTimerMode(0); // start onPress + if (onChange() == 1) startUs = micros(); if (isPressed() && getElapsedMs() > ms) { return true; } @@ -147,7 +137,7 @@ bool Toggle::pressedFor(uint16_t ms) { } bool Toggle::releasedFor(uint16_t ms) { - if (getTimerMode() != 1) setTimerMode(1); // start onRelease + if (onChange() == 2) startUs = micros(); if (isReleased() && getElapsedMs() > ms) { return true; } @@ -155,9 +145,8 @@ bool Toggle::releasedFor(uint16_t ms) { } bool Toggle::retrigger(uint16_t ms) { - if (getTimerMode()) setTimerMode(0); // start onPress if (isPressed() && getElapsedMs() > ms) { - //clearTimer(); + startUs = micros(); return true; } return false; @@ -171,13 +160,9 @@ uint8_t Toggle::pressCode(bool debug) { static uint8_t pCode = 0, code = 0; static uint32_t elapsedMs = 0; - //Serial.print(F(" startUs: ")); Serial.print(startUs); Serial.print(F(" ")); - //Serial.print(F(" elapsedMS: ")); Serial.print(getElapsedMs()); Serial.print(F(" ")); - //Serial.print(F(" pCode: ")); Serial.print(pCode, HEX); Serial.print(F(" ")); - switch (_state) { case PB_DEFAULT: - setTimerMode(2); // onChange + //setTimerMode(2); // onChange elapsedMs = getElapsedMs(); if (pCode && isReleased() && (elapsedMs > (CLICK::LONG + CLICK::MULTI))) _state = PB_DONE; if (onChange()) startUs = micros(); @@ -187,7 +172,7 @@ uint8_t Toggle::pressCode(bool debug) { } if (onRelease()) { if (debug) { - Serial.print(F(" Pressed:\t")); Serial.print(elapsedMs); Serial.println(" ms"); + Serial.print(F(" Pressed for:\t")); Serial.print(elapsedMs); Serial.println(" ms"); } _state = PB_ON_RELEASE; } diff --git a/src/Toggle.h b/src/Toggle.h index f093e69..56721c3 100644 --- a/src/Toggle.h +++ b/src/Toggle.h @@ -23,8 +23,6 @@ class Toggle { void setInputMode(inMode inputMode); // input, input_pullup, input_pulldown, input_bit, input_port void setInvertMode(bool invert); // invert false: pullup resistors are used, invert true: pulldown resistors void setSamplePeriodUs(uint16_t samplePeriodUs); // sample period in microseconds - void setTimerMode(uint8_t mode = 0); // start onPress(0), onRelease(1), onChange(2) - uint8_t getTimerMode(); // start onPress(0), onRelease(1), onChange(2) uint32_t getElapsedMs(); // get elapsed ms since the last state change selected by timer mode bool isPressed(uint8_t bit = 0); // returns true if pressed @@ -33,12 +31,12 @@ class Toggle { bool onRelease(); // returns true if just released uint8_t onChange(); // returns (0) no change, (1) onPress, (2) onRelease bool toggle(bool invert = 0, uint8_t bit = 0); // returns true/false (toggle) on each press - - bool blink(uint16_t ms); // returns true for given ms (blink) on mode press(0), release(1), change(2) + bool blink(uint16_t ms, uint8_t mode = 0); // returns true for given ms (blink) on mode press(0), release(1), change(2) bool pressedFor(uint16_t ms); // returns true if pressed for at least the given ms bool releasedFor(uint16_t ms); // returns true if released for at least the given ms bool retrigger(uint16_t ms); // returns true each time the given ms expires while the button is pressed uint8_t pressCode(bool debug = 0); // returns byte code for number of fast, short and long clicks + uint8_t debounceInput(uint8_t bit = 0); // input debouncer bool isUP(); // functions for using 2 inputs with 3-position switches bool isMID(); @@ -50,7 +48,7 @@ class Toggle { private: - enum CLICK : uint32_t {MULTI = 400, LONG = 1000}; + enum CLICK : uint16_t {MULTI = 400, LONG = 1000}; enum fsm_t : uint8_t { // finite state machine PB_DEFAULT = 0, PB_ON_PRESS = 1, @@ -63,8 +61,6 @@ class Toggle { fsm_t _state = PB_DEFAULT; - uint8_t debounceInput(uint8_t bit = 0); // input debouncer - inMode _inputMode = inMode::input_pullup; // input mode uint8_t _inA, _inB; // input pin uint8_t *_in; // referenced to input variable @@ -74,7 +70,7 @@ class Toggle { uint32_t us_timestamp; // most recent sample time μs uint8_t out = 0xFF, pOut = 0xFF; // debounced output and previous debounced output uint8_t csr = 0b10101010; // B7-B4: debounceCount, B3: first run B2: invert, B1-B0 algorithm - uint8_t lsr = 0b00000000; // B7-6: mode, B5 lastState, B4 toggle, B1 onRelease, B0 onPress + uint8_t lsr = 0b00000000; // B5 lastState, B4 toggle, B1 onRelease, B0 onPress }; #endif