Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Some fixes and a new blink on change example
  • Loading branch information
Dlloydev committed May 31, 2022
1 parent 2d204f4 commit 006195f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 36 deletions.
25 changes: 25 additions & 0 deletions examples/Blink_On_Change/Blink_On_Change.ino
Original file line number Diff line number Diff line change
@@ -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 <Toggle.h>

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();
}
3 changes: 2 additions & 1 deletion examples/Eight_buttons/Eight_buttons.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -31,6 +31,7 @@ void loop() {
} else {
ledState--;
}
sw[i].onPress();
}
digitalWrite(ledPin, ledState);
}
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ releasedFor KEYWORD2
retrigger KEYWORD2
retrigger KEYWORD2
pressCode KEYWORD2
debounceInput KEYWORD2
isUP KEYWORD2
isMID KEYWORD2
isDN KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -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":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Toggle
version=3.1.1
version=3.1.2
author=David Lloyd
maintainer=David Lloyd <[email protected]>
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.
Expand Down
35 changes: 10 additions & 25 deletions src/Toggle.cpp
Original file line number Diff line number Diff line change
@@ -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.
************************************************/
Expand Down Expand Up @@ -121,43 +121,32 @@ 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;
}
return false;
}

bool Toggle::releasedFor(uint16_t ms) {
if (getTimerMode() != 1) setTimerMode(1); // start onRelease
if (onChange() == 2) startUs = micros();
if (isReleased() && getElapsedMs() > ms) {
return true;
}
return false;
}

bool Toggle::retrigger(uint16_t ms) {
if (getTimerMode()) setTimerMode(0); // start onPress
if (isPressed() && getElapsedMs() > ms) {
//clearTimer();
startUs = micros();
return true;
}
return false;
Expand All @@ -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();
Expand All @@ -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;
}
Expand Down
12 changes: 4 additions & 8 deletions src/Toggle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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

0 comments on commit 006195f

Please sign in to comment.