split execute.rs

This commit is contained in:
Vinzenz Schroeter 2025-02-13 20:14:29 +01:00
parent 5777d7d47d
commit 24fcd7a6fe
5 changed files with 81 additions and 77 deletions

20
src/brightness.rs Normal file
View file

@ -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:?}");
}

View file

@ -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,

View file

@ -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:?}");
}

View file

@ -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 {

34
src/pixels.rs Normal file
View file

@ -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");
}