-
Notifications
You must be signed in to change notification settings - Fork 0
/
debouncer_integrator.h
107 lines (84 loc) · 2.96 KB
/
debouncer_integrator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
debouncer_integrator.h
Debounces an input using integration.
Detects rising edges, falling edges and both edges.
Enable the repeat count by defining DEBOUNCER_REPEAT_COUNT as true.
MIT License
Copyright (c) 2021 Timothy Mathias
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#ifndef DEBOUNCER_INTEGRATOR_H
#define DEBOUNCER_INTEGRATOR_H
#include <Arduino.h>
#define DEBOUNCER_REPEAT_COUNT false
class DebouncerIntegrator
{
private:
const byte INPUT_PIN;
volatile bool output_state : 1;
volatile bool edge : 1;
volatile bool rise : 1;
volatile bool fall : 1;
const unsigned long DEBOUNCE_DELAY_ms;
volatile unsigned long previous_ms;
volatile unsigned long sum_ms;
#if DEBOUNCER_REPEAT_COUNT
const unsigned long REPEAT_DELAY_ms;
volatile unsigned long previous_repeat_ms;
volatile unsigned long repeat_count;
#endif
public:
DebouncerIntegrator
(
const byte INPUT_PIN
, const unsigned long DEBOUNCE_DELAY_ms = 50
#if DEBOUNCER_REPEAT_COUNT
, const unsigned long REPEAT_DELAY_ms = 100
#endif
)
: INPUT_PIN(INPUT_PIN)
, DEBOUNCE_DELAY_ms(DEBOUNCE_DELAY_ms)
#if DEBOUNCER_REPEAT_COUNT
, REPEAT_DELAY_ms(REPEAT_DELAY_ms)
#endif
, output_state(digitalRead(INPUT_PIN))
, edge(false)
, rise(false)
, fall(false)
, sum_ms(0)
{
#if DEBOUNCER_REPEAT_COUNT
previous_repeat_ms =
#endif
previous_ms = millis();
}
bool Output() const;
bool Edge() const;
bool Rise() const;
bool Fall() const;
#if DEBOUNCER_REPEAT_COUNT
unsigned long RepeatCount() const;
#endif
// To be called from a polling loop where interrupts may or may not be enabled.
void Update();
// For use as an Interrupt Service Routine where interrupts are disabled upon calling.
void UpdateISR();
private:
void Update_(const bool input_state, const unsigned long& current_ms);
};
#endif /* DEBOUNCER_INTEGRATOR_H */