@@ -13,7 +13,10 @@ use embedded_graphics::{
1313 } ,
1414} ;
1515use embedded_text:: TextBox ;
16- use esp_idf_svc:: sys:: EspError ;
16+ use esp_idf_svc:: {
17+ hal:: { self , gpio:: Pin , ledc:: LedcDriver } ,
18+ sys:: { ledc_timer_config_t, EspError } ,
19+ } ;
1720use u8g2_fonts:: U8g2TextStyle ;
1821
1922pub type ColorFormat = Rgb565 ;
@@ -35,16 +38,21 @@ fn init_spi() -> Result<(), EspError> {
3538 use esp_idf_svc:: sys:: * ;
3639 const GPIO_NUM_NC : i32 = -1 ;
3740
38- #[ cfg( not( feature = "cube" ) ) ]
41+ #[ cfg( all ( not( feature = "cube" ) , not ( feature = "cube2" ) ) ) ]
3942 const DISPLAY_MOSI_PIN : i32 = 47 ;
40- #[ cfg( not( feature = "cube" ) ) ]
43+ #[ cfg( all ( not( feature = "cube" ) , not ( feature = "cube2" ) ) ) ]
4144 const DISPLAY_CLK_PIN : i32 = 21 ;
4245
4346 #[ cfg( feature = "cube" ) ]
4447 const DISPLAY_MOSI_PIN : i32 = 41 ;
4548 #[ cfg( feature = "cube" ) ]
4649 const DISPLAY_CLK_PIN : i32 = 42 ;
4750
51+ #[ cfg( feature = "cube2" ) ]
52+ const DISPLAY_MOSI_PIN : i32 = 10 ;
53+ #[ cfg( feature = "cube2" ) ]
54+ const DISPLAY_CLK_PIN : i32 = 9 ;
55+
4856 let mut buscfg = spi_bus_config_t:: default ( ) ;
4957 buscfg. __bindgen_anon_1 . mosi_io_num = DISPLAY_MOSI_PIN ;
5058 buscfg. __bindgen_anon_2 . miso_io_num = GPIO_NUM_NC ;
@@ -66,11 +74,19 @@ static mut ESP_LCD_PANEL_HANDLE: esp_idf_svc::sys::esp_lcd_panel_handle_t = std:
6674#[ cfg( feature = "boards" ) ]
6775fn init_lcd ( ) -> Result < ( ) , EspError > {
6876 use esp_idf_svc:: sys:: * ;
69- #[ cfg( not( feature = "cube" ) ) ]
77+ #[ cfg( all ( not( feature = "cube" ) , not ( feature = "cube2" ) ) ) ]
7078 const DISPLAY_CS_PIN : i32 = 41 ;
7179 #[ cfg( feature = "cube" ) ]
7280 const DISPLAY_CS_PIN : i32 = 21 ;
81+ #[ cfg( feature = "cube2" ) ]
82+ const DISPLAY_CS_PIN : i32 = 14 ;
83+
84+ #[ cfg( not( feature = "cube2" ) ) ]
7385 const DISPLAY_DC_PIN : i32 = 40 ;
86+
87+ #[ cfg( feature = "cube2" ) ]
88+ const DISPLAY_DC_PIN : i32 = 8 ;
89+
7490 :: log:: info!( "Install panel IO" ) ;
7591 let mut panel_io: esp_lcd_panel_io_handle_t = std:: ptr:: null_mut ( ) ;
7692 let mut io_config = esp_lcd_panel_io_spi_config_t:: default ( ) ;
@@ -86,7 +102,12 @@ fn init_lcd() -> Result<(), EspError> {
86102 } ) ?;
87103
88104 :: log:: info!( "Install LCD driver" ) ;
105+ #[ cfg( not( feature = "cube2" ) ) ]
89106 const DISPLAY_RST_PIN : i32 = 45 ;
107+
108+ #[ cfg( feature = "cube2" ) ]
109+ const DISPLAY_RST_PIN : i32 = 18 ;
110+
90111 let mut panel_config = esp_lcd_panel_dev_config_t:: default ( ) ;
91112 let mut panel: esp_lcd_panel_handle_t = std:: ptr:: null_mut ( ) ;
92113
@@ -145,6 +166,33 @@ pub fn lcd_init() -> Result<(), EspError> {
145166 Ok ( ( ) )
146167}
147168
169+ #[ allow( unused) ]
170+ pub fn backlight_init ( bl_pin : hal:: gpio:: AnyIOPin ) -> anyhow:: Result < LedcDriver < ' static > > {
171+ let config = hal:: ledc:: config:: TimerConfig :: new ( )
172+ . resolution ( hal:: ledc:: Resolution :: Bits13 )
173+ . frequency ( hal:: units:: Hertz ( 5000 ) ) ;
174+ let time = unsafe { hal:: ledc:: TIMER0 :: new ( ) } ;
175+ let timer_driver = hal:: ledc:: LedcTimerDriver :: new ( time, & config) ?;
176+
177+ let ledc_driver =
178+ hal:: ledc:: LedcDriver :: new ( unsafe { hal:: ledc:: CHANNEL0 :: new ( ) } , timer_driver, bl_pin) ?;
179+
180+ Ok ( ledc_driver)
181+ }
182+
183+ const LEDC_MAX_DUTY : u32 = ( 1 << 13 ) - 1 ;
184+ #[ allow( unused) ]
185+ pub fn set_backlight < ' d > (
186+ ledc_driver : & mut hal:: ledc:: LedcDriver < ' d > ,
187+ light : u8 ,
188+ ) -> anyhow:: Result < ( ) > {
189+ let light = 100 . min ( light) as u32 ;
190+ let duty = LEDC_MAX_DUTY - ( 81 * ( 100 - light) ) ;
191+ let duty = if light == 0 { 0 } else { duty } ;
192+ ledc_driver. set_duty ( duty) ?;
193+ Ok ( ( ) )
194+ }
195+
148196#[ inline( always) ]
149197fn get_esp_lcd_panel_handle ( ) -> esp_idf_svc:: sys:: esp_lcd_panel_handle_t {
150198 #[ cfg( feature = "boards" ) ]
0 commit comments