extract methods, CommandExecutor

This commit is contained in:
Vinzenz Schroeter 2025-01-19 11:49:44 +01:00
parent ad5f1e8abe
commit c8f9cca47a
4 changed files with 325 additions and 280 deletions

View file

@ -3,210 +3,226 @@ use servicepoint::{
Bitmap, BrightnessGrid, CharGrid, Command, Cp437Grid, Grid, Origin, Tiles,
PIXEL_COUNT, PIXEL_WIDTH, TILE_SIZE,
};
use std::sync::{RwLock, RwLockWriteGuard};
use std::sync::RwLock;
use crate::font::Cp437Font;
use crate::font_renderer::FontRenderer8x8;
pub(crate) fn execute_command(
command: Command,
cp436_font: &Cp437Font,
utf8_font: &FontRenderer8x8,
display_ref: &RwLock<Bitmap>,
luma_ref: &RwLock<BrightnessGrid>,
) -> bool {
debug!("received {command:?}");
match command {
Command::Clear => {
info!("clearing display");
display_ref.write().unwrap().fill(false);
}
Command::HardReset => {
warn!("display shutting down");
return false;
}
Command::BitmapLinearWin(Origin { x, y, .. }, pixels, _) => {
let mut display = display_ref.write().unwrap();
print_pixel_grid(x, y, &pixels, &mut display);
}
Command::Cp437Data(origin, grid) => {
let mut display = display_ref.write().unwrap();
print_cp437_data(origin, &grid, cp436_font, &mut display);
}
#[allow(deprecated)]
Command::BitmapLegacy => {
warn!("ignoring deprecated command {:?}", command);
}
// TODO: how to deduplicate this code in a rusty way?
Command::BitmapLinear(offset, vec, _) => {
if !check_bitmap_valid(offset as u16, vec.len()) {
return true;
}
let mut display = display_ref.write().unwrap();
for bitmap_index in 0..vec.len() {
let (x, y) = get_coordinates_for_index(offset, bitmap_index);
display.set(x, y, vec[bitmap_index]);
}
}
Command::BitmapLinearAnd(offset, vec, _) => {
if !check_bitmap_valid(offset as u16, vec.len()) {
return true;
}
let mut display = display_ref.write().unwrap();
for bitmap_index in 0..vec.len() {
let (x, y) = get_coordinates_for_index(offset, bitmap_index);
let old_value = display.get(x, y);
display.set(x, y, old_value && vec[bitmap_index]);
}
}
Command::BitmapLinearOr(offset, vec, _) => {
if !check_bitmap_valid(offset as u16, vec.len()) {
return true;
}
let mut display = display_ref.write().unwrap();
for bitmap_index in 0..vec.len() {
let (x, y) = get_coordinates_for_index(offset, bitmap_index);
let old_value = display.get(x, y);
display.set(x, y, old_value || vec[bitmap_index]);
}
}
Command::BitmapLinearXor(offset, vec, _) => {
if !check_bitmap_valid(offset as u16, vec.len()) {
return true;
}
let mut display = display_ref.write().unwrap();
for bitmap_index in 0..vec.len() {
let (x, y) = get_coordinates_for_index(offset, bitmap_index);
let old_value = display.get(x, y);
display.set(x, y, old_value ^ vec[bitmap_index]);
}
}
Command::CharBrightness(origin, grid) => {
let mut luma = luma_ref.write().unwrap();
for inner_y in 0..grid.height() {
for inner_x in 0..grid.width() {
let brightness = grid.get(inner_x, inner_y);
luma.set(
origin.x + inner_x,
origin.y + inner_y,
brightness,
);
}
}
}
Command::Brightness(brightness) => {
luma_ref.write().unwrap().fill(brightness);
}
Command::FadeOut => {
error!("command not implemented: {command:?}")
}
Command::Utf8Data(origin, grid) => {
let mut display = display_ref.write().unwrap();
print_utf8_data(origin, &grid, utf8_font, &mut display);
}
};
true
pub struct CommandExecutor<'t> {
display: &'t RwLock<Bitmap>,
luma: &'t RwLock<BrightnessGrid>,
cp437_font: Cp437Font,
utf8_font: FontRenderer8x8,
}
fn check_bitmap_valid(offset: u16, payload_len: usize) -> bool {
if offset as usize + payload_len > PIXEL_COUNT {
error!("bitmap with offset {offset} is too big ({payload_len} bytes)");
return false;
impl<'t> CommandExecutor<'t> {
pub fn new(
display: &'t RwLock<Bitmap>,
luma: &'t RwLock<BrightnessGrid>,
) -> Self {
CommandExecutor {
display,
luma,
utf8_font: FontRenderer8x8::default(),
cp437_font: Cp437Font::default(),
}
}
true
}
pub(crate) fn execute(&self, command: Command) -> bool {
debug!("received {command:?}");
match command {
Command::Clear => {
info!("clearing display");
self.display.write().unwrap().fill(false);
}
Command::HardReset => {
warn!("display shutting down");
return false;
}
Command::BitmapLinearWin(Origin { x, y, .. }, pixels, _) => {
self.print_pixel_grid(x, y, &pixels);
}
Command::Cp437Data(origin, grid) => {
self.print_cp437_data(origin, &grid);
}
#[allow(deprecated)]
Command::BitmapLegacy => {
warn!("ignoring deprecated command {:?}", command);
}
// TODO: how to deduplicate this code in a rusty way?
Command::BitmapLinear(offset, vec, _) => {
if !Self::check_bitmap_valid(offset as u16, vec.len()) {
return true;
}
let mut display = self.display.write().unwrap();
for bitmap_index in 0..vec.len() {
let (x, y) =
Self::get_coordinates_for_index(offset, bitmap_index);
display.set(x, y, vec[bitmap_index]);
}
}
Command::BitmapLinearAnd(offset, vec, _) => {
if !Self::check_bitmap_valid(offset as u16, vec.len()) {
return true;
}
let mut display = self.display.write().unwrap();
for bitmap_index in 0..vec.len() {
let (x, y) =
Self::get_coordinates_for_index(offset, bitmap_index);
let old_value = display.get(x, y);
display.set(x, y, old_value && vec[bitmap_index]);
}
}
Command::BitmapLinearOr(offset, vec, _) => {
if !Self::check_bitmap_valid(offset as u16, vec.len()) {
return true;
}
let mut display = self.display.write().unwrap();
for bitmap_index in 0..vec.len() {
let (x, y) =
Self::get_coordinates_for_index(offset, bitmap_index);
let old_value = display.get(x, y);
display.set(x, y, old_value || vec[bitmap_index]);
}
}
Command::BitmapLinearXor(offset, vec, _) => {
if !Self::check_bitmap_valid(offset as u16, vec.len()) {
return true;
}
let mut display = self.display.write().unwrap();
for bitmap_index in 0..vec.len() {
let (x, y) =
Self::get_coordinates_for_index(offset, bitmap_index);
let old_value = display.get(x, y);
display.set(x, y, old_value ^ vec[bitmap_index]);
}
}
Command::CharBrightness(origin, grid) => {
let mut luma = self.luma.write().unwrap();
for inner_y in 0..grid.height() {
for inner_x in 0..grid.width() {
let brightness = grid.get(inner_x, inner_y);
luma.set(
origin.x + inner_x,
origin.y + inner_y,
brightness,
);
}
}
}
Command::Brightness(brightness) => {
self.luma.write().unwrap().fill(brightness);
}
Command::FadeOut => {
error!("command not implemented: {command:?}")
}
Command::Utf8Data(origin, grid) => {
self.print_utf8_data(origin, &grid);
}
};
fn print_cp437_data(
origin: Origin<Tiles>,
grid: &Cp437Grid,
font: &Cp437Font,
display: &mut RwLockWriteGuard<Bitmap>,
) {
let Origin { x, y, .. } = origin;
for char_y in 0usize..grid.height() {
for char_x in 0usize..grid.width() {
let char_code = grid.get(char_x, char_y);
trace!(
true
}
fn check_bitmap_valid(offset: u16, payload_len: usize) -> bool {
if offset as usize + payload_len > PIXEL_COUNT {
error!(
"bitmap with offset {offset} is too big ({payload_len} bytes)"
);
return false;
}
true
}
fn print_cp437_data(&self, origin: Origin<Tiles>, grid: &Cp437Grid) {
let font = &self.cp437_font;
let Origin { x, y, .. } = origin;
for char_y in 0usize..grid.height() {
for char_x in 0usize..grid.width() {
let char_code = grid.get(char_x, char_y);
trace!(
"drawing char_code {char_code:#04x} (if this was UTF-8, it would be {})",
char::from(char_code)
);
let tile_x = char_x + x;
let tile_y = char_y + y;
let tile_x = char_x + x;
let tile_y = char_y + y;
let bitmap = font.get_bitmap(char_code);
if !print_pixel_grid(
tile_x * TILE_SIZE,
tile_y * TILE_SIZE,
bitmap,
display,
) {
error!("stopping drawing text because char draw failed");
return;
let bitmap = font.get_bitmap(char_code);
if !self.print_pixel_grid(
tile_x * TILE_SIZE,
tile_y * TILE_SIZE,
bitmap,
) {
error!("stopping drawing text because char draw failed");
return;
}
}
}
}
}
fn print_utf8_data(
origin: Origin<Tiles>,
grid: &CharGrid,
font: &FontRenderer8x8,
display: &mut RwLockWriteGuard<Bitmap>,
) {
let Origin { x, y, .. } = origin;
for char_y in 0usize..grid.height() {
for char_x in 0usize..grid.width() {
let char = grid.get(char_x, char_y);
trace!("drawing {char}");
fn print_utf8_data(&self, origin: Origin<Tiles>, grid: &CharGrid) {
let mut display = self.display.write().unwrap();
let tile_x = char_x + x;
let tile_y = char_y + y;
let Origin { x, y, .. } = origin;
for char_y in 0usize..grid.height() {
for char_x in 0usize..grid.width() {
let char = grid.get(char_x, char_y);
trace!("drawing {char}");
if let Err(e) = font.render(
char,
display,
Origin::new(tile_x * TILE_SIZE, tile_y * TILE_SIZE),
) {
error!("stopping drawing text because char draw failed: {e}");
return;
let tile_x = char_x + x;
let tile_y = char_y + y;
if let Err(e) = self.utf8_font.render(
char,
&mut display,
Origin::new(tile_x * TILE_SIZE, tile_y * TILE_SIZE),
) {
error!(
"stopping drawing text because char draw failed: {e}"
);
return;
}
}
}
}
}
fn print_pixel_grid(
offset_x: usize,
offset_y: usize,
pixels: &Bitmap,
display: &mut RwLockWriteGuard<Bitmap>,
) -> bool {
debug!(
"printing {}x{} grid at {offset_x} {offset_y}",
pixels.width(),
pixels.height()
);
for inner_y in 0..pixels.height() {
for inner_x in 0..pixels.width() {
let is_set = pixels.get(inner_x, inner_y);
let x = offset_x + inner_x;
let y = offset_y + inner_y;
fn print_pixel_grid(
&self,
offset_x: usize,
offset_y: usize,
pixels: &Bitmap,
) -> bool {
debug!(
"printing {}x{} grid at {offset_x} {offset_y}",
pixels.width(),
pixels.height()
);
let mut display = self.display.write().unwrap();
for inner_y in 0..pixels.height() {
for inner_x in 0..pixels.width() {
let is_set = pixels.get(inner_x, inner_y);
let x = offset_x + inner_x;
let y = offset_y + inner_y;
if x >= display.width() || y >= display.height() {
error!("stopping pixel grid draw because coordinate {x} {y} is out of bounds");
return false;
if x >= display.width() || y >= display.height() {
error!("stopping pixel grid draw because coordinate {x} {y} is out of bounds");
return false;
}
display.set(x, y, is_set);
}
display.set(x, y, is_set);
}
true
}
true
}
fn get_coordinates_for_index(offset: usize, index: usize) -> (usize, usize) {
let pixel_index = offset + index;
(pixel_index % PIXEL_WIDTH, pixel_index / PIXEL_WIDTH)
fn get_coordinates_for_index(
offset: usize,
index: usize,
) -> (usize, usize) {
let pixel_index = offset + index;
(pixel_index % PIXEL_WIDTH, pixel_index / PIXEL_WIDTH)
}
}

View file

@ -1,18 +1,33 @@
use crate::font_renderer::RenderError::{GlyphNotFound, OutOfBounds};
use font_kit::canvas::{Canvas, Format, RasterizationOptions};
use font_kit::error::GlyphLoadingError;
use font_kit::family_name::FamilyName;
use font_kit::font::Font;
use font_kit::hinting::HintingOptions;
use font_kit::properties::Properties;
use font_kit::source::SystemSource;
use pathfinder_geometry::transform2d::Transform2F;
use pathfinder_geometry::vector::{vec2f, vec2i};
use font_kit::{
canvas::{Canvas, Format, RasterizationOptions},
error::GlyphLoadingError,
family_name::FamilyName,
font::Font,
hinting::HintingOptions,
properties::Properties,
source::SystemSource,
};
use pathfinder_geometry::{
transform2d::Transform2F,
vector::{vec2f, vec2i},
};
use servicepoint::{Bitmap, Grid, Origin, Pixels, TILE_SIZE};
use std::sync::Mutex;
struct SendFont(Font);
// struct is only using primitives and pointers - lets try if it is only missing the declaration
unsafe impl Send for SendFont {}
impl AsRef<Font> for SendFont {
fn as_ref(&self) -> &Font {
&self.0
}
}
pub struct FontRenderer8x8 {
font: Font,
font: SendFont,
canvas: Mutex<Canvas>,
fallback_char: Option<u32>,
}
@ -35,7 +50,7 @@ impl FontRenderer8x8 {
assert_eq!(canvas.stride, TILE_SIZE);
let fallback_char = fallback_char.and_then(|c| font.glyph_for_char(c));
let result = Self {
font,
font: SendFont(font),
fallback_char,
canvas: Mutex::new(canvas),
};
@ -49,14 +64,18 @@ impl FontRenderer8x8 {
offset: Origin<Pixels>,
) -> Result<(), RenderError> {
let mut canvas = self.canvas.lock().unwrap();
let glyph_id = self.font.glyph_for_char(char).or(self.fallback_char);
let glyph_id = self
.font
.as_ref()
.glyph_for_char(char)
.or(self.fallback_char);
let glyph_id = match glyph_id {
None => return Err(GlyphNotFound(char)),
Some(val) => val,
};
canvas.pixels.fill(0);
self.font.rasterize_glyph(
self.font.as_ref().rasterize_glyph(
&mut canvas,
glyph_id,
TILE_SIZE as f32,
@ -85,7 +104,14 @@ impl FontRenderer8x8 {
impl Default for FontRenderer8x8 {
fn default() -> Self {
let utf8_font = SystemSource::new()
.select_best_match(&[FamilyName::Monospace], &Properties::new())
.select_best_match(
&[
FamilyName::Title("Roboto Mono".to_string()),
FamilyName::Title("Fira Mono".to_string()),
FamilyName::Monospace,
],
&Properties::new(),
)
.unwrap()
.load()
.unwrap();

View file

@ -1,3 +1,4 @@
use std::slice::ChunksExactMut;
use std::sync::mpsc::Sender;
use std::sync::RwLock;
@ -62,24 +63,25 @@ impl<'t> App<'t> {
fn draw(&mut self) {
let window = self.window.as_ref().unwrap();
let mut pixels = {
let window_size = window.inner_size();
let surface_texture = SurfaceTexture::new(
window_size.width,
window_size.height,
&window,
);
Pixels::new(
self.logical_size.width as u32,
self.logical_size.height as u32,
surface_texture,
)
.unwrap()
};
let window_size = window.inner_size();
let surface_texture =
SurfaceTexture::new(window_size.width, window_size.height, &window);
let mut pixels = Pixels::new(
self.logical_size.width as u32,
self.logical_size.height as u32,
surface_texture,
)
.unwrap();
let mut frame = pixels.frame_mut().chunks_exact_mut(4);
self.draw_frame(&mut frame);
pixels.render().expect("could not render");
}
fn draw_frame(&self, frame: &mut ChunksExactMut<u8>) {
let display = self.display.read().unwrap();
let luma = self.luma.read().unwrap();
for y in 0..PIXEL_HEIGHT {
if self.cli.spacers && y != 0 && y % TILE_SIZE == 0 {
// cannot just frame.skip(PIXEL_WIDTH as usize * SPACER_HEIGHT as usize) because of typing
@ -90,30 +92,29 @@ impl<'t> App<'t> {
for x in 0..PIXEL_WIDTH {
let is_set = display.get(x, y);
let brightness: u8 =
luma.get(x / TILE_SIZE, y / TILE_SIZE).into();
let max_brightness: u8 = Brightness::MAX.into();
let scale: f32 = (u8::MAX as f32) / (max_brightness as f32);
let brightness =
u8::from(luma.get(x / TILE_SIZE, y / TILE_SIZE));
let scale =
(u8::MAX as f32) / (u8::from(Brightness::MAX) as f32);
let brightness = (scale * brightness as f32) as u8;
let color = if is_set {
[
if self.cli.red { brightness } else { 0u8 },
if self.cli.green { brightness } else { 0u8 },
if self.cli.blue { brightness } else { 0u8 },
255,
]
} else {
[0u8, 0, 0, 255]
};
let color = self.get_color(is_set, brightness);
let pixel = frame.next().unwrap();
pixel.copy_from_slice(&color);
}
}
}
pixels.render().expect("could not render");
fn get_color(&self, is_set: bool, brightness: u8) -> [u8; 4] {
if is_set {
[
if self.cli.red { brightness } else { 0u8 },
if self.cli.green { brightness } else { 0u8 },
if self.cli.blue { brightness } else { 0u8 },
255,
]
} else {
[0u8, 0, 0, 255]
}
}
}

View file

@ -1,20 +1,16 @@
#![deny(clippy::all)]
use crate::execute_command::CommandExecutor;
use crate::gui::{App, AppEvents};
use clap::Parser;
use log::{info, warn, LevelFilter};
use servicepoint::*;
use std::io::ErrorKind;
use std::net::UdpSocket;
use std::sync::{mpsc, RwLock};
use std::time::Duration;
use clap::Parser;
use log::{info, warn, LevelFilter};
use servicepoint::*;
use winit::event_loop::{ControlFlow, EventLoop};
use crate::execute_command::execute_command;
use crate::font::Cp437Font;
use crate::font_renderer::FontRenderer8x8;
use crate::gui::{App, AppEvents};
mod execute_command;
mod font;
mod font_renderer;
@ -34,6 +30,8 @@ struct Cli {
blue: bool,
}
const BUF_SIZE: usize = 8985;
fn main() {
env_logger::builder()
.filter_level(LevelFilter::Info)
@ -57,19 +55,8 @@ fn main() {
luma.fill(Brightness::MAX);
let luma = RwLock::new(luma);
run(&display, &luma, socket, Cp437Font::default(), &cli);
}
fn run(
display_ref: &RwLock<Bitmap>,
luma_ref: &RwLock<BrightnessGrid>,
socket: UdpSocket,
cp437_font: Cp437Font,
cli: &Cli,
) {
let (stop_udp_tx, stop_udp_rx) = mpsc::channel();
let mut app = App::new(display_ref, luma_ref, stop_udp_tx, cli);
let mut app = App::new(&display, &luma, stop_udp_tx, &cli);
let event_loop = EventLoop::with_user_event()
.build()
@ -77,46 +64,23 @@ fn run(
event_loop.set_control_flow(ControlFlow::Wait);
let event_proxy = event_loop.create_proxy();
let command_executor = CommandExecutor::new(&display, &luma);
std::thread::scope(move |scope| {
let udp_thread = scope.spawn(move || {
let mut buf = [0; 8985];
let utf8_font = FontRenderer8x8::default();
scope.spawn(move || {
let mut buf = [0; BUF_SIZE];
while stop_udp_rx.try_recv().is_err() {
let (amount, _) = match socket.recv_from(&mut buf) {
Err(err) if err.kind() == ErrorKind::WouldBlock => {
std::thread::sleep(Duration::from_millis(1));
continue;
}
Ok(result) => result,
other => other.unwrap(),
let amount = match receive_into_buf(&socket, &mut buf) {
Some(value) => value,
None => continue,
};
if amount == buf.len() {
warn!(
"the received package may have been truncated to a length of {}",
amount
);
}
let package = match servicepoint::Packet::try_from(&buf[..amount]) {
Err(_) => {
warn!("could not load packet with length {amount} into header");
continue;
}
Ok(package) => package,
let command = match command_from_slice(&buf[..amount]) {
Some(value) => value,
None => continue,
};
let command = match Command::try_from(package) {
Err(err) => {
warn!("could not read command for packet: {:?}", err);
continue;
}
Ok(val) => val,
};
if !execute_command(command, &cp437_font, &utf8_font, display_ref, luma_ref) {
if !command_executor.execute(command) {
// hard reset
event_proxy
.send_event(AppEvents::UdpThreadClosed)
@ -129,11 +93,49 @@ fn run(
.expect("could not send packet handled event");
}
});
event_loop
.run_app(&mut app)
.expect("could not run event loop");
udp_thread.join().expect("could not join udp thread");
});
}
fn command_from_slice(slice: &[u8]) -> Option<Command> {
let package = match servicepoint::Packet::try_from(slice) {
Err(_) => {
warn!("could not load packet with length {}", slice.len());
return None;
}
Ok(package) => package,
};
let command = match Command::try_from(package) {
Err(err) => {
warn!("could not read command for packet: {:?}", err);
return None;
}
Ok(val) => val,
};
Some(command)
}
fn receive_into_buf(
socket: &UdpSocket,
buf: &mut [u8; BUF_SIZE],
) -> Option<usize> {
let (amount, _) = match socket.recv_from(buf) {
Err(err) if err.kind() == ErrorKind::WouldBlock => {
std::thread::sleep(Duration::from_millis(1));
return None;
}
Ok(result) => result,
other => other.unwrap(),
};
if amount == buf.len() {
warn!(
"the received package may have been truncated to a length of {}",
amount
);
}
Some(amount)
}