44class Signal -- control and sense external I/O devices
55======================================================
66
7- The Signal class is a simple extension of Pin class. Unlike Pin, which
7+ The Signal class is a simple extension of the ` Pin ` class. Unlike Pin, which
88can be only in "absolute" 0 and 1 states, a Signal can be in "asserted"
99(on) or "deasserted" (off) states, while being inverted (active-low) or
10- not. Summing up , it adds logical inversion support to Pin functionality.
10+ not. In other words , it adds logical inversion support to Pin functionality.
1111While this may seem a simple addition, it is exactly what is needed to
1212support wide array of simple digital devices in a way portable across
1313different boards, which is one of the major MicroPython goals. Regardless
14- whether different users have an active-high or active-low LED, a normally
15- open or normally closed relay - you can develop single, nicely looking
14+ of whether different users have an active-high or active-low LED, a normally
15+ open or normally closed relay - you can develop a single, nicely looking
1616application which works with each of them, and capture hardware
17- configuration differences in few lines on the config file of your app.
17+ configuration differences in few lines in the config file of your app.
18+
19+ Example::
20+
21+ from machine import Pin, Signal
22+
23+ # Suppose you have an active-high LED on pin 0
24+ led1_pin = Pin(0, Pin.OUT)
25+ # ... and active-low LED on pin 1
26+ led2_pin = Pin(1, Pin.OUT)
27+
28+ # Now to light up both of them using Pin class, you'll need to set
29+ # them to different values
30+ led1_pin.value(1)
31+ led2_pin.value(0)
32+
33+ # Signal class allows to abstract away active-high/active-low
34+ # difference
35+ led1 = Signal(led1_pin, invert=False)
36+ led2 = Signal(led2_pin, invert=True)
37+
38+ # Now lighting up them looks the same
39+ led1.value(1)
40+ led2.value(1)
41+
42+ # Even better:
43+ led1.on()
44+ led2.on()
1845
1946Following is the guide when Signal vs Pin should be used:
2047
@@ -33,11 +60,11 @@ architecture of MicroPython: Pin offers the lowest overhead, which may
3360be important when bit-banging protocols. But Signal adds additional
3461flexibility on top of Pin, at the cost of minor overhead (much smaller
3562than if you implemented active-high vs active-low device differences in
36- Python manually!). Also, Pin is low-level object which needs to be
63+ Python manually!). Also, Pin is a low-level object which needs to be
3764implemented for each support board, while Signal is a high-level object
3865which comes for free once Pin is implemented.
3966
40- If in doubt, give the Signal a try! Once again, it is developed to save
67+ If in doubt, give the Signal a try! Once again, it is offered to save
4168developers from the need to handle unexciting differences like active-low
4269vs active-high signals, and allow other users to share and enjoy your
4370application, instead of being frustrated by the fact that it doesn't
0 commit comments