WIP next servicepoint version

This commit is contained in:
Vinzenz Schroeter 2024-06-26 17:23:41 +02:00
parent 8b4b41de76
commit 6101e91615
6 changed files with 44 additions and 113 deletions

View file

@ -2,8 +2,8 @@ use std::sync::{RwLock, RwLockWriteGuard};
use log::{debug, error, info, warn};
use servicepoint::{
ByteGrid, Command, Grid, Origin, PIXEL_COUNT, PIXEL_WIDTH, PixelGrid,
TILE_SIZE,
BrightnessGrid, Command, Cp437Grid, Grid, Origin, PixelGrid, Tiles,
PIXEL_COUNT, PIXEL_WIDTH, TILE_SIZE,
};
use crate::font::BitmapFont;
@ -12,7 +12,7 @@ pub(crate) fn execute_command(
command: Command,
font: &BitmapFont,
display_ref: &RwLock<PixelGrid>,
luma_ref: &RwLock<ByteGrid>,
luma_ref: &RwLock<BrightnessGrid>,
) -> bool {
debug!("received {command:?}");
match command {
@ -24,7 +24,7 @@ pub(crate) fn execute_command(
warn!("display shutting down");
return false;
}
Command::BitmapLinearWin(Origin(x, y), pixels, _) => {
Command::BitmapLinearWin(Origin { x, y, .. }, pixels, _) => {
let mut display = display_ref.write().unwrap();
print_pixel_grid(x, y, &pixels, &mut display);
}
@ -43,8 +43,7 @@ pub(crate) fn execute_command(
}
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 (x, y) = get_coordinates_for_index(offset, bitmap_index);
display.set(x, y, vec[bitmap_index]);
}
}
@ -54,8 +53,7 @@ pub(crate) fn execute_command(
}
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 (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]);
}
@ -66,8 +64,7 @@ pub(crate) fn execute_command(
}
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 (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]);
}
@ -78,22 +75,19 @@ pub(crate) fn execute_command(
}
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 (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 Origin(offset_x, offset_y) = origin;
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(
offset_x + inner_x,
offset_y + inner_y,
origin.x + inner_x,
origin.y + inner_y,
brightness,
);
}
@ -123,12 +117,12 @@ fn check_bitmap_valid(offset: u16, payload_len: usize) -> bool {
}
fn print_cp437_data(
origin: Origin,
grid: &ByteGrid,
origin: Origin<Tiles>,
grid: &Cp437Grid,
font: &BitmapFont,
display: &mut RwLockWriteGuard<PixelGrid>,
) {
let Origin(x, y) = origin;
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);

View file

@ -15,9 +15,8 @@ pub struct BitmapFont {
impl BitmapFont {
pub fn load(font: Font) -> BitmapFont {
let mut bitmaps = core::array::from_fn(|_| {
PixelGrid::new(TILE_SIZE, TILE_SIZE)
});
let mut bitmaps =
core::array::from_fn(|_| PixelGrid::new(TILE_SIZE, TILE_SIZE));
for char_code in u8::MIN..u8::MAX {
let char = char_code as char;

View file

@ -4,7 +4,10 @@ use std::sync::RwLock;
use log::{info, warn};
use pixels::wgpu::TextureFormat;
use pixels::{Pixels, PixelsBuilder, SurfaceTexture};
use servicepoint::{ByteGrid, PixelGrid, PIXEL_HEIGHT, PIXEL_WIDTH, TILE_SIZE, Grid};
use servicepoint::{
Brightness, BrightnessGrid, Grid, PixelGrid, PIXEL_HEIGHT, PIXEL_WIDTH,
TILE_SIZE,
};
use winit::application::ApplicationHandler;
use winit::dpi::LogicalSize;
use winit::event::WindowEvent;
@ -16,7 +19,7 @@ use crate::Cli;
pub struct App<'t> {
display: &'t RwLock<PixelGrid>,
luma: &'t RwLock<ByteGrid>,
luma: &'t RwLock<BrightnessGrid>,
window: Option<Window>,
pixels: Option<Pixels>,
stop_udp_tx: Sender<()>,
@ -34,7 +37,7 @@ pub enum AppEvents {
impl<'t> App<'t> {
pub fn new(
display: &'t RwLock<PixelGrid>,
luma: &'t RwLock<ByteGrid>,
luma: &'t RwLock<BrightnessGrid>,
stop_udp_tx: Sender<()>,
cli: &'t Cli,
) -> Self {
@ -64,14 +67,21 @@ impl<'t> App<'t> {
for x in 0..PIXEL_WIDTH {
let is_set = display.get(x, y);
let brightness =
luma.get(x / TILE_SIZE, y / TILE_SIZE);
let brightness = luma.get(x / TILE_SIZE, y / TILE_SIZE);
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 },
if self.cli.red { brightness.into() } else { 0u8 },
if self.cli.green {
brightness.into()
} else {
0u8
},
if self.cli.blue {
brightness.into()
} else {
0u8
},
255,
]
} else {
@ -153,7 +163,7 @@ impl ApplicationHandler<AppEvents> for App<'_> {
if event.physical_key == KeyC && !event.repeat =>
{
self.display.write().unwrap().fill(false);
self.luma.write().unwrap().fill(u8::MAX);
self.luma.write().unwrap().fill(Brightness::MAX);
self.window.as_ref().unwrap().request_redraw();
}
_ => {}

View file

@ -7,7 +7,10 @@ use std::time::Duration;
use clap::Parser;
use log::{info, warn, LevelFilter};
use servicepoint::{ByteGrid, Command, PixelGrid, PIXEL_HEIGHT, PIXEL_WIDTH, TILE_HEIGHT, TILE_WIDTH, Grid};
use servicepoint::{
Brightness, BrightnessGrid, Command, Grid, PixelGrid, PIXEL_HEIGHT,
PIXEL_WIDTH, TILE_HEIGHT, TILE_WIDTH,
};
use winit::event_loop::{ControlFlow, EventLoop};
use crate::execute_command::execute_command;
@ -49,13 +52,10 @@ fn main() {
.set_nonblocking(true)
.expect("could not enter non blocking mode");
let display = RwLock::new(PixelGrid::new(
PIXEL_WIDTH,
PIXEL_HEIGHT,
));
let display = RwLock::new(PixelGrid::new(PIXEL_WIDTH, PIXEL_HEIGHT));
let mut luma = ByteGrid::new(TILE_WIDTH, TILE_HEIGHT);
luma.fill(u8::MAX);
let mut luma = BrightnessGrid::new(TILE_WIDTH, TILE_HEIGHT);
luma.fill(Brightness::MAX);
let luma = RwLock::new(luma);
run(&display, &luma, socket, BitmapFont::default(), &cli);
@ -63,7 +63,7 @@ fn main() {
fn run(
display_ref: &RwLock<PixelGrid>,
luma_ref: &RwLock<ByteGrid>,
luma_ref: &RwLock<BrightnessGrid>,
socket: UdpSocket,
font: BitmapFont,
cli: &Cli,