0

I use Pi4j on a Raspberry Pi to control a LED strip. It is made on chip ws2811. I have rewrote the example from the official homepage to have the ability to switch on some LEDs. The color is only white. I set the values of the first LED (parameter1) and last LED (parameter2) from external classes. When I call the function execute() I want that the LEDs with numbers from parameter1 to parameter2 will be switched on. But in the reality all the LEDs are shifted on one LED to end. For example: when I set parameter1 on 0 and parameter2 on 4 the LEDs from 1 to 5 will be switched on. But must be LEDs with numbers 0 to 4. Yesterday after this command the LED with the number 0 flashed red and from 1 to 5 - white. Help me please find the error.

public class LedChainManager extends Manager{
    private LinuxRaspberryPiGpioManager linuxRaspberryPiGpioManager;
    private static LedStrip ledStrip;
    private final int leds = 10;
    private int parameter1, parameter2;

    public LedChainManager(LinuxRaspberryPiGpioManager linuxRaspberryPiGpioManager) {
        this.linuxRaspberryPiGpioManager = linuxRaspberryPiGpioManager;
        init();
    }

    private void init() {
        ledStrip = new LedStrip(linuxRaspberryPiGpioManager.getPi4j(), leds, 1);
    }
    
    protected void delay(long milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    public void setParameter1(int parameter1) {
        this.parameter1 = parameter1;
    }

    public void setParameter2(int parameter2) {
        this.parameter2 = parameter2;
    }


    public void execute() {
        Logger.logGpio("LED colors changing started");
        ledStrip.allOff();     //Sometimes some LEDs stay switched on or start to flash red
        delay(5);
        ledStrip.allOff();     //I clear all the LEDs again. 
        if (parameter1 <= parameter2 ){
            if (parameter1 == 0 && parameter2 == 0){    //When parameter1 and parameter2 are 0 the LED strip must be switched off
                Logger.logGpio("All LEDs were switched off");
            }
            else {
                long startTime = System.currentTimeMillis();
                if (parameter1 >= 0 && parameter2 <= 9) {
                    for (int i = 0 ; i < leds; i++){
                        if (i>=parameter1 && i <= parameter2) {
                            ledStrip.setPixelColor(i, PixelColor.WHITE);
                        }
                    }
                    ledStrip.render();
                }
                Logger.logGpio("LED strip colors were changed in " + (System.currentTimeMillis()-startTime) + " milliseconds. LEDs from: " + parameter1 + " to " + parameter2 + " flash");
            }
        }
    }

    public void dispose(){
        ledStrip.close();
    }
}
  • `LinuxRaspberryPiGpioManager` - what package is that in please? – g00se Apr 12 '23 at 12:27
  • @g00se, this is my class that controls all my external devises. The important here is that it contains the field: Context pi4j that is got in the init() function. Context is a class from the library and lays in the package com.pi4j.context; – Alexander Gorodilov Apr 12 '23 at 12:36
  • It's probably good that you have made a high level api, but posting that code is not really going to help as it's hiding the actual implementation. As a particular point of code, I would rename 'leds' to `NUM_LEDS` (or something - it's a constant) *and* not assign it with a literal, but via a device query. – g00se Apr 12 '23 at 13:05
  • @g00se Class LinuxRaspberryPiGpioManager doesn't important in this question. For the actual problem it is only container with only one field (Context) and no methods. Pi4j is an open source project and interface Context is here: https://github.com/Pi4J/pi4j-v2/blob/develop/pi4j-core/src/main/java/com/pi4j/context/Context.java .NUM_LEDS seems to be a good name. I don't understand how can I get the number of connected devices on chip ws2811. It is only a receiver of data. – Alexander Gorodilov Apr 12 '23 at 15:23

0 Answers0