Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 16-bit buffers and 8/16 conversion to LXLayeredComponent. #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/heronarts/lx/HybridBuffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package heronarts.lx;

import heronarts.lx.color.LXColor;
import heronarts.lx.color.LXColor16;

/**
* Manages a pair of color buffers (one in 8-bit color and one in 16-bit color),
* converting data between them automatically as needed. Clients should call
* markBufferModified() or markBuffer16Modified() after writing into either buffer;
* then getBuffer() and getBuffer16() will convert data when necessary. The buffers
* are allocated on demand; if only one is used, no memory is wasted on the other.
*/
public class HybridBuffer {
private LX lx = null;
private LXBuffer buffer = null;
private LXBuffer16 buffer16 = null;

enum State {BUFFER_IS_NEWER, BUFFER16_IS_NEWER, IN_SYNC}
private State state = State.IN_SYNC;

public HybridBuffer(LX lx) {
this.lx = lx;
}

/** Gets the 8-bit buffer, converting from the 16-bit buffer if needed. */
public LXBuffer getBuffer() {
if (buffer == null) {
buffer = new ModelBuffer(lx);
if (buffer16 != null) {
state = State.BUFFER16_IS_NEWER;
}
}
if (state == State.BUFFER16_IS_NEWER) {
sync();
}
return buffer;
}

/** Gets the 16-bit buffer, converting from the 8-bit buffer if needed. */
public LXBuffer16 getBuffer16() {
if (buffer16 == null) {
buffer16 = new ModelBuffer16(lx);
if (buffer != null) {
state = State.BUFFER_IS_NEWER;
}
}
if (state == State.BUFFER_IS_NEWER) {
sync();
}
return buffer16;
}

/** Sets the 8-bit buffer where data will be read from and written to. */
public void setBuffer(LXBuffer buffer) {
this.buffer = buffer;
if (buffer != null) {
markBufferModified();
}
}

/** Sets the 16-bit buffer where data will be read from and written to. */
public void setBuffer16(LXBuffer16 buffer16) {
this.buffer16 = buffer16;
if (buffer16 != null) {
markBuffer16Modified();
}
}

/** Marks the 8-bit buffer as containing newer data than the 16-bit buffer. */
public void markBufferModified() {
state = State.BUFFER_IS_NEWER;
}

/** Marks the 16-bit buffer as containing newer data than the 8-bit buffer. */
public void markBuffer16Modified() {
state = State.BUFFER16_IS_NEWER;
}

/**
* If both the 8-bit and 16-buffers exist, ensures that they have matching data,
* performing a conversion if necessary that gives priority to whichever has newer data.
*/
public void sync() {
if (buffer != null && buffer16 != null) {
switch (state) {
case BUFFER_IS_NEWER:
LXColor.intsToLongs(buffer.getArray(), buffer16.getArray16());
break;
case BUFFER16_IS_NEWER:
LXColor16.longsToInts(buffer16.getArray16(), buffer.getArray());
break;
}
state = State.IN_SYNC;
}
}
}
5 changes: 5 additions & 0 deletions src/heronarts/lx/LXBuffer16.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package heronarts.lx;

public interface LXBuffer16 {
public long[] getArray16();
}
1 change: 1 addition & 0 deletions src/heronarts/lx/LXEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public final void onLoop(double deltaMs) {
double enabledDamped = this.enabledDamped.getValue();
if (enabledDamped > 0) {
run(deltaMs, enabledDamped);
markBufferModified();
}
this.timer.runNanos = System.nanoTime() - runStart;
}
Expand Down
5 changes: 3 additions & 2 deletions src/heronarts/lx/LXLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ protected LXLayer(LX lx) {
super(lx);
}

protected LXLayer(LX lx, LXDeviceComponent buffer) {
super(lx, buffer);
protected LXLayer(LX lx, LXDeviceComponent component) {
super(lx, component);
}

@Override
Expand All @@ -44,6 +44,7 @@ public String getLabel() {
@Override
protected final void onLoop(double deltaMs) {
run(deltaMs);
markBufferModified();
}

/**
Expand Down
Loading