Skip to content

Commit

Permalink
Merge pull request #138 from TheNitek/rust
Browse files Browse the repository at this point in the history
Get rid of unsafes in Rust example
  • Loading branch information
witnessmenow authored Mar 4, 2024
2 parents 645122a + d1a0b1c commit 10d99e5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 35 deletions.
5 changes: 3 additions & 2 deletions Examples/Rust/1-HelloWorld/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ target = "xtensa-esp32-espidf"

[target.xtensa-esp32-espidf]
linker = "ldproxy"
# runner = "espflash --monitor" # Select this runner for espflash v1.x.x
runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x
runner = "espflash flash --monitor --flash-freq=80mhz --baud=460800" # Select this runner for espflash v2.x.x
rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110

[unstable]
Expand All @@ -14,3 +13,5 @@ build-std = ["std", "panic_abort"]
MCU="esp32"
# Note: this variable is not used by the pio builder (`cargo build --features pio`)
ESP_IDF_VERSION = "v5.1.1"
ESP_IDF_PATH_ISSUES = 'warn'
ESP_IDF_SDKCONFIG_DEFAULTS = "sdkconfig.defaults"
3 changes: 3 additions & 0 deletions Examples/Rust/1-HelloWorld/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
/.embuild
/Cargo.lock
4 changes: 2 additions & 2 deletions Examples/Rust/1-HelloWorld/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cyd"
version = "0.1.0"
authors = ["Martin <[email protected]>"]
authors = ["Martin <[email protected]>", "Claus Näveke <[email protected]>"]
edition = "2021"
resolver = "2"
rust-version = "1.75"
Expand Down Expand Up @@ -38,4 +38,4 @@ display-interface = "0.4.1"
display-interface-spi = "0.4.1"

[build-dependencies]
embuild = "0.31.3"
embuild = "0.31.4"
5 changes: 5 additions & 0 deletions Examples/Rust/1-HelloWorld/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CONFIG_ESP_MAIN_TASK_STACK_SIZE=20000
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=8192
CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=8192

CONFIG_FREERTOS_IDLE_TASK_STACKSIZE=4096
71 changes: 40 additions & 31 deletions Examples/Rust/1-HelloWorld/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use display_interface_spi::SPIInterfaceNoCS;
use embedded_graphics::{
mono_font::{ascii::FONT_9X18, MonoTextStyle},
pixelcolor::Rgb666,
pixelcolor::Rgb565,
prelude::*,
text::renderer::CharacterStyle,
text::Text,
};

use esp_idf_svc::hal::{gpio, prelude::Peripherals};

use esp_idf_hal::{
delay::Ets,
gpio,
gpio::PinDriver,
spi::{config::Config, config::DriverConfig, SpiDeviceDriver, SpiDriver, SPI2},
spi::{config::{Config, DriverConfig}, Dma, SpiDeviceDriver}, units::MegaHertz,
};

use mipidsi::Builder;
Expand All @@ -25,64 +25,73 @@ fn main() -> Result<(), Box<dyn Error>> {
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_sys::link_patches();

let peripherals = Peripherals::take().unwrap();
let pins = peripherals.pins;

// Reset, -1 or 4
let rst = PinDriver::input_output_od(unsafe { gpio::Gpio4::new() })?;
let rst = gpio::PinDriver::output(pins.gpio4)?;
// Data Command control pin
let dc = PinDriver::input_output_od(unsafe { gpio::Gpio2::new() })?;
let dc = gpio::PinDriver::output(pins.gpio2)?;

// Espressif built-in delay provider for small delays
let mut delay = Ets;

// Pin 14, Serial Clock
let sclk = unsafe { gpio::Gpio14::new() };
let spi = unsafe { SPI2::new() };
let sclk = pins.gpio14;
let spi = peripherals.spi2;
// Pin 13, MOSI, Master Out Slave In
let sdo = unsafe { gpio::Gpio13::new() };
let sdo = pins.gpio13;
// Pin 12, MISO, Master In Slave Out
let sdi = unsafe { gpio::Gpio12::new() };

// Serial Peripheral Interface
let spi = SpiDriver::new(spi, sclk, sdo, Some(sdi), &DriverConfig::new())?;

// Pin 15, Chip select
let cs = unsafe { gpio::Gpio15::new() };

let spi = SpiDeviceDriver::new(spi, Some(cs), &Config::new())?;
let di = SPIInterfaceNoCS::new(spi, dc);

let mut display = Builder::ili9341_rgb666(di)
.with_color_order(mipidsi::ColorOrder::Bgr) // Colors are shifted, Blue -> Red
.with_orientation(mipidsi::options::Orientation::Landscape(false)) // Mirror on text
let sdi = pins.gpio12;

let cs = pins.gpio15;

let di = SPIInterfaceNoCS::new(
SpiDeviceDriver::new_single(
spi,
sclk,
sdo,
Some(sdi),
Some(cs),
&DriverConfig::new().dma(Dma::Disabled),
&Config::new().baudrate(MegaHertz(40).into()),
)?,
dc,
);

let mut display = Builder::ili9341_rgb565(di)
.with_color_order(mipidsi::ColorOrder::Rgb)
.with_orientation(mipidsi::options::Orientation::LandscapeInverted(true)) // Mirror on text
.init(&mut delay, Some(rst))
.map_err(|_| Box::<dyn Error>::from("display init"))?;

// Pin 21, Backlight
let mut bl = PinDriver::output(unsafe { gpio::Gpio21::new() })?;
let mut bl = gpio::PinDriver::output(pins.gpio21)?;
// Turn on backlight
bl.set_high()?;
// Force the GPIO to hold it's high state and not go to sleep
// TO check: why do we need this?
unsafe { gpio_hold_en(21) };

// Force the GPIO to hold it's high state
core::mem::forget(bl);

// fill the screen with black
// TO check: this is quite slow somehow?
display
.clear(Rgb666::BLACK)
.clear(Rgb565::BLACK)
.map_err(|_| Box::<dyn Error>::from("clear display"))?;

// Create text style
let mut style = MonoTextStyle::new(&FONT_9X18, Rgb666::WHITE);
let mut style = MonoTextStyle::new(&FONT_9X18, Rgb565::WHITE);

// Position x:5, y: 10
Text::new("Hello", Point::new(5, 10), style)
.draw(&mut display)
.map_err(|_| Box::<dyn Error>::from("draw hello"))?;

// Turn text to blue
style.set_text_color(Some(Rgb666::BLUE));
style.set_text_color(Some(Rgb565::BLUE));
Text::new("World", Point::new(160, 26), style)
.draw(&mut display)
.map_err(|_| Box::<dyn Error>::from("draw world"))?;

Ok(())
}
}

0 comments on commit 10d99e5

Please sign in to comment.