-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
135 lines (108 loc) · 4.23 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use esp_idf_hal::delay::{Ets, FreeRtos};
use esp_idf_hal::gpio::{AnyIOPin, PinDriver};
use esp_idf_hal::spi::{SpiDeviceDriver, SpiDriver, SpiDriverConfig, config::Config};
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use log::*;
use ssd1680::prelude::*;
use ssd1680::color::{Black, Red};
use embedded_graphics::{
prelude::*,
primitives::{Circle, Line, Rectangle},
mono_font::{ascii::FONT_5X8, MonoTextStyle},
pixelcolor::{BinaryColor},
primitives::PrimitiveStyle,
text::{Alignment, Text, TextStyleBuilder}
};
fn main() {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_sys::link_patches();
// Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default();
let peripherals = Peripherals::take().unwrap();
let spi = peripherals.spi2;
let rst = PinDriver::output(peripherals.pins.gpio18).unwrap();
let dc = PinDriver::output(peripherals.pins.gpio10).unwrap();
let busy = PinDriver::input(peripherals.pins.gpio19).unwrap();
let mut delay = Ets;
let sclk = peripherals.pins.gpio6;
let sdo = peripherals.pins.gpio7;
let spi = SpiDriver::new(
spi,
sclk,
sdo,
None::<AnyIOPin>,
&SpiDriverConfig::default()
).unwrap();
let cs = peripherals.pins.gpio9;
let mut spi = SpiDeviceDriver::new(spi, Some(cs), &Config::new()).unwrap();
// Initialise display controller
let mut ssd1680 = Ssd1680::new(&mut spi, busy, dc, rst, &mut delay).unwrap();
// Clear frames on the display driver
ssd1680.clear_red_frame().unwrap();
ssd1680.clear_bw_frame().unwrap();
// Create buffer for black and white
let mut display_bw = Display2in13::bw();
draw_rotation_and_rulers(&mut display_bw);
display_bw.set_rotation(DisplayRotation::Rotate0);
Rectangle::new(Point::new(60, 60), Size::new(40, 40))
.into_styled(PrimitiveStyle::with_fill(Black))
.draw(&mut display_bw)
.unwrap();
info!("Send bw frame to display");
ssd1680.update_bw_frame(display_bw.buffer()).unwrap();
// Draw red color
let mut display_red = Display2in13::red();
Circle::new(Point::new(80, 80), 40)
.into_styled(PrimitiveStyle::with_fill(Red))
.draw(&mut display_red)
.unwrap();
// println!("Send red frame to display");
ssd1680.update_red_frame(display_red.buffer()).unwrap();
info!("Update display");
ssd1680.display_frame(&mut FreeRtos).unwrap();
info!("Done");
loop {
FreeRtos::delay_ms(1000)
}
}
fn draw_rotation_and_rulers(display: &mut Display2in13) {
display.set_rotation(DisplayRotation::Rotate0);
draw_text(display, "rotation 0", 50, 35);
draw_ruler(display);
display.set_rotation(DisplayRotation::Rotate90);
draw_text(display, "rotation 90", 50, 35);
draw_ruler(display);
display.set_rotation(DisplayRotation::Rotate180);
draw_text(display, "rotation 180", 50, 35);
draw_ruler(display);
display.set_rotation(DisplayRotation::Rotate270);
draw_text(display, "rotation 270", 50, 35);
draw_ruler(display);
}
fn draw_ruler(display: &mut Display2in13) {
for col in 1..ssd1680::WIDTH {
if col % 25 == 0 {
Line::new(Point::new(col as i32, 0), Point::new(col as i32, 10))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(display)
.unwrap();
}
if col % 50 == 0 {
let label = col.to_string();
draw_text(display, &label, col as i32, 20);
}
}
}
fn draw_text(display: &mut Display2in13, text: &str, x: i32, y: i32) {
let style = MonoTextStyle::new(&FONT_5X8, BinaryColor::Off);
let _ = Text::with_text_style(text,
Point::new(x, y),
style,
TextStyleBuilder::new()
.alignment(Alignment::Center)
.build(),
)
.draw(display);
}