Skip to content

Commit

Permalink
full code
Browse files Browse the repository at this point in the history
  • Loading branch information
ImplFerris committed Jan 13, 2025
1 parent 57c2878 commit 11bb1af
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion src/oled/hello-rust/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,75 @@ git clone https://github.com/ImplFerris/esp32-projects
cd esp32-projects/hello-oled
```

## Full code


```rust
#![no_std]
#![no_main]

use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use embedded_graphics::{
mono_font::{ascii::FONT_6X10, MonoTextStyleBuilder},
pixelcolor::BinaryColor,
prelude::Point,
text::{Baseline, Text},
};
use esp_backtrace as _;
use esp_hal::prelude::*;
use log::info;
use ssd1306::{
mode::DisplayConfigAsync, prelude::DisplayRotation, size::DisplaySize128x64,
I2CDisplayInterface, Ssd1306Async,
};

use embedded_graphics::prelude::*;

#[main]
async fn main(_spawner: Spawner) {
let peripherals = esp_hal::init({
let mut config = esp_hal::Config::default();
config.cpu_clock = CpuClock::max();
config
});

esp_println::logger::init_logger_from_env();

let timer0 = esp_hal::timer::timg::TimerGroup::new(peripherals.TIMG1);
esp_hal_embassy::init(timer0.timer0);

info!("Embassy initialized!");

let i2c0 = esp_hal::i2c::master::I2c::new(
peripherals.I2C0,
esp_hal::i2c::master::Config {
frequency: 400.kHz(),
timeout: Some(100),
},
)
.with_scl(peripherals.GPIO18)
.with_sda(peripherals.GPIO23)
.into_async();

let interface = I2CDisplayInterface::new(i2c0);
// initialize the display
let mut display = Ssd1306Async::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
.into_buffered_graphics_mode();
display.init().await.unwrap();

let text_style = MonoTextStyleBuilder::new()
.font(&FONT_6X10)
.text_color(BinaryColor::On)
.build();

Text::with_baseline("Hello, Rust!", Point::new(0, 16), text_style, Baseline::Top)
.draw(&mut display)
.unwrap();

display.flush().await.unwrap();

loop {
Timer::after(Duration::from_secs(1)).await;
}
}
```

0 comments on commit 11bb1af

Please sign in to comment.