重新着色可寻址 LED 灯带的问题

Problem with recoloring adressable LED strip

我正在使用名为 Diozero.

的库从 Java 中的 raspberry pi 控制 WS2811 LED 灯条

当我第一次为 LED 设置颜色时,没有问题,但是当我尝试再次设置时,LED 只是变成白色,而不是我为它们设置的颜色。 当我重新启动应用程序时,我能够为 LED 重新着色,即使我没有重新启动它们,但同样,只有一次。

请帮忙!

package venus;

import com.diozero.api.DigitalOutputDevice;
import com.diozero.ws281xj.LedDriverInterface;
import com.diozero.ws281xj.PixelColour;
import com.diozero.ws281xj.StripType;
import com.diozero.ws281xj.rpiws281x.WS281x;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author RETEC error
 */
public class Venus {

    private static Venus instance;

    private LedDriverInterface driver;
    private DigitalOutputDevice power;

    public Venus() {
        int dataPin = 10; //bcm 10
        int brightness = 127;
        int numPixels = 50;

        int powerPin = 26; //bcm 26 (inside, second from bottom)

        //power = new DigitalOutputDevice(powerPin); //create a toggleable power supply
        //powerOn(); //add power
        driver = new WS281x(0, 0, dataPin, brightness, numPixels, StripType.WS2811_RGB); //create the driver for the leds

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        instance = new Venus();
        instance.paintGradient(Color.red, Color.blue);
    }

    //power control methods
    /**
     * sudo Gives power to the designated power pin.
     */
    private void powerOn() {
        power.on();
    }

    /**
     * Cuts power from the designated power pin.
     */
    private void powerOff() {
        power.off();
    }

    /**
     * Cuts the power from the designated power pin and gives back power after 5
     * secs.
     */
    private void powerCycle() {
        powerOff();
        try {
            Thread.sleep(5000);

        } catch (InterruptedException ex) {
            Logger.getLogger(Venus.class.getName()).log(Level.SEVERE, null, ex);
        }

        powerOn();
    }

    //color control methods
    /**
     * Paints a gradient on the full length of the LED driver, fading from c1 to
     * c2.
     *
     * @param c1 Starting color
     * @param c2 Ending color
     */
    public void paintGradient(Color c1, Color c2) {
        int[] colorValues = new int[driver.getNumPixels()]; //the 24 bit color values to be sent to the LEDs

        for (int i = 0; i < colorValues.length; i++) {
            float t = (float) i / (float) colorValues.length - 1;
            Vector3 v1 = new Vector3(c1.getRed(), c1.getGreen(), c1.getBlue());
            Vector3 v2 = new Vector3(c2.getRed(), c2.getGreen(), c2.getBlue());

            Vector3 color = Vector3.Lerp(v1, v2, t);
            colorValues[i] = PixelColour.createColourRGB((int) color.x, (int) color.y, (int) color.z);
        }

        //paint the pixels
        for (int i = 0; i < driver.getNumPixels(); i++) {
            driver.setPixelColour(i, colorValues[i]);
        }
        driver.render();
    }

}

在较新的 raspberrypi 4-s 上,在以高精度计时发出信号时存在问题。这源于 cpu 动态调整其速度并导致 SPI 通信所需的时序也发生漂移。 要解决此问题,请将以下行添加到 boot/congig.txt "core_freq=500" "Core_freq_min=500"