new rendering engine
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use client_domain::{BoundingBox, DisplayPort};
|
||||
use client_domain::{BoundingBox, Color, DisplayPort, FontSize};
|
||||
use embedded_graphics::{
|
||||
mono_font::{ascii::FONT_6X10, ascii::FONT_10X20, MonoTextStyle},
|
||||
pixelcolor::Rgb565,
|
||||
@@ -6,7 +6,6 @@ use embedded_graphics::{
|
||||
primitives::{PrimitiveStyle, Rectangle},
|
||||
text::Text,
|
||||
};
|
||||
use embedded_text::{TextBox, style::TextBoxStyleBuilder};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DisplayError {
|
||||
@@ -26,10 +25,8 @@ pub struct Esp32DisplayAdapter {
|
||||
}
|
||||
|
||||
trait ErasedDisplay {
|
||||
fn clear_region(&mut self, bounds: BoundingBox) -> Result<(), DisplayError>;
|
||||
fn draw_text(&mut self, text: &str, x: u16, y: u16, bounds: BoundingBox) -> Result<(), DisplayError>;
|
||||
fn draw_icon(&mut self, icon: &str, x: u16, y: u16) -> Result<(), DisplayError>;
|
||||
fn fill_background(&mut self, bounds: BoundingBox) -> Result<(), DisplayError>;
|
||||
fn draw_text_span(&mut self, text: &str, x: u16, y: u16, color: Rgb565, font: FontSize) -> Result<(), DisplayError>;
|
||||
fn fill_rect(&mut self, bounds: BoundingBox, color: Rgb565) -> Result<(), DisplayError>;
|
||||
fn flush(&mut self) -> Result<(), DisplayError>;
|
||||
}
|
||||
|
||||
@@ -38,55 +35,37 @@ where
|
||||
D: DrawTarget<Color = Rgb565>,
|
||||
D::Error: std::fmt::Debug,
|
||||
{
|
||||
fn clear_region(&mut self, bounds: BoundingBox) -> Result<(), DisplayError> {
|
||||
fn draw_text_span(&mut self, text: &str, x: u16, y: u16, color: Rgb565, font: FontSize) -> Result<(), DisplayError> {
|
||||
let (style, y_offset) = match font {
|
||||
FontSize::Small => (MonoTextStyle::new(&FONT_6X10, color), 10),
|
||||
FontSize::Large => (MonoTextStyle::new(&FONT_10X20, color), 20),
|
||||
};
|
||||
Text::new(text, Point::new(x as i32, y as i32 + y_offset), style)
|
||||
.draw(self)
|
||||
.map_err(|e| DisplayError::Draw(format!("{e:?}")))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fill_rect(&mut self, bounds: BoundingBox, color: Rgb565) -> Result<(), DisplayError> {
|
||||
Rectangle::new(
|
||||
Point::new(bounds.x as i32, bounds.y as i32),
|
||||
Size::new(bounds.width as u32, bounds.height as u32),
|
||||
)
|
||||
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK))
|
||||
.into_styled(PrimitiveStyle::with_fill(color))
|
||||
.draw(self)
|
||||
.map_err(|e| DisplayError::Draw(format!("{e:?}")))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw_text(&mut self, text: &str, _x: u16, _y: u16, bounds: BoundingBox) -> Result<(), DisplayError> {
|
||||
let style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE);
|
||||
let textbox_style = TextBoxStyleBuilder::new().build();
|
||||
let rect = Rectangle::new(
|
||||
Point::new(bounds.x as i32, bounds.y as i32),
|
||||
Size::new(bounds.width as u32, bounds.height as u32),
|
||||
);
|
||||
TextBox::with_textbox_style(text, rect, style, textbox_style)
|
||||
.draw(self)
|
||||
.map_err(|e| DisplayError::Draw(format!("{e:?}")))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw_icon(&mut self, icon: &str, x: u16, y: u16) -> Result<(), DisplayError> {
|
||||
let style = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE);
|
||||
let icon_char = match icon {
|
||||
"sunny" | "clear" => "*",
|
||||
"cloud_rain" | "rain" => "~",
|
||||
"cloud" | "cloudy" => "=",
|
||||
"dollar" | "money" => "$",
|
||||
"music" | "note" => "#",
|
||||
_ => "?",
|
||||
};
|
||||
Text::new(icon_char, Point::new(x as i32, y as i32 + 20), style)
|
||||
.draw(self)
|
||||
.map_err(|e| DisplayError::Draw(format!("{e:?}")))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fill_background(&mut self, bounds: BoundingBox) -> Result<(), DisplayError> {
|
||||
self.clear_region(bounds)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> Result<(), DisplayError> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn to_rgb565(c: Color) -> Rgb565 {
|
||||
Rgb565::new(c.0 >> 3, c.1 >> 2, c.2 >> 3)
|
||||
}
|
||||
|
||||
impl Esp32DisplayAdapter {
|
||||
pub fn new<D>(display: D) -> Self
|
||||
where
|
||||
@@ -102,20 +81,19 @@ impl Esp32DisplayAdapter {
|
||||
impl DisplayPort for Esp32DisplayAdapter {
|
||||
type Error = DisplayError;
|
||||
|
||||
fn clear_region(&mut self, bounds: BoundingBox) -> Result<(), Self::Error> {
|
||||
self.inner.clear_region(bounds)
|
||||
fn draw_text_span(
|
||||
&mut self,
|
||||
text: &str,
|
||||
x: u16,
|
||||
y: u16,
|
||||
color: Color,
|
||||
font: FontSize,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.inner.draw_text_span(text, x, y, to_rgb565(color), font)
|
||||
}
|
||||
|
||||
fn draw_text(&mut self, text: &str, x: u16, y: u16, bounds: BoundingBox) -> Result<(), Self::Error> {
|
||||
self.inner.draw_text(text, x, y, bounds)
|
||||
}
|
||||
|
||||
fn draw_icon(&mut self, icon: &str, x: u16, y: u16) -> Result<(), Self::Error> {
|
||||
self.inner.draw_icon(icon, x, y)
|
||||
}
|
||||
|
||||
fn fill_background(&mut self, bounds: BoundingBox) -> Result<(), Self::Error> {
|
||||
self.inner.fill_background(bounds)
|
||||
fn fill_rect(&mut self, bounds: BoundingBox, color: Color) -> Result<(), Self::Error> {
|
||||
self.inner.fill_rect(bounds, to_rgb565(color))
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> Result<(), Self::Error> {
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::thread;
|
||||
use esp_idf_hal::io::Write;
|
||||
use esp_idf_svc::http::server::{Configuration as HttpConfig, EspHttpServer};
|
||||
use esp_idf_svc::nvs::{EspNvsPartition, NvsDefault};
|
||||
use client_domain::{BoundingBox, DisplayPort};
|
||||
use client_domain::{BoundingBox, Color, DisplayPort, FontSize};
|
||||
use log::{info, error};
|
||||
|
||||
use super::{DeviceConfig, save_config};
|
||||
@@ -190,12 +190,14 @@ fn hex_val(b: u8) -> Option<u8> {
|
||||
}
|
||||
|
||||
const FULL_SCREEN: BoundingBox = BoundingBox { x: 0, y: 0, width: 320, height: 240 };
|
||||
const WHITE: Color = Color(0xFF, 0xFF, 0xFF);
|
||||
const DARK_BG: Color = Color(0x08, 0x10, 0x18);
|
||||
|
||||
fn draw_setup_screen<D: DisplayPort>(display: &mut D) {
|
||||
let _ = display.fill_background(FULL_SCREEN);
|
||||
let _ = display.draw_text("K-Frame Setup", 0, 0, BoundingBox { x: 80, y: 50, width: 160, height: 20 });
|
||||
let _ = display.draw_text("Connect to WiFi:", 0, 0, BoundingBox { x: 40, y: 90, width: 240, height: 14 });
|
||||
let _ = display.draw_text("KFrame-Setup", 0, 0, BoundingBox { x: 80, y: 110, width: 160, height: 14 });
|
||||
let _ = display.draw_text("Then open browser", 0, 0, BoundingBox { x: 40, y: 150, width: 240, height: 14 });
|
||||
let _ = display.draw_text("to configure", 0, 0, BoundingBox { x: 60, y: 170, width: 200, height: 14 });
|
||||
let _ = display.fill_rect(FULL_SCREEN, DARK_BG);
|
||||
let _ = display.draw_text_span("K-Frame Setup", 100, 50, WHITE, FontSize::Small);
|
||||
let _ = display.draw_text_span("Connect to WiFi:", 60, 90, WHITE, FontSize::Small);
|
||||
let _ = display.draw_text_span("KFrame-Setup", 90, 110, WHITE, FontSize::Small);
|
||||
let _ = display.draw_text_span("Then open browser", 60, 150, WHITE, FontSize::Small);
|
||||
let _ = display.draw_text_span("to configure", 80, 170, WHITE, FontSize::Small);
|
||||
}
|
||||
|
||||
@@ -1,55 +1,63 @@
|
||||
use std::sync::mpsc;
|
||||
use client_domain::{BoundingBox, DisplayPort};
|
||||
use client_application::ClientApp;
|
||||
use std::time::{Duration, Instant};
|
||||
use std::collections::HashMap;
|
||||
use client_domain::{
|
||||
BoundingBox, DisplayPort, FontMetrics, RenderEngine, ScrollState, ThemeConfig,
|
||||
};
|
||||
use client_application::{ClientApp, RepaintCommand};
|
||||
use domain::{DisplayHint, Value};
|
||||
use protocol::ServerMessage;
|
||||
use crate::config::{RENDER_POLL_INTERVAL, SCREEN};
|
||||
use crate::config::RENDER_POLL_INTERVAL;
|
||||
use crate::adapters::display::Esp32DisplayAdapter;
|
||||
use log::*;
|
||||
|
||||
const LINE_HEIGHT: u16 = 12;
|
||||
const TEXT_PADDING: u16 = 4;
|
||||
const SCROLL_TICK: Duration = Duration::from_millis(50);
|
||||
|
||||
struct WidgetCache {
|
||||
hint: DisplayHint,
|
||||
data: Vec<(String, Value)>,
|
||||
bounds: BoundingBox,
|
||||
scroll: ScrollState,
|
||||
}
|
||||
|
||||
pub fn run(
|
||||
screen: BoundingBox,
|
||||
mut display: Esp32DisplayAdapter,
|
||||
rx: mpsc::Receiver<ServerMessage>,
|
||||
) {
|
||||
let metrics = FontMetrics {
|
||||
small: (6, 10),
|
||||
large: (10, 20),
|
||||
};
|
||||
let mut engine = RenderEngine::new(metrics, ThemeConfig::default());
|
||||
let mut app = ClientApp::new(screen);
|
||||
let mut widgets: HashMap<u16, WidgetCache> = HashMap::new();
|
||||
let mut first_update = true;
|
||||
let mut last_tick = Instant::now();
|
||||
|
||||
info!("Render loop started");
|
||||
|
||||
loop {
|
||||
match rx.recv_timeout(RENDER_POLL_INTERVAL) {
|
||||
let timeout = RENDER_POLL_INTERVAL.min(SCROLL_TICK);
|
||||
match rx.recv_timeout(timeout) {
|
||||
Ok(msg) => {
|
||||
let is_screen_update = matches!(msg, ServerMessage::ScreenUpdate { .. });
|
||||
let repaints = app.handle_message(msg);
|
||||
|
||||
if app.take_theme_changed() {
|
||||
engine.set_theme(app.theme().clone());
|
||||
}
|
||||
|
||||
if !repaints.is_empty() && (first_update || is_screen_update) {
|
||||
display.fill_background(SCREEN).unwrap();
|
||||
let bg = engine.theme().background;
|
||||
display.fill_rect(screen, bg).unwrap();
|
||||
first_update = false;
|
||||
}
|
||||
|
||||
for cmd in &repaints {
|
||||
display.clear_region(cmd.bounds).unwrap();
|
||||
|
||||
let mut y_offset = TEXT_PADDING;
|
||||
for kv in &cmd.state.data {
|
||||
if let protocol::WireValue::String(s) = &kv.value {
|
||||
let text_bounds = BoundingBox::new(
|
||||
cmd.bounds.x + TEXT_PADDING,
|
||||
cmd.bounds.y + y_offset,
|
||||
cmd.bounds.width.saturating_sub(TEXT_PADDING * 2),
|
||||
LINE_HEIGHT,
|
||||
);
|
||||
display.draw_text(
|
||||
&format!("{}: {s}", kv.key),
|
||||
text_bounds.x,
|
||||
text_bounds.y,
|
||||
text_bounds,
|
||||
).unwrap();
|
||||
y_offset += LINE_HEIGHT + 2;
|
||||
}
|
||||
}
|
||||
let cache = update_cache(&engine, cmd);
|
||||
draw_widget(&engine, &mut display, &cache);
|
||||
widgets.insert(cmd.widget_id, cache);
|
||||
}
|
||||
|
||||
if !repaints.is_empty() {
|
||||
@@ -62,5 +70,57 @@ pub fn run(
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let now = Instant::now();
|
||||
let elapsed = now.duration_since(last_tick);
|
||||
last_tick = now;
|
||||
|
||||
let mut needs_flush = false;
|
||||
for cache in widgets.values_mut() {
|
||||
if cache.scroll.tick(elapsed) {
|
||||
let bg = engine.theme().background;
|
||||
display.fill_rect(cache.bounds, bg).unwrap();
|
||||
draw_widget(&engine, &mut display, cache);
|
||||
needs_flush = true;
|
||||
}
|
||||
}
|
||||
if needs_flush {
|
||||
display.flush().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_cache(engine: &RenderEngine, cmd: &RepaintCommand) -> WidgetCache {
|
||||
let hint: DisplayHint = cmd.display_hint.clone().into();
|
||||
let data: Vec<(String, Value)> = cmd.state.data
|
||||
.iter()
|
||||
.map(|kv| (kv.key.clone(), kv.value.clone().into()))
|
||||
.collect();
|
||||
|
||||
let content_h = engine.content_height(&hint, &data, cmd.bounds.width);
|
||||
let scroll = ScrollState::new(cmd.bounds.height, content_h);
|
||||
|
||||
WidgetCache {
|
||||
hint,
|
||||
data,
|
||||
bounds: cmd.bounds,
|
||||
scroll,
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_widget(
|
||||
engine: &RenderEngine,
|
||||
display: &mut Esp32DisplayAdapter,
|
||||
cache: &WidgetCache,
|
||||
) {
|
||||
let draw_cmds = engine.render_widget(
|
||||
&cache.hint,
|
||||
&cache.data,
|
||||
cache.bounds,
|
||||
cache.scroll.offset(),
|
||||
);
|
||||
|
||||
for dc in &draw_cmds {
|
||||
display.draw_text_span(&dc.text, dc.x, dc.y, dc.color, dc.font).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user