signal_led – control if WS2812 LEDs

This module provides a simplified driver for a single WS2812 LED

Note

The module may be unavailable on some boards (either completely unavailable, or replaced by the multi_led module). It is not recommended to create an instance of either class in this module. Instead, import RGB (an instance of the SmartLED class), or GLED and RLED instances of the SignalLED class from the rfid_utils module

class SmartLED

This class stores pixel data for a WS2812 LED connected to a pin.

class signal_led.SmartLED(pin)

Construct a SmartLED object. The parameters are:

  • pin is a GPIO number

set(rgb, show=True)

Set the RGB color of the first led in the strip. If show==True, apply changes immediately. The rgb parameter may be an integer (0xRRGGBB), a string / a bytes-like object ('#RRGGBB' or '#RGB'), or a tuple of 3 integer values in the [0..255] range.

color(index, value=None, show=True)

Get (if value==None) or set the color component index of the led (the indices start from zero: 0 - red, 1 - green, 2 - blue). The value must be an integer in the range [0..255].

The returned value is an integer in the same range representing the color component of the LED (the current one or the newly set).

led(clr)

Creates and returns an instance of the SignalLED class. The instance will control the selected color of the LED. The format of the clr parameter is the same as outlined above.

__len__()

Returns the number of LEDs in the strip (1 for this module).

__getitem__(idx)

Returns the color of the LED idx (the only valid value is zero). The returned value is an integer (0xRRGGBB).

__setitem__(idx, value)

Sets the color for the LED at index. The change is applied immediately. The format of the value must be the same as the one for the set() method.

fill(value)

Sets the color of each LED in the strip. This is effectively the same as set(value, show=True) (kept for compatibility with the multi_led module).

class SignalLED

This class provides a wrapper over a single WS2812 LED, converting it into a virtual fixed-color LED.

Note

It is possible to create multiple instances of the SignaLED class that control the same LED. This will work as intended as long as only one virtual LED is switched on at a time. The GLED and RLED objects in the rfid_utils are an example of such a case.

class signal_led.SignalLED(parent, color)

Construct a SignalLED object

  • parent - an instance of the SmartLED class

  • color - this object’s fixed color value (ref set() for the format)

Each instance occupies one hardware timer. If no more timers are available, a RuntimeError is raised.

on()

Switch the LED on

off()

Switch the LED off. Also deactivates the “glow”, the “heartbeat”, and the “blink” effects.

property extended

Read-only. Returns True. This can be used to determine which module is being used: signal_led (this one, True), or multi_led (will return False).

property enabled

Getter: Returns the LED state. Setter: calls on() or off() respectively

Starts the timer, that will switch the LED on and off at a specified interval.

heartbeat()

Starts the timer with the heartbeat effect. Deactivates all the other effects.

glow()

Starts the timer with the glow effect. Deactivates all the other effects.