Skip to content

Instantly share code, notes, and snippets.

@MGDSStudio
Created April 21, 2023 09:11
Show Gist options
  • Save MGDSStudio/1b6e060223eb06190c0bedcb56a5a547 to your computer and use it in GitHub Desktop.
Save MGDSStudio/1b6e060223eb06190c0bedcb56a5a547 to your computer and use it in GitHub Desktop.
Code to control a LED-strip based on chips ws2811. It switches on a line of LEDs white (parameter 2>parameter1) or switches all the LEDs off (parameter 1 = 0; parameter 2 = 0)
public class LedChainManager extends Manager{
private LinuxRaspberryPiGpioManager linuxRaspberryPiGpioManager;
private int parameter1, parameter2;
private static LedStrip ledStrip;
private final int leds = 10;
public LedChainManager(LinuxRaspberryPiGpioManager linuxRaspberryPiGpioManager) {
this.linuxRaspberryPiGpioManager = linuxRaspberryPiGpioManager;
init();
}
private void init() {
ledStrip = new LedStrip(linuxRaspberryPiGpioManager.getPi4j(), leds, 1);
}
public void execute() {
Logger.logGpio("LED colors changing started");
ledStrip.allOff();
delay(5);
ledStrip.allOff(); //Sometimes not every LED can be switched off after the first trying
if (parameter1 <= parameter2 ){
if (parameter1 == 0 && parameter2 == 0){
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();
}
if (GlobalVariables.debug) Logger.logGpio("LED strip colors were changed in " + (System.currentTimeMillis()-startTime) + " milliseconds. LEDs from: " + parameter1 + " to " + parameter2 + " flash");
}
}
}
public void dispose(){
ledStrip.close();
}
public void setParameter1(int startLed) {
this.parameter1 = startLed;
}
public void setParameter2(int lastLed) {
this.parameter2 = lastLed;
}
}
@MGDSStudio
Copy link
Author

Sometimes one LED can start to shine red. First LED never shines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment