Skip to content

Commit 5a08f26

Browse files
committed
Do not need to specify whether an event source is EDGE or LEVEL triggered
when adding it to the Selector. The Selector looks for an attribute __trigger__ on the event source. If not found, assumes LEVEL. Can be overridden if required by explicitly setting the trigger when adding the event source to the Selector.
1 parent b7cf645 commit 5a08f26

3 files changed

Lines changed: 9 additions & 7 deletions

File tree

examples/epoll-button-blink

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import sys
44
import os
55
from contextlib import closing
66
from quick2wire.gpio import GPIOPin, Both, In, Out, exported
7-
from quick2wire.selector import Selector, INPUT, EDGE, Timer
7+
from quick2wire.selector import Selector, Timer
88

99
blink_rate=2.0
1010

@@ -13,8 +13,8 @@ with closing(Selector()) as selector, \
1313
exported(GPIOPin(0, direction=In, interrupt=Both)) as button_pin, \
1414
exported(GPIOPin(1, direction=Out)) as led_pin:
1515

16-
selector.add(button_pin, INPUT, trigger=EDGE)
17-
selector.add(timer, INPUT)
16+
selector.add(button_pin)
17+
selector.add(timer)
1818

1919
print("ready")
2020

quick2wire/gpio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import subprocess
77
from contextlib import contextmanager
88
from quick2wire.board_revision import revision
9+
from quick2wire.selector import EDGE
910

1011
# Maps header pin numbers to SoC GPIO numbers
1112
# See http://elinux.org/RPi_Low-level_peripherals
@@ -94,6 +95,8 @@ class _IOPin(object):
9495
PullDown = "pulldown"
9596
PullUp = "pullup"
9697

98+
__trigger__ = EDGE
99+
97100
def __init__(self, user_pin_number, soc_pin_number, direction=None, interrupt=None, pull=None):
98101
"""Creates a pin
99102

quick2wire/selector.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def __init__(self, size_hint=-1):
2323
"""Initialises a Selector.
2424
2525
Arguments:
26-
2726
size_hint -- A hint of the number of event sources that will
2827
be added to the Selector, or -1 for the default.
2928
Used to optimize internal data structures, it
@@ -39,11 +38,10 @@ def fileno(self):
3938
"""Returns the Selector's file descriptor."""
4039
return self._epoll.fileno()
4140

42-
def add(self, source, eventmask=INPUT|ERROR, trigger=LEVEL, identifier=None):
41+
def add(self, source, eventmask=INPUT|ERROR, trigger=None, identifier=None):
4342
"""Adds an event source to the Selector.
4443
4544
Arguments:
46-
4745
source -- the event source to add. Must provide a fileno()
4846
method that returns its file descriptor.
4947
@@ -66,6 +64,8 @@ def add(self, source, eventmask=INPUT|ERROR, trigger=LEVEL, identifier=None):
6664
source itself.
6765
"""
6866
fileno = source.fileno()
67+
trigger = trigger if trigger is not None else getattr(source, "__trigger__", LEVEL)
68+
6969
self._sources[fileno] = identifier if identifier is not None else source
7070
self._epoll.register(fileno, eventmask|(select.EPOLLET*trigger))
7171

@@ -94,7 +94,6 @@ def wait(self, timeout=-1):
9494
timeout, the `ready` property is `None`.
9595
9696
Arguments:
97-
9897
timeout -- maximum time to wait for an event. Specified in
9998
seconds (can be less than one). Default is no
10099
timeout: wait forever for an event.

0 commit comments

Comments
 (0)