Skip to content

Commit d0e5b6b

Browse files
authored
Merge pull request #12 from jbornschein/eg-update
Update to embedded_graphics V0.6 and st7735 V0.7
2 parents f955816 + b771755 commit d0e5b6b

File tree

4 files changed

+93
-90
lines changed

4 files changed

+93
-90
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ gd32vf103xx-hal = "0.3.0"
1414
embedded-hal = "0.2.3"
1515
nb = "0.1.2"
1616
riscv = "0.5.4"
17-
st7735-lcd = { version = "0.6.1", optional = true }
17+
st7735-lcd = { version = "0.7", optional = true }
1818

1919
[dev-dependencies]
2020
riscv-rt = "0.6.1"
2121
panic-halt = "0.2.0"
22-
embedded-graphics = "0.5"
22+
embedded-graphics = "0.6"
2323

2424
[features]
2525
lcd = ["st7735-lcd"]

examples/display.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,53 @@
33

44
use panic_halt as _;
55

6-
use riscv_rt::entry;
7-
use gd32vf103xx_hal::pac;
8-
use gd32vf103xx_hal::prelude::*;
9-
use longan_nano::lcd_pins;
10-
use longan_nano::lcd::Lcd;
11-
use embedded_graphics::prelude::*;
12-
use embedded_graphics::fonts::Font6x8;
6+
use embedded_graphics::fonts::{Font6x8, Text};
137
use embedded_graphics::pixelcolor::Rgb565;
8+
use embedded_graphics::prelude::*;
149
use embedded_graphics::primitives::Rectangle;
10+
use embedded_graphics::{primitive_style, text_style};
11+
use gd32vf103xx_hal::pac;
12+
use gd32vf103xx_hal::prelude::*;
13+
use longan_nano::{lcd, lcd_pins};
14+
use riscv_rt::entry;
1515

1616
#[entry]
1717
fn main() -> ! {
1818
let dp = pac::Peripherals::take().unwrap();
1919

2020
// Configure clocks
21-
let mut rcu = dp.RCU.configure().ext_hf_clock(8.mhz()).sysclk(108.mhz()).freeze();
21+
let mut rcu = dp
22+
.RCU
23+
.configure()
24+
.ext_hf_clock(8.mhz())
25+
.sysclk(108.mhz())
26+
.freeze();
2227
let mut afio = dp.AFIO.constrain(&mut rcu);
2328

2429
let gpioa = dp.GPIOA.split(&mut rcu);
2530
let gpiob = dp.GPIOB.split(&mut rcu);
2631

2732
let lcd_pins = lcd_pins!(gpioa, gpiob);
28-
let mut lcd = Lcd::new(dp.SPI0, lcd_pins, &mut afio, &mut rcu);
29-
let (width, height) = (lcd.width() as i32, lcd.height() as i32);
33+
let mut lcd = lcd::configure(dp.SPI0, lcd_pins, &mut afio, &mut rcu);
34+
let (width, height) = (lcd.size().width as i32, lcd.size().height as i32);
3035

3136
// Clear screen
32-
lcd.draw(Rectangle::new(Coord::new(0, 0), Coord::new(width - 1, height - 1))
33-
.fill(Some(Rgb565::from(0u16))));
34-
35-
let t = Font6x8::render_str(" Hello Rust! ")
36-
.fill(Some(Rgb565::from((0,0xff,0))))
37-
.translate(Coord::new(40, 35));
38-
lcd.draw(t);
37+
Rectangle::new(Point::new(0, 0), Point::new(width - 1, height - 1))
38+
.into_styled(primitive_style!(fill_color = Rgb565::BLACK))
39+
.draw(&mut lcd)
40+
.unwrap();
41+
42+
let style = text_style!(
43+
font = Font6x8,
44+
text_color = Rgb565::BLACK,
45+
background_color = Rgb565::GREEN
46+
);
47+
48+
// Create a text at position (20, 30) and draw it using style defined above
49+
Text::new(" Hello Rust! ", Point::new(40, 35))
50+
.into_styled(style)
51+
.draw(&mut lcd)
52+
.unwrap();
3953

4054
loop {}
4155
}

examples/ferris.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44
use panic_halt as _;
55

6-
use riscv_rt::entry;
7-
use gd32vf103xx_hal::prelude::*;
8-
use gd32vf103xx_hal::pac;
9-
use longan_nano::lcd_pins;
10-
use longan_nano::lcd::Lcd;
11-
use embedded_graphics::prelude::*;
6+
use embedded_graphics::image::{Image, ImageRaw};
7+
use embedded_graphics::pixelcolor::raw::LittleEndian;
128
use embedded_graphics::pixelcolor::Rgb565;
13-
use embedded_graphics::image::Image16BPP;
9+
use embedded_graphics::prelude::*;
10+
use embedded_graphics::primitive_style;
1411
use embedded_graphics::primitives::Rectangle;
12+
use gd32vf103xx_hal::pac;
13+
use gd32vf103xx_hal::prelude::*;
14+
use longan_nano::{lcd, lcd_pins};
15+
use riscv_rt::entry;
1516

1617
const FERRIS: &[u8] = include_bytes!("ferris.raw");
1718

@@ -20,23 +21,32 @@ fn main() -> ! {
2021
let dp = pac::Peripherals::take().unwrap();
2122

2223
// Configure clocks
23-
let mut rcu = dp.RCU.configure().ext_hf_clock(8.mhz()).sysclk(108.mhz()).freeze();
24+
let mut rcu = dp
25+
.RCU
26+
.configure()
27+
.ext_hf_clock(8.mhz())
28+
.sysclk(108.mhz())
29+
.freeze();
2430
let mut afio = dp.AFIO.constrain(&mut rcu);
2531

2632
let gpioa = dp.GPIOA.split(&mut rcu);
2733
let gpiob = dp.GPIOB.split(&mut rcu);
2834

2935
let lcd_pins = lcd_pins!(gpioa, gpiob);
30-
let mut lcd = Lcd::new(dp.SPI0, lcd_pins, &mut afio, &mut rcu);
31-
let (width, height) = (lcd.width() as i32, lcd.height() as i32);
36+
let mut lcd = lcd::configure(dp.SPI0, lcd_pins, &mut afio, &mut rcu);
37+
let (width, height) = (lcd.size().width as i32, lcd.size().height as i32);
3238

3339
// Clear screen
34-
lcd.draw(Rectangle::new(Coord::new(0, 0), Coord::new(width - 1, height - 1))
35-
.fill(Some(Rgb565::from(0u16))));
36-
37-
let image: Image<Rgb565, _> = Image16BPP::new(&FERRIS, 86, 64);
38-
let image = image.translate(Coord::new(width/2 - 43, height/2 - 32));
39-
lcd.draw(&image);
40+
Rectangle::new(Point::new(0, 0), Point::new(width - 1, height - 1))
41+
.into_styled(primitive_style!(fill_color = Rgb565::BLACK))
42+
.draw(&mut lcd)
43+
.unwrap();
44+
45+
// Load Image Data
46+
let raw_image: ImageRaw<Rgb565, LittleEndian> = ImageRaw::new(&FERRIS, 86, 64);
47+
Image::new(&raw_image, Point::new(width / 2 - 43, height / 2 - 32))
48+
.draw(&mut lcd)
49+
.unwrap();
4050

4151
loop {}
4252
}

src/lcd.rs

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
//! On-board LCD
22
3-
use gd32vf103xx_hal::gpio::{Input, Output, Alternate, PushPull, Floating};
3+
use embedded_hal::digital::v2::OutputPin;
4+
use gd32vf103xx_hal::afio::Afio;
5+
use gd32vf103xx_hal::delay::McycleDelay;
46
use gd32vf103xx_hal::gpio::gpioa::{PA5, PA6, PA7};
57
use gd32vf103xx_hal::gpio::gpiob::{PB0, PB1, PB2};
8+
use gd32vf103xx_hal::gpio::{Alternate, Floating, Input, Output, PushPull};
9+
use gd32vf103xx_hal::pac::SPI0;
610
use gd32vf103xx_hal::rcu::Rcu;
7-
use gd32vf103xx_hal::afio::Afio;
811
use gd32vf103xx_hal::spi::{Spi, MODE_0};
9-
use gd32vf103xx_hal::pac::SPI0;
10-
use st7735_lcd::{ST7735, Orientation};
11-
use embedded_hal::digital::v2::OutputPin;
1212
use gd32vf103xx_hal::time::U32Ext;
13-
use gd32vf103xx_hal::delay::McycleDelay;
14-
use core::ops::{Deref, DerefMut};
13+
use st7735_lcd::{Orientation, ST7735};
1514

1615
/// Sets up all the needed GPIO pins for the LCD
1716
///
@@ -31,7 +30,7 @@ macro_rules! lcd_pins {
3130
dc: $gpiob.pb0.into_push_pull_output(),
3231
rst: $gpiob.pb1.into_push_pull_output(),
3332
}
34-
}}
33+
}};
3534
}
3635

3736
type MisoPin = PA6<Input<Floating>>;
@@ -41,7 +40,7 @@ type CsPin = PB2<Output<PushPull>>;
4140
type DcPin = PB0<Output<PushPull>>;
4241
type RstPin = PB1<Output<PushPull>>;
4342
type SpiType = Spi<SPI0, (SckPin, MisoPin, MosiPin)>;
44-
type LcdType = ST7735<SpiType, DcPin, RstPin>;
43+
pub type Lcd = ST7735<SpiType, DcPin, RstPin>;
4544

4645
/// Pins consumed by LCD driver
4746
pub struct LcdPins {
@@ -53,53 +52,33 @@ pub struct LcdPins {
5352
pub rst: RstPin,
5453
}
5554

56-
/// LCD driver wrapper
57-
pub struct Lcd {
58-
driver: LcdType,
59-
}
60-
61-
impl Lcd {
62-
/// Constructs LCD driver from the required components
63-
pub fn new(spi: SPI0, pins: LcdPins, afio: &mut Afio, rcu: &mut Rcu) -> Lcd {
64-
let spi0 = Spi::spi0(spi, (pins.sck, pins.miso, pins.mosi), afio, MODE_0, 16.mhz(), rcu);
55+
/// Constructs LCD driver from the required components
56+
pub fn configure(spi: SPI0, pins: LcdPins, afio: &mut Afio, rcu: &mut Rcu) -> Lcd {
57+
let (width, height) = (160, 80);
58+
let spi0 = Spi::spi0(
59+
spi,
60+
(pins.sck, pins.miso, pins.mosi),
61+
afio,
62+
MODE_0,
63+
16.mhz(),
64+
rcu,
65+
);
6566

66-
let mut cs = pins.cs;
67-
cs.set_low().unwrap();
67+
let mut cs = pins.cs;
68+
cs.set_low().unwrap();
6869

69-
let mut lcd = ST7735::new(spi0, pins.dc, pins.rst, false, true);
70-
let mut delay = McycleDelay::new(&rcu.clocks);
71-
lcd.init(&mut delay).unwrap();
72-
lcd.set_orientation(&Orientation::Landscape).unwrap();
73-
lcd.set_offset(1, 26);
70+
let mut lcd = ST7735::new(
71+
spi0,
72+
pins.dc,
73+
pins.rst,
74+
false,
75+
true,
76+
width,
77+
height);
78+
let mut delay = McycleDelay::new(&rcu.clocks);
79+
lcd.init(&mut delay).unwrap();
80+
lcd.set_orientation(&Orientation::Landscape).unwrap();
81+
lcd.set_offset(1, 26);
7482

75-
Lcd {
76-
driver: lcd
77-
}
83+
lcd
7884
}
79-
80-
/// Screen width
81-
pub const fn width(&self) -> u32 {
82-
160
83-
}
84-
85-
/// Screen height
86-
pub const fn height(&self) -> u32 {
87-
80
88-
}
89-
}
90-
91-
impl Deref for Lcd {
92-
type Target = LcdType;
93-
94-
#[inline(always)]
95-
fn deref(&self) -> &Self::Target {
96-
&self.driver
97-
}
98-
}
99-
100-
impl DerefMut for Lcd {
101-
#[inline(always)]
102-
fn deref_mut(&mut self) -> &mut Self::Target {
103-
&mut self.driver
104-
}
105-
}

0 commit comments

Comments
 (0)