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

Allow RGB values to be read from AddressableLEDBuffers without allocating Color objects #6332

Closed
SamCarlberg opened this issue Feb 1, 2024 · 0 comments · Fixed by #6333
Closed
Labels

Comments

@SamCarlberg
Copy link
Member

If I want to iterate over a buffer to read or copy data, I currently have to use a pattern like this:

for (int i = 0; i < buffer.getLength(); i++) {
  Color c = buffer.getLED(i);
}

This results in an object allocation for each LED in the buffer. If this code has to run periodically then this can easily result in thousands of new objects per second, even for lower density LED strips like 60/meter - 60 LEDs * 1 object/LED * 50Hz = 3000 objects/sec.

Let's add methods to read the individual R, G, and B channel values directly. A functional-style iterator method would be nice to have to avoid unnecessary indexed for-loops

public int getRed(int index)
public int getGreen(int index)
public int getBlue(int index)

// functional iteration
interface Iterator {
  void accept(int index, int r, int g, int b);
}

public void forEach(Iterator iterator) {
  for (int i = 0; i < getLength(); i++) {
    iterator.accept(i, getRed(i), getGreen(i), getBlue(i));
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant