Skip to content

Commit

Permalink
Add debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
waveform80 committed Sep 26, 2018
1 parent 46e9e2a commit 4c41df6
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions gpiozero/pins/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,20 @@ def _run(self, factory, queue):
pins = factory.pins
while not self._stop_evt.wait(0):
try:
pin, ticks, state = queue.get(timeout=0.1)
num, ticks, state = queue.get(timeout=0.1)
except Empty:
continue
try:
pins[pin]._call_when_changed(ticks, state)
pin = pins[num]
except KeyError:
pass
else:
if (
pin._last_call is None or pin._bounce is None or
factory.ticks_diff(ticks, pin._last_call) > pin._bounce
):
pin._call_when_changed(ticks, state)
pin._last_call = ticks


class NativeFactory(LocalPiFactory):
Expand Down Expand Up @@ -347,6 +354,7 @@ def __init__(self, factory, number):
self._rising_shift = number % 32
self._falling_offset = self.factory.mem.GPFEN_OFFSET + (number // 32)
self._falling_shift = number % 32
self._last_call = None
self._when_changed = None
self._change_thread = None
self._change_event = Event()
Expand Down Expand Up @@ -408,6 +416,12 @@ def _set_pull(self, value):
self.factory.mem[self._pull_offset] = 0
self._pull = value

def _get_bounce(self):
return self._bounce

def _set_bounce(self, value):
self._bounce = None if value is None else float(value)

def _get_edges(self):
try:
with io.open(self.factory.fs.path_edge(self.number), 'r') as f:
Expand Down

0 comments on commit 4c41df6

Please sign in to comment.