From b5433d705581de21094515a28f5e8895ef00935a Mon Sep 17 00:00:00 2001 From: Martin Schaer Date: Sun, 13 Aug 2023 11:53:34 +0100 Subject: [PATCH 01/12] add spi st7789 rpi zero w example --- .cargo/config | 2 + Cargo.toml | 3 +- .../examples/spi-st7789-rpi-zero-w/Cargo.toml | 10 +++ .../examples/spi-st7789-rpi-zero-w/README.md | 32 ++++++++ .../spi-st7789-rpi-zero-w/src/main.rs | 78 +++++++++++++++++++ 5 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 .cargo/config create mode 100644 mipidsi/examples/spi-st7789-rpi-zero-w/Cargo.toml create mode 100644 mipidsi/examples/spi-st7789-rpi-zero-w/README.md create mode 100644 mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..6fb7d4c --- /dev/null +++ b/.cargo/config @@ -0,0 +1,2 @@ +[target.arm-unknown-linux-musleabihf] +linker = "arm-linux-musleabihf-gcc" diff --git a/Cargo.toml b/Cargo.toml index 6f55c87..4f66460 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,6 @@ members = [ "mipidsi", + "mipidsi/examples/*", "mipidsi-async", -] \ No newline at end of file +] diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/Cargo.toml b/mipidsi/examples/spi-st7789-rpi-zero-w/Cargo.toml new file mode 100644 index 0000000..7b5b61a --- /dev/null +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "spi-st7789-rpi-zero-w" +version = "0.1.0" +edition = "2021" + +[dependencies] +display-interface-spi = "0.4.1" +embedded-graphics = "0.8.0" +mipidsi = "0.7.1" +rppal = { version = "0.14.1", features = ["hal"] } diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/README.md b/mipidsi/examples/spi-st7789-rpi-zero-w/README.md new file mode 100644 index 0000000..86cc553 --- /dev/null +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/README.md @@ -0,0 +1,32 @@ +# SPI ST7789 on a Raspberry Pi Zero W Example + +Tested with [Display HAT Mini by Pomoroni](https://shop.pimoroni.com/products/display-hat-mini?variant=39496084717651). + +## Build, strip, copy, and run + +### Mac OS + +Prerequisite: musl-cross (Homebrew users: `brew install filosottile/musl-cross/musl-cross`) + +https://github.com/FiloSottile/homebrew-musl-cross + +```bash +# build for rpi zero 2 w +cargo build --release --target=arm-unknown-linux-musleabihf -p spi-st7789-rpi-zero-w +# look at the size of the bin file +ls -lh target/arm-unknown-linux-musleabihf/release/spi-st7789-rpi-zero-w +# strip it +arm-linux-musleabihf-strip target/arm-unknown-linux-musleabihf/release/spi-st7789-rpi-zero-w +# look at it now ;) +ls -lh target/arm-unknown-linux-musleabihf/release/spi-st7789-rpi-zero-w +# copy over ssh +scp target/arm-unknown-linux-musleabihf/release/spi-st7789-rpi-zero-w pi@raspberrypi.local:~/ +# ssh into the rpi to run it +ssh pi@raspberrypi.local +# run it +./spi-st7789-rpi-zero-w +``` + +### Linux + +Not tested. Follow this article [Raspberry Pi Zero Raspbian/Rust Primer](https://dev.to/jeikabu/raspberry-pi-zero-raspbian-rust-primer-3aj6). diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs b/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs new file mode 100644 index 0000000..cfb24ce --- /dev/null +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs @@ -0,0 +1,78 @@ +use display_interface_spi::SPIInterface; +use embedded_graphics::{ + pixelcolor::Rgb565, + prelude::*, + primitives::{PrimitiveStyleBuilder, Rectangle}, +}; +use mipidsi::Builder; +use rppal::gpio::{Gpio, OutputPin}; +use rppal::hal::Delay; +use rppal::spi::{Bus, Mode, SlaveSelect, Spi}; +use std::process::ExitCode; +use std::thread::sleep; +use std::time::Duration; + +const SPI_DC: u8 = 9; +const SPI_CS: u8 = 1; +const BACKLIGHT: u8 = 13; + +fn main() -> ExitCode { + let gpio = Gpio::new().unwrap(); + let dc = gpio.get(SPI_DC).unwrap().into_output(); + let cs = gpio.get(SPI_CS).unwrap().into_output(); + let mut backlight = gpio.get(BACKLIGHT).unwrap().into_output(); + + let mut delay = Delay::new(); + + let clock_speed = 60_000_000_u32; + let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss1, clock_speed, Mode::Mode0).unwrap(); + let di = SPIInterface::new(spi, dc, cs); + + let w = 240_u16; + let h = 320_u16; + let mut display = Builder::st7789(di) + .with_display_size(w, h) + // .with_orientation(mipidsi::Orientation::Landscape(false)) + .with_invert_colors(mipidsi::ColorInversion::Inverted) + .init(&mut delay, None::) + .unwrap(); + + // Turn on backlight + backlight.set_low(); + sleep(Duration::from_millis(150)); + backlight.set_high(); + + // Clear the display initially + let style = PrimitiveStyleBuilder::new() + .fill_color(Rgb565::BLACK) + .build(); + Rectangle::new(Point::new(0, 0), Size::new(w.into(), h.into())) + .into_styled(style) + .draw(&mut display) + .unwrap(); + sleep(Duration::from_millis(150)); + + loop { + // Fill the display with red + let style = PrimitiveStyleBuilder::new().fill_color(Rgb565::RED).build(); + Rectangle::new(Point::new(0, 0), Size::new(w.into(), h.into())) + .into_styled(style) + .draw(&mut display) + .unwrap(); + + // Wait for some time + sleep(Duration::from_millis(500)); + + // Clear the display + let style = PrimitiveStyleBuilder::new() + .fill_color(Rgb565::BLUE) + .build(); + Rectangle::new(Point::new(0, 0), Size::new(w.into(), h.into())) + .into_styled(style) + .draw(&mut display) + .unwrap(); + + // Wait for some time + sleep(Duration::from_millis(500)); + } +} From 94e5eb3fc03146ded15b6fba7eb1d1e74dc490fe Mon Sep 17 00:00:00 2001 From: Martin Schaer Date: Sun, 13 Aug 2023 12:00:34 +0100 Subject: [PATCH 02/12] clean example readme --- mipidsi/examples/spi-st7789-rpi-zero-w/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/README.md b/mipidsi/examples/spi-st7789-rpi-zero-w/README.md index 86cc553..75e01e0 100644 --- a/mipidsi/examples/spi-st7789-rpi-zero-w/README.md +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/README.md @@ -8,8 +8,6 @@ Tested with [Display HAT Mini by Pomoroni](https://shop.pimoroni.com/products/di Prerequisite: musl-cross (Homebrew users: `brew install filosottile/musl-cross/musl-cross`) -https://github.com/FiloSottile/homebrew-musl-cross - ```bash # build for rpi zero 2 w cargo build --release --target=arm-unknown-linux-musleabihf -p spi-st7789-rpi-zero-w From 450a3fd70a539c49f26c44c7bf1b97ae6a8da185 Mon Sep 17 00:00:00 2001 From: Martin Schaer Date: Sun, 13 Aug 2023 18:56:19 +0100 Subject: [PATCH 03/12] fix(examples/spi-st7789-rpi-zero-w): fix orientation and add scrolling text --- .../spi-st7789-rpi-zero-w/src/main.rs | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs b/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs index cfb24ce..10b0c99 100644 --- a/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs @@ -1,8 +1,9 @@ use display_interface_spi::SPIInterface; use embedded_graphics::{ + mono_font::{ascii::FONT_6X10, MonoTextStyle}, pixelcolor::Rgb565, prelude::*, - primitives::{PrimitiveStyleBuilder, Rectangle}, + text::Text, }; use mipidsi::Builder; use rppal::gpio::{Gpio, OutputPin}; @@ -28,51 +29,50 @@ fn main() -> ExitCode { let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss1, clock_speed, Mode::Mode0).unwrap(); let di = SPIInterface::new(spi, dc, cs); - let w = 240_u16; - let h = 320_u16; + const W: i32 = 320; + const H: i32 = 240; let mut display = Builder::st7789(di) - .with_display_size(w, h) - // .with_orientation(mipidsi::Orientation::Landscape(false)) + // width and height are switched on porpuse because of the orientation + .with_display_size(H as u16, W as u16) + // this orientation applies for the Display HAT Mini by Pimoroni + .with_orientation(mipidsi::Orientation::LandscapeInverted(true)) .with_invert_colors(mipidsi::ColorInversion::Inverted) .init(&mut delay, None::) .unwrap(); - // Turn on backlight - backlight.set_low(); - sleep(Duration::from_millis(150)); - backlight.set_high(); + // Text + let character_style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE); + let text = "Hello World ^_^"; + let text_w = text.len() as i32 * 6; + let mut text_x = W; + + // Anternating color + let colors = [Rgb565::RED, Rgb565::GREEN, Rgb565::BLUE]; + let mut color_index = 0; // Clear the display initially - let style = PrimitiveStyleBuilder::new() - .fill_color(Rgb565::BLACK) - .build(); - Rectangle::new(Point::new(0, 0), Size::new(w.into(), h.into())) - .into_styled(style) - .draw(&mut display) - .unwrap(); - sleep(Duration::from_millis(150)); + display.clear(colors[0]).unwrap(); + + // Turn on backlight + backlight.set_high(); loop { - // Fill the display with red - let style = PrimitiveStyleBuilder::new().fill_color(Rgb565::RED).build(); - Rectangle::new(Point::new(0, 0), Size::new(w.into(), h.into())) - .into_styled(style) - .draw(&mut display) - .unwrap(); + // Text scroll + text_x = (text_x - 6) % (W + text_w); + if text_x < -text_w { + text_x = W; + } - // Wait for some time - sleep(Duration::from_millis(500)); + // Fill the display with red + display.clear(colors[color_index]).unwrap(); + color_index = (color_index + 1) % colors.len(); - // Clear the display - let style = PrimitiveStyleBuilder::new() - .fill_color(Rgb565::BLUE) - .build(); - Rectangle::new(Point::new(0, 0), Size::new(w.into(), h.into())) - .into_styled(style) + // Draw text + Text::new(text, Point::new(text_x, H / 2), character_style) .draw(&mut display) .unwrap(); // Wait for some time - sleep(Duration::from_millis(500)); + sleep(Duration::from_millis(250)); } } From 180be2b7847ac247da6e6e53c0fa096cc549ac3e Mon Sep 17 00:00:00 2001 From: Martin Schaer Date: Sun, 13 Aug 2023 21:29:02 +0100 Subject: [PATCH 04/12] fix(examples/spi-st7789-rpi-zero-w): workspace --- Cargo.toml | 1 - mipidsi/examples/spi-st7789-rpi-zero-w/.cargo/config.toml | 2 ++ mipidsi/examples/spi-st7789-rpi-zero-w/.gitignore | 2 ++ mipidsi/examples/spi-st7789-rpi-zero-w/Cargo.toml | 2 ++ mipidsi/examples/spi-st7789-rpi-zero-w/README.md | 2 +- 5 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 mipidsi/examples/spi-st7789-rpi-zero-w/.cargo/config.toml create mode 100644 mipidsi/examples/spi-st7789-rpi-zero-w/.gitignore diff --git a/Cargo.toml b/Cargo.toml index 4f66460..852f0d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,5 @@ members = [ "mipidsi", - "mipidsi/examples/*", "mipidsi-async", ] diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/.cargo/config.toml b/mipidsi/examples/spi-st7789-rpi-zero-w/.cargo/config.toml new file mode 100644 index 0000000..5082bb7 --- /dev/null +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +target = "arm-unknown-linux-musleabihf" diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/.gitignore b/mipidsi/examples/spi-st7789-rpi-zero-w/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/Cargo.toml b/mipidsi/examples/spi-st7789-rpi-zero-w/Cargo.toml index 7b5b61a..e28c5a8 100644 --- a/mipidsi/examples/spi-st7789-rpi-zero-w/Cargo.toml +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/Cargo.toml @@ -8,3 +8,5 @@ display-interface-spi = "0.4.1" embedded-graphics = "0.8.0" mipidsi = "0.7.1" rppal = { version = "0.14.1", features = ["hal"] } + +[workspace] diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/README.md b/mipidsi/examples/spi-st7789-rpi-zero-w/README.md index 75e01e0..d285e61 100644 --- a/mipidsi/examples/spi-st7789-rpi-zero-w/README.md +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/README.md @@ -10,7 +10,7 @@ Prerequisite: musl-cross (Homebrew users: `brew install filosottile/musl-cross/m ```bash # build for rpi zero 2 w -cargo build --release --target=arm-unknown-linux-musleabihf -p spi-st7789-rpi-zero-w +cargo build --release --target=arm-unknown-linux-musleabihf # look at the size of the bin file ls -lh target/arm-unknown-linux-musleabihf/release/spi-st7789-rpi-zero-w # strip it From b0f0388735849b29feb9219421232743dcf24e2c Mon Sep 17 00:00:00 2001 From: Martin Schaer Date: Mon, 14 Aug 2023 21:04:57 +0100 Subject: [PATCH 05/12] fix(examples/spi-st7789-rpi-zero-w): refactor alternating colors and text position --- .../examples/spi-st7789-rpi-zero-w/README.md | 2 +- .../spi-st7789-rpi-zero-w/src/main.rs | 39 +++++++++---------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/README.md b/mipidsi/examples/spi-st7789-rpi-zero-w/README.md index d285e61..f1131aa 100644 --- a/mipidsi/examples/spi-st7789-rpi-zero-w/README.md +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/README.md @@ -9,7 +9,7 @@ Tested with [Display HAT Mini by Pomoroni](https://shop.pimoroni.com/products/di Prerequisite: musl-cross (Homebrew users: `brew install filosottile/musl-cross/musl-cross`) ```bash -# build for rpi zero 2 w +# build for rpi zero w cargo build --release --target=arm-unknown-linux-musleabihf # look at the size of the bin file ls -lh target/arm-unknown-linux-musleabihf/release/spi-st7789-rpi-zero-w diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs b/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs index 10b0c99..0f12ad5 100644 --- a/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs @@ -13,10 +13,15 @@ use std::process::ExitCode; use std::thread::sleep; use std::time::Duration; -const SPI_DC: u8 = 9; +// Pins const SPI_CS: u8 = 1; +const SPI_DC: u8 = 9; const BACKLIGHT: u8 = 13; +// Display +const W: i32 = 320; +const H: i32 = 240; + fn main() -> ExitCode { let gpio = Gpio::new().unwrap(); let dc = gpio.get(SPI_DC).unwrap().into_output(); @@ -25,12 +30,9 @@ fn main() -> ExitCode { let mut delay = Delay::new(); - let clock_speed = 60_000_000_u32; - let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss1, clock_speed, Mode::Mode0).unwrap(); + let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss1, 60_000_000_u32, Mode::Mode0).unwrap(); let di = SPIInterface::new(spi, dc, cs); - const W: i32 = 320; - const H: i32 = 240; let mut display = Builder::st7789(di) // width and height are switched on porpuse because of the orientation .with_display_size(H as u16, W as u16) @@ -41,36 +43,31 @@ fn main() -> ExitCode { .unwrap(); // Text - let character_style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE); + let char_w = 6; + let text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE); let text = "Hello World ^_^"; - let text_w = text.len() as i32 * 6; let mut text_x = W; - // Anternating color - let colors = [Rgb565::RED, Rgb565::GREEN, Rgb565::BLUE]; - let mut color_index = 0; + // Alternating color + let mut colors = [Rgb565::RED, Rgb565::GREEN, Rgb565::BLUE] + .into_iter() + .cycle(); // Clear the display initially - display.clear(colors[0]).unwrap(); + display.clear(colors.nth(0).unwrap()).unwrap(); // Turn on backlight backlight.set_high(); loop { - // Text scroll - text_x = (text_x - 6) % (W + text_w); - if text_x < -text_w { - text_x = W; - } - - // Fill the display with red - display.clear(colors[color_index]).unwrap(); - color_index = (color_index + 1) % colors.len(); + // Fill the display with alternating colors + display.clear(colors.next().unwrap()).unwrap(); // Draw text - Text::new(text, Point::new(text_x, H / 2), character_style) + let right = Text::new(text, Point::new(text_x, H / 2), text_style) .draw(&mut display) .unwrap(); + text_x = if right.x <= 0 { W } else { text_x - char_w }; // Wait for some time sleep(Duration::from_millis(250)); From 3810156341203b5a60b3f8e92c7bc4008124d38b Mon Sep 17 00:00:00 2001 From: Martin Schaer Date: Mon, 14 Aug 2023 23:08:15 +0100 Subject: [PATCH 06/12] fix(examples/spi-st7789-rpi-zero): add leds and buttons --- .../spi-st7789-rpi-zero-w/src/main.rs | 101 +++++++++++++++--- 1 file changed, 84 insertions(+), 17 deletions(-) diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs b/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs index 0f12ad5..1fdf87b 100644 --- a/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs @@ -1,6 +1,6 @@ use display_interface_spi::SPIInterface; use embedded_graphics::{ - mono_font::{ascii::FONT_6X10, MonoTextStyle}, + mono_font::{ascii::FONT_10X20, MonoTextStyle}, pixelcolor::Rgb565, prelude::*, text::Text, @@ -10,29 +10,48 @@ use rppal::gpio::{Gpio, OutputPin}; use rppal::hal::Delay; use rppal::spi::{Bus, Mode, SlaveSelect, Spi}; use std::process::ExitCode; -use std::thread::sleep; -use std::time::Duration; // Pins + const SPI_CS: u8 = 1; const SPI_DC: u8 = 9; const BACKLIGHT: u8 = 13; +const BUTTON_A: u8 = 5; +const BUTTON_B: u8 = 6; +const BUTTON_X: u8 = 16; +const BUTTON_Y: u8 = 24; + +const LED_R: u8 = 17; +const LED_G: u8 = 27; +const LED_B: u8 = 22; + // Display const W: i32 = 320; const H: i32 = 240; fn main() -> ExitCode { + // GPIO let gpio = Gpio::new().unwrap(); let dc = gpio.get(SPI_DC).unwrap().into_output(); let cs = gpio.get(SPI_CS).unwrap().into_output(); let mut backlight = gpio.get(BACKLIGHT).unwrap().into_output(); - let mut delay = Delay::new(); + // LEDs + let mut led_r = gpio.get(LED_R).unwrap().into_output(); + let mut led_g = gpio.get(LED_G).unwrap().into_output(); + let mut led_b = gpio.get(LED_B).unwrap().into_output(); + // Buttons + let button_a = gpio.get(BUTTON_A).unwrap().into_input_pullup(); + let button_b = gpio.get(BUTTON_B).unwrap().into_input_pullup(); + let button_x = gpio.get(BUTTON_X).unwrap().into_input_pullup(); + let button_y = gpio.get(BUTTON_Y).unwrap().into_input_pullup(); + + // SPI Display let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss1, 60_000_000_u32, Mode::Mode0).unwrap(); let di = SPIInterface::new(spi, dc, cs); - + let mut delay = Delay::new(); let mut display = Builder::st7789(di) // width and height are switched on porpuse because of the orientation .with_display_size(H as u16, W as u16) @@ -43,33 +62,81 @@ fn main() -> ExitCode { .unwrap(); // Text - let char_w = 6; - let text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE); - let text = "Hello World ^_^"; + let char_w = 10; + let char_h = 20; + let text_style = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE); + let text = "Hello World ^_^;"; let mut text_x = W; + let mut text_y = H / 2; // Alternating color - let mut colors = [Rgb565::RED, Rgb565::GREEN, Rgb565::BLUE] - .into_iter() - .cycle(); + let colors = [Rgb565::RED, Rgb565::GREEN, Rgb565::BLUE]; // Clear the display initially - display.clear(colors.nth(0).unwrap()).unwrap(); + display.clear(colors[0]).unwrap(); // Turn on backlight backlight.set_high(); + // Set LEDs to PWM mode + led_r.set_pwm_frequency(50., 1.).unwrap(); + led_g.set_pwm_frequency(50., 1.).unwrap(); + led_b.set_pwm_frequency(50., 1.).unwrap(); + + let start = std::time::Instant::now(); + let mut last = std::time::Instant::now(); + let mut led_flags = 0b000; + let mut counter = 0; loop { - // Fill the display with alternating colors - display.clear(colors.next().unwrap()).unwrap(); + let elapsed = last.elapsed().as_secs_f64(); + if elapsed < 0.125 { + continue; + } + last = std::time::Instant::now(); + counter += 1; + + // X: move text up + if button_x.is_low() { + text_y -= char_h; + } + // Y: move text down + if button_y.is_low() { + text_y += char_h; + } + // A: change led color + if button_a.is_low() { + led_flags = (led_flags + 1) % 7; + } + // B: exit + if button_b.is_low() { + break; + } + + // Fill the display with alternating colors every 8 frames + display.clear(colors[(counter / 8) % colors.len()]).unwrap(); // Draw text - let right = Text::new(text, Point::new(text_x, H / 2), text_style) + let right = Text::new(text, Point::new(text_x, text_y), text_style) .draw(&mut display) .unwrap(); text_x = if right.x <= 0 { W } else { text_x - char_w }; - // Wait for some time - sleep(Duration::from_millis(250)); + // Led + let y = ((start.elapsed().as_secs_f64().sin() + 1.) * 50.).round() / 100.; + led_r + .set_pwm_frequency(50., if led_flags & 0b100 != 0 { y } else { 1. }) + .unwrap(); + led_g + .set_pwm_frequency(50., if led_flags & 0b010 != 0 { y } else { 1. }) + .unwrap(); + led_b + .set_pwm_frequency(50., if led_flags & 0b001 != 0 { y } else { 1. }) + .unwrap(); } + + // Turn off backlight and clear the display + backlight.set_low(); + display.clear(Rgb565::BLACK).unwrap(); + + ExitCode::SUCCESS } From b85e23ccaa437ddd94ea855c3736c72dcc6f8b7a Mon Sep 17 00:00:00 2001 From: Martin Schaer Date: Mon, 14 Aug 2023 23:16:43 +0100 Subject: [PATCH 07/12] clean --- .cargo/config | 2 -- Cargo.toml | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 .cargo/config diff --git a/.cargo/config b/.cargo/config deleted file mode 100644 index 6fb7d4c..0000000 --- a/.cargo/config +++ /dev/null @@ -1,2 +0,0 @@ -[target.arm-unknown-linux-musleabihf] -linker = "arm-linux-musleabihf-gcc" diff --git a/Cargo.toml b/Cargo.toml index 852f0d9..41bf05c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ members = [ "mipidsi", "mipidsi-async", ] + From b8546f2e124d24f42fc7d0ced27065cf1f972f97 Mon Sep 17 00:00:00 2001 From: Martin Schaer Date: Mon, 14 Aug 2023 23:18:06 +0100 Subject: [PATCH 08/12] clean --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 41bf05c..852f0d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,3 @@ members = [ "mipidsi", "mipidsi-async", ] - From fc6051bb22d4c7e495a2e37f3fd22dfa3b94c5d4 Mon Sep 17 00:00:00 2001 From: Martin Schaer Date: Mon, 14 Aug 2023 23:19:45 +0100 Subject: [PATCH 09/12] clean --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 852f0d9..6f55c87 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,4 +3,4 @@ members = [ "mipidsi", "mipidsi-async", -] +] \ No newline at end of file From 2b5f6428ae9728f105b17fc66b26bb2aeb2541d6 Mon Sep 17 00:00:00 2001 From: Martin Schaer Date: Wed, 16 Aug 2023 22:42:41 +0100 Subject: [PATCH 10/12] fix(examples/spi-st7789-rpi-zero-w): fix build --- mipidsi/examples/spi-st7789-rpi-zero-w/.cargo/config.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/.cargo/config.toml b/mipidsi/examples/spi-st7789-rpi-zero-w/.cargo/config.toml index 5082bb7..6fb7d4c 100644 --- a/mipidsi/examples/spi-st7789-rpi-zero-w/.cargo/config.toml +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/.cargo/config.toml @@ -1,2 +1,2 @@ -[build] -target = "arm-unknown-linux-musleabihf" +[target.arm-unknown-linux-musleabihf] +linker = "arm-linux-musleabihf-gcc" From f5a25f5077620486da3a0af3cd33a9b6f6533998 Mon Sep 17 00:00:00 2001 From: Martin Schaer Date: Wed, 16 Aug 2023 22:46:00 +0100 Subject: [PATCH 11/12] fix(examples/spi-st7789-rpi-zero-w): remove spi cs --- .../spi-st7789-rpi-zero-w/src/main.rs | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs b/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs index 1fdf87b..e489346 100644 --- a/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/src/main.rs @@ -1,4 +1,22 @@ -use display_interface_spi::SPIInterface; +/* +# SPI ST7789 on a Raspberry Pi Zero W Example + +This example demonstrates how to use the [Display HAT Mini by Pomoroni](https://shop.pimoroni.com/products/display-hat-mini?variant=39496084717651) +on a Raspberry Pi Zero W. + +The example shows a scrolling text and a pulsing RGB LED. + +Buttons: + +- A: change LED color +- B: exit +- X: move text up +- Y: move text down + +Read the README.md for more information. +*/ + +use display_interface_spi::SPIInterfaceNoCS; use embedded_graphics::{ mono_font::{ascii::FONT_10X20, MonoTextStyle}, pixelcolor::Rgb565, @@ -13,7 +31,6 @@ use std::process::ExitCode; // Pins -const SPI_CS: u8 = 1; const SPI_DC: u8 = 9; const BACKLIGHT: u8 = 13; @@ -34,7 +51,6 @@ fn main() -> ExitCode { // GPIO let gpio = Gpio::new().unwrap(); let dc = gpio.get(SPI_DC).unwrap().into_output(); - let cs = gpio.get(SPI_CS).unwrap().into_output(); let mut backlight = gpio.get(BACKLIGHT).unwrap().into_output(); // LEDs @@ -50,10 +66,10 @@ fn main() -> ExitCode { // SPI Display let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss1, 60_000_000_u32, Mode::Mode0).unwrap(); - let di = SPIInterface::new(spi, dc, cs); + let di = SPIInterfaceNoCS::new(spi, dc); let mut delay = Delay::new(); let mut display = Builder::st7789(di) - // width and height are switched on porpuse because of the orientation + // width and height are switched on purpose because of the orientation .with_display_size(H as u16, W as u16) // this orientation applies for the Display HAT Mini by Pimoroni .with_orientation(mipidsi::Orientation::LandscapeInverted(true)) @@ -105,7 +121,7 @@ fn main() -> ExitCode { } // A: change led color if button_a.is_low() { - led_flags = (led_flags + 1) % 7; + led_flags = (led_flags + 1) % 8; } // B: exit if button_b.is_low() { From 724f4139cc0b9b1c471b877922e54f2f38fa4f32 Mon Sep 17 00:00:00 2001 From: Martin Schaer Date: Wed, 16 Aug 2023 22:46:36 +0100 Subject: [PATCH 12/12] docs(examples/spi-st7789-rpi-zero-w): update readme --- .../examples/spi-st7789-rpi-zero-w/README.md | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/mipidsi/examples/spi-st7789-rpi-zero-w/README.md b/mipidsi/examples/spi-st7789-rpi-zero-w/README.md index f1131aa..3f5aa83 100644 --- a/mipidsi/examples/spi-st7789-rpi-zero-w/README.md +++ b/mipidsi/examples/spi-st7789-rpi-zero-w/README.md @@ -1,12 +1,32 @@ # SPI ST7789 on a Raspberry Pi Zero W Example -Tested with [Display HAT Mini by Pomoroni](https://shop.pimoroni.com/products/display-hat-mini?variant=39496084717651). +This example demonstrates how to use the [Display HAT Mini by Pomoroni](https://shop.pimoroni.com/products/display-hat-mini?variant=39496084717651) on a Raspberry Pi Zero W. + +The example shows a scrolling text and a pulsing RGB LED. + +Buttons: + +- A: change LED color +- B: exit +- X: move text up +- Y: move text down + +## Pre-requisite + +**Enable SPI** by any of this options: + +- `sudo raspi-config` +- `sudo raspi-config nonint do_spi 0` +- manually adding `dtparam=spi=on` to /boot/config.txt +- the graphical Raspberry Pi Configuration UI + +[More info about SPI](https://docs.golemparts.com/rppal/0.14.1/rppal/spi/index.html#spi0) ## Build, strip, copy, and run ### Mac OS -Prerequisite: musl-cross (Homebrew users: `brew install filosottile/musl-cross/musl-cross`) +Pre-requisite: musl-cross (Homebrew users: `brew install FiloSottile/musl-cross/musl-cross --without-x86_64 --with-arm-hf`) ```bash # build for rpi zero w