Skip to content

Commit 05d1e6e

Browse files
committed
aswitch.py: Delay_ms now has can_alloc constructor arg.
1 parent 5c1a6b6 commit 05d1e6e

File tree

3 files changed

+277
-77
lines changed

3 files changed

+277
-77
lines changed

DRIVERS.md

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ events.
1010

1111
# 2. Modules
1212

13-
1. ``aledflash.py`` Flashes the four Pyboard LED's asynchronously for 10s. The
13+
1. `aledflash.py` Flashes the four Pyboard LED's asynchronously for 10s. The
1414
simplest uasyncio demo. Import it to run.
15-
2. ``aswitch.py`` This provides classes for interfacing switches and
16-
pushbuttons and also a software retriggerable delay object. Pushbuttons are a
15+
2. `aswitch.py` This provides classes for interfacing switches and pushbuttons
16+
and also a software retriggerable delay object. Pushbuttons are a
1717
generalisation of switches providing logical rather than physical status along
1818
with double-clicked and long pressed events.
19-
3. ``asyn.py`` Provides synchronisation primitives. Required by ``aswitch.py``.
20-
4. ``astests.py`` Test/demonstration programs for ``aswitch.py``.
19+
3. `asyn.py` Provides synchronisation primitives. Required by `aswitch.py`.
20+
4. `astests.py` Test/demonstration programs for `aswitch.py`.
2121

2222
# 3. Module aswitch.py
2323

2424
This module provides the following classes:
2525

26-
* ``Switch`` This supports debouncing a normally open switch connected between
26+
* `Switch` This supports debouncing a normally open switch connected between
2727
a pin and ground. Can run callbacks or schedule coros on contact closure
2828
and/or opening.
29-
* ``Pushbutton`` A generalisation of ``Switch`` to support normally open or
29+
* `Pushbutton` A generalisation of `Switch` to support normally open or
3030
normally closed switches connected to ground or 3V3. Can run callbacks or
3131
schedule coros on double-click or long press events.
32-
* ``Delay_ms`` A class providing a retriggerable delay measured in ms. Can be
32+
* `Delay_ms` A class providing a retriggerable delay measured in ms. Can be
3333
used to run a callback or to schedule a coro. Its state can be tested by any
3434
coro.
3535

36-
The module ``astests.py`` provides examples of usage.
36+
The module `astests.py` provides examples of usage.
3737

3838
## 3.1 Switch class
3939

@@ -46,31 +46,31 @@ these functions.
4646

4747
Constructor argument (mandatory):
4848

49-
1. ``pin`` The initialised Pin instance.
49+
1. `pin` The initialised Pin instance.
5050

5151
Methods:
5252

53-
1. ``close_func`` Args: ``func`` (mandatory) a function to run on contact
54-
closure. ``args`` a tuple of arguments for the function (default ``()``)
55-
2. ``open_func`` Args: ``func`` (mandatory) a function to run on contact open.
56-
``args`` a tuple of arguments for the function (default ``()``)
57-
3. ``__call__`` Call syntax e.g. ``myswitch()`` returns the physical debounced
58-
state of the switch i.e. 0 if grounded, 1 if connected to ``3V3``.
53+
1. `close_func` Args: `func` (mandatory) a function to run on contact
54+
closure. `args` a tuple of arguments for the function (default `()`)
55+
2. `open_func` Args: `func` (mandatory) a function to run on contact open.
56+
`args` a tuple of arguments for the function (default `()`)
57+
3. `__call__` Call syntax e.g. `myswitch()` returns the physical debounced
58+
state of the switch i.e. 0 if grounded, 1 if connected to `3V3`.
5959

6060
Methods 1 and 2 should be called before starting the scheduler.
6161

6262
Class attribute:
63-
1. ``debounce_ms`` Debounce time in ms. Default 50.
63+
1. `debounce_ms` Debounce time in ms. Default 50.
6464

6565
## 3.2 Pushbutton class
6666

67-
This can support normally open or normally closed switches, connected to ``gnd``
68-
(with a pullup) or to ``3V3`` (with a pull-down). The ``Pin`` object should be
67+
This can support normally open or normally closed switches, connected to `gnd`
68+
(with a pullup) or to `3V3` (with a pull-down). The `Pin` object should be
6969
initialised appropriately. The assumption is that on initialisation the button
7070
is not pressed.
7171

7272
The Pushbutton class uses logical rather than physical state: a button's state
73-
is considered ``True`` if pressed, otherwise ``False`` regardless of its
73+
is considered `True` if pressed, otherwise `False` regardless of its
7474
physical implementation.
7575

7676
Functions may be specified to run on button press, release, double click or
@@ -79,61 +79,68 @@ scheduled for execution and will run asynchronously.
7979

8080
Constructor argument (mandatory):
8181

82-
1. ``pin`` The initialised Pin instance.
82+
1. `pin` The initialised Pin instance.
8383

8484
Methods:
8585

86-
1. ``press_func`` Args: ``func`` (mandatory) a function to run on button push.
87-
``args`` a tuple of arguments for the function (default ``()``).
88-
2. ``release_func`` Args: ``func`` (mandatory) a function to run on button
89-
release. ``args`` a tuple of arguments for the function (default ``()``).
90-
3. ``long_func`` Args: ``func`` (mandatory) a function to run on long button
91-
push. ``args`` a tuple of arguments for the function (default ``()``).
92-
4. ``double_func`` Args: ``func`` (mandatory) a function to run on double
93-
push. ``args`` a tuple of arguments for the function (default ``()``).
94-
5. ``__call__`` Call syntax e.g. ``mybutton()`` Returns the logical debounced
95-
state of the button (``True`` corresponds to pressed).
96-
6. ``rawstate()`` Returns the logical instantaneous state of the button. There
86+
1. `press_func` Args: `func` (mandatory) a function to run on button push.
87+
`args` a tuple of arguments for the function (default `()`).
88+
2. `release_func` Args: `func` (mandatory) a function to run on button
89+
release. `args` a tuple of arguments for the function (default `()`).
90+
3. `long_func` Args: `func` (mandatory) a function to run on long button
91+
push. `args` a tuple of arguments for the function (default `()`).
92+
4. `double_func` Args: `func` (mandatory) a function to run on double
93+
push. `args` a tuple of arguments for the function (default `()`).
94+
5. `__call__` Call syntax e.g. `mybutton()` Returns the logical debounced
95+
state of the button (`True` corresponds to pressed).
96+
6. `rawstate()` Returns the logical instantaneous state of the button. There
9797
is probably no reason to use this.
9898

9999
Methods 1 - 4 should be called before starting the scheduler.
100100

101101
Class attributes:
102-
1. ``debounce_ms`` Debounce time in ms. Default 50.
103-
2. ``long_press_ms`` Threshold time in ms for a long press. Default 1000.
104-
3. ``double_click_ms`` Threshold time in ms for a double click. Default 400.
102+
1. `debounce_ms` Debounce time in ms. Default 50.
103+
2. `long_press_ms` Threshold time in ms for a long press. Default 1000.
104+
3. `double_click_ms` Threshold time in ms for a double click. Default 400.
105105

106106
## 3.3 Delay_ms class
107107

108108
This implements the software equivalent of a retriggerable monostable or a
109-
watchdog timer. It has a boolean ``running`` state. When instantiated it does
110-
nothing, with ``running`` ``False`` until triggered, when ``running`` becomes
111-
``True``. A timer is then initiated. This can be prevented from timing out by
112-
triggering it again (with a new timeout duration). So long as it is triggered
113-
before the time specified in the preceeding trigger it will never time out.
114-
115-
If it does time out the ``running`` state will revert to ``False``. This can be
116-
interrogated by the object's ``running()`` method. In addition a function can
109+
watchdog timer. It has an internal boolean `running` state. When instantiated
110+
the `Delay_ms` instance does nothing, with `running` `False` until triggered.
111+
Then `running` becomes `True` and a timer is initiated. This can be prevented
112+
from timing out by triggering it again (with a new timeout duration). So long
113+
as it is triggered before the time specified in the preceeding trigger it will
114+
never time out.
115+
116+
If it does time out the `running` state will revert to `False`. This can be
117+
interrogated by the object's `running()` method. In addition a function can
117118
be specified to the constructor. This will execute when a timeout occurs. The
118119
function can be a callback or a coroutine; in the latter case it will be
119120
scheduled for execution and will run asynchronously.
120121

121122
Constructor arguments (defaults in brackets):
122123

123-
1. ``func`` The function to call on timeout (default ``None``).
124-
2. ``args`` A tuple of arguments for the function (default ``()``).
124+
1. `func` The function to call on timeout (default `None`).
125+
2. `args` A tuple of arguments for the function (default `()`).
126+
3. `can_alloc` Boolean, default `True`. See below.
125127

126128
Methods:
127129

128-
1. ``trigger`` mandatory argument ``duration``. A timeout will occur after
129-
``duration`` ms unless retriggered.
130-
2. ``stop`` No argument. Cancels the timeout, setting the ``running`` status
131-
``False``. The timer can be restarted by issuing ``trigger`` again.
132-
3. ``running`` No argument. Returns the running status of the object.
130+
1. `trigger` mandatory argument `duration`. A timeout will occur after
131+
`duration` ms unless retriggered.
132+
2. `stop` No argument. Cancels the timeout, setting the `running` status
133+
`False`. The timer can be restarted by issuing `trigger` again.
134+
3. `running` No argument. Returns the running status of the object.
135+
136+
If the `trigger` method is to be called from an interrupt service routine the
137+
`can_alloc` constructor arg should be `False`. This causes the delay object
138+
to use a slightly less efficient mode which avoids RAM allocation when
139+
`trigger` runs.
133140

134141
# 4. Module astests.py
135142

136-
This provides demonstration/test functions for the ``Switch`` and ``Pushbutton``
143+
This provides demonstration/test functions for the `Switch` and `Pushbutton`
137144
classes. They assume a switch or button wired between pin X1 and gnd. Tests may
138145
be terminated by grounding X2.
139146

0 commit comments

Comments
 (0)