Skip to content

Commit 552fd2f

Browse files
authored
Merge pull request peterhinch#38 from andydhobbs/default-pin-state
Default pin state
2 parents 1738720 + 603b624 commit 552fd2f

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

v3/docs/DRIVERS.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ double-click or long press events.
133133

134134
This can support normally open or normally closed switches, connected to `gnd`
135135
(with a pullup) or to `3V3` (with a pull-down). The `Pin` object should be
136-
initialised appropriately. The assumption is that on instantiation the button
137-
is not pressed.
136+
initialised appropriately. The default state of the switch can be passed in the
137+
optional "sense" parameter on the constructor, otherwise the assumption is that
138+
on instantiation the button is not pressed.
138139

139140
The Pushbutton class uses logical rather than physical state: a button's state
140141
is considered `True` if pressed, otherwise `False` regardless of its physical
@@ -151,6 +152,8 @@ Constructor arguments:
151152
1. `pin` Mandatory. The initialised Pin instance.
152153
2. `suppress` Default `False`. See
153154
[section 4.1.1](./DRIVERS.md#411-the-suppress-constructor-argument).
155+
3. `sense` Default `None`. See
156+
[section 4.1.1](./DRIVERS.md#412-the-sense-constructor-argument).
154157

155158
Methods:
156159

@@ -221,6 +224,14 @@ set, `release_func` will be launched as follows:
221224
4. If `double_func` exists and a double click occurs, `release_func` will not
222225
be launched.
223226

227+
### 4.1.2 The sense constructor argument
228+
229+
When the pin value changes, the new value is compared with `sense` to determine
230+
if the button is closed or open. This is to allow the designer to specify if
231+
the `closed` state of the button is active `high` or active `low`.
232+
233+
This parameter will default to the current value of `pin` for convienence.
234+
224235
###### [Contents](./DRIVERS.md#1-contents)
225236

226237
# 5. ADC monitoring

v3/primitives/pushbutton.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Pushbutton:
1515
debounce_ms = 50
1616
long_press_ms = 1000
1717
double_click_ms = 400
18-
def __init__(self, pin, suppress=False):
18+
def __init__(self, pin, suppress=False, sense=None):
1919
self.pin = pin # Initialise for input
2020
self._supp = suppress
2121
self._dblpend = False # Doubleclick waiting for 2nd click
@@ -26,7 +26,7 @@ def __init__(self, pin, suppress=False):
2626
self._lf = False
2727
self._ld = False # Delay_ms instance for long press
2828
self._dd = False # Ditto for doubleclick
29-
self.sense = pin.value() # Convert from electrical to logical value
29+
self.sense = pin.value() if sense is None else sense # Convert from electrical to logical value
3030
self.state = self.rawstate() # Initial state
3131
asyncio.create_task(self.buttoncheck()) # Thread runs forever
3232

0 commit comments

Comments
 (0)