Skip to content

Commit dbf480c

Browse files
committed
In response to comments
1 parent c12517b commit dbf480c

File tree

4 files changed

+35
-39
lines changed

4 files changed

+35
-39
lines changed

adafruit_debouncer.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,85 +47,78 @@
4747

4848
import time
4949
import digitalio
50-
import microcontroller
50+
51+
_DEBOUNCED_STATE = 0x01
52+
_UNSTABLE_STATE = 0x02
53+
_CHANGED_STATE = 0x04
5154

5255
class Debouncer(object):
5356
"""Debounce an input pin or an arbitrary predicate"""
5457

55-
DEBOUNCED_STATE = 0x01
56-
UNSTABLE_STATE = 0x02
57-
CHANGED_STATE = 0x04
58-
59-
60-
def __init__(self, pin_or_predicate, mode=None, interval=0.010):
58+
def __init__(self, io_or_predicate, interval=0.010):
6159
"""Make am instance.
62-
:param int/function pin_or_predicate: the pin (from board) to debounce
63-
:param int mode: digitalio.Pull.UP or .DOWN (default is no pull up/down)
60+
:param DigitalInOut/function io_or_predicate: the pin (from board) to debounce
6461
:param int interval: bounce threshold in seconds (default is 0.010, i.e. 10 milliseconds)
6562
"""
6663
self.state = 0x00
67-
if isinstance(pin_or_predicate, microcontroller.Pin):
68-
pin = digitalio.DigitalInOut(pin_or_predicate)
69-
pin.direction = digitalio.Direction.INPUT
70-
if mode is not None:
71-
pin.pull = mode
72-
self.function = lambda: pin.value
64+
if isinstance(io_or_predicate, digitalio.DigitalInOut):
65+
self.function = lambda: io_or_predicate.value
7366
else:
74-
self.function = pin_or_predicate
67+
self.function = io_or_predicate
7568
if self.function():
76-
self.__set_state(Debouncer.DEBOUNCED_STATE | Debouncer.UNSTABLE_STATE)
69+
self._set_state(_DEBOUNCED_STATE | _UNSTABLE_STATE)
7770
self.previous_time = 0
7871
if interval is None:
7972
self.interval = 0.010
8073
else:
8174
self.interval = interval
8275

8376

84-
def __set_state(self, bits):
77+
def _set_state(self, bits):
8578
self.state |= bits
8679

8780

88-
def __unset_state(self, bits):
81+
def _unset_state(self, bits):
8982
self.state &= ~bits
9083

9184

92-
def __toggle_state(self, bits):
85+
def _toggle_state(self, bits):
9386
self.state ^= bits
9487

9588

96-
def __get_state(self, bits):
89+
def _get_state(self, bits):
9790
return (self.state & bits) != 0
9891

9992

10093
def update(self):
10194
"""Update the debouncer state. MUST be called frequently"""
10295
now = time.monotonic()
103-
self.__unset_state(Debouncer.CHANGED_STATE)
96+
self._unset_state(_CHANGED_STATE)
10497
current_state = self.function()
105-
if current_state != self.__get_state(Debouncer.UNSTABLE_STATE):
98+
if current_state != self._get_state(_UNSTABLE_STATE):
10699
self.previous_time = now
107-
self.__toggle_state(Debouncer.UNSTABLE_STATE)
100+
self._toggle_state(_UNSTABLE_STATE)
108101
else:
109102
if now - self.previous_time >= self.interval:
110-
if current_state != self.__get_state(Debouncer.DEBOUNCED_STATE):
103+
if current_state != self._get_state(_DEBOUNCED_STATE):
111104
self.previous_time = now
112-
self.__toggle_state(Debouncer.DEBOUNCED_STATE)
113-
self.__set_state(Debouncer.CHANGED_STATE)
105+
self._toggle_state(_DEBOUNCED_STATE)
106+
self._set_state(_CHANGED_STATE)
114107

115108

116109
@property
117110
def value(self):
118111
"""Return the current debounced value."""
119-
return self.__get_state(Debouncer.DEBOUNCED_STATE)
112+
return self._get_state(_DEBOUNCED_STATE)
120113

121114

122115
@property
123116
def rose(self):
124117
"""Return whether the debounced value went from low to high at the most recent update."""
125-
return self.__get_state(self.DEBOUNCED_STATE) and self.__get_state(self.CHANGED_STATE)
118+
return self._get_state(_DEBOUNCED_STATE) and self._get_state(_CHANGED_STATE)
126119

127120

128121
@property
129122
def fell(self):
130123
"""Return whether the debounced value went from high to low at the most recent update."""
131-
return (not self.__get_state(self.DEBOUNCED_STATE)) and self.__get_state(self.CHANGED_STATE)
124+
return (not self._get_state(_DEBOUNCED_STATE)) and self._get_state(_CHANGED_STATE)

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# Uncomment the below if you use native CircuitPython modules such as
2121
# digitalio, micropython and busio. List the modules you use. Without it, the
2222
# autodoc module docs will fail to generate with a warning.
23-
autodoc_mock_imports = ["digitalio", "microcontroller"]
23+
autodoc_mock_imports = ["digitalio"]
2424

2525

2626
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}

examples/debouncer_crickit_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@
3434

3535
ss = crickit.seesaw
3636

37-
def make_criket_signal_debouncer(pin):
37+
def make_crikit_signal_debouncer(pin):
3838
"""Return a lambda to read the specified pin"""
3939
ss.pin_mode(pin, ss.INPUT_PULLUP)
4040
return Debouncer(lambda: ss.digital_read(pin))
4141

4242
# Two buttons are pullups, connect to ground to activate
43-
clock = make_criket_signal_debouncer(crickit.SIGNAL1)
44-
signal_2 = make_criket_signal_debouncer(crickit.SIGNAL2)
45-
signal_3 = make_criket_signal_debouncer(crickit.SIGNAL3)
46-
signal_4 = make_criket_signal_debouncer(crickit.SIGNAL4)
47-
signal_5 = make_criket_signal_debouncer(crickit.SIGNAL5)
43+
clock = make_crikit_signal_debouncer(crickit.SIGNAL1)
44+
signal_2 = make_crikit_signal_debouncer(crickit.SIGNAL2)
45+
signal_3 = make_crikit_signal_debouncer(crickit.SIGNAL3)
46+
signal_4 = make_crikit_signal_debouncer(crickit.SIGNAL4)
47+
signal_5 = make_crikit_signal_debouncer(crickit.SIGNAL5)
4848

4949
while True:
5050
clock.update()

examples/debouncer_simpletest.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
# pylint: disable=invalid-name
2626

2727
import board
28-
from digitalio import Pull
28+
import digitalio
2929
from adafruit_debouncer import Debouncer
3030

31-
switch = Debouncer(board.D12, Pull.UP)
31+
pin = digitalio.DigitalInOut(board.D12)
32+
pin.direction = digitalio.Direction.INPUT
33+
pin.pull = digitalio.Pull.UP
34+
switch = Debouncer(pin)
3235

3336
while True:
3437
switch.update()

0 commit comments

Comments
 (0)