Skip to content

Commit 006195f

Browse files
committed
Update
Some fixes and a new blink on change example
1 parent 2d204f4 commit 006195f

File tree

7 files changed

+44
-36
lines changed

7 files changed

+44
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/***********************************************************
2+
Toggle Blink On Change Example:
3+
===============================
4+
A simple example that blinks an LED each time a button is
5+
pressed or released.
6+
**********************************************************/
7+
8+
#include <Toggle.h>
9+
10+
const byte buttonPin = 2;
11+
const byte ledPin = LED_BUILTIN;
12+
13+
Toggle sw1(buttonPin);
14+
15+
void setup() {
16+
sw1.begin(buttonPin);
17+
pinMode(ledPin, OUTPUT);
18+
}
19+
20+
void loop() {
21+
sw1.poll();
22+
digitalWrite(ledPin, sw1.blink(100, 2));
23+
sw1.onPress();
24+
sw1.onRelease();
25+
}

examples/Eight_buttons/Eight_buttons.ino

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
const byte num = 8; // number of buttons
1212
const byte pin[num] = {2, 3, 4, 5, 6, 7, 8, 9}; //button pins
13-
const byte ledPin = 13;
13+
const byte ledPin = LED_BUILTIN;
1414
const unsigned int blinkMs = 100; // led blink duration (ms)
1515

1616
Toggle *sw = new Toggle[num];
@@ -31,6 +31,7 @@ void loop() {
3131
} else {
3232
ledState--;
3333
}
34+
sw[i].onPress();
3435
}
3536
digitalWrite(ledPin, ledState);
3637
}

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ releasedFor KEYWORD2
4444
retrigger KEYWORD2
4545
retrigger KEYWORD2
4646
pressCode KEYWORD2
47+
debounceInput KEYWORD2
4748
isUP KEYWORD2
4849
isMID KEYWORD2
4950
isDN KEYWORD2

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Toggle",
3-
"version": "3.1.1",
3+
"version": "3.1.2",
44
"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.",
55
"keywords": "debounce, toggle, button, switch, data, deglitch",
66
"repository":

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Toggle
2-
version=3.1.1
2+
version=3.1.2
33
author=David Lloyd
44
maintainer=David Lloyd <dlloydev@testcor.ca>
55
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.

src/Toggle.cpp

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/************************************************
2-
Toggle Library for Arduino - Version 3.1.1
2+
Toggle Library for Arduino - Version 3.1.2
33
by dlloydev https://github.com/Dlloydev/Toggle
44
Licensed under the MIT License.
55
************************************************/
@@ -121,43 +121,32 @@ bool Toggle::toggle(bool invert, uint8_t bit) {
121121

122122
/************* button timer functions ****************/
123123

124-
void Toggle::setTimerMode(uint8_t mode) {
125-
lsr &= ~0b11000000; // onPress (default)
126-
if (mode == 1) lsr |= 0b01000000; // onRelease
127-
else if (mode == 2) { // onChange
128-
lsr &= ~0b01000000;
129-
lsr |= 0b10000000;
130-
}
131-
}
132-
133-
uint8_t Toggle::getTimerMode() {
134-
return (lsr & 0b11000000) >> 6;
135-
}
136-
137-
bool Toggle::blink(uint16_t ms) {
124+
bool Toggle::blink(uint16_t ms, uint8_t mode) {
125+
if (mode == 2 && onChange()) startUs = micros();
126+
else if (mode == 1 && onChange() == 2) startUs = micros();
127+
else if (onChange() == 1) startUs = micros();
138128
return (bool)(ms > (getElapsedMs()));
139129
}
140130

141131
bool Toggle::pressedFor(uint16_t ms) {
142-
if (getTimerMode()) setTimerMode(0); // start onPress
132+
if (onChange() == 1) startUs = micros();
143133
if (isPressed() && getElapsedMs() > ms) {
144134
return true;
145135
}
146136
return false;
147137
}
148138

149139
bool Toggle::releasedFor(uint16_t ms) {
150-
if (getTimerMode() != 1) setTimerMode(1); // start onRelease
140+
if (onChange() == 2) startUs = micros();
151141
if (isReleased() && getElapsedMs() > ms) {
152142
return true;
153143
}
154144
return false;
155145
}
156146

157147
bool Toggle::retrigger(uint16_t ms) {
158-
if (getTimerMode()) setTimerMode(0); // start onPress
159148
if (isPressed() && getElapsedMs() > ms) {
160-
//clearTimer();
149+
startUs = micros();
161150
return true;
162151
}
163152
return false;
@@ -171,13 +160,9 @@ uint8_t Toggle::pressCode(bool debug) {
171160
static uint8_t pCode = 0, code = 0;
172161
static uint32_t elapsedMs = 0;
173162

174-
//Serial.print(F(" startUs: ")); Serial.print(startUs); Serial.print(F(" "));
175-
//Serial.print(F(" elapsedMS: ")); Serial.print(getElapsedMs()); Serial.print(F(" "));
176-
//Serial.print(F(" pCode: ")); Serial.print(pCode, HEX); Serial.print(F(" "));
177-
178163
switch (_state) {
179164
case PB_DEFAULT:
180-
setTimerMode(2); // onChange
165+
//setTimerMode(2); // onChange
181166
elapsedMs = getElapsedMs();
182167
if (pCode && isReleased() && (elapsedMs > (CLICK::LONG + CLICK::MULTI))) _state = PB_DONE;
183168
if (onChange()) startUs = micros();
@@ -187,7 +172,7 @@ uint8_t Toggle::pressCode(bool debug) {
187172
}
188173
if (onRelease()) {
189174
if (debug) {
190-
Serial.print(F(" Pressed:\t")); Serial.print(elapsedMs); Serial.println(" ms");
175+
Serial.print(F(" Pressed for:\t")); Serial.print(elapsedMs); Serial.println(" ms");
191176
}
192177
_state = PB_ON_RELEASE;
193178
}

src/Toggle.h

+4-8
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class Toggle {
2323
void setInputMode(inMode inputMode); // input, input_pullup, input_pulldown, input_bit, input_port
2424
void setInvertMode(bool invert); // invert false: pullup resistors are used, invert true: pulldown resistors
2525
void setSamplePeriodUs(uint16_t samplePeriodUs); // sample period in microseconds
26-
void setTimerMode(uint8_t mode = 0); // start onPress(0), onRelease(1), onChange(2)
27-
uint8_t getTimerMode(); // start onPress(0), onRelease(1), onChange(2)
2826
uint32_t getElapsedMs(); // get elapsed ms since the last state change selected by timer mode
2927

3028
bool isPressed(uint8_t bit = 0); // returns true if pressed
@@ -33,12 +31,12 @@ class Toggle {
3331
bool onRelease(); // returns true if just released
3432
uint8_t onChange(); // returns (0) no change, (1) onPress, (2) onRelease
3533
bool toggle(bool invert = 0, uint8_t bit = 0); // returns true/false (toggle) on each press
36-
37-
bool blink(uint16_t ms); // returns true for given ms (blink) on mode press(0), release(1), change(2)
34+
bool blink(uint16_t ms, uint8_t mode = 0); // returns true for given ms (blink) on mode press(0), release(1), change(2)
3835
bool pressedFor(uint16_t ms); // returns true if pressed for at least the given ms
3936
bool releasedFor(uint16_t ms); // returns true if released for at least the given ms
4037
bool retrigger(uint16_t ms); // returns true each time the given ms expires while the button is pressed
4138
uint8_t pressCode(bool debug = 0); // returns byte code for number of fast, short and long clicks
39+
uint8_t debounceInput(uint8_t bit = 0); // input debouncer
4240

4341
bool isUP(); // functions for using 2 inputs with 3-position switches
4442
bool isMID();
@@ -50,7 +48,7 @@ class Toggle {
5048

5149
private:
5250

53-
enum CLICK : uint32_t {MULTI = 400, LONG = 1000};
51+
enum CLICK : uint16_t {MULTI = 400, LONG = 1000};
5452
enum fsm_t : uint8_t { // finite state machine
5553
PB_DEFAULT = 0,
5654
PB_ON_PRESS = 1,
@@ -63,8 +61,6 @@ class Toggle {
6361

6462
fsm_t _state = PB_DEFAULT;
6563

66-
uint8_t debounceInput(uint8_t bit = 0); // input debouncer
67-
6864
inMode _inputMode = inMode::input_pullup; // input mode
6965
uint8_t _inA, _inB; // input pin
7066
uint8_t *_in; // referenced to input variable
@@ -74,7 +70,7 @@ class Toggle {
7470
uint32_t us_timestamp; // most recent sample time μs
7571
uint8_t out = 0xFF, pOut = 0xFF; // debounced output and previous debounced output
7672
uint8_t csr = 0b10101010; // B7-B4: debounceCount, B3: first run B2: invert, B1-B0 algorithm
77-
uint8_t lsr = 0b00000000; // B7-6: mode, B5 lastState, B4 toggle, B1 onRelease, B0 onPress
73+
uint8_t lsr = 0b00000000; // B5 lastState, B4 toggle, B1 onRelease, B0 onPress
7874

7975
};
8076
#endif

0 commit comments

Comments
 (0)