Commit a9425cd
committed
digitalio: Improve DigitalInOut constructor, add finaliser
Now it's possible to construct outputs or enable pulls immediately:
```py
>>> d = digitalio.DigitalInOut(board.A0, pull=digitalio.Pull.UP)
>>> d.pull
digitalio.Pull.UP
```
```py
>>> d = digitalio.DigitalInOut(board.A0, value=True)
>>> d.direction
digitalio.Direction.OUTPUT
>>> d.value
True
```
```py
>>> d = digitalio.DigitalInOut(board.A0, value=False, drive_mode=digitalio.DriveMode.OPEN_DRAIN)
>>> d.drive_mode
digitalio.DriveMode.OPEN_DRAIN
```
The finaliser means that if a pin object is allowed to go out of scope,
then it will be deinitialized automatically when GC runs. **this is a
potential incompatibility**: some hypothetical code that intentionally
leaked a pin might function differently, i.e.
```py
digitalio.DigitalInOut(board.A0).switch_to_output(True)
```
as a way to stick a pin in output mode indefinitely will no longer work
(but may still appear to work "for awhile" because gc doesn't run right
away).
I made this change in part because it simplifies error handling in the
constructor: An incorrect argument would otherwise have left the pin
stuck in allocated state until the interpreter was reset; with this
change, it's available again though not until the GC runs so it would
remain perplexing in interactive situations:
```py
>>> d = digitalio.DigitalInOut(board.A0, pull=7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pull must be of type Pull or None, not int
>>> d = digitalio.DigitalInOut(board.A0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: A0 in use
>>> gc.collect()
>>> d = digitalio.DigitalInOut(board.A0)
>>> d
<DigitalInOut>
```1 parent 3a0b97d commit a9425cd
File tree
1 file changed
+41
-8
lines changed1 file changed
+41
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
29 | 32 | | |
30 | 33 | | |
31 | 34 | | |
| |||
58 | 61 | | |
59 | 62 | | |
60 | 63 | | |
61 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
62 | 72 | | |
63 | | - | |
64 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
65 | 77 | | |
66 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
67 | 84 | | |
68 | 85 | | |
69 | | - | |
70 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
71 | 96 | | |
72 | | - | |
| 97 | + | |
73 | 98 | | |
74 | | - | |
| 99 | + | |
75 | 100 | | |
76 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
77 | 109 | | |
78 | 110 | | |
79 | 111 | | |
| |||
325 | 357 | | |
326 | 358 | | |
327 | 359 | | |
| 360 | + | |
328 | 361 | | |
329 | 362 | | |
330 | 363 | | |
| |||
0 commit comments