Set duty cycle as cli arg, rename executable

This commit is contained in:
Reinout Meliesie 2025-08-24 12:23:04 +02:00
commit ff0e74e87d
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
3 changed files with 137 additions and 33 deletions

83
Cargo.lock generated
View file

@ -20,6 +20,15 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
[[package]]
name = "colored"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fde0e0ec90c9dfb3b4b1a0891a7dcd0e2bffde2f7efed5fe7c9bb00e5bfb915e"
dependencies = [
"windows-sys",
]
[[package]]
name = "core-foundation"
version = "0.10.1"
@ -66,6 +75,7 @@ dependencies = [
name = "kulifuli-host"
version = "0.1.0"
dependencies = [
"colored",
"eyre",
"serialport",
]
@ -238,3 +248,76 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

View file

@ -6,6 +6,11 @@ edition = "2024"
[profile.release]
lto = true
[[bin]]
name = "kulifuli"
path = "src/main.rs"
[dependencies]
colored = "3.0.0"
eyre = "0.6.12"
serialport = "4.7.2"

View file

@ -1,48 +1,64 @@
use colored::Colorize;
use eyre::{Result, bail, ensure, eyre};
use serialport::TTYPort;
use std::env::args;
use std::io::{Read, Write, stdin, stdout};
use std::io::{Read, Write};
use std::process::exit;
use std::str::FromStr;
use std::time::Duration;
fn main() -> Result<()> {
let args = args().collect::<Vec<_>>();
let serial_device = args.get(1);
if serial_device.is_none() {
println!("Please give a serial device path as program argument");
exit(1);
}
let serial_device = serial_device.unwrap();
struct ParsedArgs {
serial_device: String,
duty_cycle: u8,
}
let mut port = serialport::new(serial_device, 9600)
.timeout(Duration::from_secs(3600))
fn main() -> Result<()> {
let args = parse_args();
let mut port = serialport::new(args.serial_device, 9600)
.timeout(Duration::from_secs(1))
.open_native()?;
loop {
print!("Enter duty cycle: ");
stdout().flush()?;
let mut requested_dc = String::new();
stdin().read_line(&mut requested_dc)?;
println!("Sending to fan controller...");
let bytes_written = port.write(&[188, 92, 136, 14, 154, 39, 154, 139, args.duty_cycle])?;
if bytes_written != 9 {
bail!("Could not write enough bytes to serial device");
}
println!("Awaiting confirmation...");
if let Ok(duty_cycle) = u8::from_str(requested_dc.trim())
&& duty_cycle <= 79
{
println!("Sending to fan controller...");
let bytes_written = port.write(&[188, 92, 136, 14, 154, 39, 154, 139, duty_cycle])?;
if bytes_written != 9 {
bail!("Could not write enough bytes to serial device");
}
println!("Awaiting confirmation...");
let response_byte = read_byte(&mut port)?;
if response_byte == args.duty_cycle {
println!("Duty cycle set succesfully");
}
let response_byte = read_byte(&mut port)?;
if response_byte == duty_cycle {
println!("Duty cycle set succesfully");
}
} else {
println!("Invalid duty cycle, should be an integer between 0 and 79");
}
println!();
Ok(())
}
fn parse_args() -> ParsedArgs {
let args = args().collect::<Vec<_>>();
if args.len() != 3 {
println!("Incorrect number of arguments");
println!("Usage: kulifuli {}", "serial-device duty-cycle".italic());
exit(1);
}
let serial_device = args.get(1).unwrap().clone();
let requested_duty_cycle = args.get(2).unwrap();
let duty_cycle = u8::from_str(requested_duty_cycle);
if duty_cycle.is_err() {
println!("Invalid duty cycle, should be an integer between 0 and 79");
exit(1);
}
let duty_cycle = duty_cycle.unwrap();
if duty_cycle > 79 {
println!("Invalid duty cycle, should be an integer between 0 and 79");
exit(1);
}
ParsedArgs {
serial_device,
duty_cycle,
}
}