Skip to content

Commit 8a379c1

Browse files
committed
Update
1 parent 1ff89b0 commit 8a379c1

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

src/Toggle.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/************************************************
2-
Toggle Library for Arduino - Version 3.1.0
2+
Toggle Library for Arduino - Version 3.1.1
33
by dlloydev https://github.com/Dlloydev/Toggle
44
Licensed under the MIT License.
55
************************************************/
@@ -167,7 +167,7 @@ uint32_t Toggle::getElapsedMs() {
167167
return (micros() - startUs) * 0.001;
168168
}
169169

170-
uint8_t Toggle::pressCode() {
170+
uint8_t Toggle::pressCode(bool debug) {
171171
static uint8_t pCode = 0, code = 0;
172172
static uint32_t elapsedMs = 0;
173173

@@ -179,14 +179,16 @@ uint8_t Toggle::pressCode() {
179179
case PB_DEFAULT:
180180
setTimerMode(2); // onChange
181181
elapsedMs = getElapsedMs();
182-
if (pCode && (elapsedMs > CLICK::LONG)) _state = PB_DONE;
182+
if (pCode && isReleased() && (elapsedMs > (CLICK::LONG + CLICK::MULTI))) _state = PB_DONE;
183183
if (onChange()) startUs = micros();
184184
if (onPress()) {
185-
//Serial.print(F(" releasedMs: ")); Serial.print(elapsedMs); Serial.print(F(" "));
185+
//Serial.print(F(" Released:\t")); Serial.print(elapsedMs); Serial.println(" ms");
186186
_state = PB_ON_PRESS;
187187
}
188188
if (onRelease()) {
189-
//Serial.print(F(" pressedMs: ")); Serial.print(elapsedMs); Serial.print(F(" "));
189+
if (debug) {
190+
Serial.print(F(" Pressed:\t")); Serial.print(elapsedMs); Serial.println(" ms");
191+
}
190192
_state = PB_ON_RELEASE;
191193
}
192194
break;
@@ -218,24 +220,26 @@ uint8_t Toggle::pressCode() {
218220
break;
219221

220222
case PB_DONE:
223+
if (debug) {
224+
Serial.print(F(" Code:\t\t")); Serial.println(pCode, HEX); Serial.println();
225+
}
221226
code = pCode;
222227
pCode = 0;
223228
_state = PB_DEFAULT;
224-
return code;
229+
if (code) return code;
225230
break;
226231

227232
default:
228233
_state = PB_DEFAULT;
229234
break;
230235
}
231-
return code;
236+
return 0;
232237
}
233238

234-
/************** debouncer ***********************************************************************
239+
/************** debouncer ************************************************************************
235240
The debounceInput() function by default uses a robust algorithm that ignores up to 2 spurious
236241
signal transitions (glitches) and only adds up to 2 sample periods time lag to the output signal.
237-
Optionally, this can be changed to 1 glitch with 1 sample lag or to immediate response mode.
238-
*************************************************************************************************/
242+
**************************************************************************************************/
239243
uint8_t Toggle::debounceInput(uint8_t bit) {
240244
pOut = out;
241245
uint8_t a, b, c, bits = 2;

src/Toggle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Toggle {
1919
void begin(uint8_t inA, uint8_t inB = 255); // required in setup
2020
void poll(uint8_t bit = 0); // required at top of loop
2121

22-
uint8_t setAlgorithm(uint8_t glitches = 2); // (2) robust mode, (1) normal response, (0) quick response
22+
uint8_t setAlgorithm(uint8_t glitches = 2); // (2) robust mode, (1) average mode, (0) common mode
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
@@ -38,7 +38,7 @@ class Toggle {
3838
bool pressedFor(uint16_t ms); // returns true if pressed for at least the given ms
3939
bool releasedFor(uint16_t ms); // returns true if released for at least the given ms
4040
bool retrigger(uint16_t ms); // returns true each time the given ms expires while the button is pressed
41-
uint8_t pressCode(); // returns byte code for number of fast, short and long clicks
41+
uint8_t pressCode(bool debug = 0); // returns byte code for number of fast, short and long clicks
4242

4343
bool isUP(); // functions for using 2 inputs with 3-position switches
4444
bool isMID();

0 commit comments

Comments
 (0)