Skip to content

Commit

Permalink
Added Button and Led classes
Browse files Browse the repository at this point in the history
  • Loading branch information
digicosmos86 committed Apr 23, 2018
1 parent b4609b3 commit a53c062
Showing 1 changed file with 83 additions and 1 deletion.
84 changes: 83 additions & 1 deletion source/actuators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ Relay

Deactivate the relay, open the circuit, and turn off whatever appliance that's connected to the relay.

.. method:: Relay.is_on()

Returns ``True`` if the relay is on, or ``False`` if the relay is off.

Grow Light Strip
----------------------------------

Expand All @@ -58,6 +62,84 @@ Grow Light Strip

Turns off the LED strip.

.. method:: GrowLight.is_on()

Returns ``True`` if the grow light is on, or ``False`` if it is off.

.. hint::

This class is a subclass of MicroPython's ``neopixel.NeoPixel`` class, so it can be programmed the same way as the Neo Pixel. See `this page <http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/neopixel.html>`_ for more details and examples.
This class is a subclass of MicroPython's ``neopixel.NeoPixel`` class, so it can be programmed the same way as the Neo Pixel. See `this page <http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/neopixel.html>`_ for more details and examples.

.. class:: Led(port[=1])

Allows control of a Grove LED socket. It is possible to switch the LEDs on the socket. The LEDs have polarities. The longer leg is positive.

.. method:: Led.on(fade[=False], duration[=None])

Turns on the LED. If the ``fade`` parameter is set to ``True``, then the led will turn on gradually in the number of seconds set to the ``duration`` parameter.

.. method:: Led.off(fade[=False], duration[=None])

Turns off the LED. If the ``fade`` parameter is set to ``True``, then the led will turn off gradually in the number of seconds set to the ``duration`` parameter.

.. method:: Led.is_on()

Returns ``True`` if the grow light is on, or ``False`` if it is off.

.. class:: Button(port[=2], pullup[=True])

Allows control of a Grove Button. There are two ways to use a button. First, you can access ``Button.is_pressed`` property or ``Button.is_pressed()`` method to determine if the button is pressed. Alternatively, you can also set a callback function with ``Button.on_release()`` method use the interrupt mechanism. Please see example of how to use the callback.

.. method:: Button.is_pressed()

Returns ``True`` if the button is pressed, or ``False`` if it is not.

.. method:: Button.on_press(callback)

Executes the ``callback`` function provided to the method when the button is pressed.

.. method:: Button.on_release(callback)

Executes the ``callback`` function provided to the method when the button is released.

Example
^^^^^^^^^^^^^^^^^^^^^

Controlling the LED with the Button

.. code-block:: python
from actuators import Led, Button
led = Led(1) # Specifies an LED at Port 1
button = Button(2) # Specifies a button at Port 2
## Turns on the LED when the button is pressed
while True:
if button.is_pressed():
led.on()
else:
led.off()
Turns the LED on/off with a callback function

.. code-block:: python
from actuators import Led, Button
led = Led(1) # Specifies an LED at Port 1
button = Button(2) # Specifies a button at Port 2
## Define a callback function
def turn_on_led():
global led # Need this line to refer to the led object outside the function.
if led.is_on():
led.off()
else:
led.on()
## Set the callback function to Button.on_release method.
button.on_release(callback=turn_on_led) # Note that no () are needed.

0 comments on commit a53c062

Please sign in to comment.