|
| 1 | +.. currentmodule:: machine |
| 2 | +.. _machine.Signal: |
| 3 | + |
| 4 | +class Signal -- control and sense external I/O devices |
| 5 | +====================================================== |
| 6 | + |
| 7 | +The Signal class is a simple extension of Pin class. Unlike Pin, which |
| 8 | +can be only in "absolute" 0 and 1 states, a Signal can be in "asserted" |
| 9 | +(on) or "deasserted" (off) states, while being inverted (active-low) or |
| 10 | +not. Summing up, it adds logical inversion support to Pin functionality. |
| 11 | +While this may seem a simple addition, it is exactly what is needed to |
| 12 | +support wide array of simple digital devices in a way portable across |
| 13 | +different 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 |
| 16 | +application which works with each of them, and capture hardware |
| 17 | +configuration differences in few lines on the config file of your app. |
| 18 | + |
| 19 | +Following is the guide when Signal vs Pin should be used: |
| 20 | + |
| 21 | +* Use Signal: If you want to control a simple on/off (including software |
| 22 | + PWM!) devices like LEDs, multi-segment indicators, relays, buzzers, or |
| 23 | + read simple binary sensors, like normally open or normally closed buttons, |
| 24 | + pulled high or low, Reed switches, moisture/flame detectors, etc. etc. |
| 25 | + Summing up, if you have a real physical device/sensor requiring GPIO |
| 26 | + access, you likely should use a Signal. |
| 27 | + |
| 28 | +* Use Pin: If you implement a higher-level protocol or bus to communicate |
| 29 | + with more complex devices. |
| 30 | + |
| 31 | +The split between Pin and Signal come from the usecases above and the |
| 32 | +architecture of MicroPython: Pin offers the lowest overhead, which may |
| 33 | +be important when bit-banging protocols. But Signal adds additional |
| 34 | +flexibility on top of Pin, at the cost of minor overhead (much smaller |
| 35 | +than if you implemented active-high vs active-low device differences in |
| 36 | +Python manually!). Also, Pin is low-level object which needs to be |
| 37 | +implemented for each support board, while Signal is a high-level object |
| 38 | +which comes for free once Pin is implemented. |
| 39 | + |
| 40 | +If in doubt, give the Signal a try! Once again, it is developed to save |
| 41 | +developers from the need to handle unexciting differences like active-low |
| 42 | +vs active-high signals, and allow other users to share and enjoy your |
| 43 | +application, instead of being frustrated by the fact that it doesn't |
| 44 | +work for them simply because their LEDs or relays are wired in a slightly |
| 45 | +different way. |
| 46 | + |
| 47 | +Constructors |
| 48 | +------------ |
| 49 | + |
| 50 | +.. class:: Signal(pin_obj, invert=False) |
| 51 | + Signal(pin_arguments..., \*, invert=False) |
| 52 | + |
| 53 | + Create a Signal object. There're two ways to create it: |
| 54 | + |
| 55 | + * By wrapping existing Pin object - universal method which works for |
| 56 | + any board. |
| 57 | + * By passing required Pin parameters directly to Signal constructor, |
| 58 | + skipping the need to create intermediate Pin object. Available on |
| 59 | + many, but not all boards. |
| 60 | + |
| 61 | + The arguments are: |
| 62 | + |
| 63 | + - ``pin_obj`` is existing Pin object. |
| 64 | + |
| 65 | + - ``pin_arguments`` are the same arguments as can be passed to Pin constructor. |
| 66 | + |
| 67 | + - ``invert`` - if True, the signal will be inverted (active low). |
| 68 | + |
| 69 | +Methods |
| 70 | +------- |
| 71 | + |
| 72 | +.. method:: Signal.value([x]) |
| 73 | + |
| 74 | + This method allows to set and get the value of the signal, depending on whether |
| 75 | + the argument ``x`` is supplied or not. |
| 76 | + |
| 77 | + If the argument is omitted then this method gets the signal level, 1 meaning |
| 78 | + signal is asserted (active) and 0 - signal inactive. |
| 79 | + |
| 80 | + If the argument is supplied then this method sets the signal level. The |
| 81 | + argument ``x`` can be anything that converts to a boolean. If it converts |
| 82 | + to ``True``, the signal is active, otherwise it is inactive. |
| 83 | + |
| 84 | + Correspondence between signal being active and actual logic level on the |
| 85 | + underlying pin depends on whether signal is inverted (active-low) or not. |
| 86 | + For non-inverted signal, active status corresponds to logical 1, inactive - |
| 87 | + to logical 0. For inverted/active-low signal, active status corresponds |
| 88 | + to logical 0, while inactive - to logical 1. |
| 89 | + |
| 90 | +.. method:: Signal.on() |
| 91 | + |
| 92 | + Activate signal. |
| 93 | + |
| 94 | +.. method:: Signal.off() |
| 95 | + |
| 96 | + Deactivate signal. |
0 commit comments