diff --git a/src/brightness.rs b/src/brightness.rs new file mode 100644 index 0000000..09b7e55 --- /dev/null +++ b/src/brightness.rs @@ -0,0 +1,20 @@ +use servicepoint::{Brightness, Command, Connection}; +use log::info; +use crate::cli::BrightnessCommand; + +pub(crate) fn brightness(connection: &Connection, brightness_command: BrightnessCommand) { + match brightness_command { + BrightnessCommand::Max => brightness_set(connection, Brightness::MAX), + BrightnessCommand::Min => brightness_set(connection, Brightness::MIN), + BrightnessCommand::Set { brightness } => { + brightness_set(connection, Brightness::saturating_from(brightness)) + } + } +} + +pub(crate) fn brightness_set(connection: &Connection, brightness: Brightness) { + connection + .send(Command::Brightness(brightness)) + .expect("Failed to set brightness"); + info!("set brightness to {brightness:?}"); +} \ No newline at end of file diff --git a/src/cli.rs b/src/cli.rs index 3ef2286..8f205f1 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -53,6 +53,7 @@ pub enum PixelCommand { #[command( visible_alias = "r", visible_alias = "reset", + visible_alias = "clear", about = "Reset all pixels to the default (off) state" )] Off, diff --git a/src/execute.rs b/src/execute.rs deleted file mode 100644 index 17ac5df..0000000 --- a/src/execute.rs +++ /dev/null @@ -1,73 +0,0 @@ -use crate::cli::{BrightnessCommand, Mode, PixelCommand, StreamCommand}; -use crate::stream_stdin::stream_stdin; -use crate::stream_window::stream_window; -use log::info; -use servicepoint::{BitVec, Brightness, Command, CompressionCode, Connection, PIXEL_COUNT}; - -pub fn execute_mode(mode: Mode, connection: Connection) { - match mode { - Mode::ResetEverything => { - brightness_reset(&connection); - pixels_reset(&connection); - } - Mode::Pixels { pixel_command } => pixels(&connection, pixel_command), - Mode::Brightness { brightness_command } => brightness(&connection, brightness_command), - Mode::Stream { stream_command } => match stream_command { - StreamCommand::Stdin { slow } => stream_stdin(connection, slow), - StreamCommand::Screen { options } => stream_window(&connection, options), - }, - } -} - -fn pixels(connection: &Connection, pixel_command: PixelCommand) { - match pixel_command { - PixelCommand::Off => pixels_reset(connection), - PixelCommand::Invert => pixels_invert(connection), - PixelCommand::On => pixels_on(connection), - } -} - -fn pixels_on(connection: &Connection) { - let mask = BitVec::repeat(true, PIXEL_COUNT); - connection - .send(Command::BitmapLinear(0, mask, CompressionCode::Lzma)) - .expect("could not send command") -} - -fn pixels_invert(connection: &Connection) { - let mask = BitVec::repeat(true, PIXEL_COUNT); - connection - .send(Command::BitmapLinearXor(0, mask, CompressionCode::Lzma)) - .expect("could not send command") -} - -fn brightness(connection: &Connection, brightness_command: BrightnessCommand) { - match brightness_command { - BrightnessCommand::Max => brightness_reset(connection), - BrightnessCommand::Min => brightness_set(connection, Brightness::MIN), - BrightnessCommand::Set { brightness } => { - brightness_set(connection, Brightness::saturating_from(brightness)) - } - } -} - -fn pixels_reset(connection: &Connection) { - connection - .send(Command::Clear) - .expect("failed to clear pixels"); - info!("Reset pixels"); -} - -fn brightness_reset(connection: &Connection) { - connection - .send(Command::Brightness(Brightness::MAX)) - .expect("Failed to reset brightness to maximum"); - info!("Reset brightness"); -} - -fn brightness_set(connection: &Connection, brightness: Brightness) { - connection - .send(Command::Brightness(brightness)) - .expect("Failed to set brightness"); - info!("set brightness to {brightness:?}"); -} diff --git a/src/main.rs b/src/main.rs index a9878ca..bbdd20a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,17 @@ -use crate::cli::{Cli, Protocol}; +use crate::{ + brightness::{brightness, brightness_set}, + cli::{Cli, Mode, Protocol, StreamCommand}, + pixels::{pixels, pixels_off}, + stream_stdin::stream_stdin, + stream_window::stream_window, +}; use clap::Parser; use log::debug; -use servicepoint::Connection; +use servicepoint::{Brightness, Connection}; +mod brightness; mod cli; -mod execute; +mod pixels; mod stream_stdin; mod stream_window; @@ -16,7 +23,22 @@ fn main() { let connection = make_connection(cli.destination, cli.transport); debug!("connection established: {:#?}", connection); - execute::execute_mode(cli.command, connection); + execute_mode(cli.command, connection); +} + +pub fn execute_mode(mode: Mode, connection: Connection) { + match mode { + Mode::ResetEverything => { + brightness_set(&connection, Brightness::MAX); + pixels_off(&connection); + } + Mode::Pixels { pixel_command } => pixels(&connection, pixel_command), + Mode::Brightness { brightness_command } => brightness(&connection, brightness_command), + Mode::Stream { stream_command } => match stream_command { + StreamCommand::Stdin { slow } => stream_stdin(connection, slow), + StreamCommand::Screen { options } => stream_window(&connection, options), + }, + } } fn make_connection(destination: String, transport: Protocol) -> Connection { diff --git a/src/pixels.rs b/src/pixels.rs new file mode 100644 index 0000000..0e4597e --- /dev/null +++ b/src/pixels.rs @@ -0,0 +1,34 @@ +use servicepoint::{BitVec, Command, CompressionCode, Connection, PIXEL_COUNT}; +use log::info; +use crate::cli::PixelCommand; + +pub(crate) fn pixels(connection: &Connection, pixel_command: PixelCommand) { + match pixel_command { + PixelCommand::Off => pixels_off(connection), + PixelCommand::Invert => pixels_invert(connection), + PixelCommand::On => pixels_on(connection), + } +} + +fn pixels_on(connection: &Connection) { + let mask = BitVec::repeat(true, PIXEL_COUNT); + connection + .send(Command::BitmapLinear(0, mask, CompressionCode::Lzma)) + .expect("could not send command"); + info!("turned on all pixels") +} + +fn pixels_invert(connection: &Connection) { + let mask = BitVec::repeat(true, PIXEL_COUNT); + connection + .send(Command::BitmapLinearXor(0, mask, CompressionCode::Lzma)) + .expect("could not send command"); + info!("inverted all pixels"); +} + +pub(crate) fn pixels_off(connection: &Connection) { + connection + .send(Command::Clear) + .expect("failed to clear pixels"); + info!("reset pixels"); +} \ No newline at end of file