display is working

This commit is contained in:
Priec
2026-01-08 23:36:54 +01:00
parent 5efb08ef99
commit 5e7285f45d
5 changed files with 167 additions and 15 deletions

View File

@@ -3,35 +3,92 @@
use embassy_executor::task;
use embassy_time::{Duration, Timer};
use esp_hal::{
gpio::Io,
i2c::master::{Config, I2c},
peripherals::Peripherals,
};
use esp_println::println;
use ssd1306::mode::BufferedGraphicsMode;
use log::info;
#[task]
pub async fn display_task() {
use mousefood::{EmbeddedBackend, EmbeddedBackendConfig};
use ratatui::{
layout::{Constraint, Direction, Layout},
widgets::{Block, Borders, Paragraph},
Terminal,
};
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
let peripherals = unsafe { Peripherals::steal() };
let i2c = I2c::new(peripherals.I2C0, Config::default())
.expect("Failed to initialize I2C")
.with_sda(peripherals.GPIO21)
.with_scl(peripherals.GPIO22);
let interface = I2CDisplayInterface::new(i2c);
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
.into_buffered_graphics_mode();
display.init().unwrap();
let config = EmbeddedBackendConfig {
flush_callback: alloc::boxed::Box::new(|display| {
let d: &mut Ssd1306<_, _, BufferedGraphicsMode<DisplaySize128x64>> = display;
d.flush().unwrap();
}),
..Default::default()
};
let backend = EmbeddedBackend::new(&mut display, config);
let mut terminal = Terminal::new(backend).unwrap();
let mut counter = 0;
loop {
terminal
.draw(|f| {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(3), Constraint::Min(0)])
.split(f.area());
f.render_widget(
Block::default().title(" ESP32 Status ").borders(Borders::ALL),
chunks[0],
);
let content = alloc::format!("MQTT Active\nCounter: {}", counter);
f.render_widget(
Paragraph::new(content).block(
Block::default().borders(Borders::LEFT | Borders::RIGHT | Borders::BOTTOM),
),
chunks[1],
);
})
.unwrap();
counter += 1;
Timer::after(Duration::from_millis(1000)).await;
}
}
#[task]
pub async fn i2c_check() {
let peripherals = unsafe { Peripherals::steal() };
let _io = Io::new(peripherals.IO_MUX);
let sda = peripherals.GPIO21;
let scl = peripherals.GPIO22;
let mut i2c = I2c::new(peripherals.I2C0, Config::default())
.expect("Failed to initialize I2C")
.with_sda(sda)
.with_scl(scl);
.with_sda(peripherals.GPIO21)
.with_scl(peripherals.GPIO22);
loop {
println!("I2C bus scan start");
// Skenujeme adresy 0x03 až 0x77
info!("hehe");
esp_println::println!("I2C bus scan start");
for addr in 0x03..0x78 {
// Skúsime zapísať prázdne dáta na adresu
if i2c.write(addr, &[]).is_ok() {
println!("Device found at address 0x{:02X}", addr);
esp_println::println!("Device found at address 0x{:02X}", addr);
}
}
println!("Scan finished");
Timer::after(Duration::from_secs(5)).await;
}
}