@@ -10,30 +10,30 @@ events.
10
10
11
11
# 2. Modules
12
12
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
14
14
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
17
17
generalisation of switches providing logical rather than physical status along
18
18
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 ` .
21
21
22
22
# 3. Module aswitch.py
23
23
24
24
This module provides the following classes:
25
25
26
- * `` Switch ` ` This supports debouncing a normally open switch connected between
26
+ * ` Switch ` This supports debouncing a normally open switch connected between
27
27
a pin and ground. Can run callbacks or schedule coros on contact closure
28
28
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
30
30
normally closed switches connected to ground or 3V3. Can run callbacks or
31
31
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
33
33
used to run a callback or to schedule a coro. Its state can be tested by any
34
34
coro.
35
35
36
- The module `` astests.py ` ` provides examples of usage.
36
+ The module ` astests.py ` provides examples of usage.
37
37
38
38
## 3.1 Switch class
39
39
@@ -46,31 +46,31 @@ these functions.
46
46
47
47
Constructor argument (mandatory):
48
48
49
- 1 . `` pin ` ` The initialised Pin instance.
49
+ 1 . ` pin ` The initialised Pin instance.
50
50
51
51
Methods:
52
52
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 ` .
59
59
60
60
Methods 1 and 2 should be called before starting the scheduler.
61
61
62
62
Class attribute:
63
- 1 . `` debounce_ms ` ` Debounce time in ms. Default 50.
63
+ 1 . ` debounce_ms ` Debounce time in ms. Default 50.
64
64
65
65
## 3.2 Pushbutton class
66
66
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
69
69
initialised appropriately. The assumption is that on initialisation the button
70
70
is not pressed.
71
71
72
72
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
74
74
physical implementation.
75
75
76
76
Functions may be specified to run on button press, release, double click or
@@ -79,61 +79,68 @@ scheduled for execution and will run asynchronously.
79
79
80
80
Constructor argument (mandatory):
81
81
82
- 1 . `` pin ` ` The initialised Pin instance.
82
+ 1 . ` pin ` The initialised Pin instance.
83
83
84
84
Methods:
85
85
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
97
97
is probably no reason to use this.
98
98
99
99
Methods 1 - 4 should be called before starting the scheduler.
100
100
101
101
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.
105
105
106
106
## 3.3 Delay_ms class
107
107
108
108
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
117
118
be specified to the constructor. This will execute when a timeout occurs. The
118
119
function can be a callback or a coroutine; in the latter case it will be
119
120
scheduled for execution and will run asynchronously.
120
121
121
122
Constructor arguments (defaults in brackets):
122
123
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.
125
127
126
128
Methods:
127
129
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.
133
140
134
141
# 4. Module astests.py
135
142
136
- This provides demonstration/test functions for the `` Switch `` and `` Pushbutton ` `
143
+ This provides demonstration/test functions for the ` Switch ` and ` Pushbutton `
137
144
classes. They assume a switch or button wired between pin X1 and gnd. Tests may
138
145
be terminated by grounding X2.
139
146
0 commit comments