[−][src]Crate ssd1306
SSD1306 OLED display driver
The driver must be initialised by passing an I2C or SPI interface peripheral to the Builder
,
which will in turn create a driver instance in a particular mode. By default, the builder
returns a DisplayProperties
instance which is a low level interface to manipulate the
display properties (e.g. rotation). The driver can be coerced into a more useful mode by calling
into()
and defining the type you want to coerce to. For example, to initialise the display
with an I2C interface and GraphicsMode
, you would do something like this:
use ssd1306::{mode::GraphicsMode, Builder, I2CDIBuilder}; // Configure an I2C interface on the target device; below line shown as example only let i2c = I2cInterface; let interface = I2CDIBuilder::new().init(i2c); let mut disp: GraphicsMode<_> = Builder::new().connect(interface).into(); disp.init(); disp.set_pixel(10, 20, 1);
See the example
for more usage. The entire embedded_graphics
featureset
is supported by this driver.
There is also TerminalMode
which allows drawing of characters to the display without
using a display buffer:
use ssd1306::{mode::TerminalMode, Builder, I2CDIBuilder}; // Configure an I2C interface on the target device; below line shown as example only let i2c = I2cInterface; let interface = I2CDIBuilder::new().init(i2c); let mut disp: TerminalMode<_> = Builder::new().connect(interface).into(); disp.print_char('A');
See the example for more usage.
It's possible to customise the driver to suit your display/application. Take a look at the
Builder
for available options.
Examples
Examples can be found in the examples/ folder
Write text to the display without a framebuffer
Uses TerminalMode
. See the complete example
here.
use core::fmt::Write; use ssd1306::{mode::TerminalMode, prelude::*, Builder, I2CDIBuilder}; let interface = I2CDIBuilder::new().init(i2c); let mut disp: TerminalMode<_> = Builder::new().connect(interface).into(); disp.init().unwrap(); let _ = disp.clear(); // Spam some characters to the display for c in 97..123 { let _ = disp.write_str(unsafe { core::str::from_utf8_unchecked(&[c]) }); } for c in 65..91 { let _ = disp.write_str(unsafe { core::str::from_utf8_unchecked(&[c]) }); }
Draw some text to the display
Uses GraphicsMode
and embedded_graphics. See the
complete example here.
use embedded_graphics::{ fonts::{Font6x8, Text}, pixelcolor::BinaryColor, prelude::*, style::TextStyleBuilder, }; use ssd1306::{mode::GraphicsMode, prelude::*, Builder, I2CDIBuilder}; let interface = I2CDIBuilder::new().init(i2c); let mut disp: GraphicsMode<_> = Builder::new().connect(interface).into(); disp.init().unwrap(); let text_style = TextStyleBuilder::new(Font6x8) .text_color(BinaryColor::On) .build(); Text::new("Hello world!", Point::zero()) .into_styled(text_style) .draw(&mut disp); Text::new("Hello Rust!", Point::new(0, 16)) .into_styled(text_style) .draw(&mut disp); disp.flush().unwrap();
Re-exports
pub use crate::builder::Builder; |
pub use crate::builder::I2CDIBuilder; |
Modules
brightness | Display brightness |
builder | Interface factory |
command | Display commands. |
displayrotation | Display rotation |
displaysize | Display size |
mode | Operating modes for the SSD1306 |
prelude | Crate prelude |
properties | Container to store and set display properties |
Enums
Error | Errors in this crate |