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 ;
46use gd32vf103xx_hal:: gpio:: gpioa:: { PA5 , PA6 , PA7 } ;
57use gd32vf103xx_hal:: gpio:: gpiob:: { PB0 , PB1 , PB2 } ;
8+ use gd32vf103xx_hal:: gpio:: { Alternate , Floating , Input , Output , PushPull } ;
9+ use gd32vf103xx_hal:: pac:: SPI0 ;
610use gd32vf103xx_hal:: rcu:: Rcu ;
7- use gd32vf103xx_hal:: afio:: Afio ;
811use 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 ;
1212use 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
3736type MisoPin = PA6 < Input < Floating > > ;
@@ -41,7 +40,7 @@ type CsPin = PB2<Output<PushPull>>;
4140type DcPin = PB0 < Output < PushPull > > ;
4241type RstPin = PB1 < Output < PushPull > > ;
4342type 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
4746pub 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