mirror of
https://github.com/esp-rs/esp-hal.git
synced 2025-09-28 21:00:59 +00:00

* Add some config options to the UART driver * Use esp-println 0.2.0 * Remove the NoPin type * Serial constructor now doesn't return a Result anymore
140 lines
3.5 KiB
Rust
140 lines
3.5 KiB
Rust
//! I2C Display example
|
|
//!
|
|
//! This example prints some text on an SSD1306-based
|
|
//! display (via I2C)
|
|
//!
|
|
//! The following wiring is assumed:
|
|
//! - SDA => GPIO35
|
|
//! - SCL => GPIO36
|
|
|
|
#![no_std]
|
|
#![no_main]
|
|
|
|
use core::fmt::Write;
|
|
|
|
use embedded_graphics::{
|
|
mono_font::{
|
|
ascii::{FONT_6X10, FONT_9X18_BOLD},
|
|
MonoTextStyleBuilder,
|
|
},
|
|
pixelcolor::BinaryColor,
|
|
prelude::*,
|
|
text::{Alignment, Text},
|
|
};
|
|
use esp32s2_hal::{
|
|
clock::ClockControl,
|
|
gpio::IO,
|
|
i2c::I2C,
|
|
pac::Peripherals,
|
|
prelude::*,
|
|
RtcCntl,
|
|
Serial,
|
|
Timer,
|
|
};
|
|
use nb::block;
|
|
use panic_halt as _;
|
|
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
|
|
use xtensa_lx_rt::entry;
|
|
|
|
#[entry]
|
|
fn main() -> ! {
|
|
let peripherals = Peripherals::take().unwrap();
|
|
let mut system = peripherals.SYSTEM.split();
|
|
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
|
|
|
let mut timer0 = Timer::new(peripherals.TIMG0, clocks.apb_clock);
|
|
let mut serial0 = Serial::new(peripherals.UART0);
|
|
let mut rtc_cntl = RtcCntl::new(peripherals.RTC_CNTL);
|
|
|
|
// Disable watchdog timer
|
|
timer0.disable();
|
|
rtc_cntl.set_wdt_global_enable(false);
|
|
|
|
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
|
|
|
|
writeln!(serial0, "Enabling peripheral!").unwrap();
|
|
|
|
// Create a new peripheral object with the described wiring
|
|
// and standard I2C clock speed
|
|
let i2c = I2C::new(
|
|
peripherals.I2C0,
|
|
io.pins.gpio35,
|
|
io.pins.gpio36,
|
|
100u32.kHz(),
|
|
&mut system.peripheral_clock_control,
|
|
&clocks,
|
|
)
|
|
.unwrap();
|
|
|
|
// Start timer (5 second interval)
|
|
timer0.start(5u64.secs());
|
|
|
|
writeln!(serial0, "Starting timer!").unwrap();
|
|
|
|
// Initialize display
|
|
let interface = I2CDisplayInterface::new(i2c);
|
|
let mut display = Ssd1306::new(interface, 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();
|
|
let text_style_big = MonoTextStyleBuilder::new()
|
|
.font(&FONT_9X18_BOLD)
|
|
.text_color(BinaryColor::On)
|
|
.build();
|
|
|
|
loop {
|
|
writeln!(serial0, "In Loop!").unwrap();
|
|
|
|
// Fill display bufffer with a centered text with two lines (and two text
|
|
// styles)
|
|
Text::with_alignment(
|
|
"esp-hal",
|
|
display.bounding_box().center() + Point::new(0, 0),
|
|
text_style_big,
|
|
Alignment::Center,
|
|
)
|
|
.draw(&mut display)
|
|
.unwrap();
|
|
|
|
Text::with_alignment(
|
|
"Chip: ESP32S2",
|
|
display.bounding_box().center() + Point::new(0, 14),
|
|
text_style,
|
|
Alignment::Center,
|
|
)
|
|
.draw(&mut display)
|
|
.unwrap();
|
|
|
|
// Write buffer to display
|
|
display.flush().unwrap();
|
|
// Clear display buffer
|
|
display.clear();
|
|
|
|
// Wait 5 seconds
|
|
block!(timer0.wait()).unwrap();
|
|
|
|
// Write single-line centered text "Hello World" to buffer
|
|
Text::with_alignment(
|
|
"Hello World!",
|
|
display.bounding_box().center(),
|
|
text_style_big,
|
|
Alignment::Center,
|
|
)
|
|
.draw(&mut display)
|
|
.unwrap();
|
|
|
|
// Write buffer to display
|
|
display.flush().unwrap();
|
|
// Clear display buffer
|
|
display.clear();
|
|
|
|
// Wait 5 seconds
|
|
block!(timer0.wait()).unwrap();
|
|
}
|
|
}
|