forked from cschuhen/oled_drivers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotation.rs
114 lines (98 loc) · 3.35 KB
/
rotation.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! Print:
//!
//! Hello world!
//!
//! <<=== (R) ===>> // Moving RUST logo
//!
//! Hello rust!
//!
//! the text with a 6x8 pixel font.
//!
//! This example is tested with an STM32G431 board connected to a SH1107 based display via SPI or I2C.
//!
//! It should be easy to modify:
//! - Display type: Choose below
//! - Chip/board, if STM32:
//! - Modify chip in Cargo.toml
//! - Choose different periperals and pins in bsp.rs
//!
//! Run with: `cargo run --example image --features=embassy-stm32 --features=spi --release`.
//! or
//! Run with: `cargo run --example image --features=embassy-stm32 --features=i2c --release`.
//!
#![no_std]
#![no_main]
mod bsp;
use embassy_executor::Spawner;
use embedded_graphics::{
image::{Image, ImageRawLE},
mono_font::{ascii::FONT_6X10, MonoTextStyleBuilder},
pixelcolor::BinaryColor,
prelude::*,
text::{Baseline, Text},
};
use oled_async::{prelude::*, Builder};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let (di, mut reset, mut delay) = bsp::board::get_board();
type Display = oled_async::displays::sh1107::Sh1107_128_128;
//type Display = oled_async::displays::sh1108::Sh1108_64_160;
//type Display = oled_async::displays::ssd1309::Ssd1309_128_64;
let raw_disp = Builder::new(Display {})
.with_rotation(crate::DisplayRotation::Rotate180)
.connect(di);
let mut disp: GraphicsMode<_, _, { 128 * 128 / 8 }> = raw_disp.into();
disp.reset(&mut reset, &mut delay).unwrap();
disp.init().await.unwrap();
disp.clear();
disp.flush().await.unwrap();
let im: ImageRawLE<BinaryColor> = ImageRawLE::new(include_bytes!("./rust.raw"), 64);
let (x_diff, y_diff) = {
let dwidth = disp.get_dimensions().0 as i32;
let dheight = disp.get_dimensions().1 as i32;
let iwidth = im.size().width as i32;
let iheight = im.size().height as i32;
(dwidth - iwidth, dheight - iheight)
};
let text_style = MonoTextStyleBuilder::new()
.font(&FONT_6X10)
.text_color(BinaryColor::On)
.build();
let mut dir = 1;
let mut x = 0;
loop {
disp.clear();
Text::with_baseline("Hello world!", Point::zero(), text_style, Baseline::Top)
.draw(&mut disp)
.unwrap();
Text::with_baseline(
"Hello Rust!",
Point::new(0, disp.get_dimensions().1 as i32),
text_style,
Baseline::Bottom,
)
.draw(&mut disp)
.unwrap();
Image::new(&im, Point::new(x, y_diff / 2))
.draw(&mut disp)
.unwrap();
x += dir;
if dir > 0 && x >= x_diff {
dir = -1;
} else if dir < 0 && x <= 0 {
dir = 1;
// Contrived example to test builder and instance methods. Sets rotation to 270 degress
// or 90 degress counterclockwise
disp.set_rotation(match disp.get_rotation() {
DisplayRotation::Rotate0 => DisplayRotation::Rotate90,
DisplayRotation::Rotate90 => DisplayRotation::Rotate180,
DisplayRotation::Rotate180 => DisplayRotation::Rotate270,
DisplayRotation::Rotate270 => DisplayRotation::Rotate0,
})
.await
.unwrap();
}
disp.flush().await.unwrap();
}
}