Skip to content

Commit

Permalink
Merge pull request #543 from alistair23/alistair/st7789
Browse files Browse the repository at this point in the history
examples: st7789: Initial commit
  • Loading branch information
jrvanwhy committed Jun 13, 2024
2 parents fd2f66d + 10a3d07 commit 4b7cba1
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/Cargo.lock
/nightly/target
/target
/demos/*/target
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ members = [
"apis/sensors/proximity",
"apis/sensors/temperature",
"apis/storage/key_value",
"demos/st7789",
"panic_handlers/debug_panic",
"panic_handlers/small_panic",
"platform",
Expand Down
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ examples: toolchain
EXCLUDE_RUNTIME := --exclude libtock --exclude libtock_runtime \
--exclude libtock_debug_panic --exclude libtock_small_panic

# Arguments to pass to cargo to exclude demo crates.
EXCLUDE_RUNTIME := $(EXCLUDE_RUNTIME) --exclude st7789

# Arguments to pass to cargo to exclude crates that cannot be tested by Miri. In
# addition to excluding libtock_runtime, Miri also cannot test proc macro crates
# (and in fact will generate broken data that causes cargo test to fail).
Expand All @@ -130,6 +133,7 @@ test: examples
--target=thumbv7em-none-eabi --workspace
LIBTOCK_PLATFORM=hifive1 cargo clippy $(EXCLUDE_STD) \
--target=riscv32imac-unknown-none-elf --workspace
$(MAKE) apollo3-st7789
cd nightly && \
MIRIFLAGS="-Zmiri-strict-provenance -Zmiri-symbolic-alignment-check" \
cargo miri test $(EXCLUDE_MIRI) --manifest-path=../Cargo.toml \
Expand Down Expand Up @@ -215,6 +219,14 @@ $(1): toolchain
mkdir -p target/tbf/$(1)
cp target/$(1)/$(2)/release/examples/$(EXAMPLE).{tab,tbf} \
target/tbf/$(1)

.PHONY: $(1)-st7789
$(1)-st7789: toolchain
cd demos/st7789 && LIBTOCK_PLATFORM=$(1) cargo run $(features) \
$(release) --target=$(2) --target-dir=target/$(1)
mkdir -p target/tbf/$(1)
cp demos/st7789/target/$(1)/$(2)/release/st7789.{tab,tbf} \
target/tbf/$(1)
endef

# Creates the `make flash-<BOARD> EXAMPLE=<EXAMPLE>` targets. Arguments:
Expand All @@ -225,6 +237,12 @@ flash-$(1): toolchain
LIBTOCK_PLATFORM=$(1) cargo run --example $(EXAMPLE) $(features) \
$(release) --target=$(2) --target-dir=target/flash-$(1) -- \
--deploy=tockloader

.PHONY: flash-$(1)-st7789
flash-$(1)-st7789: toolchain
cd demos/st7789 && LIBTOCK_PLATFORM=$(1) cargo run $(features) \
$(release) --target=$(2) --target-dir=target/flash-$(1) -- \
--deploy=tockloader
endef

$(eval $(call platform_build,apollo3,thumbv7em-none-eabi))
Expand Down Expand Up @@ -261,4 +279,5 @@ $(eval $(call platform_flash,clue_nrf52840,thumbv7em-none-eabi))
clean:
cargo clean
rm -fr nightly/target/
cd demos/st7789 && cargo clean
$(MAKE) -C tock clean
20 changes: 20 additions & 0 deletions demos/st7789/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "st7789"
version = "0.1.0"
edition = "2021"
rust-version.workspace = true
authors = ["Alistair Francis <alistair.francis@wdc.com>"]
description = """A demo to drive a ST7789 display via SPI using libtock-rs."""
license = "Apache-2.0 OR MIT"

[dependencies]
libtock = { path = "../../", features = ["rust_embedded"] }

embedded-hal = "1.0"

mipidsi = "0.8.0"
display-interface-spi = "0.5"
embedded-graphics = "0.8"

[build-dependencies]
libtock_build_scripts = { path = "../../build_scripts" }
3 changes: 3 additions & 0 deletions demos/st7789/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
libtock_build_scripts::auto_layout();
}
76 changes: 76 additions & 0 deletions demos/st7789/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! This sample demonstrates displaying text on a ST7789 display
//! using a rust-embedded based crate

#![no_main]
#![no_std]
use core::fmt::Write;
use libtock::alarm::{Alarm, Milliseconds};
use libtock::console::Console;
use libtock::gpio::Gpio;
use libtock::platform::Syscalls;
use libtock::runtime::{set_main, stack_size, TockSyscalls};
use libtock::spi_controller::EmbeddedHalSpi;

use display_interface_spi::SPIInterface;
use embedded_graphics::{
mono_font::{ascii::FONT_10X20, MonoTextStyle},
pixelcolor::Rgb565,
prelude::*,
text::Text,
};
use mipidsi::{models::ST7789, options::ColorInversion, Builder};

set_main! {main}
stack_size! {0x1000}

// Display
const W: i32 = 240;
const H: i32 = 240;

struct Delay;

impl embedded_hal::delay::DelayNs for Delay {
fn delay_ns(&mut self, ns: u32) {
Alarm::sleep_for(Milliseconds(ns / (1000 * 1000))).unwrap();
}
}

fn main() {
writeln!(Console::writer(), "st7789: example\r").unwrap();

let mut gpio_dc = Gpio::get_pin(0).unwrap();
let dc = gpio_dc.make_output().unwrap();

let mut gpio_reset = Gpio::get_pin(1).unwrap();
let reset = gpio_reset.make_output().unwrap();

let di = SPIInterface::new(EmbeddedHalSpi, dc);

let mut delay = Delay;
let mut display = Builder::new(ST7789, di)
.display_size(W as u16, H as u16)
.invert_colors(ColorInversion::Inverted)
.reset_pin(reset)
.init(&mut delay)
.unwrap();

// Text
let text_style = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE);
let text = "Hello World ^_^;";
let text_x = W;
let text_y = H / 2;

// Clear the display initially
display.clear(Rgb565::BLUE).unwrap();

writeln!(Console::writer(), "Clear complete\r").unwrap();

// Draw text
Text::new(text, Point::new(text_x, text_y), text_style)
.draw(&mut display)
.unwrap();

loop {
TockSyscalls::yield_wait();
}
}

0 comments on commit 4b7cba1

Please sign in to comment.