Source code for pitop.pma.led

from gpiozero import LED as gpiozero_LED

from pitop.core.mixins import Recreatable, Stateful
from pitop.pma.common import get_pin_for_port

LED_COLORS = ["red", "green", "yellow"]


[docs]class LED(Stateful, Recreatable, gpiozero_LED): """Encapsulates the behaviour of an LED. An LED (Light Emitting Diode) is a simple light source that can be controlled directly. :param str port_name: The ID for the port to which this component is connected :param str name: Component name, defaults to `led`. Used to access this component when added to a :class:`pitop.Pitop` object. """ def __init__(self, port_name, name="led", color=None): self._pma_port = port_name self.name = name self.color = color if color in LED_COLORS else LED_COLORS[0] Stateful.__init__(self) Recreatable.__init__( self, {"port_name": port_name, "name": self.name, "color": self.color} ) gpiozero_LED.__init__(self, get_pin_for_port(self._pma_port)) @property def own_state(self): return { "is_lit": self.is_lit, "value": self.value, }
[docs] def close(self): """Shut down the device and release all associated resources. This method can be called on an already closed device without raising an exception. This method is primarily intended for interactive use at the command line. It disables the device and releases its pin(s) for use by another device. You can attempt to do this simply by deleting an object, but unless you've cleaned up all references to the object this may not work (even if you've cleaned up all references, there's still no guarantee the garbage collector will actually delete the object at that point). By contrast, the close method provides a means of ensuring that the object is shut down. For example, if you have a buzzer connected to port D0, but then wish to attach an LED instead: >>> from pitop import Buzzer, LED >>> bz = Buzzer("D0") >>> bz.on() >>> bz.off() >>> bz.close() >>> led = LED("D0") >>> led.blink() :class:`Device` descendents can also be used as context managers using the :keyword:`with` statement. For example: >>> from pitop import Buzzer, LED >>> with Buzzer("D0") as bz: ... bz.on() ... >>> with LED("D0") as led: ... led.on() ... """ super(LED, self).close()