Skip to content

Commit 6607123

Browse files
committed
DRIVERS.md Improve and add code samples.
1 parent 26fcad8 commit 6607123

File tree

1 file changed

+92
-26
lines changed

1 file changed

+92
-26
lines changed

DRIVERS.md

Lines changed: 92 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ This module provides the following classes:
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. In the following text the
37+
term **function** implies a Python `callable`: namely a function, bound method,
38+
coroutine or bound coroutine interchangeably.
3739

3840
## 3.1 Switch class
3941

4042
This assumes a normally open switch connected between a pin and ground. The pin
41-
should be initialised as an input with a pullup. Functions may be specified to
42-
run on contact closure or opening. Functions can be callbacks or coroutines;
43-
coroutines will be scheduled for execution and will run asynchronously.
43+
should be initialised as an input with a pullup. A **function** may be
44+
specified to run on contact closure or opening; where the **function** is a
45+
coroutine it will be scheduled for execution and will run asynchronously.
4446
Debouncing is implicit: contact bounce will not cause spurious execution of
4547
these functions.
4648

@@ -50,10 +52,10 @@ Constructor argument (mandatory):
5052

5153
Methods:
5254

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 `()`)
55+
1. `close_func` Args: `func` (mandatory) a **function** to run on contact
56+
closure. `args` a tuple of arguments for the **function** (default `()`)
57+
2. `open_func` Args: `func` (mandatory) a **function** to run on contact open.
58+
`args` a tuple of arguments for the **function** (default `()`)
5759
3. `__call__` Call syntax e.g. `myswitch()` returns the physical debounced
5860
state of the switch i.e. 0 if grounded, 1 if connected to `3V3`.
5961

@@ -62,6 +64,28 @@ Methods 1 and 2 should be called before starting the scheduler.
6264
Class attribute:
6365
1. `debounce_ms` Debounce time in ms. Default 50.
6466

67+
```python
68+
from pyb import LED
69+
from machine import Pin
70+
import uasyncio as asyncio
71+
from aswitch import Switch
72+
73+
async def pulse(led, ms):
74+
led.on()
75+
await asyncio.sleep_ms(ms)
76+
led.off()
77+
78+
async def my_app():
79+
await asyncio.sleep(60) # Dummy application code
80+
81+
pin = Pin('X1', Pin.IN, Pin.PULL_UP) # Hardware: switch to gnd
82+
red = LED(1)
83+
sw = Switch(pin)
84+
sw.close_func(pulse, (red, 1000)) # Note how coro and args are passed
85+
loop = asyncio.get_event_loop()
86+
loop.run_until_complete(my_app()) # Run main application code
87+
```
88+
6589
## 3.2 Pushbutton class
6690

6791
This can support normally open or normally closed switches, connected to `gnd`
@@ -70,11 +94,11 @@ initialised appropriately. The assumption is that on initialisation the button
7094
is not pressed.
7195

7296
The Pushbutton class uses logical rather than physical state: a button's state
73-
is considered `True` if pressed, otherwise `False` regardless of its
74-
physical implementation.
97+
is considered `True` if pressed, otherwise `False` regardless of its physical
98+
implementation.
7599

76-
Functions may be specified to run on button press, release, double click or
77-
long press events. Functions can be callbacks or coroutines; coroutines will be
100+
**function** instances may be specified to run on button press, release, double
101+
click or long press events; where the **function** is a coroutine it will be
78102
scheduled for execution and will run asynchronously.
79103

80104
Constructor argument (mandatory):
@@ -83,14 +107,14 @@ Constructor argument (mandatory):
83107

84108
Methods:
85109

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 `()`).
110+
1. `press_func` Args: `func` (mandatory) a **function** to run on button push.
111+
`args` a tuple of arguments for the **function** (default `()`).
112+
2. `release_func` Args: `func` (mandatory) a **function** to run on button
113+
release. `args` a tuple of arguments for the **function** (default `()`).
114+
3. `long_func` Args: `func` (mandatory) a **function** to run on long button
115+
push. `args` a tuple of arguments for the **function** (default `()`).
116+
4. `double_func` Args: `func` (mandatory) a **function** to run on double
117+
push. `args` a tuple of arguments for the **function** (default `()`).
94118
5. `__call__` Call syntax e.g. `mybutton()` Returns the logical debounced
95119
state of the button (`True` corresponds to pressed).
96120
6. `rawstate()` Returns the logical instantaneous state of the button. There
@@ -103,6 +127,26 @@ Class attributes:
103127
2. `long_press_ms` Threshold time in ms for a long press. Default 1000.
104128
3. `double_click_ms` Threshold time in ms for a double click. Default 400.
105129

130+
```python
131+
from pyb import LED
132+
from machine import Pin
133+
import uasyncio as asyncio
134+
from aswitch import Pushbutton
135+
136+
def toggle(led):
137+
led.toggle()
138+
139+
async def my_app():
140+
await asyncio.sleep(60) # Dummy
141+
142+
pin = Pin('X1', Pin.IN, Pin.PULL_UP) # Pushbutton to gnd
143+
red = LED(1)
144+
pb = Pushbutton(pin)
145+
pb.press_func(toggle, (red,)) # Note how function and args are passed
146+
loop = asyncio.get_event_loop()
147+
loop.run_until_complete(my_app()) # Run main application code
148+
```
149+
106150
## 3.3 Delay_ms class
107151

108152
This implements the software equivalent of a retriggerable monostable or a
@@ -114,15 +158,15 @@ as it is triggered before the time specified in the preceeding trigger it will
114158
never time out.
115159

116160
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
118-
be specified to the constructor. This will execute when a timeout occurs. The
119-
function can be a callback or a coroutine; in the latter case it will be
120-
scheduled for execution and will run asynchronously.
161+
interrogated by the object's `running()` method. In addition a **function** can
162+
be specified to the constructor. This will execute when a timeout occurs; where
163+
the **function** is a coroutine it will be scheduled for execution and will run
164+
asynchronously.
121165

122166
Constructor arguments (defaults in brackets):
123167

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

128172
Methods:
@@ -138,6 +182,28 @@ If the `trigger` method is to be called from an interrupt service routine the
138182
to use a slightly less efficient mode which avoids RAM allocation when
139183
`trigger` runs.
140184

185+
In this example a 3 second timer starts when the button is pressed. If it is
186+
pressed repeatedly the timeout will not be triggered. If it is not pressed for
187+
3 seconds the timeout triggers and the LED lights.
188+
189+
```python
190+
from pyb import LED
191+
from machine import Pin
192+
import uasyncio as asyncio
193+
from aswitch import Pushbutton, Delay_ms
194+
195+
async def my_app():
196+
await asyncio.sleep(60) # Dummy
197+
198+
pin = Pin('X1', Pin.IN, Pin.PULL_UP) # Pushbutton to gnd
199+
red = LED(1)
200+
pb = Pushbutton(pin)
201+
d = Delay_ms(lambda led: led.on(), (red,))
202+
pb.press_func(d.trigger, (3000,)) # Note how function and args are passed
203+
loop = asyncio.get_event_loop()
204+
loop.run_until_complete(my_app()) # Run main application code
205+
```
206+
141207
# 4. Module astests.py
142208

143209
This provides demonstration/test functions for the `Switch` and `Pushbutton`

0 commit comments

Comments
 (0)