from gpiozero import Buzzer as gpiozero_Buzzer
from pitop.core.mixins import Recreatable, Stateful
from pitop.pma.common import get_pin_for_port
[docs]
class Buzzer(Stateful, Recreatable, gpiozero_Buzzer):
"""Encapsulates the behaviour of a simple buzzer that can be turned on and
off.
:param str port_name: The ID for the port to which this component is connected
:param str name: Component name, defaults to `buzzer`. Used to access this component when added to a :class:`pitop.Pitop` object.
"""
def __init__(self, port_name, name="buzzer"):
self._pma_port = port_name
self.name = name
Stateful.__init__(self)
Recreatable.__init__(self, {"port_name": port_name, "name": self.name})
gpiozero_Buzzer.__init__(self, get_pin_for_port(self._pma_port))
@property
def own_state(self):
return {
"is_active": self.is_active,
"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(Buzzer, self).close()