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 Copy + Clone for channel structs #33

Open
petekubiak opened this issue Feb 5, 2025 · 0 comments
Open

Add Copy + Clone for channel structs #33

petekubiak opened this issue Feb 5, 2025 · 0 comments

Comments

@petekubiak
Copy link

Say for example I want to perform single-ended measurements on each of the channels in an async environment, I might want to create a function like this:

type Ads<'a, IC, CONV> =
    Ads1x1x<I2cDevice<'a, NoopRawMutex, I2c<'static, Async>>, IC, CONV, ads1x1x::mode::OneShot>;

async fn read_ads_channel<'a, IC, CONV, CH>(ads: &mut Ads<'a, IC, CONV>, channel: CH) -> i16
where
    CH: ads1x1x::ChannelId<Ads<'a, IC, CONV>> + Clone + Copy,
    CONV: ads1x1x::ConvertMeasurement,
{
    loop {
        match ads.read(channel) {
            Err(nb::Error::WouldBlock) => Timer::after_millis(1).await,
            Err(nb::Error::Other(error)) => error!("{error:?}"),
            Ok(measurement) => break measurement,
        }
    }
}

Unfortunately though this won't compile as the channel can't be copied or cloned, which is required for it to be used in the loop. As far as I can tell I think #[derive(Copy, Clone)] could be added to the struct declaration macro.

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

No branches or pull requests

1 participant