-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32_i2c.rs
63 lines (52 loc) · 2.3 KB
/
esp32_i2c.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! Draw an RGB565 BMP image onto the display by converting the `Rgb565` pixel color type to
//! `BinaryColor` using a simple threshold where any pixel with a value greater than zero is treated
//! as "on".
//!
//! Note that the `bmp` feature for `embedded-graphics` must be turned on.
//!
//! This example is for the `ESP32 DevKit v1` board using I2C0.
//!
//! Wiring connections are as follows for a CRIUS-branded display:
//!
//! ```
//! Display -> Blue Pill
//! (black) GND -> GND
//! (red) +5V -> VCC
//! (yellow) SDA -> GPIO21
//! (green) SCL -> GPIO22
//! ```
//!
//! Run on a Blue Pill with `cargo run --example esp32_i2c`.
use ssd1327::{prelude::*, I2CDisplayInterface, Ssd1327};
use esp_idf_hal::{self, peripherals::Peripherals, prelude::*};
use tinybmp::Bmp;
use embedded_graphics::{image::Image, pixelcolor::Rgb565, prelude::*};
fn main() -> ! {
// Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once,
// or else some patches to the runtime implemented by esp-idf-sys might not link properly.
esp_idf_sys::link_patches();
let peripherals = Peripherals::take().unwrap();
let i2c = peripherals.i2c0;
let sda = peripherals.pins.gpio21;
let scl = peripherals.pins.gpio22;
println!("Starting I2C MSP1503 test");
let config = <esp_idf_hal::i2c::config::MasterConfig as Default>::default().baudrate(400.kHz().into());
let i2c = esp_idf_hal::i2c::Master::new(
i2c,
esp_idf_hal::i2c::MasterPins { sda, scl },
config
).expect("Failed to create I2C master");
let interface = I2CDisplayInterface::new(i2c);
let mut display = Ssd1327::new(interface, DisplaySize128x128, DisplayRotation::Rotate0)
.into_buffered_graphics_mode();
display.init().unwrap();
let bmp = Bmp::from_slice(include_bytes!("./rust.bmp")).expect("Failed to load BMP image");
// The image is an RGB565 encoded BMP, so specifying the type as `Image<Bmp<Rgb565>>` will read
// the pixels correctly
let im: Image<Bmp<Rgb565>> = Image::new(&bmp, Point::new(32, 32));
// We use the `color_converted` method here to automatically convert the RGB565 image data into
// BinaryColor values.
im.draw(&mut display.color_converted()).unwrap();
display.flush().unwrap();
loop {}
}