@@ -9,7 +9,6 @@ use cortex_m::delay::Delay;
9
9
use defmt_rtt as _;
10
10
use embedded_hal:: digital:: v2:: { InputPin , OutputPin } ;
11
11
12
- use mipidsi:: Orientation ;
13
12
use rp2040_hal:: gpio:: bank0:: Gpio18 ;
14
13
use rp2040_hal:: gpio:: { Output , Pin , PushPull } ;
15
14
//#[cfg(debug_assertions)]
@@ -18,6 +17,9 @@ use rp2040_panic_usb_boot as _;
18
17
19
18
use embedded_graphics:: pixelcolor:: Rgb565 ;
20
19
use embedded_graphics:: prelude:: * ;
20
+ use embedded_graphics:: primitives:: * ;
21
+ use embedded_hal:: blocking:: spi;
22
+ use st7306_lcd:: { Orientation , ST7306 } ;
21
23
22
24
// Provide an alias for our BSP so we can switch targets quickly.
23
25
// Uncomment the BSP you included in Cargo.toml, the rest of the code does not need to change.
@@ -29,6 +31,7 @@ use bsp::hal::{
29
31
clocks:: { init_clocks_and_plls, Clock } ,
30
32
pac,
31
33
sio:: Sio ,
34
+ gpio,
32
35
usb,
33
36
watchdog:: Watchdog ,
34
37
Timer ,
@@ -42,7 +45,7 @@ use usb_device::{class_prelude::*, prelude::*};
42
45
use usbd_serial:: { SerialPort , USB_CLASS_CDC } ;
43
46
44
47
// Used to demonstrate writing formatted strings
45
- use core:: fmt:: Write ;
48
+ use core:: fmt:: { Debug , Write } ;
46
49
use heapless:: String ;
47
50
48
51
use lotus_inputmodules:: control:: * ;
@@ -126,10 +129,12 @@ fn main() -> ! {
126
129
let _spi_sclk = pins. scl . into_mode :: < bsp:: hal:: gpio:: FunctionSpi > ( ) ;
127
130
let _spi_mosi = pins. sda . into_mode :: < bsp:: hal:: gpio:: FunctionSpi > ( ) ;
128
131
let _spi_miso = pins. miso . into_mode :: < bsp:: hal:: gpio:: FunctionSpi > ( ) ;
129
- let spi = bsp:: hal:: Spi :: < _ , _ , 8 > :: new ( pac. SPI1 ) ;
132
+ let spi = bsp:: hal:: Spi :: < _ , _ , 8 > :: new ( pac. SPI0 ) ;
130
133
// Display control pins
131
134
let dc = pins. dc . into_push_pull_output ( ) ;
132
- let mut lcd_led = pins. backlight . into_push_pull_output ( ) ;
135
+ //let mut lcd_led = pins.backlight.into_push_pull_output();
136
+ let mut cs = pins. cs . into_push_pull_output ( ) ;
137
+ cs. set_low ( ) . unwrap ( ) ;
133
138
let rst = pins. rstb . into_push_pull_output ( ) ;
134
139
135
140
let spi = spi. init (
@@ -139,27 +144,46 @@ fn main() -> ! {
139
144
& embedded_hal:: spi:: MODE_0 ,
140
145
) ;
141
146
142
- // Create a DisplayInterface from SPI and DC pin, with no manual CS control
143
- let di = display_interface_spi:: SPIInterfaceNoCS :: new ( spi, dc) ;
144
- let mut disp = mipidsi:: Builder :: st7735s ( di)
145
- . with_invert_colors ( true ) // Looks cooler. TODO: Should invert image not entire screen
146
- . with_orientation ( Orientation :: PortraitInverted ( false ) )
147
- . init ( & mut delay, Some ( rst) )
148
- . unwrap ( ) ;
149
- disp. clear ( Rgb565 :: WHITE ) . unwrap ( ) ;
147
+ let mut disp: ST7306 <
148
+ rp2040_hal:: Spi < rp2040_hal:: spi:: Enabled , pac:: SPI0 , 8 > ,
149
+ Pin < gpio:: bank0:: Gpio20 , Output < PushPull > > ,
150
+ Pin < gpio:: bank0:: Gpio17 , Output < PushPull > > ,
151
+ Pin < gpio:: bank0:: Gpio21 , Output < PushPull > > ,
152
+ 25 ,
153
+ 200 ,
154
+ > = ST7306 :: new ( spi, dc, cs, rst, false , 300 , 460 ) ;
155
+ disp. init ( & mut delay) . unwrap ( ) ;
156
+
157
+ disp. clear ( Rgb565 :: BLACK ) . unwrap ( ) ;
150
158
151
159
let logo_rect = draw_logo ( & mut disp) . unwrap ( ) ;
160
+ Rectangle :: new ( Point :: new ( 10 , 10 ) , Size :: new ( 10 , 10 ) )
161
+ . into_styled ( PrimitiveStyle :: with_fill ( Rgb565 :: WHITE ) )
162
+ . draw ( & mut disp)
163
+ . unwrap ( ) ;
164
+ Rectangle :: new ( Point :: new ( 20 , 20 ) , Size :: new ( 10 , 10 ) )
165
+ . into_styled ( PrimitiveStyle :: with_fill ( Rgb565 :: WHITE ) )
166
+ . draw ( & mut disp)
167
+ . unwrap ( ) ;
168
+ Rectangle :: new ( Point :: new ( 30 , 30 ) , Size :: new ( 10 , 10 ) )
169
+ . into_styled ( PrimitiveStyle :: with_fill ( Rgb565 :: WHITE ) )
170
+ . draw ( & mut disp)
171
+ . unwrap ( ) ;
172
+ Rectangle :: new ( Point :: new ( 40 , 40 ) , Size :: new ( 10 , 10 ) )
173
+ . into_styled ( PrimitiveStyle :: with_fill ( Rgb565 :: WHITE ) )
174
+ . draw ( & mut disp)
175
+ . unwrap ( ) ;
176
+ Rectangle :: new ( Point :: new ( 50 , 50 ) , Size :: new ( 10 , 10 ) )
177
+ . into_styled ( PrimitiveStyle :: with_fill ( Rgb565 :: WHITE ) )
178
+ . draw ( & mut disp)
179
+ . unwrap ( ) ;
152
180
draw_text (
153
181
& mut disp,
154
182
"Framework" ,
155
- Point :: new ( 0 , LOGO_OFFSET + logo_rect. size . height as i32 ) ,
183
+ Point :: new ( LOGO_OFFSET_X , LOGO_OFFSET_Y + logo_rect. size . height as i32 ) ,
156
184
)
157
185
. unwrap ( ) ;
158
186
159
- // Wait until the background and image have been rendered otherwise
160
- // the screen will show random pixels for a brief moment
161
- lcd_led. set_high ( ) . unwrap ( ) ;
162
-
163
187
let sleep = pins. sleep . into_pull_down_input ( ) ;
164
188
165
189
let mut state = State {
@@ -172,10 +196,9 @@ fn main() -> ! {
172
196
let mut prev_timer = timer. get_counter ( ) . ticks ( ) ;
173
197
174
198
loop {
175
- // TODO: Current hardware revision does not have the sleep pin wired up :(
176
199
// Go to sleep if the host is sleeping
177
- let _host_sleeping = sleep. is_low ( ) . unwrap ( ) ;
178
- // handle_sleep(host_sleeping, &mut state, &mut matrix , &mut delay );
200
+ let host_sleeping = sleep. is_low ( ) . unwrap ( ) ;
201
+ handle_sleep ( host_sleeping, & mut state, & mut delay , & mut disp ) ;
179
202
180
203
// Handle period display updates. Don't do it too often
181
204
if timer. get_counter ( ) . ticks ( ) > prev_timer + 20_000 {
@@ -212,7 +235,7 @@ fn main() -> ! {
212
235
Ok ( count) => {
213
236
if let Some ( command) = parse_command ( count, & buf) {
214
237
if let Command :: Sleep ( go_sleeping) = command {
215
- handle_sleep ( go_sleeping , & mut state, & mut delay, & mut lcd_led ) ;
238
+ handle_sleep ( host_sleeping , & mut state, & mut delay, & mut disp ) ;
216
239
} else if let SleepState :: Awake = state. sleeping {
217
240
// While sleeping no command is handled, except waking up
218
241
//handle_command(&command, &mut disp, logo_rect);
@@ -227,20 +250,26 @@ fn main() -> ! {
227
250
}
228
251
}
229
252
230
- fn handle_sleep (
253
+ fn handle_sleep < SPI , DC , CS , RST , const COLS : usize , const ROWS : usize > (
231
254
go_sleeping : bool ,
232
255
state : & mut State ,
233
256
_delay : & mut Delay ,
234
- lcd_led : & mut Pin < Gpio18 , Output < PushPull > > ,
235
- ) {
257
+ disp : & mut ST7306 < SPI , DC , CS , RST , COLS , ROWS > ,
258
+ ) where
259
+ SPI : spi:: Write < u8 > ,
260
+ DC : OutputPin ,
261
+ CS : OutputPin ,
262
+ RST : OutputPin ,
263
+ <SPI as spi:: Write < u8 > >:: Error : Debug ,
264
+ {
236
265
match ( state. sleeping . clone ( ) , go_sleeping) {
237
266
( SleepState :: Awake , false ) => ( ) ,
238
267
( SleepState :: Awake , true ) => {
239
268
state. sleeping = SleepState :: Sleeping ;
240
269
//state.grid = display_sleep();
241
270
242
271
// Turn off backlight
243
- lcd_led . set_low ( ) . unwrap ( ) ;
272
+ disp . on_off ( false ) ;
244
273
245
274
// TODO: Power Display controller down
246
275
@@ -253,9 +282,7 @@ fn handle_sleep(
253
282
state. sleeping = SleepState :: Awake ;
254
283
255
284
// TODO: Power display controller back on
256
-
257
- // Turn backlight back on
258
- lcd_led. set_high ( ) . unwrap ( ) ;
285
+ disp. on_off ( true ) ;
259
286
}
260
287
}
261
288
}
0 commit comments