ssd1315/lib.rs
1//! # SSD1315
2//!
3//! SSD1315 driver.
4//!
5//! ## Usage
6//!
7//! Here is an example of how to use `ssd1315`:
8//!
9//! ```rust
10//! use ssd1315::*;
11//! use embedded_graphics::{
12//! pixelcolor::BinaryColor,
13//! prelude::*,
14//! primitives::{Circle, PrimitiveStyle},
15//! };
16//!
17//! let interface = interface::I2cDisplayInterface::new(i2c);
18//! // let interface = interface::SpiDisplayInterface::new(spi, dc);
19//!
20//! let mut display = Ssd1315::new(interface);
21//!
22//! Circle::new(Point::new(0, 0), 40)
23//! .into_styled(PrimitiveStyle::with_fill(BinaryColor::On))
24//! .draw(&mut display)
25//! .unwrap();
26//!
27//! display.init_screen();
28//! display.flush_screen();
29//! ```
30//!
31//! Congratulations! Now you can see a small circle displayed on your OLED screen!
32//!
33//! If you want to apply your own configuration for the SSD1315 (for example, to change the contrast),
34//! follow this example:
35//!
36//! ```rust
37//! use ssd1315::*;
38//! use embedded_graphics::{
39//! pixelcolor::BinaryColor,
40//! prelude::*,
41//! primitives::{Circle, PrimitiveStyle},
42//! };
43//!
44//! let interface = interface::I2cDisplayInterface::new(i2c);
45//! // let interface = interface::SpiDisplayInterface::new(spi, dc);
46//!
47//! let mut config = config::Ssd1315DisplayConfig::new();
48//! config.contrast = 0xff;
49//!
50//! let mut display = Ssd1315::new(interface);
51//! display.set_custom_config(config);
52//!
53//! Circle::new(Point::new(0, 0), 40)
54//! .into_styled(PrimitiveStyle::with_fill(BinaryColor::On))
55//! .draw(&mut display)
56//! .unwrap();
57//!
58//! display.init_screen();
59//! display.flush_screen();
60//! ```
61//!
62//! Alternatively, you can use a preset configuration provided by `ssd1315`:
63//!
64//! ```rust
65//! let config = config::Ssd1315DisplayConfig::preset_config();
66//! ```
67//!
68//! Now you can see the change in contrast!
69//!
70//! You might also want to draw some raw image(s) manually to fit your specific requirements.
71//! That's no problem! You can draw it/them in an easy way:
72//!
73//! ```rust
74//! let mut display = Ssd1315::new(interface);
75//!
76//! let raw_image = [[0b1010_1010; 8]; 128];
77//! raw_image.draw_from_raw(&mut display);
78//!
79//! display.init_screen();
80//! display.flush_screen();
81//! ```
82
83#![no_std]
84
85mod command;
86pub mod config;
87mod draw_buffer;
88pub mod interface;
89
90use command::{oled_init, oled_set_cursor};
91use config::Ssd1315DisplayConfig;
92
93use display_interface::{DataFormat, WriteOnlyDataCommand};
94
95/// A virtual SSD1315 device that holds interface data, a buffer
96/// that maps to the actual buffer in the SSD1315 and a configuration.
97pub struct Ssd1315<DI> {
98 interface: DI,
99 buffer: [[u8; 128]; 8],
100 config: Ssd1315DisplayConfig,
101}
102
103impl<DI: WriteOnlyDataCommand> Ssd1315<DI> {
104 /// Creates a new instance of SSD1315.
105 ///
106 /// The `interface` can be either an I2C or an SPI interface.
107 pub fn new(interface: DI) -> Self {
108 Self {
109 interface,
110 buffer: [[0; 128]; 8],
111 config: Default::default(),
112 }
113 }
114
115 /// Sets your custom configuration for the SSD1315.
116 /// If you do not call this, the display will be initialized with the default configuration.
117 pub fn set_custom_config(&mut self, config: Ssd1315DisplayConfig) {
118 self.config = config;
119 }
120
121 /// Initializes the SSD1315.
122 pub fn init_screen(&mut self) {
123 oled_init(&mut self.interface, self.config);
124 }
125
126 /// Flushes the SSD1315 buffer to display its contents on the OLED screen.
127 pub fn flush_screen(&mut self) {
128 for (page, data) in self.buffer.iter().enumerate() {
129 oled_set_cursor(&mut self.interface, page as u8);
130 self.interface.send_data(DataFormat::U8(data)).unwrap();
131 }
132
133 self.buffer = [[0; 128]; 8];
134 }
135}
136
137pub trait DrawFromRaw {
138 fn draw_from_raw<DI>(&self, instance: &mut Ssd1315<DI>);
139}