commit 3a0ab4cbfc74cb1cdd73c320fbfcc654b4c4bfe7 Author: Tomáš Mládek Date: Sat Dec 11 15:44:10 2021 +0100 Initial commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..19605c0 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,178 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "libc" +version = "0.2.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "869d572136620d55835903746bcb5cdc54cb2851fd0aeec53220b4bb65ef3013" + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "sunamp" +version = "0.1.0" +dependencies = [ + "chrono", + "clap", + "suncalc", +] + +[[package]] +name = "suncalc" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a406450f3d4692b1acf60bfbedb959ae6c2b456c1331fd3780f84b9468a464db" + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "unicode-width" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9e25b09 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "sunamp" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = "2.33" +chrono = "0.4" +suncalc = "0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9b65542 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,118 @@ +use std::{thread, time}; +extern crate clap; +use chrono::prelude::*; +use clap::{App, Arg}; +use std::process::Command; + +fn main() { + let matches = App::new("SunAmp") + .version("0.1") + .arg( + Arg::with_name("period") + .short("n") + .takes_value(true) + .default_value("30"), + ) + .arg( + Arg::with_name("latitude") + .long("latitude") + .takes_value(true) + .required(true), + ) + .arg( + Arg::with_name("longitude") + .long("longitude") + .takes_value(true) + .required(true), + ) + .arg( + Arg::with_name("min") + .long("min") + .takes_value(true) + .default_value("0"), + ) + .arg( + Arg::with_name("max") + .long("max") + .takes_value(true) + .default_value("100"), + ) + .arg( + Arg::with_name("device") + .long("device") + .takes_value(true) + .default_value("Master"), + ) + .get_matches(); + + let lat = matches + .value_of("latitude") + .unwrap() + .parse::() + .expect("\"latitude\" has to be a positive number!"); + + let lon = matches + .value_of("longitude") + .unwrap() + .parse::() + .expect("\"latitude\" has to be a positive number!"); + + let period = time::Duration::from_secs( + matches + .value_of("period") + .unwrap() + .parse::() + .expect("\"period\" has to be a positive number!"), + ); + + let min = matches + .value_of("min") + .unwrap() + .parse::() + .expect("\"minimal offset\" has to be a positive number!"); + + let max = matches + .value_of("max") + .unwrap() + .parse::() + .expect("\"minimal offset\" has to be a positive number!"); + + loop { + let now = suncalc::Timestamp(Local::now().timestamp_millis()); + let times = suncalc::get_times(now, lat, lon, None); + let max_altitude = suncalc::get_position(times.solar_noon, lat, lon).altitude; + let current_altitude = suncalc::get_position(now, lat, lon).altitude; + + let sun_power = (current_altitude / max_altitude * 100.0).clamp(0.00, 100.0); + let sound_power = (sun_power / 100.0 * (max - min) + min) + .clamp(0.0, 100.0) + .round(); + + println!( + "CURRENT SUN POWER: {:.2}%, TRANSLATED TO SOUND POWER: {}%", + sun_power, sound_power + ); + + let output = Command::new("amixer") + .arg("-M") + .arg("set") + .arg(matches.value_of("device").unwrap()) + .arg(format!("{}%", sound_power)) + .output(); + + // println!("{:?}", output.unwrap()); + + match output { + Ok(output) => { + if !output.status.success() { + print!("ERROR! {}", std::str::from_utf8(&output.stderr).unwrap()); + } + } + Err(error) => { + print!("ERROR! {}", error); + } + } + + thread::sleep(period); + } +}