[−][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 RawMode
instance which isn't very useful by itself. You can coerce the driver
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::{Builder, mode::GraphicsMode}; // Configure an I2C interface on the target device; below line shown as example only let i2c = I2cInterface; let mut disp: GraphicsMode<_> = Builder::new().connect_i2c(i2c).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}; // Configure an I2C interface on the target device; below line shown as example only let i2c = I2cInterface; let mut disp: TerminalMode<_> = Builder::new().connect_i2c(i2c).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::{prelude::*, mode::TerminalMode, Builder}; let mut disp: TerminalMode<_> = Builder::new().connect_i2c(i2c).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 ssd1306::{prelude::*, mode::GraphicsMode, Builder}; use embedded_graphics::{ fonts::Font6x8, pixelcolor::BinaryColor, prelude::*, }; let mut disp: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into(); disp.init().unwrap(); disp.draw( Font6x8::render_str("Hello world!") .stroke(Some(BinaryColor::On)) .into_iter(), ); disp.draw( Font6x8::render_str("Hello Rust!") .stroke(Some(BinaryColor::On)) .translate(Point::new(0, 16)) .into_iter(), ); disp.flush().unwrap();
Re-exports
pub use crate::builder::Builder; |
Modules
builder | Interface factory |
displayrotation | Display rotation |
interface | SSD1306 Communication Interface (I2C/SPI) |
mode | Operating modes for the SSD1306 |
prelude | Crate prelude |
properties | Container to store and set display properties |
Enums
Error | Errors in this crate |