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 an iterable source for the ws2812 led strip #3783

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

m5p3nc3r
Copy link
Contributor

This allows for a 'swizzling' iterator if the led's are in a grid format without the need to create an intermediate buffer

Signed-off-by: Matt Spencer [email protected]

@m5p3nc3r
Copy link
Contributor Author

I am working through using this for my use-case now, and I would like to make a further suggestion.

I would like to separate the sync and async part of the writing top the LED strip. This is because the context where I write to the back 'words' buffer may not be async friendly, and there is no need for the logic to be async.

The suggestion is to move words into the PioWs2812 struct. This should be net zero cost.

Then make the write and write_iter functions sync, storing the values in the words buffer. Something like this:

pub fn write_iter(&mut self, iter: impl Iterator<Item = RGB8>) {
    iter.zip(self.words.iter_mut()).for_each(|(color, word)| {
        *word = Self::pack_rgb32(color);
    });
}

Then add a flush function, which is the only async part of the operation

pub async fn flush(&mut self) {
    // DMA transfer
    self.sm.tx().dma_push(self.dma.reborrow(), &self.words).await;

    Timer::after_micros(55).await;
}

One last possible change - can we change the write function to make use of the write_iter as they have identical functionality?

pub fn write(&mut self, colors: &[RGB8; N]) {
    self.write_iter(colors.iter().cloned());
}

I am just unsure what the performance impact of the cloned is.

@m5p3nc3r m5p3nc3r force-pushed the ws2812_iter branch 2 times, most recently from 888c88f to ee47e5e Compare March 19, 2025 11:13
@m5p3nc3r
Copy link
Contributor Author

Hey Guys

I came back to this today after a while away from the project. I am trying to tidy up the commit so it is in a better place to merge, but I don't understand the build and build-nightly failures. I'm pretty sure this is nothing to do with the minimal one file change I have made.

Can somebody explain why these tests are failing, especially seeing as all the other tests are passing.

This allows for a 'swizzling' iterator if the led's are in a grid
format without the need to create an intermediate buffer
Enable use in non-async contexts and split the async function to
flush the DMA buffer

Signed-off-by: Matt Spencer [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants