internal data sources (clock, static text), connection indicator, rendering fixes

DataSourceConfig refactored to enum: External/Clock/StaticText. Clock
generates formatted time via chrono, static text emits configured string.

ESP32: connection status indicator (green/red dot bottom-right), per-widget
clear before redraw, RenderEvent enum for local + server messages.

Polling uses DataUpdate instead of ScreenUpdate to avoid wiping widget state.
Empty mappings passthrough raw source data for internal sources.
This commit is contained in:
2026-06-19 11:26:49 +02:00
parent b448fa15fe
commit a51d22649a
25 changed files with 630 additions and 214 deletions

View File

@@ -3,7 +3,7 @@ use embedded_graphics::{
mono_font::{ascii::FONT_6X10, ascii::FONT_10X20, MonoTextStyle},
pixelcolor::Rgb565,
prelude::*,
primitives::{PrimitiveStyle, Rectangle},
primitives::{Circle, PrimitiveStyle, Rectangle},
text::Text,
};
@@ -27,6 +27,7 @@ pub struct Esp32DisplayAdapter {
trait ErasedDisplay {
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 fill_circle(&mut self, x: u16, y: u16, diameter: u16, color: Rgb565) -> Result<(), DisplayError>;
fn flush(&mut self) -> Result<(), DisplayError>;
}
@@ -57,6 +58,14 @@ where
Ok(())
}
fn fill_circle(&mut self, x: u16, y: u16, diameter: u16, color: Rgb565) -> Result<(), DisplayError> {
Circle::new(Point::new(x as i32, y as i32), diameter as u32)
.into_styled(PrimitiveStyle::with_fill(color))
.draw(self)
.map_err(|e| DisplayError::Draw(format!("{e:?}")))?;
Ok(())
}
fn flush(&mut self) -> Result<(), DisplayError> {
Ok(())
}
@@ -100,3 +109,9 @@ impl DisplayPort for Esp32DisplayAdapter {
self.inner.flush()
}
}
impl Esp32DisplayAdapter {
pub fn fill_circle(&mut self, x: u16, y: u16, diameter: u16, color: Color) -> Result<(), DisplayError> {
self.inner.fill_circle(x, y, diameter, to_rgb565(color))
}
}

View File

@@ -50,7 +50,7 @@ fn run_station(
info!("Connecting WiFi...");
match hal::wifi::init(modem, sysloop.clone(), nvs.clone(), &cfg.wifi_ssid, &cfg.wifi_pass) {
Ok(_wifi) => {
let (tx, rx) = mpsc::channel();
let (tx, rx) = mpsc::channel::<tasks::RenderEvent>();
tasks::network::spawn(cfg.server_addr, tx);
tasks::render::run(config::SCREEN, display, rx);
}

View File

@@ -1,2 +1,9 @@
pub mod network;
pub mod render;
use protocol::ServerMessage;
pub enum RenderEvent {
Server(ServerMessage),
ConnectionStatus(bool),
}

View File

@@ -1,12 +1,13 @@
use std::sync::mpsc;
use std::thread;
use client_domain::NetworkPort;
use protocol::{ServerMessage, decode_server_message};
use protocol::decode_server_message;
use super::RenderEvent;
use crate::config::{NET_THREAD_STACK_SIZE, NET_POLL_INTERVAL, NET_RECONNECT_DELAY};
use crate::adapters::network::Esp32Network;
use log::*;
pub fn spawn(server_addr: String, tx: mpsc::Sender<ServerMessage>) {
pub fn spawn(server_addr: String, tx: mpsc::Sender<RenderEvent>) {
thread::Builder::new()
.stack_size(NET_THREAD_STACK_SIZE)
.name("net".into())
@@ -14,16 +15,20 @@ pub fn spawn(server_addr: String, tx: mpsc::Sender<ServerMessage>) {
.expect("failed to spawn network thread");
}
fn run(server_addr: String, tx: mpsc::Sender<ServerMessage>) {
fn run(server_addr: String, tx: mpsc::Sender<RenderEvent>) {
let mut net = Esp32Network::new();
loop {
if !net.is_connected() {
info!("Connecting to server {server_addr}...");
match net.connect(&server_addr) {
Ok(()) => info!("Server connected"),
Ok(()) => {
info!("Server connected");
let _ = tx.send(RenderEvent::ConnectionStatus(true));
}
Err(e) => {
error!("Connection failed: {e}, retrying...");
let _ = tx.send(RenderEvent::ConnectionStatus(false));
thread::sleep(NET_RECONNECT_DELAY);
continue;
}
@@ -33,7 +38,7 @@ fn run(server_addr: String, tx: mpsc::Sender<ServerMessage>) {
match net.receive() {
Ok(Some(payload)) => {
match decode_server_message(&payload) {
Ok(msg) => { let _ = tx.send(msg); }
Ok(msg) => { let _ = tx.send(RenderEvent::Server(msg)); }
Err(e) => error!("Decode error: {e}"),
}
}
@@ -43,6 +48,7 @@ fn run(server_addr: String, tx: mpsc::Sender<ServerMessage>) {
Err(e) => {
error!("Receive error: {e}, reconnecting...");
let _ = net.disconnect();
let _ = tx.send(RenderEvent::ConnectionStatus(false));
thread::sleep(NET_RECONNECT_DELAY);
}
}

View File

@@ -2,16 +2,21 @@ use std::sync::mpsc;
use std::time::{Duration, Instant};
use std::collections::HashMap;
use client_domain::{
BoundingBox, DisplayPort, FontMetrics, RenderEngine, ScrollState, ThemeConfig,
BoundingBox, Color, DisplayPort, FontMetrics, RenderEngine, ScrollState, ThemeConfig,
};
use client_application::{ClientApp, RepaintCommand};
use domain::{DisplayHint, Value};
use protocol::ServerMessage;
use super::RenderEvent;
use crate::config::RENDER_POLL_INTERVAL;
use crate::adapters::display::Esp32DisplayAdapter;
use log::*;
const SCROLL_TICK: Duration = Duration::from_millis(50);
const INDICATOR_DIAMETER: u16 = 8;
const INDICATOR_MARGIN: u16 = 4;
const COLOR_CONNECTED: Color = Color(0, 200, 0);
const COLOR_DISCONNECTED: Color = Color(200, 0, 0);
struct WidgetCache {
hint: DisplayHint,
@@ -23,7 +28,7 @@ struct WidgetCache {
pub fn run(
screen: BoundingBox,
mut display: Esp32DisplayAdapter,
rx: mpsc::Receiver<ServerMessage>,
rx: mpsc::Receiver<RenderEvent>,
) {
let metrics = FontMetrics {
small: (6, 10),
@@ -34,13 +39,23 @@ pub fn run(
let mut widgets: HashMap<u16, WidgetCache> = HashMap::new();
let mut first_update = true;
let mut last_tick = Instant::now();
let mut connected = false;
info!("Render loop started");
draw_indicator(&mut display, screen, connected);
display.flush().unwrap();
loop {
let timeout = RENDER_POLL_INTERVAL.min(SCROLL_TICK);
match rx.recv_timeout(timeout) {
Ok(msg) => {
Ok(RenderEvent::ConnectionStatus(status)) => {
if status != connected {
connected = status;
draw_indicator(&mut display, screen, connected);
display.flush().unwrap();
}
}
Ok(RenderEvent::Server(msg)) => {
let is_screen_update = matches!(msg, ServerMessage::ScreenUpdate { .. });
let repaints = app.handle_message(msg);
@@ -49,19 +64,20 @@ pub fn run(
engine.set_theme(app.theme().clone());
}
let bg = engine.theme().background;
if !repaints.is_empty() && (first_update || is_screen_update || theme_changed) {
let bg = engine.theme().background;
display.fill_rect(screen, bg).unwrap();
first_update = false;
}
for cmd in &repaints {
let cache = update_cache(&engine, cmd);
display.fill_rect(cache.bounds, bg).unwrap();
draw_widget(&engine, &mut display, &cache);
widgets.insert(cmd.widget_id, cache);
}
if !repaints.is_empty() {
draw_indicator(&mut display, screen, connected);
display.flush().unwrap();
}
}
@@ -86,11 +102,19 @@ pub fn run(
}
}
if needs_flush {
draw_indicator(&mut display, screen, connected);
display.flush().unwrap();
}
}
}
fn draw_indicator(display: &mut Esp32DisplayAdapter, screen: BoundingBox, connected: bool) {
let color = if connected { COLOR_CONNECTED } else { COLOR_DISCONNECTED };
let x = screen.x + screen.width - INDICATOR_DIAMETER - INDICATOR_MARGIN;
let y = screen.y + screen.height - INDICATOR_DIAMETER - INDICATOR_MARGIN;
display.fill_circle(x, y, INDICATOR_DIAMETER, color).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