Created
September 14, 2023 22:16
-
-
Save Nereuxofficial/e34f0f264a01243175071fc747a9e9c1 to your computer and use it in GitHub Desktop.
ESP32 bare-metal Rust display code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
#![no_main] | |
extern crate alloc; | |
use core::mem::MaybeUninit; | |
use embedded_graphics::geometry::Dimensions; | |
use embedded_graphics::mono_font::ascii::FONT_6X10; | |
use embedded_graphics::mono_font::MonoTextStyleBuilder; | |
use embedded_graphics::pixelcolor::BinaryColor; | |
use embedded_graphics::prelude::*; | |
use embedded_graphics::text::{Alignment, Text}; | |
use esp_backtrace as _; | |
use esp_println::println; | |
use hal::i2c::I2C; | |
use hal::timer::TimerGroup; | |
use hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, Delay, IO}; | |
use log::info; | |
use ssd1306::prelude::DisplayRotation; | |
use ssd1306::prelude::*; | |
use ssd1306::{I2CDisplayInterface, Ssd1306}; | |
#[global_allocator] | |
static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty(); | |
fn init_heap() { | |
const HEAP_SIZE: usize = 32 * 1024; | |
static mut HEAP: MaybeUninit<[u8; HEAP_SIZE]> = MaybeUninit::uninit(); | |
unsafe { | |
ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, HEAP_SIZE); | |
} | |
} | |
#[entry] | |
fn main() -> ! { | |
init_heap(); | |
let peripherals = Peripherals::take(); | |
let mut system = peripherals.DPORT.split(); | |
let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); | |
let mut delay = Delay::new(&clocks); | |
// setup logger | |
// To change the log_level change the env section in .config/cargo.toml | |
// or remove it and set ESP_LOGLEVEL manually before running cargo run | |
// this requires a clean rebuild because of https://github.com/rust-lang/cargo/issues/10358 | |
esp_println::logger::init_logger_from_env(); | |
info!("Logger is setup"); | |
println!("Hello world!"); | |
let timer_group0 = TimerGroup::new( | |
peripherals.TIMG0, | |
&clocks, | |
&mut system.peripheral_clock_control, | |
); | |
let mut timer = timer_group0.timer0; | |
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); | |
let i2c = I2C::new( | |
peripherals.I2C0, | |
io.pins.gpio17, | |
io.pins.gpio18, | |
100_u32.kHz(), | |
&mut system.peripheral_clock_control, | |
&clocks, | |
); | |
timer.start(5u64.secs()); | |
let i2c_interface = I2CDisplayInterface::new(i2c); | |
let mut display = Ssd1306::new( | |
i2c_interface, | |
ssd1306::size::DisplaySize128x64, | |
DisplayRotation::Rotate0, | |
) | |
.into_buffered_graphics_mode(); | |
display.init().unwrap(); | |
// Specify different text styles | |
let text_style = MonoTextStyleBuilder::new() | |
.font(&FONT_6X10) | |
.text_color(BinaryColor::On) | |
.build(); | |
Text::with_alignment( | |
"esp-hal", | |
display.bounding_box().center() + Point::new(0, 0), | |
text_style, | |
Alignment::Center, | |
) | |
.draw(&mut display) | |
.unwrap(); | |
loop { | |
println!("Loop..."); | |
delay.delay_ms(500u32); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment