rust-embedded-community / ssd1306

SSD1306 OLED driver
Apache License 2.0
300 stars 68 forks source link

Nonblocking implementation #146

Open ost-ing opened 3 years ago

ost-ing commented 3 years ago

Nonblocking I2C support

Hello, I'm attempting to build an application that utilizes this library in conjunction with doing real-time DAC/ADC functions. I have noticed that my DAC output, which is executed from a Timer producing a simple sine-wave is blocked by the I2C communication that this library executes, which distorts the output of the DAC.

I know that the embedded-hal doesn't support non-blocking for various reasons. What I would like to do is write my own interface adapter and implement I2C interrupt handling within my app to unblock the communication to the display.

Looking at the test_helpers.rs, I see this:

#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
pub struct StubInterface;

impl WriteOnlyDataCommand for StubInterface {
    fn send_commands(
        &mut self,
        _cmd: display_interface::DataFormat<'_>,
    ) -> Result<(), DisplayError> {
        Ok(())
    }
    fn send_data(&mut self, _buf: display_interface::DataFormat<'_>) -> Result<(), DisplayError> {
        Ok(())
    }
}

I assume that I could use the WriteOnlyDataCommand trait to achieve this?

Kind regards Oliver

jamwaffles commented 3 years ago

Sorry for the delay! @therealprof maintains the crate that WriteOnlyDataCommand so might be able to offer more insight, but yes I think it's enough to add a custom impl of WriteOnlyDataCommand and pass that into Builder::new().connect(interface).into() instead of the provided blocking implementations.

quentinmit commented 2 years ago

There's an embedded-hal-async now that provides async/await-compatible bus interfaces. See https://github.com/jamwaffles/ssd1331/pull/13 for a PR adding support to the ssd1311 crate and follow https://github.com/embedded-graphics/embedded-graphics/issues/622 for an upstream embedded-graphics trait.

It looks like someone already has an async fork: https://github.com/simmsb/ssd1306

bugadani commented 1 year ago

cc https://github.com/jamwaffles/ssd1306/pull/178