Skip to content

Commit

Permalink
neopixel updates
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick committed Nov 26, 2023
1 parent 4318300 commit 0756b1f
Showing 1 changed file with 42 additions and 33 deletions.
75 changes: 42 additions & 33 deletions src/main/java/org/myrobotlab/service/NeoPixel.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ public String toString() {
return String.format("%d:%d,%d,%d,%d", address, red, green, blue, white);
}
}


public static class PixelSet {
public long delayMs = 0;
public List<Pixel> pixels = new ArrayList<>();

public int[] flatten() {
Expand Down Expand Up @@ -123,7 +121,7 @@ public void run() {
try {
LedDisplayData display = displayQueue.take();
// get led display data
log.info(display.toString());
log.debug(display.toString());

NeoPixelController npc = (NeoPixelController) Runtime.getService(controller);
if (npc == null) {
Expand All @@ -132,30 +130,31 @@ public void run() {
}

if ("animation".equals(display.action)) {
sleep(100);
npc.neoPixelClear(getName());
// sleep(100);
Double fps = fpsToWaitMs(speedFps);
npc.neoPixelSetAnimation(getName(), animations.get(display.animation), red, green, blue, white, fps.intValue());
currentAnimation = display.animation;
} else if ("clear".equals(display.action)) {
sleep(100);
// sleep(100);
npc.neoPixelClear(getName());
currentAnimation = null;
} else if ("writeMatrix".equals(display.action)) {
sleep(100);
// sleep(100);
npc.neoPixelWriteMatrix(getName(), getPixelSet().flatten());
} else if ("fill".equals(display.action)) {
} else if ("fill".equals(display.action)) {
Flash f = display.flashes.get(0);
sleep(100);
// sleep(100);
npc.neoPixelFill(getName(), display.beginAddress, display.onCount, f.red, f.green, f.blue, f.white);
} else if ("brightness".equals(display.action)) {
sleep(100);
display.brightness = (display.brightness > 255)?255:display.brightness;
display.brightness = (display.brightness < 0)?0:display.brightness;
// sleep(100);
display.brightness = (display.brightness > 255) ? 255 : display.brightness;
display.brightness = (display.brightness < 0) ? 0 : display.brightness;
npc.neoPixelSetBrightness(getName(), display.brightness);
} else if ("flash".equals(display.action)) {

// FIXME disable currentAnimation ??? // save it ?
sleep(100);
// sleep(100);
npc.neoPixelClear(getName());
for (int count = 0; count < display.flashes.size(); count++) {
Flash flash = display.flashes.get(count);
Expand Down Expand Up @@ -201,8 +200,6 @@ public synchronized void stop() {

private static final long serialVersionUID = 1L;



public static void main(String[] args) throws InterruptedException {

try {
Expand Down Expand Up @@ -372,7 +369,7 @@ private void addDisplayTask(LedDisplayData data) {
displayQueue.add(data);
}
}

@Deprecated /* use clear() */
public void animationStop() {
clear();
Expand Down Expand Up @@ -590,18 +587,18 @@ public void flash(String name) {
error("requested flash %s not found in flash map", name);
}
}
public void flashBrightness(double brightness) {

public void flashBrightness(double brightness) {
LedDisplayData data = new LedDisplayData("brightness");
// adafruit neopixel library does not recover from setting

// adafruit neopixel library does not recover from setting
// brightness to 0 - so we have to hack around it
if (data.brightness < 10) {
return;
}
}
addDisplayTask(data);
}

// utility to convert frames per second to milliseconds per frame.
private double fpsToWaitMs(int fps) {
if (fps == 0) {
Expand Down Expand Up @@ -752,8 +749,9 @@ public void onLedDisplay(LedDisplayData data) {
}

/**
* takes a scalar value and fills with the appropriate brightness
* using the peak color if available
* takes a scalar value and fills with the appropriate brightness using the
* peak color if available
*
* @param value
*/
public void onPeak(double value) {
Expand Down Expand Up @@ -793,7 +791,7 @@ synchronized public void playAnimation(String animation) {
if (speedFps > maxFps) {
speedFps = maxFps;
}

LedDisplayData data = new LedDisplayData("animation");
data.animation = animation;
addDisplayTask(data);
Expand Down Expand Up @@ -910,16 +908,13 @@ public void setPin(String pin) {
}
}

/**
* basic setting of a pixel
*/
@Override
public void setPixel(int address, int red, int green, int blue) {
setPixel(currentMatrix, currentSequence, address, red, green, blue, 0, 0);
setPixel(currentMatrix, currentSequence, address, red, green, blue, 0);
}

public void setPixel(int address, int red, int green, int blue, int white) {
setPixel(currentMatrix, currentSequence, address, red, green, blue, white, 0);
setPixel(currentMatrix, currentSequence, address, red, green, blue, white);
}

/**
Expand All @@ -934,12 +929,10 @@ public void setPixel(int address, int red, int green, int blue, int white) {
* @param white
* @param delayMs
*/
public void setPixel(String matrixName, Integer pixelSetIndex, int address, int red, int green, int blue, int white, Integer delayMs) {
public void setPixel(String matrixName, Integer pixelSetIndex, int address, int red, int green, int blue, int white) {
// get and update memory cache
PixelSet ps = getPixelSet(matrixName, pixelSetIndex);

ps.delayMs = delayMs;

// NeoPixelController c = (NeoPixelController)
// Runtime.getService(controller);
ServiceInterface sc = Runtime.getService(controller);
Expand All @@ -953,6 +946,13 @@ public void setPixel(String matrixName, Integer pixelSetIndex, int address, int

// update memory
ps.pixels.set(address, pixel);
}

/**
* Both sets and writes an individual pixel
*/
public void writePixel(int address, int red, int green, int blue) {
setPixel(address, red, green, blue);
LedDisplayData data = new LedDisplayData("writeMatrix");
addDisplayTask(data);
}
Expand Down Expand Up @@ -1034,12 +1034,21 @@ public void writeMatrix() {
@Override
public void onAudioStart(AudioData data) {
if (config.audioAnimation != null) {
playAnimation(config.audioAnimation);
playAnimation(config.audioAnimation);
}
}

@Override
public void onAudioEnd(AudioData data) {
clear();
}

public void setPixel(int address, String color) {
int rgb[] = CodecUtils.getColor(color);
if (rgb == null) {
error("could not get color %s", color);
return;
}
setPixel(address, rgb[0], rgb[1], rgb[2]);
}
}

0 comments on commit 0756b1f

Please sign in to comment.