diff --git a/src/gui.rs b/src/gui.rs index 2efe9b4..379a906 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -2,10 +2,10 @@ use std::sync::mpsc::Sender; use std::sync::RwLock; use log::{info, warn}; -use pixels::wgpu::TextureFormat; use pixels::{Pixels, PixelsBuilder, SurfaceTexture}; +use pixels::wgpu::TextureFormat; use servicepoint2::{ - ByteGrid, PixelGrid, PIXEL_HEIGHT, PIXEL_WIDTH, TILE_SIZE, + ByteGrid, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid, TILE_SIZE, }; use winit::application::ApplicationHandler; use winit::dpi::{LogicalSize, Size}; @@ -13,13 +13,15 @@ use winit::event::WindowEvent; use winit::event_loop::ActiveEventLoop; use winit::window::{Window, WindowId}; +use crate::Cli; + pub struct App<'t> { display: &'t RwLock, luma: &'t RwLock, window: Option, pixels: Option, stop_udp_tx: Sender<()>, - spacers: bool, + cli: &'t Cli, } const SPACER_HEIGHT: u16 = 4; @@ -35,7 +37,7 @@ impl<'t> App<'t> { display: &'t RwLock, luma: &'t RwLock, stop_udp_tx: Sender<()>, - spacers: bool, + cli: &'t Cli, ) -> Self { App { display, @@ -43,14 +45,14 @@ impl<'t> App<'t> { stop_udp_tx, pixels: None, window: None, - spacers, + cli, } } } impl ApplicationHandler for App<'_> { fn resumed(&mut self, event_loop: &ActiveEventLoop) { - let height = if self.spacers { + let height = if self.cli.spacers { let num_spacers = (PIXEL_HEIGHT / TILE_SIZE) - 1; PIXEL_HEIGHT + num_spacers * SPACER_HEIGHT } else { @@ -122,7 +124,7 @@ impl ApplicationHandler for App<'_> { let luma = self.luma.read().unwrap(); for y in 0..PIXEL_HEIGHT as usize { - if self.spacers && y != 0 && y % TILE_SIZE as usize == 0 { + if self.cli.spacers && y != 0 && y % TILE_SIZE as usize == 0 { // cannot just frame.skip(PIXEL_WIDTH as usize * SPACER_HEIGHT as usize) because of typing for _ in 0..PIXEL_WIDTH as usize * SPACER_HEIGHT as usize { frame.next().unwrap(); @@ -135,7 +137,12 @@ impl ApplicationHandler for App<'_> { luma.get(x / TILE_SIZE as usize, y / TILE_SIZE as usize); let color = if is_set { - [0u8, brightness, 0, 255] + [ + 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] }; diff --git a/src/main.rs b/src/main.rs index 30ceb35..367a2b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,19 +23,28 @@ mod gui; #[derive(Parser, Debug)] struct Cli { - #[arg(short, long, default_value = "0.0.0.0:2342")] + #[arg(long, default_value = "0.0.0.0:2342")] bind: String, #[arg(short, long, default_value_t = false)] spacers: bool, + #[arg(short, long, default_value_t = false)] + red: bool, + #[arg(short, long, default_value_t = false)] + green: bool, + #[arg(short, long, default_value_t = false)] + blue: bool, } fn main() { env_logger::init(); - let cli = Cli::parse(); - info!("starting with args: {:?}", &cli); + let mut cli = Cli::parse(); + if !(cli.red || cli.blue || cli.green) { + cli.green = true; + } - let socket = UdpSocket::bind(cli.bind).expect("could not bind socket"); + info!("starting with args: {:?}", &cli); + let socket = UdpSocket::bind(&cli.bind).expect("could not bind socket"); socket .set_nonblocking(true) .expect("could not enter non blocking mode"); @@ -46,16 +55,24 @@ fn main() { PIXEL_WIDTH as usize, PIXEL_HEIGHT as usize, )); - let display_ref = &display; let mut luma = ByteGrid::new(TILE_WIDTH as usize, TILE_HEIGHT as usize); luma.fill(u8::MAX); let luma = RwLock::new(luma); - let luma_ref = &luma; + run(&display, &luma, socket, font, &cli); +} + +fn run( + display_ref: &RwLock, + luma_ref: &RwLock, + socket: UdpSocket, + font: BitmapFont, + cli: &Cli, +) { let (stop_udp_tx, stop_udp_rx) = mpsc::channel(); - let mut app = App::new(display_ref, luma_ref, stop_udp_tx, cli.spacers); + let mut app = App::new(display_ref, luma_ref, stop_udp_tx, cli); let event_loop = EventLoop::with_user_event() .build()