Skip to content

Commit c968f3f

Browse files
committed
Implement ST7306 display
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 9db273a commit c968f3f

File tree

7 files changed

+130
-93
lines changed

7 files changed

+130
-93
lines changed

Cargo.lock

Lines changed: 12 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

b1display/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ usbd-serial = "0.1.1"
2626
usbd-hid = "0.5.1"
2727
fugit = "0.3.6"
2828

29-
display-interface-spi = "0.4.1"
30-
mipidsi = "0.6.0"
29+
st7306-lcd = { git = "ssh://[email protected]/FrameworkComputer/st7306.git" }
3130
embedded-graphics = "0.7"
3231
tinybmp = "0.4.0"
3332

b1display/src/main.rs

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use cortex_m::delay::Delay;
99
use defmt_rtt as _;
1010
use embedded_hal::digital::v2::{InputPin, OutputPin};
1111

12-
use mipidsi::Orientation;
1312
use rp2040_hal::gpio::bank0::Gpio18;
1413
use rp2040_hal::gpio::{Output, Pin, PushPull};
1514
//#[cfg(debug_assertions)]
@@ -18,6 +17,9 @@ use rp2040_panic_usb_boot as _;
1817

1918
use embedded_graphics::pixelcolor::Rgb565;
2019
use embedded_graphics::prelude::*;
20+
use embedded_graphics::primitives::*;
21+
use embedded_hal::blocking::spi;
22+
use st7306_lcd::{Orientation, ST7306};
2123

2224
// Provide an alias for our BSP so we can switch targets quickly.
2325
// 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::{
2931
clocks::{init_clocks_and_plls, Clock},
3032
pac,
3133
sio::Sio,
34+
gpio,
3235
usb,
3336
watchdog::Watchdog,
3437
Timer,
@@ -42,7 +45,7 @@ use usb_device::{class_prelude::*, prelude::*};
4245
use usbd_serial::{SerialPort, USB_CLASS_CDC};
4346

4447
// Used to demonstrate writing formatted strings
45-
use core::fmt::Write;
48+
use core::fmt::{Debug, Write};
4649
use heapless::String;
4750

4851
use lotus_inputmodules::control::*;
@@ -126,10 +129,12 @@ fn main() -> ! {
126129
let _spi_sclk = pins.scl.into_mode::<bsp::hal::gpio::FunctionSpi>();
127130
let _spi_mosi = pins.sda.into_mode::<bsp::hal::gpio::FunctionSpi>();
128131
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);
130133
// Display control pins
131134
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();
133138
let rst = pins.rstb.into_push_pull_output();
134139

135140
let spi = spi.init(
@@ -139,27 +144,46 @@ fn main() -> ! {
139144
&embedded_hal::spi::MODE_0,
140145
);
141146

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();
150158

151159
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();
152180
draw_text(
153181
&mut disp,
154182
"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),
156184
)
157185
.unwrap();
158186

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-
163187
let sleep = pins.sleep.into_pull_down_input();
164188

165189
let mut state = State {
@@ -172,10 +196,9 @@ fn main() -> ! {
172196
let mut prev_timer = timer.get_counter().ticks();
173197

174198
loop {
175-
// TODO: Current hardware revision does not have the sleep pin wired up :(
176199
// 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);
179202

180203
// Handle period display updates. Don't do it too often
181204
if timer.get_counter().ticks() > prev_timer + 20_000 {
@@ -212,7 +235,7 @@ fn main() -> ! {
212235
Ok(count) => {
213236
if let Some(command) = parse_command(count, &buf) {
214237
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);
216239
} else if let SleepState::Awake = state.sleeping {
217240
// While sleeping no command is handled, except waking up
218241
//handle_command(&command, &mut disp, logo_rect);
@@ -227,20 +250,26 @@ fn main() -> ! {
227250
}
228251
}
229252

230-
fn handle_sleep(
253+
fn handle_sleep<SPI, DC, CS, RST, const COLS: usize, const ROWS: usize>(
231254
go_sleeping: bool,
232255
state: &mut State,
233256
_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+
{
236265
match (state.sleeping.clone(), go_sleeping) {
237266
(SleepState::Awake, false) => (),
238267
(SleepState::Awake, true) => {
239268
state.sleeping = SleepState::Sleeping;
240269
//state.grid = display_sleep();
241270

242271
// Turn off backlight
243-
lcd_led.set_low().unwrap();
272+
disp.on_off(false);
244273

245274
// TODO: Power Display controller down
246275

@@ -253,9 +282,7 @@ fn handle_sleep(
253282
state.sleeping = SleepState::Awake;
254283

255284
// TODO: Power display controller back on
256-
257-
// Turn backlight back on
258-
lcd_led.set_high().unwrap();
285+
disp.on_off(true);
259286
}
260287
}
261288
}

lotus-inputmodules/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ fugit = "0.3.6"
3030
is31fl3741 = { git = "https://github.com/JohnAZoidberg/is31fl3741", branch = "all-at-once", optional = true }
3131

3232
# B1 Display
33-
display-interface-spi = { version = "0.4.1", optional = true }
34-
mipidsi = { version = "0.6.0", optional = true }
33+
st7306-lcd = { git = "ssh://[email protected]/FrameworkComputer/st7306.git", optional = true }
3534
embedded-graphics = { version = "0.7", optional = true }
3635
tinybmp = { version = "0.4.0", optional = true }
3736

@@ -42,5 +41,5 @@ ws2812-pio = { version = "0.5.0", optional = true }
4241
[features]
4342
default = []
4443
ledmatrix = [ "is31fl3741" ]
45-
b1display = [ "display-interface-spi", "mipidsi", "embedded-graphics", "tinybmp" ]
44+
b1display = [ "st7306-lcd", "embedded-graphics", "tinybmp" ]
4645
c1minimal = ["smart-leds", "ws2812-pio" ]

0 commit comments

Comments
 (0)