1
- # 1 . Introduction
1
+ # 0 . Introduction
2
2
3
3
Drivers for switches and pushbuttons are provided, plus a retriggerable delay
4
4
class. The switch and button drivers support debouncing. The switch driver
@@ -11,6 +11,22 @@ events.
11
11
The asynchronous ADC supports pausing a task until the value read from an ADC
12
12
goes outside defined bounds.
13
13
14
+ # 1. Contents
15
+
16
+ 1 . [ Contents] ( ./DRIVERS.md#1-contents )
17
+ 2 . [ Installation and usage] ( ./DRIVERS.md#2-installation-and-usage )
18
+ 3 . [ Interfacing switches] ( ./DRIVERS.md#3-interfacing-switches ) Switch debouncer with callbacks.
19
+ 3.1 [ Switch class] ( ./DRIVERS.md#31-switch-class )
20
+ 4 . [ Interfacing pushbuttons] ( ./DRIVERS.md#4-interfacing-pushbuttons ) Extends Switch for long and double click events
21
+ 4.1 [ Pushbutton class] ( ./DRIVERS.md#41-pushbutton-class )
22
+   ;  ;  ;  ;  ; 4.1.1 [ The suppress constructor argument] ( ./DRIVERS.md#411-the-suppress-constructor-argument )
23
+ 5 . [ ADC monitoring] ( ./DRIVERS.md#5-adc-monitoring ) Pause until an ADC goes out of bounds
24
+ 5.1 [ AADC class] ( ./DRIVERS.md#51-aadc-class )
25
+ 5.2 [ Design note] ( ./DRIVERS.md#52-design-note )
26
+ 6 . [ Additional functions] ( ./DRIVERS.md#6-additional-functions )
27
+ 6.1 [ launch] ( ./DRIVERS.md#61-launch ) Run a coro or callback interchangeably
28
+ 6.2 [ set_global_exception] ( ./DRIVERS.md#62-set_global_exception ) Simplify debugging with a global exception handler
29
+
14
30
###### [ Tutorial] ( ./TUTORIAL.md#contents )
15
31
16
32
# 2. Installation and usage
@@ -38,11 +54,13 @@ from primitives.tests.adctest import test
38
54
test()
39
55
```
40
56
41
- # 3. primitives.switch
57
+ ###### [ Contents] ( ./DRIVERS.md#1-contents )
58
+
59
+ # 3. Interfacing switches
42
60
43
- This module provides the ` Switch ` class. This supports debouncing a normally
44
- open switch connected between a pin and ground. Can run callbacks or schedule
45
- coros on contact closure and/or opening.
61
+ The ` primitives.switch ` module provides the ` Switch ` class. This supports
62
+ debouncing a normally open switch connected between a pin and ground. Can run
63
+ callbacks or schedule coros on contact closure and/or opening.
46
64
47
65
In the following text the term ` callable ` implies a Python ` callable ` : namely a
48
66
function, bound method, coroutine or bound coroutine. The term implies that any
@@ -102,11 +120,14 @@ sw.close_func(pulse, (red, 1000)) # Note how coro and args are passed
102
120
asyncio.run(my_app()) # Run main application code
103
121
```
104
122
105
- # 4. primitives.pushbutton
123
+ ###### [ Contents] ( ./DRIVERS.md#1-contents )
124
+
125
+ # 4. Interfacing pushbuttons
106
126
107
- The ` Pushbutton ` class is generalisation of ` Switch ` to support normally open
108
- or normally closed switches connected to ground or 3V3. Can run a ` callable ` on
109
- on press, release, double-click or long press events.
127
+ The ` primitives.pushbutton ` module provides the ` Pushbutton ` class. This is a
128
+ generalisation of ` Switch ` to support normally open or normally closed switches
129
+ connected to ground or 3V3. Can run a ` callable ` on on press, release,
130
+ double-click or long press events.
110
131
111
132
## 4.1 Pushbutton class
112
133
@@ -175,8 +196,10 @@ pb.press_func(toggle, (red,)) # Note how function and args are passed
175
196
asyncio.run(my_app()) # Run main application code
176
197
```
177
198
178
- An alternative Pushbutton class with lower RAM usage is available
179
- [ here] ( https://github.com/kevinkk525/pysmartnode/blob/dev/pysmartnode/utils/abutton.py ) .
199
+ An alternative, compatible ` Pushbutton ` implementation is available
200
+ [ here] ( https://github.com/kevinkk525/pysmartnode/blob/dev/pysmartnode/utils/abutton.py ) :
201
+ this implementation avoids the use of the ` Delay_ms ` class to minimise the
202
+ number of coroutines.
180
203
181
204
### 4.1.1 The suppress constructor argument
182
205
@@ -209,15 +232,16 @@ the `closed` state of the button is active `high` or active `low`.
209
232
210
233
This parameter will default to the current value of ` pin ` for convienence.
211
234
235
+ ###### [ Contents] ( ./DRIVERS.md#1-contents )
212
236
213
- # 5. primitives.aadc
237
+ # 5. ADC monitoring
214
238
215
- The ` AADC ` (asynchronous ADC) class provides for coroutines which pause until
216
- the value returned by an ADC goes outside predefined bounds. The bounds can be
217
- absolute or relative to the current value. The data from ADC's is usually
218
- noisy. Relative bounds provide a simple (if crude) means of eliminating this.
219
- Absolute bounds can be used to raise an alarm, or log data, if the value goes
220
- out of range. Typical usage:
239
+ The ` primitives.aadc ` module provides the ` AADC ` (asynchronous ADC) class. This
240
+ provides for coroutines which pause until the value returned by an ADC goes
241
+ outside predefined bounds. Bounds may be absolute or relative to the current
242
+ value. Data from ADC's is usually noisy. Relative bounds provide a simple (if
243
+ crude) means of eliminating this. Absolute bounds can be used to raise an alarm
244
+ or log data, if the value goes out of range. Typical usage:
221
245
``` python
222
246
import uasyncio as asyncio
223
247
from machine import ADC
@@ -282,3 +306,50 @@ The `AADC` class uses the `uasyncio` stream I/O mechanism. This is not the most
282
306
obvious design. It was chosen because the plan for ` uasyncio ` is that it will
283
307
include an option for prioritising I/O. I wanted this class to be able to use
284
308
this for applications requiring rapid response.
309
+
310
+ ###### [ Contents] ( ./DRIVERS.md#1-contents )
311
+
312
+ # 6. Additional functions
313
+
314
+ ## 6.1 Launch
315
+
316
+ Importe as follows:
317
+ ``` python
318
+ from primitives import launch
319
+ ```
320
+ ` launch ` enables a function to accept a coro or a callback interchangeably. It
321
+ accepts the callable plus a tuple of args. If a callback is passed, ` launch `
322
+ runs it and returns the callback's return value. If a coro is passed, it is
323
+ converted to a ` task ` and run asynchronously. The return value is the ` task `
324
+ instance. A usage example is in ` primitives/switch.py ` .
325
+
326
+ ## 6.2 set_global_exception
327
+
328
+ Import as follows:
329
+ ``` python
330
+ from primitives import set_global_exception
331
+ ```
332
+ ` set_global_exception ` is a convenience funtion to enable a global exception
333
+ handler to simplify debugging. The function takes no args. It is called as
334
+ follows:
335
+
336
+ ``` python
337
+ import uasyncio as asyncio
338
+ from primitives import set_global_exception
339
+
340
+ async def main ():
341
+ set_global_exception()
342
+ # Main body of application code omitted
343
+
344
+ try :
345
+ asyncio.run(main())
346
+ finally :
347
+ asyncio.new_event_loop() # Clear retained state
348
+ ```
349
+ This is explained in the tutorial. In essence if an exception occurs in a task,
350
+ the default behaviour is for the task to stop but for the rest of the code to
351
+ continue to run. This means that the failure can be missed and the sequence of
352
+ events can be hard to deduce. A global handler ensures that the entire
353
+ application stops allowing the traceback and other debug prints to be studied.
354
+
355
+ ###### [ Contents] ( ./DRIVERS.md#1-contents )
0 commit comments