arduino-audio-tools
Loading...
Searching...
No Matches
Debouncer.h
Go to the documentation of this file.
1#pragma once
2
3#include "AudioLogger.h"
4
5namespace audio_tools {
6
11 class Debouncer {
12
13 public:
14 Debouncer(uint16_t timeoutMs = 5000, void* ref = nullptr) {
15 setDebounceTimeout(timeoutMs);
16 p_ref = ref;
17 }
18
19 void setDebounceTimeout(uint16_t timeoutMs) {
20 ms = timeoutMs;
21 }
22
24 bool debounce(void(*cb)(void* ref) = nullptr) {
25 bool result = false;
26 // signed-difference comparison so this keeps working correctly
27 // across a millis() rollover (~49.7 days), unlike a plain
28 // millis() > debounce_ms check
29 if ((long)(millis() - debounce_ms) >= 0) {
30 LOGI("accepted");
31 if (cb != nullptr) cb(p_ref);
32 // new time limit
33 debounce_ms = millis() + ms;
34 result = true;
35 }
36 else {
37 LOGI("rejected");
38 }
39 return result;
40 }
41
42 protected:
43 unsigned long debounce_ms = 0; // Debounce sensitive touch
44 uint16_t ms;
45 void* p_ref = nullptr;
46
47 };
48
49}
#define LOGI(...)
Definition AudioLoggerIDF.h:28
Helper class to debounce user input from a push button.
Definition Debouncer.h:11
void setDebounceTimeout(uint16_t timeoutMs)
Definition Debouncer.h:19
void * p_ref
Definition Debouncer.h:45
unsigned long debounce_ms
Definition Debouncer.h:43
uint16_t ms
Definition Debouncer.h:44
Debouncer(uint16_t timeoutMs=5000, void *ref=nullptr)
Definition Debouncer.h:14
bool debounce(void(*cb)(void *ref)=nullptr)
Prevents that the same method is executed multiple times within the indicated time limit.
Definition Debouncer.h:24
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
uint32_t millis()
Returns the milliseconds since the start.
Definition Arduino.h:260