rust-embedded-community / ssd1306

SSD1306 OLED driver
Apache License 2.0
315 stars 70 forks source link

`DrawTarget` is not implemented for `Ssd1306<I2CInterface<avr_hal_generic::i2c::I2c<.....` #186

Closed work-in-progress-danny closed 1 year ago

work-in-progress-danny commented 1 year ago

Description of the problem

I'm trying to print "Hello Rust!" on my oled in buffered graphics mode, so I can eventually switch individual characters out and around. But I'm getting an error I don't fully understand and I'm not sure exactly why this is happening. I don't know whether it's an avr-hal problem, a ssd1306 or an embedded graphics issue or it's just me trying to fit a round peg in a square hole. Any help on this would be appreciated

Test case

Code

#![no_std]
#![no_main]

use embedded_graphics::{
    mono_font::{ascii::FONT_6X10, MonoTextStyle},
    pixelcolor::Rgb565,
    prelude::*,
    text::Text,
};
use panic_halt as _;
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};

#[arduino_hal::entry]
fn main() -> ! {
    let dp = arduino_hal::Peripherals::take().unwrap();
    let pins = arduino_hal::pins!(dp);

    let i2c = arduino_hal::I2c::new(
        dp.TWI,
        pins.a4.into_pull_up_input(),
        pins.a5.into_pull_up_input(),
        50000,
    );

    let interface = I2CDisplayInterface::new(i2c);
    let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
        .into_buffered_graphics_mode();

    display.init().unwrap();

    let style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE);

    Text::new("Hello Rust!", Point::new(20, 30), style)
        .draw(&mut display) // <- Error here
        .unwrap();

    loop {}
}

Error

error[E0277]: the trait bound `Ssd1306<I2CInterface<avr_hal_generic::i2c::I2c<Atmega, arduino_hal::pac::TWI, avr_hal_generic::port::Pin<Input, PC4>, avr_hal_generic::port::Pin<Input, PC5>, MHz16>>, ssd1306::prelude::DisplaySize128x64, BufferedGraphicsMode<ssd1306::prelude::DisplaySize128x64>>: DrawTarget` is not satisfied
   --> src/main.rs:35:15
    |
35  |         .draw(&mut display)
    |          ---- ^^^^^^^^^^^^ the trait `DrawTarget` is not implemented for `Ssd1306<I2CInterface<avr_hal_generic::i2c::I2c<Atmega, arduino_hal::pac::TWI, avr_hal_generic::port::Pin<Input, PC4>, avr_hal_generic::port::Pin<Input, PC5>, MHz16>>, ssd1306::prelude::DisplaySize128x64, BufferedGraphicsMode<ssd1306::prelude::DisplaySize128x64>>`
    |          |
    |          required by a bound introduced by this call
    |
    = help: the following other types implement trait `DrawTarget`:
              Clipped<'_, T>
              ColorConverted<'_, T, C>
              Cropped<'_, T>
              Framebuffer<C, RawU1, BO, WIDTH, HEIGHT, N>
              Framebuffer<C, RawU16, BigEndian, WIDTH, HEIGHT, N>
              Framebuffer<C, RawU16, LittleEndian, WIDTH, HEIGHT, N>
              Framebuffer<C, RawU2, BO, WIDTH, HEIGHT, N>
              Framebuffer<C, RawU24, BigEndian, WIDTH, HEIGHT, N>
            and 10 others
note: required by a bound in `embedded_graphics::Drawable::draw`
   --> /Users/danny/.cargo/registry/src/github.com-1ecc6299db9ec823/embedded-graphics-core-0.4.0/src/drawable.rs:106:12
    |
106 |         D: DrawTarget<Color = Self::Color>;
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `embedded_graphics::Drawable::draw`
bugadani commented 1 year ago

Can you try the github master version of the crate? You are using the new embedded-graphics which isn't yet supported by the released version of this crate.

work-in-progress-danny commented 1 year ago

Same code*

Cargo.toml

[dependencies]
panic-halt = "0.2.0"
ufmt = "0.1.0"
nb = "0.1.2"
embedded-hal = "0.2.3"
ssd1306 = { git = "https://github.com/jamwaffles/ssd1306.git", branch = "master" }
embedded-graphics = "0.8.0"

New Error

error[E0271]: type mismatch resolving `<Ssd1306<I2CInterface<avr_hal_generic::i2c::I2c<Atmega, arduino_hal::pac::TWI, avr_hal_generic::port::Pin<Input, PC4>, avr_hal_generic::port::Pin<Input, PC5>, MHz16>>, ssd1306::prelude::DisplaySize128x64, BufferedGraphicsMode<ssd1306::prelude::DisplaySize128x64>> as DrawTarget>::Color == Rgb565`
   --> src/main.rs:35:10
    |
35  |         .draw(&mut display)
    |          ^^^^ expected struct `Rgb565`, found enum `BinaryColor`
    |
note: required by a bound in `embedded_graphics::Drawable::draw`
   --> /Users/danny/.cargo/registry/src/github.com-1ecc6299db9ec823/embedded-graphics-core-0.4.0/src/drawable.rs:106:23
    |
106 |         D: DrawTarget<Color = Self::Color>;
    |                       ^^^^^^^^^^^^^^^^^^^ required by this bound in `embedded_graphics::Drawable::draw`
bugadani commented 1 year ago

You can't use Rgb565 with this display, you can only use BinaryColor which has an On and an Off option.

work-in-progress-danny commented 1 year ago

Thanks @bugadani it was the embedded_graphics::pixelcolor::Rgb565, instead using embedded_graphics::pixelcolor::binarycolor fixed it 🚀