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