Skip to content

neopixel

loboris edited this page Jan 3, 2018 · 7 revisions

machine Module

Class Neopixel


This class includes full support for various types of Neopixel, individually-addressable RGB(W) LEDs.
ESP32 RMT peripheral is used for very precise timing ( +/- 50 ns ).
Up to 8 Neopixel strips can be used, 1~1024 pixels each.


Connecting the Neopixel strip

ESP32 pin Neopixel Notes
Any output pin DIN Neopixel data input pin
GND GND Neopixel ground
4 - 7V 5VDC Neopixel Power

Make shure the Neopixel Power can supply enough power to drive all connected LEDs.
Do not use ESP32 3.3V


Before using the display, the machine module must be imported and the instance of the Neopixel class has to be created:

np = machine(pin, pixels, type)

pin can be given as pin number or machine.Pin object. pixels is the number of the pixels, 1 ~ 1024 type Neopixel type: 0 (machine.Neopixel.RGB) for RGB LEDs, or 1 (machine.Neopixel.RGBQ) for RGBW LEDs

import display
np = display.TFT(22, 115, 0)
npw = display.TFT(machine.Pin(25), 24, machine.Neopixel.RGBW)

Colors

color value are given as 24 bit integer numbers, 8-bit per R,G,B color.
For example: 0xFF0000 represents the RED color.
The following color constants are defined and can be used as color arguments:
BLACK, WHITE, RED, LIME, BLUE, YELLOW, CYAN, MAGENTA, SILVER, GRAY, MAROON, OLIVE, GREEN, PURPLE, TEAL, NAVY

For RGBW Neopixels, white argument can be given as integer 0 ~ 255.


Methods

np.set( pos, color [, white, num, update] )

Set the color of the pixe at position pos

Argument Description
pos required, pixel position; 1 ~ pix_num
color required, pixel color; use nuneric value or predifined color constant
white optional; default: 0; white level; only for RGBW Neopixels
num optional; default: 1; number of pixels to set to the same color, starting from pos
update optional, default: True; update the Neopixel strip. If False np.show() has to be used to update the strip

np.get( pos )

Get the color of the pixe at position pos For 24-bit RGB Neopixels returns the integer representing the RGB color value. For 32-bit RGBW Neopixels returns the tuple of 24-bit RGB color value and 8-bit while level.

np.setHSB( pos, hue, saturation, brightness [, white, num, update] )

Set the color of the pixe at position pos using hue/saturation/brightness color model

Argument Description
pos required, pixel position; 1 ~ pix_num
hue float: any number, the floor of this number is subtracted from it to create a fraction between 0 and 1. This fractional number is then multiplied by 360 to produce the hue angle in the HSB color model.
saturation float; 0 ~ 1.0
brightness float; 0 ~ 1.0
num optional; default: 1; number of pixels to set to the same color, starting from pos
update optional, default: True; update the Neopixel strip. If False np.show() has to be used to update the strip

np.brightness([brightness] [, update])

Set or get the brightnes factor. Brightnes factor is used to reduce the color level for all the pixels in the strip. Alloved value is 1 to 255.

If no argument is given, the current brightnes factor will be returned. If update=True the Neopixel strip will be updated immediately.

np.color_order([color_order])

The pixel color is represented as 24-bit RGB value where RED is most significant byte and BLUE the least significant value. Various Neopixels types requires the color bytes to be sent in specific order (for example WS2812 requires "GRB" color order).

The color order can be set to any combination of 'R', 'G', 'B' and 'W' (for RGBW type Neopixels). Without arguments, returns the current color order.

np.color_timings([(timings)])

Various Neopixels types may require different timings for encoding the 1 and 0 bits in comunication protocol. When Neopixel object is created, the default timings for WS2812 are used for RGB type and SK6812 timings for RGBW type.

The timings argument must be tuple with the following structure: ((T1H,T1L), (T0H, T0L), Treset). All times are given as ns (nano second) values.

Without arguments, returns the current timings tuple.

np.HSBtoRGB(hue, saturation, brightness)

Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model. Returns 24-bit integer value suitable to be used as color argiment

Arguments

Argument Description
hue float: any number, the floor of this number is subtracted from it to create a fraction between 0 and 1. This fractional number is then multiplied by 360 to produce the hue angle in the HSB color model.
saturation float; 0 ~ 1.0
brightness float; 0 ~ 1.0

np.RGBtoHSBt(color)

Converts 24-bit integer value to the the components of a color, as specified by the HSB model. Returns tuple: (hue, saturation, brightness)

np.show()

Update the Neopixel strip.

np.clear()

clear the Neopixel strip.

np.deinit()

Deinitialize the Neopixel Object and free all used memory

Example

import machine, time

np=machine.Neopixel(machine.Pin(22), 24)

def rainbow(loops=120, delay=1, sat=1.0, bri=0.2):
    for pos in range(0, loops):
        for i in range(0, 24):
            dHue = 360.0/24*(pos+i);
            hue = dHue % 360;
            np.setHSB(i, hue, sat, bri, 1, False)
        np.show()
        if delay > 0:
            time.sleep_ms(delay)

def blinkRainbow(loops=10, delay=250):
    for pos in range(0, loops):
        for i in range(0, 24):
            dHue = 360.0/24*(pos+i);
            hue = dHue % 360;
            np.setHSB(i, hue, 1.0, 0.1, 1, False)
        np.show()
        time.sleep_ms(delay)
        np.clear()
        time.sleep_ms(delay)
Clone this wiki locally