mini_oled/
lib.rs

1#![no_std]
2//! # Mini OLED
3//!
4//! `mini-oled` is an I2C/~~SPI~~ driver for the SH1106 OLED display controller, designed for embedded no-std environments.
5//! It supports basic drawing operations and integrates with `embedded-graphics` for advanced graphics.
6//!
7//! ## Usage
8//!
9//! ### With `embedded-graphics`
10//!
11//! Here is a complete example. It shows how to setup the display, draw shapes, write text, and make a simple animation.
12//!
13//! ```rust,ignore
14//! use embedded_graphics::{
15//!     mono_font::{ascii::FONT_6X10, MonoTextStyleBuilder},
16//!     pixelcolor::BinaryColor,
17//!     prelude::*,
18//!     primitives::{Circle, PrimitiveStyle, Rectangle},
19//!     text::{Alignment, Text},
20//! };
21//! use mini_oled::prelude::*;
22//! use core::fmt::Write;
23//!
24//! // ... setup your hardware I2C driver here ...
25//! let i2c = ...;
26//!
27//! // Create the I2C interface (address 0x3C is common for SH1106)
28//! let i2c_interface = I2cInterface::new(i2c, 0x3C);
29//!
30//! // Initialize the display driver
31//! let mut screen = Sh1106::new(i2c_interface);
32//!
33//! // Initialize the display
34//! screen.init().unwrap();
35//!
36//! // Set rotation
37//! screen.set_rotation(DisplayRotation::Rotate180).unwrap();
38//!
39//! // Draw a filled rectangle
40//! let fill = PrimitiveStyle::with_fill(BinaryColor::Off);
41//! Rectangle::new(Point::new(0, 0), Size::new(127, 60))
42//!     .into_styled(fill)
43//!     .draw(screen.get_mut_canvas())
44//!     .unwrap();
45//!
46//! // Prepare text style
47//! let character_style = MonoTextStyleBuilder::new()
48//!     .font(&FONT_6X10)
49//!     .text_color(BinaryColor::On)
50//!     .background_color(BinaryColor::Off)
51//!     .build();
52//!
53//! let mut i = 0;
54//! let mut old_i = 0;
55//!
56//! // Animation loop
57//! loop {
58//!     // 1. Clear previous circle
59//!     Circle::new(Point::new(old_i, 22), 40)
60//!         .into_styled(PrimitiveStyle::with_stroke(BinaryColor::Off, 1))
61//!         .draw(screen.get_mut_canvas())
62//!         .unwrap();
63//!
64//!     // 2. Draw new circle
65//!     i = (i + 1) % 128;
66//!     Circle::new(Point::new(i, 22), 40)
67//!         .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1))
68//!         .draw(screen.get_mut_canvas())
69//!         .unwrap();
70//!
71//!     old_i = i;
72//!
73//!     // 3. Update FPS text (example logic)
74//!     let mut fps_string: heapless::String<32> = heapless::String::new();
75//!     write!(fps_string, "Fps: {}", 60).unwrap(); // Example FPS value
76//!
77//!     Text::with_alignment(
78//!         &fps_string,
79//!         Point::new(64, 10),
80//!         character_style,
81//!         Alignment::Center,
82//!     )
83//!     .draw(screen.get_mut_canvas())
84//!     .unwrap();
85//!
86//!     // 4. Send changes to the display
87//!     screen.flush().unwrap();
88//! }
89//! ```
90//!
91//! ### Without `embedded-graphics`
92//!
93//! You can also use the library without `embedded-graphics`. You can change pixels directly using `set_pixel` or by accessing the buffer.
94//!
95//! ```rust,ignore
96//! use mini_oled::prelude::*;
97//!
98//! // ... setup your hardware I2C driver here ...
99//! let i2c = ...;
100//!
101//! let i2c_interface = I2cInterface::new(i2c, 0x3C);
102//! let mut screen = Sh1106::new(i2c_interface);
103//! screen.init().unwrap();
104//!
105//! // Manually set a pixel at (10, 10)
106//! // This method automatically updates the "dirty area", so flush() is efficient.
107//! screen.get_mut_canvas().set_pixel(10, 10, true);
108//! screen.flush().unwrap();
109//!
110//! // Or access the raw buffer directly
111//! let buffer = screen.get_mut_canvas().get_mut_buffer();
112//! buffer[0] = 0xFF; // Set first 8 pixels on
113//!
114//! // IMPORTANT: Changing the buffer directly does NOT update the "dirty area".
115//! // The driver does not know which pixels changed.
116//! // You must use `flush_all()` to send the entire buffer to the display.
117//! screen.flush_all().unwrap();
118//! ```
119
120pub mod command;
121pub mod error;
122pub mod interface;
123pub mod prelude;
124pub mod screen;
125
126mod tests;