display is working
This commit is contained in:
@@ -25,6 +25,7 @@ use projekt_final::mqtt::client::{
|
||||
mqtt_events, mqtt_publish, mqtt_subscribe, mqtt_task, IncomingMsg,
|
||||
};
|
||||
use projekt_final::i2c::com::i2c_check;
|
||||
use projekt_final::i2c::com::display_task;
|
||||
use defmt_rtt as _;
|
||||
|
||||
extern crate alloc;
|
||||
@@ -102,7 +103,7 @@ async fn main(spawner: Spawner) -> ! {
|
||||
spawner.spawn(mqtt_task(stack)).expect("failed to spawn MQTT task");
|
||||
info!("MQTT task started");
|
||||
|
||||
spawner.spawn(i2c_check()).expect("failed to spawn I2C task");
|
||||
spawner.spawn(display_task()).expect("failed to spawn Display task");
|
||||
info!("I2C scan task started");
|
||||
|
||||
mqtt_publish("esp32/topic", b"hello from ESP32 (init)", QualityOfService::QoS1, false).await;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#![no_std]
|
||||
extern crate alloc;
|
||||
|
||||
pub mod mqtt;
|
||||
pub mod i2c;
|
||||
|
||||
Reference in New Issue
Block a user