Demo 14 - Using the Colorful Light Ring on VisionFive 2

According to StarFive, developers have successfully used VisionFive/VisionFive 2 to control colorful light ring, and the specific instructions are as follows:

1. Preparation

  • Development board: VisionFive/VisionFive 2 SBC
  • Colorful light ring: WS2812B light ring with 24 Beads
  • DuPont Line: Many
    image

2. The principal of using the light ring

image
image
On the interface of the lamp ring, in addition to VCC (+) and GND (-), there are also DIN for outputting control signals, and DOUT for transmitting signals to subsequent lamp beads.

All WS2812B based light rings, dot matrices, etc. have the same principle of use and are connected one by one:
image
WS2812B controls a light bead requires 24 bits of data, representing the three color values of GRB:
4.WS2812B2B-1
To control multiple light beads, continuously send multiple sets of 24bit data as mentioned above:


Of course, sending the above data requires packaging and sending according to certain specifications. The specific packaging rules can be found in WS2812B version of Stargate 3.
In this demo, we use the SPI interface to send the control data required for WS2812B, thereby achieving the colorful light ring control of 24 light beads.

3. Connection

Firstly, refer to the following figure and connect the colorful light ring to VisionFive:
image
Connection:
image
Note:
The power supply voltage should be determined based on the actual sensor used. The module in this demo uses a 5V power supply voltage.

5. Coding

Then, we write the following program:

# -*- coding: utf-8 -*-
# file: ~/projects/spi/ws2812b_test.py

import time
from lib import neopixel_spidev as np
from lib.pixelbuf import wheel

with np.NeoPixelSpiDev(1, 0, n=24, pixel_order=np.GRB) as pixels:
    try:
        while True:
            print("整圈颜色变化效果:")
            pixels.fill((0,0,0))
            time.sleep(1)
            for i in range(0,255,2):
                print(" %d" % i, end="",flush=True)
                pixels.fill(wheel(i))
                time.sleep(0.01)

            print("")
            time.sleep(1)
            pixels.fill((0,0,0))
            time.sleep(1)

            print("依次点亮效果:")
            for i in range(pixels.n):
                print(" %d" % i, end="", flush=True)
                if i>0:
                    pixels[i-1] = (0,0,0)
                pixels[i] = wheel(128)
                time.sleep(0.1)

            print("")
            time.sleep(1)
            pixels.fill((0,0,0))
            time.sleep(1)

            print("数量变化效果:")
            colors = (
                (0,0,0),
                (128,0,0),
                (0,128,0),
                (0,0,128),
                (128,128,0),
                (128,0,128),
                (0,128,128),
                (128,128,128)
            )
            for n in (1,2,3,1,1,2,3,1,3,4,5,3,4,5,5,6,5,4,3,1,5,6,5,4,3,1,1,5,1,1,5,1):
                print("n=%d" % n, end="", flush=True)
                pixels.fill((0,0,0))
                time.sleep(0.1)
                for i in range(n*2):
                    print(" %d" % i, end="", flush=True)
                    pixels[i] = colors[n]
                time.sleep(0.4)
                print("")

            time.sleep(1)
            pixels.fill((0,0,0))
            time.sleep(1)

            time.sleep(5);
    except KeyboardInterrupt:
        pass

Sending the control signal of WS2812B requires specific structured data, and we can assemble it ourselves after understanding its principle. In the above code, the py-neopixel-spidev extension library is used, which outputs control signals to WS2812B through the SPI interface of the board.
In the code, a total of three effects were demonstrated:

  • The color change of the entire dazzling light ring, from (0,0,0) to (255255255)
  • Single light bead illuminated at once
  • According to the simplified notation of Two Tigers, a certain number of light beads are lit each time, and the corresponding colors are selected from the colors based on the serial number of the simplified notation

5. Operation result

To run the above program, you also need to install the corresponding StarFive GPIO support library, as follows:

# Install extension library
git clone https://github.com/fschrempf/py-neopixel-spidev.git
cd py-neopixel-spidev
python setup.py build
sudo python setup.py install

image
After writing, run ws2812b_test.py, and you can see the operation result of the light.

sudo python3 ws2812b_test.py

The following is the output:
image
The actual effect is as follows:

  • The color of the entire light ring changes:

    image
  • Single light bead illuminated at once:
    image
  • Illuminate a specified number of light beads at once:
    image
    image

6. Summary

In this demo, we learned to control the WS2812B colorful light ring.
WS2812B is used in many applications, such as city night view, Laser lighting display, advertising cabinet, jewelry display cabinet, toys, etc.
On Valentine’s Day or May 20th, you can also create a heart with brilliant effects and give it to your beloved.

2 Likes

This is a common way to drive NeoPixel (WS281x and compatibles) on chips without great low-level timings. Doing lookups of the bit patterns and then manually stuffing on SPI works fine for low numbers of pixel and is OK on a part like this with high capacity CPUs where you can burn one core just to assemble single Packet and feed the CPU one packet at a time.

Precise control of a digital output with low host CPU overhead is an area where chips like ESP32 or RP2040 shine. The latter has two tiny Programmed I/O cores that are even more Reduced than RISC-V - I think they have, like six opcodes. But set it to run at the right frequency divider on the host to match the 400 kHz signal needed to feed NeoPixel and these will feed LEDs without involving the main cores at all. The ESP has a peripheral originally meant for blinking infrared light controllers, the RMT, that lets you craft lengthy, chained, even repeating buffers via DMA. You can drive thousands of lights with something like 3-4% overhead just because the DMAC is competing on the memory bus.

We get that Vision Five might not be the ideal Christmas Vacation LED controller, but low-overhead, high-precision of digital outputs IS handy for protocol generation of various types. It’s somewhat ironic that being able to dedicated a PIC, STM32F, Arduino, ESP32, or other <$1 cpu to handle the timing works better than most “real” CPUs.

In the 7110 TRM from April (still “preliminary”) guide, I don’t see anything comparable to the RMT. We know there’s a low-power E24 RV32 core in these. Can it drive a GPIO line at a precise frequency? Can the LCD, (still undocumented) HDMI, or (still undocumented) ISP, or any other embedded peripheral controller on this part deliver a high-precision digital out with low host CPU overhead?

It may not be the target audience of this device, but it’s a clever hack for those that need digital out. Anyone have a trick up the sleeve for these?