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
135
136
137
138
139
140
141
142
143
144
145
146
147
use core::fmt::Write;
use arrayvec::ArrayString;
use embedded_graphics::{
prelude::*,
fonts,
pixelcolor::Rgb565,
};
use embedded_hal::{
self,
digital::v2::OutputPin,
};
use st7735_lcd::{
self,
Orientation,
ST7735,
};
use mynewt::{
self,
result::*,
hw::hal,
fill_zero,
};
const DISPLAY_SPI: i32 = 0;
const DISPLAY_CS: i32 = 25;
const DISPLAY_DC: i32 = 18;
const DISPLAY_RST: i32 = 26;
const DISPLAY_HIGH: i32 = 23;
static mut SPI_SETTINGS: hal::hal_spi_settings = hal::hal_spi_settings {
data_order: hal::HAL_SPI_MSB_FIRST as u8,
data_mode: hal::HAL_SPI_MODE3 as u8,
baudrate: 8000,
word_size: hal::HAL_SPI_WORD_SIZE_8BIT as u8,
};
pub fn start_display() -> MynewtResult<()> {
let mut spi_port = mynewt::SPI::new();
let mut dc_gpio = mynewt::GPIO::new();
let mut rst_gpio = mynewt::GPIO::new();
spi_port.init(
DISPLAY_SPI,
DISPLAY_CS,
unsafe { &mut SPI_SETTINGS }
) ? ;
dc_gpio.init(DISPLAY_DC) ? ;
rst_gpio.init(DISPLAY_RST) ? ;
unsafe {
BACKLIGHT_HIGH = mynewt::GPIO::new();
BACKLIGHT_HIGH.init(DISPLAY_HIGH) ? ;
BACKLIGHT_HIGH.set_low() ? ;
}
unsafe { DISPLAY = st7735_lcd::ST7735::new(
spi_port,
dc_gpio,
rst_gpio,
true,
true
) };
let mut delay = mynewt::Delay::new();
unsafe {
DISPLAY.init(&mut delay) ? ;
DISPLAY.set_orientation(&Orientation::Landscape) ? ;
}
Ok(())
}
pub fn show_touch(x: u16, y: u16) -> MynewtResult<()> {
let mut buf_x = ArrayString::<[u8; 20]>::new();
let mut buf_y = ArrayString::<[u8; 20]>::new();
write!(&mut buf_x, " X = {} ", x)
.expect("show touch fail");
write!(&mut buf_y, " Y = {} ", y)
.expect("show touch fail");
let text_x = fonts::Font12x16::<Rgb565>
::render_str(&buf_x)
.stroke(Some(Rgb565::from(( 0xff, 0xff, 0xff ))))
.fill(Some(Rgb565::from(( 0x00, 0x00, 0x00 ))))
.translate(Coord::new(40, 100));
let text_y = fonts::Font12x16::<Rgb565>
::render_str(&buf_y)
.stroke(Some(Rgb565::from(( 0xff, 0xff, 0xff ))))
.fill(Some(Rgb565::from(( 0x00, 0x00, 0x00 ))))
.translate(Coord::new(40, 130));
unsafe {
DISPLAY.draw(text_x);
DISPLAY.draw(text_y);
}
Ok(())
}
pub fn draw_to_display<T>(item: T)
where T: IntoIterator<Item = Pixel<Rgb565>> {
#[cfg(not(feature = "noblock_spi"))]
unsafe { DISPLAY.draw(item) };
#[cfg(feature = "noblock_spi")]
super::batch::draw_blocks(
unsafe { &mut DISPLAY },
item
).expect("draw blocks fail");
}
pub fn set_display_pixels<P: IntoIterator<Item = u16>>(sx: u16, sy: u16, ex: u16, ey: u16, colors: P) -> Result <(), ()> {
unsafe { DISPLAY.set_pixels(sx, sy, ex, ey, colors) }
}
pub static mut DISPLAY: Display = fill_zero!(Display);
type Display = ST7735<mynewt::SPI, mynewt::GPIO, mynewt::GPIO>;
static mut BACKLIGHT_HIGH: mynewt::GPIO = fill_zero!(MynewtGPIO);
type MynewtGPIO = mynewt::GPIO;